Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(algolia): parse user agents with new search clients (#302)
- Loading branch information
1 parent
652bde4
commit 2f32ffb
Showing
2 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
'use strict'; | ||
|
||
module.exports = function parseAlgoliaClientVersion(agent) { | ||
var parsed = agent.match(/Algolia for vanilla JavaScript (\d+\.)(\d+\.)(\d+)/); | ||
if (parsed) return [parsed[1], parsed[2], parsed[3]]; | ||
var parsed = | ||
// User agent for algoliasearch >= 3.33.0 | ||
agent.match(/Algolia for JavaScript \((\d+\.)(\d+\.)(\d+)\)/) || | ||
// User agent for algoliasearch < 3.33.0 | ||
agent.match(/Algolia for vanilla JavaScript (\d+\.)(\d+\.)(\d+)/); | ||
|
||
if (parsed) { | ||
return [parsed[1], parsed[2], parsed[3]]; | ||
} | ||
|
||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
/* eslint-env mocha, jasmine */ | ||
|
||
describe('parseAlgoliaClientVersion', function() { | ||
var parseAlgoliaClientVersion = require('../../src/common/parseAlgoliaClientVersion.js'); | ||
|
||
it('should return undefined for unknown user agents', function() { | ||
expect(parseAlgoliaClientVersion('random user agent 1.2.3')).toEqual( | ||
undefined | ||
); | ||
}); | ||
|
||
it('should parse user agents with algoliasearch < 3.33.0 format', function() { | ||
expect( | ||
parseAlgoliaClientVersion('Algolia for vanilla JavaScript 3.1.0') | ||
).toEqual(['3.', '1.', '0']); | ||
}); | ||
|
||
it('should parse user agents with algoliasearch >= 3.33.0 format', function() { | ||
expect(parseAlgoliaClientVersion('Algolia for JavaScript (3.5.0)')).toEqual([ | ||
'3.', | ||
'5.', | ||
'0' | ||
]); | ||
}); | ||
}); |