pet-shop 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +25 -6
  2. package/package.json +6 -2
package/index.js CHANGED
@@ -3,9 +3,8 @@
3
3
  function safeParse(string) {
4
4
  try {
5
5
  return JSON.parse(string);
6
- } catch {
7
- return undefined;
8
- }
6
+ // eslint-disable-next-line unicorn/prefer-optional-catch-binding
7
+ } catch (error) {}
9
8
  }
10
9
 
11
10
  export function PetShop({ storage, namespace, json = false }) {
@@ -23,11 +22,13 @@ export function PetShop({ storage, namespace, json = false }) {
23
22
  value: json
24
23
  ? function get(key) {
25
24
  const raw = storage.getItem(`${namespace}.${key}`);
26
- return raw !== null ? safeParse(raw) : undefined;
25
+
26
+ return raw === null ? undefined : safeParse(raw);
27
27
  }
28
28
  : function get(key) {
29
29
  const raw = storage.getItem(`${namespace}.${key}`);
30
- return raw !== null ? raw : undefined;
30
+
31
+ return raw === null ? undefined : raw;
31
32
  },
32
33
  },
33
34
  set: {
@@ -47,6 +48,7 @@ export function PetShop({ storage, namespace, json = false }) {
47
48
  if (typeof value !== 'string') {
48
49
  throw new TypeError('The Value should be a string');
49
50
  }
51
+
50
52
  storage.setItem(`${namespace}.${key}`, value);
51
53
  }
52
54
  },
@@ -60,12 +62,15 @@ export function PetShop({ storage, namespace, json = false }) {
60
62
  rawKeys: {
61
63
  get() {
62
64
  const keys = [];
65
+
63
66
  for (let i = 0; i < storage.length; i += 1) {
64
67
  const key = storage.key(i);
68
+
65
69
  if (key.startsWith(`${namespace}.`)) {
66
70
  keys.push(key);
67
71
  }
68
72
  }
73
+
69
74
  return keys;
70
75
  },
71
76
  },
@@ -86,7 +91,6 @@ export function PetShop({ storage, namespace, json = false }) {
86
91
  valueOf: {
87
92
  enumerable: true,
88
93
  value: function valueOf() {
89
- // eslint-disable-next-line unicorn/prefer-object-from-entries
90
94
  return this.keys.reduce(
91
95
  (io, key) => ({ [key]: this.get(key), ...io }),
92
96
  {},
@@ -116,3 +120,18 @@ export function PetShop({ storage, namespace, json = false }) {
116
120
  },
117
121
  );
118
122
  }
123
+
124
+ export function smart(store, key) {
125
+ return {
126
+ get() {
127
+ return store.get(key);
128
+ },
129
+ set(value) {
130
+ if (value !== null && value !== undefined) {
131
+ store.set(key, value);
132
+ } else {
133
+ store.remove(key);
134
+ }
135
+ },
136
+ };
137
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pet-shop",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "A simple wrapper of Web Storage API",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -28,5 +28,9 @@
28
28
  "url": "https://github.com/airkro/frontend-toolkit/issues"
29
29
  },
30
30
  "main": "index.js",
31
- "type": "module"
31
+ "type": "module",
32
+ "publishConfig": {
33
+ "access": "public",
34
+ "registry": "https://registry.npmjs.org/"
35
+ }
32
36
  }