@alwatr/local-storage 1.0.1 → 1.0.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.
- package/CHANGELOG.md +11 -3
- package/README.md +4 -4
- package/dist/main.cjs +1 -1
- package/dist/main.cjs.map +2 -2
- package/dist/main.d.ts.map +1 -1
- package/dist/main.mjs +1 -1
- package/dist/main.mjs.map +2 -2
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
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.0.3](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.2...@alwatr/local-storage@1.0.3) (2024-03-28)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @alwatr/local-storage
|
|
9
|
+
|
|
10
|
+
## [1.0.2](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.1...@alwatr/local-storage@1.0.2) (2024-01-31)
|
|
11
|
+
|
|
12
|
+
**Note:** Version bump only for package @alwatr/local-storage
|
|
13
|
+
|
|
6
14
|
## [1.0.1](https://github.com/Alwatr/nanolib/compare/@alwatr/local-storage@1.0.0...@alwatr/local-storage@1.0.1) (2024-01-24)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @alwatr/local-storage
|
|
@@ -11,9 +19,9 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
|
|
|
11
19
|
|
|
12
20
|
### Features
|
|
13
21
|
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
- **local-storage:** Rewrite local storage module ([5ed0f39](https://github.com/Alwatr/nanolib/commit/5ed0f39d090c027bb600a5d061ede887b4669198)) by @AliMD
|
|
23
|
+
- **local-storage:** separate package from `@alwatr/util` ([4cc22ed](https://github.com/Alwatr/nanolib/commit/4cc22eda8d89f291783eef3b8917434489b628e1)) by @njfamirm
|
|
16
24
|
|
|
17
25
|
### Bug Fixes
|
|
18
26
|
|
|
19
|
-
|
|
27
|
+
- **local-storage:** Remove unused dependency and update tsconfig references ([b1caaea](https://github.com/Alwatr/nanolib/commit/b1caaea8565cd497d31d91e4e2ea1becd84a82a4)) by @AliMD
|
package/README.md
CHANGED
|
@@ -21,13 +21,13 @@ yarn add @alwatr/local-storage
|
|
|
21
21
|
First, you need to import `localJsonStorage` from the package:
|
|
22
22
|
|
|
23
23
|
```typescript
|
|
24
|
-
import {
|
|
24
|
+
import {localJsonStorage} from '@alwatr/local-storage';
|
|
25
25
|
```
|
|
26
26
|
|
|
27
27
|
Or for CommonJS:
|
|
28
28
|
|
|
29
29
|
```javascript
|
|
30
|
-
const {
|
|
30
|
+
const {localJsonStorage} = require('@alwatr/local-storage');
|
|
31
31
|
```
|
|
32
32
|
|
|
33
33
|
### Getting an Item
|
|
@@ -35,7 +35,7 @@ const { localJsonStorage } = require('@alwatr/local-storage');
|
|
|
35
35
|
You can get an item from local storage and parse it as JSON using the `getItem` method. If the item is not found, it will return a default value:
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
|
-
const defaultValue = {
|
|
38
|
+
const defaultValue = {a: 1, b: 2};
|
|
39
39
|
const value = localJsonStorage.getItem('item-name', defaultValue);
|
|
40
40
|
```
|
|
41
41
|
|
|
@@ -44,7 +44,7 @@ const value = localJsonStorage.getItem('item-name', defaultValue);
|
|
|
44
44
|
You can set an item in local storage as JSON using the `setItem` method:
|
|
45
45
|
|
|
46
46
|
```typescript
|
|
47
|
-
const value = {
|
|
47
|
+
const value = {a: 1, b: 2};
|
|
48
48
|
localJsonStorage.setItem('item-name', value);
|
|
49
49
|
```
|
|
50
50
|
|
package/dist/main.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* @alwatr/local-storage v1.0.
|
|
1
|
+
/* @alwatr/local-storage v1.0.3 */
|
|
2
2
|
"use strict";var l=Object.defineProperty;var i=Object.getOwnPropertyDescriptor;var a=Object.getOwnPropertyNames;var c=Object.prototype.hasOwnProperty;var g=(t,e)=>{for(var n in e)l(t,n,{get:e[n],enumerable:!0})},u=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of a(e))!c.call(t,o)&&o!==n&&l(t,o,{get:()=>e[o],enumerable:!(r=i(e,o))||r.enumerable});return t};var y=t=>u(l({},"__esModule",{value:!0}),t);var p={};g(p,{localJsonStorage:()=>J});module.exports=y(p);function m(t){try{return JSON.parse(t)}catch(e){return console.error("parseJson","invalid_json",e),null}}var J={key_(t,e=1){return`${t}.v${e}`},getItem(t,e,n=1){let r=this.key_(t,n),o=localStorage.getItem(r);if(o===null)return e;let s=m(o);return s===null||typeof s!="object"?e:s},setItem(t,e,n=1){let r=this.key_(t,n);localStorage.setItem(r,JSON.stringify(e))},removeItem(t,e=1){let n=this.key_(t,e);window.localStorage.removeItem(n)}};0&&(module.exports={localJsonStorage});
|
|
3
3
|
//# 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 type {Json, JsonValue} from '@alwatr/type-helper';\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
|
|
5
|
-
"mappings": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,sBAAAE,IAAA,eAAAC,EAAAH,GAaA,SAASI,EAA+BC,EAA2B,CACjE,GAAI,CACF,OAAO,KAAK,MAAMA,CAAO,CAC3B,OACOC,EAAK,CACV,eAAQ,MAAM,YAAa,eAAgBA,CAAG,EACvC,IACT,CACF,
|
|
4
|
+
"sourcesContent": ["import type {Json, JsonValue} from '@alwatr/type-helper';\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 local storage item and parse it as JSON.\n * If item is not found, return default value.\n *\n * @param name - Name of the item.\n * @param defaultValue - Default value of the item.\n * @param version - Data structure version of the item (default: 1).\n * @returns The parsed JSON value or the default value if 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 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": ";yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,sBAAAE,IAAA,eAAAC,EAAAH,GAaA,SAASI,EAA+BC,EAA2B,CACjE,GAAI,CACF,OAAO,KAAK,MAAMA,CAAO,CAC3B,OACOC,EAAK,CACV,eAAQ,MAAM,YAAa,eAAgBA,CAAG,EACvC,IACT,CACF,CAOO,IAAMJ,EAAmB,CAY9B,KAAKK,EAAcC,EAAU,EAAW,CACtC,MAAO,GAAGD,CAAI,KAAKC,CAAO,EAC5B,EAeA,QAAwBD,EAAcE,EAAiBD,EAAU,EAAM,CACrE,IAAME,EAAM,KAAK,KAAKH,EAAMC,CAAO,EAC7BG,EAAQ,aAAa,QAAQD,CAAG,EACtC,GAAIC,IAAU,KAAM,OAAOF,EAC3B,IAAMG,EAAOR,EAAaO,CAAK,EAC/B,OAAIC,IAAS,MAAQ,OAAOA,GAAS,SAAiBH,EAC/CG,CACT,EAaA,QAAwBL,EAAcI,EAAUH,EAAU,EAAS,CACjE,IAAME,EAAM,KAAK,KAAKH,EAAMC,CAAO,EACnC,aAAa,QAAQE,EAAK,KAAK,UAAUC,CAAK,CAAC,CACjD,EAYA,WAAWJ,EAAcC,EAAU,EAAS,CAC1C,IAAME,EAAM,KAAK,KAAKH,EAAMC,CAAO,EACnC,OAAO,aAAa,WAAWE,CAAG,CACpC,CACF",
|
|
6
6
|
"names": ["main_exports", "__export", "localJsonStorage", "__toCommonJS", "parseJson", "content", "err", "name", "version", "defaultValue", "key", "value", "json"]
|
|
7
7
|
}
|
package/dist/main.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,IAAI,EAAY,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,IAAI,EAAY,MAAM,qBAAqB,CAAC;AAyBzD;;GAEG;AACH,eAAO,MAAM,gBAAgB;IAC3B;;;;;;;;;;OAUG;0BACQ,MAAM,uBAAgB,MAAM;IAIvC;;;;;;;;;;;;OAYG;6CAC2B,MAAM,gBAAgB,CAAC,uBAAgB,CAAC;IAStE;;;;;;;;;;OAUG;+CAC2B,MAAM,mCAA0B,IAAI;IAKlE;;;;;;;;;OASG;gCACc,MAAM,uBAAgB,IAAI;CAInC,CAAC"}
|
package/dist/main.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
/* @alwatr/local-storage v1.0.
|
|
1
|
+
/* @alwatr/local-storage v1.0.3 */
|
|
2
2
|
function l(t){try{return JSON.parse(t)}catch(e){return console.error("parseJson","invalid_json",e),null}}var i={key_(t,e=1){return`${t}.v${e}`},getItem(t,e,n=1){let o=this.key_(t,n),s=localStorage.getItem(o);if(s===null)return e;let r=l(s);return r===null||typeof r!="object"?e:r},setItem(t,e,n=1){let o=this.key_(t,n);localStorage.setItem(o,JSON.stringify(e))},removeItem(t,e=1){let n=this.key_(t,e);window.localStorage.removeItem(n)}};export{i as localJsonStorage};
|
|
3
3
|
//# 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 type {Json, JsonValue} from '@alwatr/type-helper';\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
|
|
5
|
-
"mappings": ";AAaA,SAASA,EAA+BC,EAA2B,CACjE,GAAI,CACF,OAAO,KAAK,MAAMA,CAAO,CAC3B,OACOC,EAAK,CACV,eAAQ,MAAM,YAAa,eAAgBA,CAAG,EACvC,IACT,CACF,
|
|
4
|
+
"sourcesContent": ["import type {Json, JsonValue} from '@alwatr/type-helper';\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 local storage item and parse it as JSON.\n * If item is not found, return default value.\n *\n * @param name - Name of the item.\n * @param defaultValue - Default value of the item.\n * @param version - Data structure version of the item (default: 1).\n * @returns The parsed JSON value or the default value if 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 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,SAASA,EAA+BC,EAA2B,CACjE,GAAI,CACF,OAAO,KAAK,MAAMA,CAAO,CAC3B,OACOC,EAAK,CACV,eAAQ,MAAM,YAAa,eAAgBA,CAAG,EACvC,IACT,CACF,CAOO,IAAMC,EAAmB,CAY9B,KAAKC,EAAcC,EAAU,EAAW,CACtC,MAAO,GAAGD,CAAI,KAAKC,CAAO,EAC5B,EAeA,QAAwBD,EAAcE,EAAiBD,EAAU,EAAM,CACrE,IAAME,EAAM,KAAK,KAAKH,EAAMC,CAAO,EAC7BG,EAAQ,aAAa,QAAQD,CAAG,EACtC,GAAIC,IAAU,KAAM,OAAOF,EAC3B,IAAMG,EAAOT,EAAaQ,CAAK,EAC/B,OAAIC,IAAS,MAAQ,OAAOA,GAAS,SAAiBH,EAC/CG,CACT,EAaA,QAAwBL,EAAcI,EAAUH,EAAU,EAAS,CACjE,IAAME,EAAM,KAAK,KAAKH,EAAMC,CAAO,EACnC,aAAa,QAAQE,EAAK,KAAK,UAAUC,CAAK,CAAC,CACjD,EAYA,WAAWJ,EAAcC,EAAU,EAAS,CAC1C,IAAME,EAAM,KAAK,KAAKH,EAAMC,CAAO,EACnC,OAAO,aAAa,WAAWE,CAAG,CACpC,CACF",
|
|
6
6
|
"names": ["parseJson", "content", "err", "localJsonStorage", "name", "version", "defaultValue", "key", "value", "json"]
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/local-storage",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
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": [
|
|
@@ -67,11 +67,11 @@
|
|
|
67
67
|
"clean": "rm -rfv dist *.tsbuildinfo"
|
|
68
68
|
},
|
|
69
69
|
"devDependencies": {
|
|
70
|
-
"@alwatr/nano-build": "^1.3.
|
|
70
|
+
"@alwatr/nano-build": "^1.3.4",
|
|
71
71
|
"@alwatr/prettier-config": "^1.0.4",
|
|
72
|
-
"@alwatr/tsconfig-base": "^1.1.
|
|
73
|
-
"@alwatr/type-helper": "^1.2.
|
|
74
|
-
"typescript": "^5.
|
|
72
|
+
"@alwatr/tsconfig-base": "^1.1.2",
|
|
73
|
+
"@alwatr/type-helper": "^1.2.2",
|
|
74
|
+
"typescript": "^5.4.3"
|
|
75
75
|
},
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "df149736d1e85335aaeb9c547ff3bdd1161f9b33"
|
|
77
77
|
}
|