webext-storage 1.3.2 → 2.0.1

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.
@@ -11,12 +11,14 @@ export class StorageItemMap {
11
11
  has = async (secondaryKey) => {
12
12
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
13
13
  const result = await chromeP.storage[this.areaName].get(rawStorageKey);
14
- return Object.hasOwn(result, rawStorageKey);
14
+ // Do not use Object.hasOwn() due to https://github.com/RickyMarou/jest-webextension-mock/issues/20
15
+ return result[rawStorageKey] !== undefined;
15
16
  };
16
17
  get = async (secondaryKey) => {
17
18
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
18
19
  const result = await chromeP.storage[this.areaName].get(rawStorageKey);
19
- if (!Object.hasOwn(result, rawStorageKey)) {
20
+ // Do not use Object.hasOwn() due to https://github.com/RickyMarou/jest-webextension-mock/issues/20
21
+ if (result[rawStorageKey] === undefined) {
20
22
  return this.defaultValue;
21
23
  }
22
24
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- Assumes the user never uses the Storage API directly for this key
@@ -24,7 +26,13 @@ export class StorageItemMap {
24
26
  };
25
27
  set = async (secondaryKey, value) => {
26
28
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
27
- await chromeP.storage[this.areaName].set({ [rawStorageKey]: value });
29
+ // eslint-disable-next-line unicorn/prefer-ternary -- ur rong
30
+ if (value === undefined) {
31
+ await chromeP.storage[this.areaName].remove(rawStorageKey);
32
+ }
33
+ else {
34
+ await chromeP.storage[this.areaName].set({ [rawStorageKey]: value });
35
+ }
28
36
  };
29
37
  remove = async (secondaryKey) => {
30
38
  const rawStorageKey = this.getRawStorageKey(secondaryKey);
@@ -12,18 +12,26 @@ export class StorageItem {
12
12
  }
13
13
  get = async () => {
14
14
  const result = await chromeP.storage[this.area].get(this.key);
15
- if (!Object.hasOwn(result, this.key)) {
15
+ // Do not use Object.hasOwn() due to https://github.com/RickyMarou/jest-webextension-mock/issues/20
16
+ if (result[this.key] === undefined) {
16
17
  return this.defaultValue;
17
18
  }
18
19
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return -- Assumes the user never uses the Storage API directly
19
20
  return result[this.key];
20
21
  };
21
22
  set = async (value) => {
22
- await chromeP.storage[this.area].set({ [this.key]: value });
23
+ // eslint-disable-next-line unicorn/prefer-ternary -- ur rong
24
+ if (value === undefined) {
25
+ await chromeP.storage[this.area].remove(this.key);
26
+ }
27
+ else {
28
+ await chromeP.storage[this.area].set({ [this.key]: value });
29
+ }
23
30
  };
24
31
  has = async () => {
25
32
  const result = await chromeP.storage[this.area].get(this.key);
26
- return Object.hasOwn(result, this.key);
33
+ // Do not use Object.hasOwn() due to https://github.com/RickyMarou/jest-webextension-mock/issues/20
34
+ return result[this.key] !== undefined;
27
35
  };
28
36
  remove = async () => {
29
37
  await chromeP.storage[this.area].remove(this.key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-storage",
3
- "version": "1.3.2",
3
+ "version": "2.0.1",
4
4
  "description": "A more usable typed storage API for Web Extensions",
5
5
  "keywords": [
6
6
  "browser",
package/readme.md CHANGED
@@ -24,6 +24,7 @@ options.onChanged(newValue => {
24
24
  - `item.get()` returns the raw value instead of an object
25
25
  - Every `get` and `set` operation is type-safe
26
26
  - If you provide a `defaultValue`, the return type will not be ` | undefined`
27
+ - Calling `.set(undefined)` will unset the value instead of the call being ignored
27
28
  - The `onChanged` example speaks for itself
28
29
 
29
30
  Now compare it to the native API: