@alwatr/local-storage 1.1.6 → 1.1.7

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.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.1.7](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.1.6...@alwatr/local-storage@1.1.7) (2024-10-25)
7
+
8
+ ### Code Refactoring
9
+
10
+ * **local-storage:** update removeItem method to use localStorage instead of window.localStorage ([9680a14](https://github.com/Alwatr/nanolib/commit/9680a141a4366d4bb146a73ffcd1bb63dd14f27c)) by @AliMD
11
+
6
12
  ## [1.1.6](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.1.5...@alwatr/local-storage@1.1.6) (2024-10-12)
7
13
 
8
14
  **Note:** Version bump only for package @alwatr/local-storage
@@ -11,13 +17,13 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
11
17
 
12
18
  ### Code Refactoring
13
19
 
14
- * prevent side-effects ([01e00e1](https://github.com/Alwatr/nanolib/commit/01e00e191385cc92b28677df0c01a085916ae677)) by @mohammadhonarvar
20
+ - prevent side-effects ([01e00e1](https://github.com/Alwatr/nanolib/commit/01e00e191385cc92b28677df0c01a085916ae677)) by @mohammadhonarvar
15
21
 
16
22
  ## [1.1.4](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.1.3...@alwatr/local-storage@1.1.4) (2024-10-11)
17
23
 
18
24
  ### Miscellaneous Chores
19
25
 
20
- * include LICENSE and LEGAL files to publish ([09f366f](https://github.com/Alwatr/nanolib/commit/09f366f680bfa9fb26acb2cd1ccbc68c5a9e9ad8)) by @AliMD
26
+ - include LICENSE and LEGAL files to publish ([09f366f](https://github.com/Alwatr/nanolib/commit/09f366f680bfa9fb26acb2cd1ccbc68c5a9e9ad8)) by @AliMD
21
27
 
22
28
  ## [1.1.3](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.1.2...@alwatr/local-storage@1.1.3) (2024-10-11)
23
29
 
@@ -27,7 +33,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
27
33
 
28
34
  ### Dependencies update
29
35
 
30
- * bump the development-dependencies group with 10 updates ([fa4aaf0](https://github.com/Alwatr/nanolib/commit/fa4aaf04c907ecae06aa14000ce35216170c15ad)) by @dependabot[bot]
36
+ - bump the development-dependencies group with 10 updates ([fa4aaf0](https://github.com/Alwatr/nanolib/commit/fa4aaf04c907ecae06aa14000ce35216170c15ad)) by @dependabot[bot]
31
37
 
32
38
  ## [1.1.1](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.1.0...@alwatr/local-storage@1.1.1) (2024-10-08)
33
39
 
@@ -37,21 +43,21 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
37
43
 
38
44
  ### Features
39
45
 
40
- * **local-storage:** If the version is greater than 1, remove the previous version. ([1fdff96](https://github.com/Alwatr/nanolib/commit/1fdff9696a9cf7f1ced0a4c905ea62eb4c422f7a)) by @AliMD
41
- * use `package-tracer` ([cc3c5f9](https://github.com/Alwatr/nanolib/commit/cc3c5f9c1a3d03f0d81b46835665f16a0426fd0d)) by @mohammadhonarvar
46
+ - **local-storage:** If the version is greater than 1, remove the previous version. ([1fdff96](https://github.com/Alwatr/nanolib/commit/1fdff9696a9cf7f1ced0a4c905ea62eb4c422f7a)) by @AliMD
47
+ - use `package-tracer` ([cc3c5f9](https://github.com/Alwatr/nanolib/commit/cc3c5f9c1a3d03f0d81b46835665f16a0426fd0d)) by @mohammadhonarvar
42
48
 
43
49
  ### Bug Fixes
44
50
 
45
- * all dependeny topology ([1c17f34](https://github.com/Alwatr/nanolib/commit/1c17f349adf3e98e2a80ab2da4f0f81028dc9c5f)) by @mohammadhonarvar
51
+ - all dependeny topology ([1c17f34](https://github.com/Alwatr/nanolib/commit/1c17f349adf3e98e2a80ab2da4f0f81028dc9c5f)) by @mohammadhonarvar
46
52
 
47
53
  ### Code Refactoring
48
54
 
49
- * use new type-helper global types and remove all import types ([08b5d08](https://github.com/Alwatr/nanolib/commit/08b5d08c03c7c315382337239de0426462f384b8)) by @AliMD
55
+ - use new type-helper global types and remove all import types ([08b5d08](https://github.com/Alwatr/nanolib/commit/08b5d08c03c7c315382337239de0426462f384b8)) by @AliMD
50
56
 
51
57
  ### Miscellaneous Chores
52
58
 
53
- * **local-storage:** change the license to AGPL-3.0 ([ef7ea07](https://github.com/Alwatr/nanolib/commit/ef7ea075094c88d90d2ddc6fb9612ae18d792225)) by @ArmanAsadian
54
- * Update build and lint scripts ([392d0b7](https://github.com/Alwatr/nanolib/commit/392d0b71f446bce336b0256119a80f07aff794ba)) by @AliMD
59
+ - **local-storage:** change the license to AGPL-3.0 ([ef7ea07](https://github.com/Alwatr/nanolib/commit/ef7ea075094c88d90d2ddc6fb9612ae18d792225)) by @ArmanAsadian
60
+ - Update build and lint scripts ([392d0b7](https://github.com/Alwatr/nanolib/commit/392d0b71f446bce336b0256119a80f07aff794ba)) by @AliMD
55
61
 
56
62
  ## [1.0.10](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.9...@alwatr/local-storage@1.0.10) (2024-09-21)
57
63
 
@@ -61,25 +67,25 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
61
67
 
62
68
  ### Dependencies update
63
69
 
64
- * bump the development-dependencies group across 1 directory with 10 updates ([9ed98ff](https://github.com/Alwatr/nanolib/commit/9ed98ffd0668d5a36e255c82edab3af53bffda8f)) by @dependabot[bot]
70
+ - bump the development-dependencies group across 1 directory with 10 updates ([9ed98ff](https://github.com/Alwatr/nanolib/commit/9ed98ffd0668d5a36e255c82edab3af53bffda8f)) by @dependabot[bot]
65
71
 
66
72
  ## [1.0.8](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.7...@alwatr/local-storage@1.0.8) (2024-08-31)
67
73
 
68
74
  ### Miscellaneous Chores
69
75
 
70
- * Update package.json exports for [@alwatr](https://github.com/alwatr) packages ([dacb362](https://github.com/Alwatr/nanolib/commit/dacb362b145e3c51b4aba00ff643687a3fac11d2)) by @
76
+ - Update package.json exports for [@alwatr](https://github.com/alwatr) packages ([dacb362](https://github.com/Alwatr/nanolib/commit/dacb362b145e3c51b4aba00ff643687a3fac11d2)) by @
71
77
 
72
78
  ## [1.0.7](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.6...@alwatr/local-storage@1.0.7) (2024-08-31)
73
79
 
74
80
  ### Dependencies update
75
81
 
76
- * update all dependencies ([1e0c30e](https://github.com/Alwatr/nanolib/commit/1e0c30e6a3a8e19deb5185814e24ab6c08dca573)) by @AliMD
82
+ - update all dependencies ([1e0c30e](https://github.com/Alwatr/nanolib/commit/1e0c30e6a3a8e19deb5185814e24ab6c08dca573)) by @AliMD
77
83
 
78
84
  ## [1.0.6](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.5...@alwatr/local-storage@1.0.6) (2024-07-04)
79
85
 
80
86
  ### Dependencies update
81
87
 
82
- * update all dependencies ([0e908b4](https://github.com/Alwatr/nanolib/commit/0e908b476a6b976ec2447f864c8cafcbb8a0f099)) by @
88
+ - update all dependencies ([0e908b4](https://github.com/Alwatr/nanolib/commit/0e908b476a6b976ec2447f864c8cafcbb8a0f099)) by @
83
89
 
84
90
  ## [1.0.5](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.4...@alwatr/local-storage@1.0.5) (2024-05-12)
85
91
 
package/dist/main.cjs CHANGED
@@ -1,4 +1,4 @@
1
- /* @alwatr/local-storage v1.1.6 */
1
+ /* @alwatr/local-storage v1.1.7 */
2
2
  "use strict";
3
3
  var __defProp = Object.defineProperty;
4
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -25,7 +25,7 @@ __export(main_exports, {
25
25
  });
26
26
  module.exports = __toCommonJS(main_exports);
27
27
  var import_package_tracer = require("@alwatr/package-tracer");
28
- __dev_mode__: import_package_tracer.packageTracer.add("@alwatr/local-storage", "1.1.6");
28
+ __dev_mode__: import_package_tracer.packageTracer.add("@alwatr/local-storage", "1.1.7");
29
29
  function parseJson(content) {
30
30
  try {
31
31
  return JSON.parse(content);
@@ -102,7 +102,7 @@ var localJsonStorage = {
102
102
  */
103
103
  removeItem(name, version = 1) {
104
104
  const key = this.key_(name, version);
105
- window.localStorage.removeItem(key);
105
+ localStorage.removeItem(key);
106
106
  }
107
107
  };
108
108
  // Annotate the CommonJS export names for ESM import in node:
package/dist/main.cjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/main.ts"],
4
- "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * Parse json string without throwing error.\n *\n * @param content - json string\n * @returns json object or null if json is invalid\n * @example\n * ```typescript\n * const json = parseJson('{\"a\":1,\"b\":2}');\n * console.log(json.a); // 1\n * ```\n */\nfunction parseJson<T extends JsonValue>(content: string): T | null {\n try {\n return JSON.parse(content);\n }\n catch (err) {\n console.error('parseJson', 'invalid_json', err);\n return null;\n }\n}\n\n// @TODO: localStorage polyfill (memory fallback)\n\n/**\n * A utility object for working with local storage and JSON data.\n */\nexport const localJsonStorage = {\n /**\n * Generate local storage key.\n *\n * @param name - Name of the item.\n * @param version - Version of the item (default: 1).\n * @returns The generated local storage key.\n * @example\n * ```typescript\n * localJsonStorage.key_('myItem', 1); // myItem.v1\n * ```\n */\n key_(name: string, version = 1): string {\n return `${name}.v${version}`;\n },\n\n /**\n * Get the local storage item and parse it as JSON.\n * If the item is not found, return the default value.\n * If the version is greater than 1, remove the previous version.\n * If the item is not a valid JSON object, return the default value.\n *\n * @param name - The name of the item.\n * @param defaultValue - The default value of the item.\n * @param version - The data structure version of the item (default: 1).\n * @returns The parsed JSON value or the default value if the item is not found.\n * @example\n * ```typescript\n * const value = localJsonStorage.getItem('myItem', {a: 1, b: 2});\n * ```\n */\n getItem<T extends Json>(name: string, defaultValue: T, version = 1): T {\n if (version > 1) {\n this.removeItem(name, version - 1);\n }\n const key = this.key_(name, version);\n const value = localStorage.getItem(key);\n if (value === null) return defaultValue;\n const json = parseJson<T>(value);\n if (json === null || typeof json !== 'object') return defaultValue;\n return json;\n },\n\n /**\n * Set local storage item as JSON.\n *\n * @param name - Name of the item.\n * @param value - Value of the item.\n * @param version - Version of the item.\n * @example\n * ```typescript\n * localJsonStorage.setItem('myItem', {a: 1, b: 2});\n * ```\n */\n setItem<T extends Json>(name: string, value: T, version = 1): void {\n const key = this.key_(name, version);\n localStorage.setItem(key, JSON.stringify(value));\n },\n\n /**\n * Removes an item from the local storage.\n *\n * @param name - The name of the item to remove.\n * @param version - The version of the item to remove. Default is 1.\n * @example\n * ```typescript\n * localJsonStorage.removeItem('myItem');\n * ```\n */\n removeItem(name: string, version = 1): void {\n const key = this.key_(name, version);\n window.localStorage.removeItem(key);\n },\n} as const;\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA4B;AAE5B,aAAc,qCAAc,IAAI,yBAAkB,OAAmB;AAarE,SAAS,UAA+B,SAA2B;AACjE,MAAI;AACF,WAAO,KAAK,MAAM,OAAO;AAAA,EAC3B,SACO,KAAK;AACV,YAAQ,MAAM,aAAa,gBAAgB,GAAG;AAC9C,WAAO;AAAA,EACT;AACF;AAOO,IAAM,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAY9B,KAAK,MAAc,UAAU,GAAW;AACtC,WAAO,GAAG,IAAI,KAAK,OAAO;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,QAAwB,MAAc,cAAiB,UAAU,GAAM;AACrE,QAAI,UAAU,GAAG;AACf,WAAK,WAAW,MAAM,UAAU,CAAC;AAAA,IACnC;AACA,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,UAAM,QAAQ,aAAa,QAAQ,GAAG;AACtC,QAAI,UAAU,KAAM,QAAO;AAC3B,UAAM,OAAO,UAAa,KAAK;AAC/B,QAAI,SAAS,QAAQ,OAAO,SAAS,SAAU,QAAO;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,QAAwB,MAAc,OAAU,UAAU,GAAS;AACjE,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,iBAAa,QAAQ,KAAK,KAAK,UAAU,KAAK,CAAC;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,WAAW,MAAc,UAAU,GAAS;AAC1C,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,WAAO,aAAa,WAAW,GAAG;AAAA,EACpC;AACF;",
4
+ "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * Parse json string without throwing error.\n *\n * @param content - json string\n * @returns json object or null if json is invalid\n * @example\n * ```typescript\n * const json = parseJson('{\"a\":1,\"b\":2}');\n * console.log(json.a); // 1\n * ```\n */\nfunction parseJson<T extends JsonValue>(content: string): T | null {\n try {\n return JSON.parse(content);\n }\n catch (err) {\n console.error('parseJson', 'invalid_json', err);\n return null;\n }\n}\n\n// @TODO: localStorage polyfill (memory fallback)\n\n/**\n * A utility object for working with local storage and JSON data.\n */\nexport const localJsonStorage = {\n /**\n * Generate local storage key.\n *\n * @param name - Name of the item.\n * @param version - Version of the item (default: 1).\n * @returns The generated local storage key.\n * @example\n * ```typescript\n * localJsonStorage.key_('myItem', 1); // myItem.v1\n * ```\n */\n key_(name: string, version = 1): string {\n return `${name}.v${version}`;\n },\n\n /**\n * Get the local storage item and parse it as JSON.\n * If the item is not found, return the default value.\n * If the version is greater than 1, remove the previous version.\n * If the item is not a valid JSON object, return the default value.\n *\n * @param name - The name of the item.\n * @param defaultValue - The default value of the item.\n * @param version - The data structure version of the item (default: 1).\n * @returns The parsed JSON value or the default value if the item is not found.\n * @example\n * ```typescript\n * const value = localJsonStorage.getItem('myItem', {a: 1, b: 2});\n * ```\n */\n getItem<T extends Json>(name: string, defaultValue: T, version = 1): T {\n if (version > 1) {\n this.removeItem(name, version - 1);\n }\n const key = this.key_(name, version);\n const value = localStorage.getItem(key);\n if (value === null) return defaultValue;\n const json = parseJson<T>(value);\n if (json === null || typeof json !== 'object') return defaultValue;\n return json;\n },\n\n /**\n * Set local storage item as JSON.\n *\n * @param name - Name of the item.\n * @param value - Value of the item.\n * @param version - Version of the item.\n * @example\n * ```typescript\n * localJsonStorage.setItem('myItem', {a: 1, b: 2});\n * ```\n */\n setItem<T extends Json>(name: string, value: T, version = 1): void {\n const key = this.key_(name, version);\n localStorage.setItem(key, JSON.stringify(value));\n },\n\n /**\n * Removes an item from the local storage.\n *\n * @param name - The name of the item to remove.\n * @param version - The version of the item to remove. Default is 1.\n * @example\n * ```typescript\n * localJsonStorage.removeItem('myItem');\n * ```\n */\n removeItem(name: string, version = 1): void {\n const key = this.key_(name, version);\n localStorage.removeItem(key);\n },\n} as const;\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA4B;AAE5B,aAAc,qCAAc,IAAI,yBAAkB,OAAmB;AAarE,SAAS,UAA+B,SAA2B;AACjE,MAAI;AACF,WAAO,KAAK,MAAM,OAAO;AAAA,EAC3B,SACO,KAAK;AACV,YAAQ,MAAM,aAAa,gBAAgB,GAAG;AAC9C,WAAO;AAAA,EACT;AACF;AAOO,IAAM,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAY9B,KAAK,MAAc,UAAU,GAAW;AACtC,WAAO,GAAG,IAAI,KAAK,OAAO;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,QAAwB,MAAc,cAAiB,UAAU,GAAM;AACrE,QAAI,UAAU,GAAG;AACf,WAAK,WAAW,MAAM,UAAU,CAAC;AAAA,IACnC;AACA,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,UAAM,QAAQ,aAAa,QAAQ,GAAG;AACtC,QAAI,UAAU,KAAM,QAAO;AAC3B,UAAM,OAAO,UAAa,KAAK;AAC/B,QAAI,SAAS,QAAQ,OAAO,SAAS,SAAU,QAAO;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,QAAwB,MAAc,OAAU,UAAU,GAAS;AACjE,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,iBAAa,QAAQ,KAAK,KAAK,UAAU,KAAK,CAAC;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,WAAW,MAAc,UAAU,GAAS;AAC1C,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,iBAAa,WAAW,GAAG;AAAA,EAC7B;AACF;",
6
6
  "names": []
7
7
  }
package/dist/main.mjs CHANGED
@@ -1,8 +1,8 @@
1
- /* @alwatr/local-storage v1.1.6 */
1
+ /* @alwatr/local-storage v1.1.7 */
2
2
 
3
3
  // src/main.ts
4
4
  import { packageTracer } from "@alwatr/package-tracer";
5
- __dev_mode__: packageTracer.add("@alwatr/local-storage", "1.1.6");
5
+ __dev_mode__: packageTracer.add("@alwatr/local-storage", "1.1.7");
6
6
  function parseJson(content) {
7
7
  try {
8
8
  return JSON.parse(content);
@@ -79,7 +79,7 @@ var localJsonStorage = {
79
79
  */
80
80
  removeItem(name, version = 1) {
81
81
  const key = this.key_(name, version);
82
- window.localStorage.removeItem(key);
82
+ localStorage.removeItem(key);
83
83
  }
84
84
  };
85
85
  export {
package/dist/main.mjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/main.ts"],
4
- "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * Parse json string without throwing error.\n *\n * @param content - json string\n * @returns json object or null if json is invalid\n * @example\n * ```typescript\n * const json = parseJson('{\"a\":1,\"b\":2}');\n * console.log(json.a); // 1\n * ```\n */\nfunction parseJson<T extends JsonValue>(content: string): T | null {\n try {\n return JSON.parse(content);\n }\n catch (err) {\n console.error('parseJson', 'invalid_json', err);\n return null;\n }\n}\n\n// @TODO: localStorage polyfill (memory fallback)\n\n/**\n * A utility object for working with local storage and JSON data.\n */\nexport const localJsonStorage = {\n /**\n * Generate local storage key.\n *\n * @param name - Name of the item.\n * @param version - Version of the item (default: 1).\n * @returns The generated local storage key.\n * @example\n * ```typescript\n * localJsonStorage.key_('myItem', 1); // myItem.v1\n * ```\n */\n key_(name: string, version = 1): string {\n return `${name}.v${version}`;\n },\n\n /**\n * Get the local storage item and parse it as JSON.\n * If the item is not found, return the default value.\n * If the version is greater than 1, remove the previous version.\n * If the item is not a valid JSON object, return the default value.\n *\n * @param name - The name of the item.\n * @param defaultValue - The default value of the item.\n * @param version - The data structure version of the item (default: 1).\n * @returns The parsed JSON value or the default value if the item is not found.\n * @example\n * ```typescript\n * const value = localJsonStorage.getItem('myItem', {a: 1, b: 2});\n * ```\n */\n getItem<T extends Json>(name: string, defaultValue: T, version = 1): T {\n if (version > 1) {\n this.removeItem(name, version - 1);\n }\n const key = this.key_(name, version);\n const value = localStorage.getItem(key);\n if (value === null) return defaultValue;\n const json = parseJson<T>(value);\n if (json === null || typeof json !== 'object') return defaultValue;\n return json;\n },\n\n /**\n * Set local storage item as JSON.\n *\n * @param name - Name of the item.\n * @param value - Value of the item.\n * @param version - Version of the item.\n * @example\n * ```typescript\n * localJsonStorage.setItem('myItem', {a: 1, b: 2});\n * ```\n */\n setItem<T extends Json>(name: string, value: T, version = 1): void {\n const key = this.key_(name, version);\n localStorage.setItem(key, JSON.stringify(value));\n },\n\n /**\n * Removes an item from the local storage.\n *\n * @param name - The name of the item to remove.\n * @param version - The version of the item to remove. Default is 1.\n * @example\n * ```typescript\n * localJsonStorage.removeItem('myItem');\n * ```\n */\n removeItem(name: string, version = 1): void {\n const key = this.key_(name, version);\n window.localStorage.removeItem(key);\n },\n} as const;\n"],
5
- "mappings": ";;;AAAA,SAAQ,qBAAoB;AAE5B,aAAc,eAAc,IAAI,yBAAkB,OAAmB;AAarE,SAAS,UAA+B,SAA2B;AACjE,MAAI;AACF,WAAO,KAAK,MAAM,OAAO;AAAA,EAC3B,SACO,KAAK;AACV,YAAQ,MAAM,aAAa,gBAAgB,GAAG;AAC9C,WAAO;AAAA,EACT;AACF;AAOO,IAAM,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAY9B,KAAK,MAAc,UAAU,GAAW;AACtC,WAAO,GAAG,IAAI,KAAK,OAAO;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,QAAwB,MAAc,cAAiB,UAAU,GAAM;AACrE,QAAI,UAAU,GAAG;AACf,WAAK,WAAW,MAAM,UAAU,CAAC;AAAA,IACnC;AACA,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,UAAM,QAAQ,aAAa,QAAQ,GAAG;AACtC,QAAI,UAAU,KAAM,QAAO;AAC3B,UAAM,OAAO,UAAa,KAAK;AAC/B,QAAI,SAAS,QAAQ,OAAO,SAAS,SAAU,QAAO;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,QAAwB,MAAc,OAAU,UAAU,GAAS;AACjE,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,iBAAa,QAAQ,KAAK,KAAK,UAAU,KAAK,CAAC;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,WAAW,MAAc,UAAU,GAAS;AAC1C,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,WAAO,aAAa,WAAW,GAAG;AAAA,EACpC;AACF;",
4
+ "sourcesContent": ["import {packageTracer} from '@alwatr/package-tracer';\n\n__dev_mode__: packageTracer.add(__package_name__, __package_version__);\n\n/**\n * Parse json string without throwing error.\n *\n * @param content - json string\n * @returns json object or null if json is invalid\n * @example\n * ```typescript\n * const json = parseJson('{\"a\":1,\"b\":2}');\n * console.log(json.a); // 1\n * ```\n */\nfunction parseJson<T extends JsonValue>(content: string): T | null {\n try {\n return JSON.parse(content);\n }\n catch (err) {\n console.error('parseJson', 'invalid_json', err);\n return null;\n }\n}\n\n// @TODO: localStorage polyfill (memory fallback)\n\n/**\n * A utility object for working with local storage and JSON data.\n */\nexport const localJsonStorage = {\n /**\n * Generate local storage key.\n *\n * @param name - Name of the item.\n * @param version - Version of the item (default: 1).\n * @returns The generated local storage key.\n * @example\n * ```typescript\n * localJsonStorage.key_('myItem', 1); // myItem.v1\n * ```\n */\n key_(name: string, version = 1): string {\n return `${name}.v${version}`;\n },\n\n /**\n * Get the local storage item and parse it as JSON.\n * If the item is not found, return the default value.\n * If the version is greater than 1, remove the previous version.\n * If the item is not a valid JSON object, return the default value.\n *\n * @param name - The name of the item.\n * @param defaultValue - The default value of the item.\n * @param version - The data structure version of the item (default: 1).\n * @returns The parsed JSON value or the default value if the item is not found.\n * @example\n * ```typescript\n * const value = localJsonStorage.getItem('myItem', {a: 1, b: 2});\n * ```\n */\n getItem<T extends Json>(name: string, defaultValue: T, version = 1): T {\n if (version > 1) {\n this.removeItem(name, version - 1);\n }\n const key = this.key_(name, version);\n const value = localStorage.getItem(key);\n if (value === null) return defaultValue;\n const json = parseJson<T>(value);\n if (json === null || typeof json !== 'object') return defaultValue;\n return json;\n },\n\n /**\n * Set local storage item as JSON.\n *\n * @param name - Name of the item.\n * @param value - Value of the item.\n * @param version - Version of the item.\n * @example\n * ```typescript\n * localJsonStorage.setItem('myItem', {a: 1, b: 2});\n * ```\n */\n setItem<T extends Json>(name: string, value: T, version = 1): void {\n const key = this.key_(name, version);\n localStorage.setItem(key, JSON.stringify(value));\n },\n\n /**\n * Removes an item from the local storage.\n *\n * @param name - The name of the item to remove.\n * @param version - The version of the item to remove. Default is 1.\n * @example\n * ```typescript\n * localJsonStorage.removeItem('myItem');\n * ```\n */\n removeItem(name: string, version = 1): void {\n const key = this.key_(name, version);\n localStorage.removeItem(key);\n },\n} as const;\n"],
5
+ "mappings": ";;;AAAA,SAAQ,qBAAoB;AAE5B,aAAc,eAAc,IAAI,yBAAkB,OAAmB;AAarE,SAAS,UAA+B,SAA2B;AACjE,MAAI;AACF,WAAO,KAAK,MAAM,OAAO;AAAA,EAC3B,SACO,KAAK;AACV,YAAQ,MAAM,aAAa,gBAAgB,GAAG;AAC9C,WAAO;AAAA,EACT;AACF;AAOO,IAAM,mBAAmB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAY9B,KAAK,MAAc,UAAU,GAAW;AACtC,WAAO,GAAG,IAAI,KAAK,OAAO;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAiBA,QAAwB,MAAc,cAAiB,UAAU,GAAM;AACrE,QAAI,UAAU,GAAG;AACf,WAAK,WAAW,MAAM,UAAU,CAAC;AAAA,IACnC;AACA,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,UAAM,QAAQ,aAAa,QAAQ,GAAG;AACtC,QAAI,UAAU,KAAM,QAAO;AAC3B,UAAM,OAAO,UAAa,KAAK;AAC/B,QAAI,SAAS,QAAQ,OAAO,SAAS,SAAU,QAAO;AACtD,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAaA,QAAwB,MAAc,OAAU,UAAU,GAAS;AACjE,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,iBAAa,QAAQ,KAAK,KAAK,UAAU,KAAK,CAAC;AAAA,EACjD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,WAAW,MAAc,UAAU,GAAS;AAC1C,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,iBAAa,WAAW,GAAG;AAAA,EAC7B;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alwatr/local-storage",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "`localJsonStorage` is a utility object in our TypeScript package that provides methods for interacting with the local storage in a structured and versioned manner.",
5
5
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
6
  "keywords": [
@@ -68,14 +68,14 @@
68
68
  "clean": "rm -rfv dist *.tsbuildinfo"
69
69
  },
70
70
  "dependencies": {
71
- "@alwatr/package-tracer": "^1.0.6"
71
+ "@alwatr/package-tracer": "^1.0.7"
72
72
  },
73
73
  "devDependencies": {
74
- "@alwatr/nano-build": "^2.0.2",
74
+ "@alwatr/nano-build": "^2.0.3",
75
75
  "@alwatr/prettier-config": "^1.0.6",
76
- "@alwatr/tsconfig-base": "^1.3.2",
77
- "@alwatr/type-helper": "^2.0.2",
76
+ "@alwatr/tsconfig-base": "^1.3.3",
77
+ "@alwatr/type-helper": "^2.0.3",
78
78
  "typescript": "^5.6.3"
79
79
  },
80
- "gitHead": "15d12342d2e69fbb6129675cce620132c5bd0bfc"
80
+ "gitHead": "a1e430a02f9a5711fc43f19012091bcd8806c4f9"
81
81
  }