pet-shop 0.1.3 → 0.2.0

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 (3) hide show
  1. package/LICENSE +21 -0
  2. package/index.js +48 -15
  3. package/package.json +2 -6
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Eric Chen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/index.js CHANGED
@@ -45,10 +45,6 @@ export function PetShop({ storage, namespace, json = false }) {
45
45
  if (value === undefined || value === null) {
46
46
  this.remove(key);
47
47
  } else {
48
- if (typeof value !== 'string') {
49
- throw new TypeError('The Value should be a string');
50
- }
51
-
52
48
  storage.setItem(`${namespace}.${key}`, value);
53
49
  }
54
50
  },
@@ -121,17 +117,54 @@ export function PetShop({ storage, namespace, json = false }) {
121
117
  );
122
118
  }
123
119
 
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 {
120
+ export function createProxy(store) {
121
+ return new Proxy(
122
+ {},
123
+ {
124
+ get(_, key) {
125
+ return store.get(key);
126
+ },
127
+ async set(_, key, value) {
128
+ if (value !== null && value !== undefined) {
129
+ store.set(key, value);
130
+ } else {
131
+ store.remove(key);
132
+ }
133
+
134
+ return true;
135
+ },
136
+ async deleteProperty(_, key) {
133
137
  store.remove(key);
134
- }
138
+
139
+ return true;
140
+ },
135
141
  },
136
- };
142
+ );
143
+ }
144
+
145
+ export function createCache(storage, keys) {
146
+ return Object.defineProperties(
147
+ {
148
+ clear() {
149
+ storage.clear();
150
+ },
151
+ },
152
+ Object.fromEntries(
153
+ keys.map((key) => [
154
+ key,
155
+ {
156
+ get() {
157
+ return storage.get(key);
158
+ },
159
+ set(value) {
160
+ if (value || value === 0 || value === false) {
161
+ storage.set(key, value);
162
+ } else {
163
+ storage.remove(key);
164
+ }
165
+ },
166
+ },
167
+ ]),
168
+ ),
169
+ );
137
170
  }
package/package.json CHANGED
@@ -1,12 +1,8 @@
1
1
  {
2
2
  "name": "pet-shop",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "A simple wrapper of Web Storage API",
5
5
  "license": "MIT",
6
- "author": {
7
- "name": "Eric Chen",
8
- "email": "airkro@qq.com"
9
- },
10
6
  "keywords": [
11
7
  "cache",
12
8
  "config",
@@ -33,4 +29,4 @@
33
29
  "access": "public",
34
30
  "registry": "https://registry.npmjs.org/"
35
31
  }
36
- }
32
+ }