Total Complexity | 7 |
Complexity/F | 3.5 |
Lines of Code | 36 |
Function Count | 2 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | function generateIdentifier() { |
||
2 | const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' |
||
3 | let text = '' |
||
4 | |||
5 | for (let i = 0; i < 20; i++) { |
||
6 | text += possible.charAt(Math.floor(Math.random() * possible.length)) |
||
7 | } |
||
8 | |||
9 | return text |
||
10 | } |
||
11 | |||
12 | export default function getIdentifier(userIdentifier) { |
||
13 | if (userIdentifier || userIdentifier === 0) { |
||
14 | return String(userIdentifier) |
||
15 | } |
||
16 | |||
17 | if (typeof window === 'undefined' || !('localStorage' in window)) { |
||
18 | return null |
||
19 | } |
||
20 | |||
21 | const key = '__ab_experiment_identifier__' |
||
22 | |||
23 | try { |
||
24 | userIdentifier = localStorage.getItem(key) |
||
25 | userIdentifier = userIdentifier && String(userIdentifier) |
||
26 | |||
27 | if (!userIdentifier) { |
||
28 | userIdentifier = generateIdentifier() |
||
29 | localStorage.setItem(key, userIdentifier) |
||
30 | } |
||
31 | } catch (exception) { |
||
32 | return null |
||
33 | } |
||
34 | |||
35 | return userIdentifier |
||
36 | } |
||
37 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.