nhb-toolbox 4.26.73 → 4.26.74
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 +4 -0
- package/dist/cjs/dom/storage.js +54 -25
- package/dist/dts/dom/storage.d.ts +27 -22
- package/dist/dts/types/index.d.ts +4 -0
- package/dist/esm/dom/storage.js +52 -22
- package/package.json +6 -6
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,10 @@ All notable changes to the package will be documented here.
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
+
## [4.26.74] - 2025-11-23
|
|
10
|
+
|
|
11
|
+
- **Added** optional `serializer` and `deserializer` for *local and session storage* utilities with *improved error handling*.
|
|
12
|
+
|
|
9
13
|
## [4.26.73] - 2025-11-22
|
|
10
14
|
|
|
11
15
|
- **Added** the **missing** *closing parenthesis* in `INDIA_VEDIC_SEASONS` *constant* within the `seasons` module.
|
package/dist/cjs/dom/storage.js
CHANGED
|
@@ -1,33 +1,62 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.removeFromSessionStorage = exports.saveToSessionStorage = exports.getFromSessionStorage = exports.removeFromLocalStorage = exports.saveToLocalStorage = exports.getFromLocalStorage = void 0;
|
|
4
|
-
const getFromLocalStorage = (key) => {
|
|
5
|
-
const item = localStorage.getItem(key);
|
|
6
|
-
if (!item)
|
|
7
|
-
return null;
|
|
8
|
-
return JSON.parse(item);
|
|
9
|
-
};
|
|
10
3
|
exports.getFromLocalStorage = getFromLocalStorage;
|
|
11
|
-
const saveToLocalStorage = (key, value) => {
|
|
12
|
-
localStorage.setItem(key, JSON.stringify(value));
|
|
13
|
-
};
|
|
14
4
|
exports.saveToLocalStorage = saveToLocalStorage;
|
|
15
|
-
const removeFromLocalStorage = (key) => {
|
|
16
|
-
localStorage.removeItem(key);
|
|
17
|
-
};
|
|
18
5
|
exports.removeFromLocalStorage = removeFromLocalStorage;
|
|
19
|
-
const getFromSessionStorage = (key) => {
|
|
20
|
-
const item = sessionStorage.getItem(key);
|
|
21
|
-
if (!item)
|
|
22
|
-
return null;
|
|
23
|
-
return JSON.parse(item);
|
|
24
|
-
};
|
|
25
6
|
exports.getFromSessionStorage = getFromSessionStorage;
|
|
26
|
-
const saveToSessionStorage = (key, value) => {
|
|
27
|
-
sessionStorage.setItem(key, JSON.stringify(value));
|
|
28
|
-
};
|
|
29
7
|
exports.saveToSessionStorage = saveToSessionStorage;
|
|
30
|
-
const removeFromSessionStorage = (key) => {
|
|
31
|
-
sessionStorage.removeItem(key);
|
|
32
|
-
};
|
|
33
8
|
exports.removeFromSessionStorage = removeFromSessionStorage;
|
|
9
|
+
function getFromLocalStorage(key, deserialize) {
|
|
10
|
+
const deserializer = deserialize ?? JSON.parse;
|
|
11
|
+
try {
|
|
12
|
+
const item = localStorage.getItem(key);
|
|
13
|
+
return item ? deserializer(item) : null;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function saveToLocalStorage(key, value, serialize) {
|
|
20
|
+
const serializer = serialize ?? JSON.stringify;
|
|
21
|
+
try {
|
|
22
|
+
localStorage.setItem(key, serializer(value));
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.error(`Error saving item with key "${key}" in local storage:`, error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function removeFromLocalStorage(key) {
|
|
29
|
+
try {
|
|
30
|
+
localStorage.removeItem(key);
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
console.error(`Error removing item with key "${key}" from local storage:`, error);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function getFromSessionStorage(key, deserialize) {
|
|
37
|
+
const deserializer = deserialize ?? JSON.parse;
|
|
38
|
+
try {
|
|
39
|
+
const item = sessionStorage.getItem(key);
|
|
40
|
+
return item ? deserializer(item) : null;
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function saveToSessionStorage(key, value, serialize) {
|
|
47
|
+
const serializer = serialize ?? JSON.stringify;
|
|
48
|
+
try {
|
|
49
|
+
sessionStorage.setItem(key, serializer(value));
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error(`Error saving item with key "${key}" in session storage:`, error);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
function removeFromSessionStorage(key) {
|
|
56
|
+
try {
|
|
57
|
+
sessionStorage.removeItem(key);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
console.error(`Error removing item with key "${key}" from session storage:`, error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -1,40 +1,45 @@
|
|
|
1
|
+
import type { Deserializer, Serializer } from '../types/index';
|
|
1
2
|
/**
|
|
2
|
-
* * Get item
|
|
3
|
+
* * Get item from local storage.
|
|
3
4
|
*
|
|
4
|
-
* @param key - Key to get item
|
|
5
|
-
* @
|
|
5
|
+
* @param key - Key to get item from local storage.
|
|
6
|
+
* @param deserialize - Optional deserializer function to convert the stored value back to type `T`. Defaults to `JSON.parse`.
|
|
7
|
+
* @returns Returns saved item from local storage if it exists with that key.
|
|
6
8
|
*/
|
|
7
|
-
export declare
|
|
9
|
+
export declare function getFromLocalStorage<T>(key: string, deserialize?: Deserializer<T>): T | null;
|
|
8
10
|
/**
|
|
9
|
-
* * Save item
|
|
11
|
+
* * Save item in local storage.
|
|
10
12
|
*
|
|
11
|
-
* @param key - Key to save an item
|
|
12
|
-
* @param value - The item
|
|
13
|
+
* @param key - Key to save an item.
|
|
14
|
+
* @param value - The item/value to save.
|
|
15
|
+
* @param serialize - Optional serializer function to convert the value of type `T` to a string. Defaults to `JSON.stringify`.
|
|
13
16
|
*/
|
|
14
|
-
export declare
|
|
17
|
+
export declare function saveToLocalStorage<T>(key: string, value: T, serialize?: Serializer<T>): void;
|
|
15
18
|
/**
|
|
16
|
-
* * Remove item
|
|
19
|
+
* * Remove item from local storage.
|
|
17
20
|
*
|
|
18
|
-
* @param key - Key to delete item
|
|
21
|
+
* @param key - Key to delete item from local storage.
|
|
19
22
|
*/
|
|
20
|
-
export declare
|
|
23
|
+
export declare function removeFromLocalStorage(key: string): void;
|
|
21
24
|
/**
|
|
22
|
-
* * Get item
|
|
25
|
+
* * Get item from session storage.
|
|
23
26
|
*
|
|
24
|
-
* @param key - Key to get item
|
|
25
|
-
* @
|
|
27
|
+
* @param key - Key to get item from session storage.
|
|
28
|
+
* @param deserialize - Optional deserializer function to convert the stored value back to type `T`. Defaults to `JSON.parse`.
|
|
29
|
+
* @returns Returns saved item from session storage if it exists with that key.
|
|
26
30
|
*/
|
|
27
|
-
export declare
|
|
31
|
+
export declare function getFromSessionStorage<T>(key: string, deserialize?: Deserializer<T>): T | null;
|
|
28
32
|
/**
|
|
29
|
-
* * Save item
|
|
33
|
+
* * Save item in session storage.
|
|
30
34
|
*
|
|
31
|
-
* @param key - Key to save an item
|
|
32
|
-
* @param value - The item
|
|
35
|
+
* @param key - Key to save an item.
|
|
36
|
+
* @param value - The item/value to save.
|
|
37
|
+
* @param serialize - Optional serializer function to convert the value of type `T` to a string. Defaults to `JSON.stringify`.
|
|
33
38
|
*/
|
|
34
|
-
export declare
|
|
39
|
+
export declare function saveToSessionStorage<T>(key: string, value: T, serialize?: Serializer<T>): void;
|
|
35
40
|
/**
|
|
36
|
-
* * Remove item
|
|
41
|
+
* * Remove item from session storage.
|
|
37
42
|
*
|
|
38
|
-
* @param key - Key to delete item
|
|
43
|
+
* @param key - Key to delete item from session storage.
|
|
39
44
|
*/
|
|
40
|
-
export declare
|
|
45
|
+
export declare function removeFromSessionStorage(key: string): void;
|
|
@@ -133,4 +133,8 @@ export type ValidArray<T> = [T, ...Array<T>];
|
|
|
133
133
|
* arr.push(4); // ❌ Error (readonly)
|
|
134
134
|
*/
|
|
135
135
|
export type List<T = any> = ReadonlyArray<T>;
|
|
136
|
+
/** Function type for serializing a value of type `T` to a string. */
|
|
137
|
+
export type Serializer<T> = (value: T) => string;
|
|
138
|
+
/** Function type for deserializing a string to a value of type `T`. */
|
|
139
|
+
export type Deserializer<T> = (value: string) => T;
|
|
136
140
|
export {};
|
package/dist/esm/dom/storage.js
CHANGED
|
@@ -1,24 +1,54 @@
|
|
|
1
|
-
export
|
|
2
|
-
const
|
|
3
|
-
|
|
1
|
+
export function getFromLocalStorage(key, deserialize) {
|
|
2
|
+
const deserializer = deserialize ?? JSON.parse;
|
|
3
|
+
try {
|
|
4
|
+
const item = localStorage.getItem(key);
|
|
5
|
+
return item ? deserializer(item) : null;
|
|
6
|
+
}
|
|
7
|
+
catch {
|
|
4
8
|
return null;
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
export function saveToLocalStorage(key, value, serialize) {
|
|
12
|
+
const serializer = serialize ?? JSON.stringify;
|
|
13
|
+
try {
|
|
14
|
+
localStorage.setItem(key, serializer(value));
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
console.error(`Error saving item with key "${key}" in local storage:`, error);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function removeFromLocalStorage(key) {
|
|
21
|
+
try {
|
|
22
|
+
localStorage.removeItem(key);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
console.error(`Error removing item with key "${key}" from local storage:`, error);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function getFromSessionStorage(key, deserialize) {
|
|
29
|
+
const deserializer = deserialize ?? JSON.parse;
|
|
30
|
+
try {
|
|
31
|
+
const item = sessionStorage.getItem(key);
|
|
32
|
+
return item ? deserializer(item) : null;
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
16
35
|
return null;
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function saveToSessionStorage(key, value, serialize) {
|
|
39
|
+
const serializer = serialize ?? JSON.stringify;
|
|
40
|
+
try {
|
|
41
|
+
sessionStorage.setItem(key, serializer(value));
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error(`Error saving item with key "${key}" in session storage:`, error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function removeFromSessionStorage(key) {
|
|
48
|
+
try {
|
|
49
|
+
sessionStorage.removeItem(key);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
console.error(`Error removing item with key "${key}" from session storage:`, error);
|
|
53
|
+
}
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nhb-toolbox",
|
|
3
|
-
"version": "4.26.
|
|
3
|
+
"version": "4.26.74",
|
|
4
4
|
"description": "A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -42,20 +42,20 @@
|
|
|
42
42
|
"@eslint/js": "^9.39.1",
|
|
43
43
|
"@types/jest": "^30.0.0",
|
|
44
44
|
"@types/node": "^24.10.1",
|
|
45
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
46
|
-
"@typescript-eslint/parser": "^8.
|
|
45
|
+
"@typescript-eslint/eslint-plugin": "^8.47.0",
|
|
46
|
+
"@typescript-eslint/parser": "^8.47.0",
|
|
47
47
|
"eslint": "^9.39.1",
|
|
48
48
|
"eslint-config-prettier": "^10.1.8",
|
|
49
49
|
"eslint-plugin-prettier": "^5.5.4",
|
|
50
50
|
"globals": "^16.5.0",
|
|
51
51
|
"husky": "^9.1.7",
|
|
52
52
|
"jest": "^30.2.0",
|
|
53
|
-
"lint-staged": "^16.2.
|
|
54
|
-
"nhb-scripts": "^1.8.
|
|
53
|
+
"lint-staged": "^16.2.7",
|
|
54
|
+
"nhb-scripts": "^1.8.88",
|
|
55
55
|
"prettier": "^3.6.2",
|
|
56
56
|
"ts-jest": "^29.4.5",
|
|
57
57
|
"typescript": "^5.9.3",
|
|
58
|
-
"typescript-eslint": "^8.
|
|
58
|
+
"typescript-eslint": "^8.47.0"
|
|
59
59
|
},
|
|
60
60
|
"keywords": [
|
|
61
61
|
"toolbox",
|