@alwatr/local-storage 5.0.0 → 5.4.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.
- package/CHANGELOG.md +42 -0
- package/README.md +0 -2
- package/dist/main.cjs +24 -4
- package/dist/main.cjs.map +2 -2
- package/dist/main.d.ts +12 -0
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +24 -4
- package/dist/main.mjs.map +2 -2
- package/package.json +6 -6
- package/dist/main.cjs.LEGAL.txt +0 -0
- package/dist/main.mjs.LEGAL.txt +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,48 @@
|
|
|
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
|
+
## [5.4.0](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.1.8...@alwatr/local-storage@5.4.0) (2025-02-18)
|
|
7
|
+
|
|
8
|
+
## 5.3.0 (2025-02-03)
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* **local-storage:** add setItem after getItem and add hasItem ([a1b845d](https://github.com/Alwatr/nanolib/commit/a1b845d8d9d207b514f9e06c37610220337b0235)) by @
|
|
13
|
+
|
|
14
|
+
### Miscellaneous Chores
|
|
15
|
+
|
|
16
|
+
* edit README ([3860b3d](https://github.com/Alwatr/nanolib/commit/3860b3df48ab82dc479d5236c2e8579df614aabf)) by @
|
|
17
|
+
|
|
18
|
+
### Dependencies update
|
|
19
|
+
|
|
20
|
+
* bump the development-dependencies group across 1 directory with 11 updates ([cb79d07](https://github.com/Alwatr/nanolib/commit/cb79d072a57c79e1c01abff1a293d6757bb65350)) by @
|
|
21
|
+
* update typescript and @types/node to version 5.7.3 and 22.13.0 respectively across multiple packages ([ddab05b](https://github.com/Alwatr/nanolib/commit/ddab05b5d767c30191f36a065e4bc88744e8e3fe)) by @
|
|
22
|
+
|
|
23
|
+
## 5.0.0 (2024-11-02)
|
|
24
|
+
|
|
25
|
+
### ⚠ BREAKING CHANGES
|
|
26
|
+
|
|
27
|
+
* To simplify version management and ensure consistency, all nanolib packages now use the same version as @alwatr/nanolib. This may require updates to your project's dependencies.
|
|
28
|
+
|
|
29
|
+
### Code Refactoring
|
|
30
|
+
|
|
31
|
+
* use the same version as @alwatr/nanolib ([60eb860](https://github.com/Alwatr/nanolib/commit/60eb860a0e33dfffe2d1d95e63ce54c60876be06)) by @
|
|
32
|
+
|
|
33
|
+
## [5.3.0](https://github.com/Alwatr/nanolib/compare/v5.2.1...v5.3.0) (2025-02-03)
|
|
34
|
+
|
|
35
|
+
### Features
|
|
36
|
+
|
|
37
|
+
* **local-storage:** add setItem after getItem and add hasItem ([a1b845d](https://github.com/Alwatr/nanolib/commit/a1b845d8d9d207b514f9e06c37610220337b0235)) by @ArmanAsadian
|
|
38
|
+
|
|
39
|
+
### Miscellaneous Chores
|
|
40
|
+
|
|
41
|
+
* edit README ([3860b3d](https://github.com/Alwatr/nanolib/commit/3860b3df48ab82dc479d5236c2e8579df614aabf)) by @ArmanAsadian
|
|
42
|
+
|
|
43
|
+
### Dependencies update
|
|
44
|
+
|
|
45
|
+
* bump the development-dependencies group across 1 directory with 11 updates ([cb79d07](https://github.com/Alwatr/nanolib/commit/cb79d072a57c79e1c01abff1a293d6757bb65350)) by @dependabot[bot]
|
|
46
|
+
* update typescript and @types/node to version 5.7.3 and 22.13.0 respectively across multiple packages ([ddab05b](https://github.com/Alwatr/nanolib/commit/ddab05b5d767c30191f36a065e4bc88744e8e3fe)) by @alimd
|
|
47
|
+
|
|
6
48
|
## 5.0.0 (2024-11-02)
|
|
7
49
|
|
|
8
50
|
### ⚠ BREAKING CHANGES
|
package/README.md
CHANGED
|
@@ -64,8 +64,6 @@ We plan to add more methods to `localJsonStorage` for directly interacting with
|
|
|
64
64
|
|
|
65
65
|
The following companies, organizations, and individuals support Nanolib ongoing maintenance and development. Become a Sponsor to get your logo on our README and website.
|
|
66
66
|
|
|
67
|
-
[](https://exirstudio.com)
|
|
68
|
-
|
|
69
67
|
### Contributing
|
|
70
68
|
|
|
71
69
|
Contributions are welcome! Please read our [contribution guidelines](https://github.com/Alwatr/.github/blob/next/CONTRIBUTING.md) before submitting a pull request.
|
package/dist/main.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/* @alwatr/local-storage v5.
|
|
1
|
+
/* @alwatr/local-storage v5.4.0 */
|
|
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", "5.
|
|
28
|
+
__dev_mode__: import_package_tracer.packageTracer.add("@alwatr/local-storage", "5.4.0");
|
|
29
29
|
function parseJson(content) {
|
|
30
30
|
try {
|
|
31
31
|
return JSON.parse(content);
|
|
@@ -65,16 +65,37 @@ var localJsonStorage = {
|
|
|
65
65
|
* ```
|
|
66
66
|
*/
|
|
67
67
|
getItem(name, defaultValue, version = 1) {
|
|
68
|
+
if (version > 1) {
|
|
69
|
+
this.removeItem(name, version - 1);
|
|
70
|
+
}
|
|
68
71
|
if (version > 1) {
|
|
69
72
|
this.removeItem(name, version - 1);
|
|
70
73
|
}
|
|
71
74
|
const key = this.key_(name, version);
|
|
72
75
|
const value = localStorage.getItem(key);
|
|
73
|
-
if (value === null)
|
|
76
|
+
if (value === null) {
|
|
77
|
+
localStorage.setItem(key, JSON.stringify(defaultValue));
|
|
78
|
+
return defaultValue;
|
|
79
|
+
}
|
|
74
80
|
const json = parseJson(value);
|
|
75
81
|
if (json === null || typeof json !== "object") return defaultValue;
|
|
76
82
|
return json;
|
|
77
83
|
},
|
|
84
|
+
/**
|
|
85
|
+
* Check if an item exists in local storage.
|
|
86
|
+
*
|
|
87
|
+
* @param name - The name of the item.
|
|
88
|
+
* @param version - The version of the item (default: 1).
|
|
89
|
+
* @returns True if the item exists, false otherwise.
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const exists = localJsonStorage.hasItem('myItem');
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
hasItem(name, version = 1) {
|
|
96
|
+
const key = this.key_(name, version);
|
|
97
|
+
return localStorage.getItem(key) !== null;
|
|
98
|
+
},
|
|
78
99
|
/**
|
|
79
100
|
* Set local storage item as JSON.
|
|
80
101
|
*
|
|
@@ -109,5 +130,4 @@ var localJsonStorage = {
|
|
|
109
130
|
0 && (module.exports = {
|
|
110
131
|
localJsonStorage
|
|
111
132
|
});
|
|
112
|
-
/*! For license information please see main.cjs.LEGAL.txt */
|
|
113
133
|
//# sourceMappingURL=main.cjs.map
|
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 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,
|
|
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 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) {\n localStorage.setItem(key, JSON.stringify(defaultValue));\n return defaultValue;\n }\n const json = parseJson<T>(value);\n if (json === null || typeof json !== 'object') return defaultValue;\n return json;\n },\n\n /**\n * Check if an item exists in local storage.\n *\n * @param name - The name of the item.\n * @param version - The version of the item (default: 1).\n * @returns True if the item exists, false otherwise.\n * @example\n * ```typescript\n * const exists = localJsonStorage.hasItem('myItem');\n * ```\n */\n hasItem(name: string, version = 1): boolean {\n const key = this.key_(name, version);\n return localStorage.getItem(key) !== null;\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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AC1C,UAAM,MAAM,KAAK,KAAK,MAAM,OAAO;AACnC,iBAAa,WAAW,GAAG;AAAA,EAC7B;AACF;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/main.d.ts
CHANGED
|
@@ -30,6 +30,18 @@ export declare const localJsonStorage: {
|
|
|
30
30
|
* ```
|
|
31
31
|
*/
|
|
32
32
|
readonly getItem: <T extends Json>(name: string, defaultValue: T, version?: number) => T;
|
|
33
|
+
/**
|
|
34
|
+
* Check if an item exists in local storage.
|
|
35
|
+
*
|
|
36
|
+
* @param name - The name of the item.
|
|
37
|
+
* @param version - The version of the item (default: 1).
|
|
38
|
+
* @returns True if the item exists, false otherwise.
|
|
39
|
+
* @example
|
|
40
|
+
* ```typescript
|
|
41
|
+
* const exists = localJsonStorage.hasItem('myItem');
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
readonly hasItem: (name: string, version?: number) => boolean;
|
|
33
45
|
/**
|
|
34
46
|
* Set local storage item as JSON.
|
|
35
47
|
*
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AA2BA;;GAEG;AACH,eAAO,MAAM,gBAAgB;IAC3B;;;;;;;;;;OAUG;0BACQ,MAAM,uBAAgB,MAAM;IAIvC;;;;;;;;;;;;;;OAcG;uBACK,CAAC,SAAS,IAAI,QAAQ,MAAM,gBAAgB,CAAC,uBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AA2BA;;GAEG;AACH,eAAO,MAAM,gBAAgB;IAC3B;;;;;;;;;;OAUG;0BACQ,MAAM,uBAAgB,MAAM;IAIvC;;;;;;;;;;;;;;OAcG;uBACK,CAAC,SAAS,IAAI,QAAQ,MAAM,gBAAgB,CAAC,uBAAgB,CAAC;IAkBtE;;;;;;;;;;OAUG;6BACW,MAAM,uBAAgB,OAAO;IAK3C;;;;;;;;;;OAUG;uBACK,CAAC,SAAS,IAAI,QAAQ,MAAM,SAAS,CAAC,uBAAgB,IAAI;IAKlE;;;;;;;;;OASG;gCACc,MAAM,uBAAgB,IAAI;CAInC,CAAC"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
/* @alwatr/local-storage v5.
|
|
1
|
+
/* @alwatr/local-storage v5.4.0 */
|
|
2
2
|
|
|
3
3
|
// src/main.ts
|
|
4
4
|
import { packageTracer } from "@alwatr/package-tracer";
|
|
5
|
-
__dev_mode__: packageTracer.add("@alwatr/local-storage", "5.
|
|
5
|
+
__dev_mode__: packageTracer.add("@alwatr/local-storage", "5.4.0");
|
|
6
6
|
function parseJson(content) {
|
|
7
7
|
try {
|
|
8
8
|
return JSON.parse(content);
|
|
@@ -42,16 +42,37 @@ var localJsonStorage = {
|
|
|
42
42
|
* ```
|
|
43
43
|
*/
|
|
44
44
|
getItem(name, defaultValue, version = 1) {
|
|
45
|
+
if (version > 1) {
|
|
46
|
+
this.removeItem(name, version - 1);
|
|
47
|
+
}
|
|
45
48
|
if (version > 1) {
|
|
46
49
|
this.removeItem(name, version - 1);
|
|
47
50
|
}
|
|
48
51
|
const key = this.key_(name, version);
|
|
49
52
|
const value = localStorage.getItem(key);
|
|
50
|
-
if (value === null)
|
|
53
|
+
if (value === null) {
|
|
54
|
+
localStorage.setItem(key, JSON.stringify(defaultValue));
|
|
55
|
+
return defaultValue;
|
|
56
|
+
}
|
|
51
57
|
const json = parseJson(value);
|
|
52
58
|
if (json === null || typeof json !== "object") return defaultValue;
|
|
53
59
|
return json;
|
|
54
60
|
},
|
|
61
|
+
/**
|
|
62
|
+
* Check if an item exists in local storage.
|
|
63
|
+
*
|
|
64
|
+
* @param name - The name of the item.
|
|
65
|
+
* @param version - The version of the item (default: 1).
|
|
66
|
+
* @returns True if the item exists, false otherwise.
|
|
67
|
+
* @example
|
|
68
|
+
* ```typescript
|
|
69
|
+
* const exists = localJsonStorage.hasItem('myItem');
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
hasItem(name, version = 1) {
|
|
73
|
+
const key = this.key_(name, version);
|
|
74
|
+
return localStorage.getItem(key) !== null;
|
|
75
|
+
},
|
|
55
76
|
/**
|
|
56
77
|
* Set local storage item as JSON.
|
|
57
78
|
*
|
|
@@ -85,5 +106,4 @@ var localJsonStorage = {
|
|
|
85
106
|
export {
|
|
86
107
|
localJsonStorage
|
|
87
108
|
};
|
|
88
|
-
/*! For license information please see main.mjs.LEGAL.txt */
|
|
89
109
|
//# sourceMappingURL=main.mjs.map
|
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 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,
|
|
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 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) {\n localStorage.setItem(key, JSON.stringify(defaultValue));\n return defaultValue;\n }\n const json = parseJson<T>(value);\n if (json === null || typeof json !== 'object') return defaultValue;\n return json;\n },\n\n /**\n * Check if an item exists in local storage.\n *\n * @param name - The name of the item.\n * @param version - The version of the item (default: 1).\n * @returns True if the item exists, false otherwise.\n * @example\n * ```typescript\n * const exists = localJsonStorage.hasItem('myItem');\n * ```\n */\n hasItem(name: string, version = 1): boolean {\n const key = this.key_(name, version);\n return localStorage.getItem(key) !== null;\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,MA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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": "^5.
|
|
71
|
+
"@alwatr/package-tracer": "^5.4.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@alwatr/nano-build": "^5.
|
|
74
|
+
"@alwatr/nano-build": "^5.4.0",
|
|
75
75
|
"@alwatr/prettier-config": "^5.0.0",
|
|
76
76
|
"@alwatr/tsconfig-base": "^5.0.0",
|
|
77
|
-
"@alwatr/type-helper": "^5.
|
|
78
|
-
"typescript": "^5.
|
|
77
|
+
"@alwatr/type-helper": "^5.3.0",
|
|
78
|
+
"typescript": "^5.7.3"
|
|
79
79
|
},
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "074be0d0fe780bf929e163045f8f598cd2697636"
|
|
81
81
|
}
|
package/dist/main.cjs.LEGAL.txt
DELETED
|
File without changes
|
package/dist/main.mjs.LEGAL.txt
DELETED
|
File without changes
|