Skip to content

Commit 2f32ffb

Browse files
fix(algolia): parse user agents with new search clients (#302)
1 parent 652bde4 commit 2f32ffb

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
+11-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
'use strict';
2+
23
module.exports = function parseAlgoliaClientVersion(agent) {
3-
var parsed = agent.match(/Algolia for vanilla JavaScript (\d+\.)(\d+\.)(\d+)/);
4-
if (parsed) return [parsed[1], parsed[2], parsed[3]];
4+
var parsed =
5+
// User agent for algoliasearch >= 3.33.0
6+
agent.match(/Algolia for JavaScript \((\d+\.)(\d+\.)(\d+)\)/) ||
7+
// User agent for algoliasearch < 3.33.0
8+
agent.match(/Algolia for vanilla JavaScript (\d+\.)(\d+\.)(\d+)/);
9+
10+
if (parsed) {
11+
return [parsed[1], parsed[2], parsed[3]];
12+
}
13+
514
return undefined;
615
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
/* eslint-env mocha, jasmine */
4+
5+
describe('parseAlgoliaClientVersion', function() {
6+
var parseAlgoliaClientVersion = require('../../src/common/parseAlgoliaClientVersion.js');
7+
8+
it('should return undefined for unknown user agents', function() {
9+
expect(parseAlgoliaClientVersion('random user agent 1.2.3')).toEqual(
10+
undefined
11+
);
12+
});
13+
14+
it('should parse user agents with algoliasearch < 3.33.0 format', function() {
15+
expect(
16+
parseAlgoliaClientVersion('Algolia for vanilla JavaScript 3.1.0')
17+
).toEqual(['3.', '1.', '0']);
18+
});
19+
20+
it('should parse user agents with algoliasearch >= 3.33.0 format', function() {
21+
expect(parseAlgoliaClientVersion('Algolia for JavaScript (3.5.0)')).toEqual([
22+
'3.',
23+
'5.',
24+
'0'
25+
]);
26+
});
27+
});

0 commit comments

Comments
 (0)