src/helpers/storage.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 2

Size

Lines of Code 62
Function Count 5

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
wmc 10
c 2
b 0
f 0
nc 1
mnd 1
bc 9
fnc 5
dl 0
loc 62
rs 10
bpm 1.8
cpm 2
noi 5

5 Functions

Rating   Name   Duplication   Size   Complexity  
A storage.js ➔ removeStorageItem 0 4 1
A storage.js ➔ getStorageItem 0 6 3
A storage.js ➔ buildStorageKey 0 3 1
A storage.js ➔ setStorageItem 0 7 2
A storage.js ➔ isJson 0 8 2
1
/*eslint no-undef: 0*/
2
3
import { get, isString, isUndefined } from 'lodash';
4
import { debug } from './log';
5
6
if (isUndefined(STORAGE_PREFIX)) {
0 ignored issues
show
Bug introduced by
The variable STORAGE_PREFIX seems to be never declared. If this is a global, consider adding a /** global: STORAGE_PREFIX */ comment.

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.

Loading history...
7
  throw new Error('STORAGE_PREFIX must be set.');
8
}
9
10
/**
11
 *
12
 * @param {string} key
13
 */
14
export function getStorageItem(key) {
15
  const [root, ...rest] = key.split('.');
16
  const item = localStorage.getItem(buildStorageKey(root));
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

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.

Loading history...
17
  const value = isJson(item) ? JSON.parse(item) : item;
18
  return rest.length ? get(value, rest.join('.')) : value;
19
}
20
21
/**
22
 *
23
 * @param {string} key
24
 * @param {*} value
25
 */
26
export function setStorageItem(key, value) {
27
  if (!isString(value)) {
28
    value = JSON.stringify(value);
29
  }
30
  localStorage.setItem(buildStorageKey(key), value);
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

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.

Loading history...
31
  debug('storage item set: %s -> %s', key, value);
32
}
33
34
/**
35
 *
36
 * @param {string} key
37
 */
38
export function removeStorageItem(key) {
39
  localStorage.removeItem(buildStorageKey(key));
0 ignored issues
show
Bug introduced by
The variable localStorage seems to be never declared. If this is a global, consider adding a /** global: localStorage */ comment.

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.

Loading history...
40
  debug('storage item removed: %s', key);
41
}
42
43
/**
44
 *
45
 * @param {string} key
46
 * @returns {string}
47
 */
48
function buildStorageKey(key) {
49
  return [STORAGE_PREFIX, key].join('.');
0 ignored issues
show
Bug introduced by
The variable STORAGE_PREFIX seems to be never declared. If this is a global, consider adding a /** global: STORAGE_PREFIX */ comment.

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.

Loading history...
50
}
51
52
/**
53
 *
54
 * @param {*} value
55
 * @returns {boolean}
56
 */
57
function isJson(value) {
58
  try {
59
    JSON.parse(value);
60
  } catch (e) {
61
    return false;
62
  }
63
  return true;
64
}
65