Skip to content

Commit 450bbf7

Browse files
authored
Generate installationId as UUID (#972)
1 parent aa3d6e6 commit 450bbf7

File tree

2 files changed

+6
-17
lines changed

2 files changed

+6
-17
lines changed

src/InstallationController.js

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,10 @@
1010
*/
1111

1212
import Storage from './Storage';
13+
const uuidv4 = require('uuid/v4');
1314

1415
let iidCache = null;
1516

16-
function hexOctet() {
17-
return Math.floor(
18-
(1 + Math.random()) * 0x10000
19-
).toString(16).substring(1);
20-
}
21-
22-
function generateId() {
23-
return (
24-
hexOctet() + hexOctet() + '-' +
25-
hexOctet() + '-' +
26-
hexOctet() + '-' +
27-
hexOctet() + '-' +
28-
hexOctet() + hexOctet() + hexOctet()
29-
);
30-
}
31-
3217
const InstallationController = {
3318
currentInstallationId(): Promise<string> {
3419
if (typeof iidCache === 'string') {
@@ -37,7 +22,7 @@ const InstallationController = {
3722
const path = Storage.generatePath('installationId');
3823
return Storage.getItemAsync(path).then((iid) => {
3924
if (!iid) {
40-
iid = generateId();
25+
iid = uuidv4();
4126
return Storage.setItemAsync(path, iid).then(() => {
4227
iidCache = iid;
4328
return iid;

src/__tests__/InstallationController-test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ jest.dontMock('../CoreManager');
1111
jest.dontMock('../InstallationController');
1212
jest.dontMock('../Storage');
1313
jest.dontMock('../StorageController.default');
14+
jest.mock('uuid/v4', () => {
15+
let value = 0;
16+
return () => value++ + '';
17+
});
1418

1519
const CoreManager = require('../CoreManager');
1620
const InstallationController = require('../InstallationController');

0 commit comments

Comments
 (0)