@kikiutils/shared 9.1.0 → 9.3.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/README.md +37 -15
- package/dist/clipboard.cjs +87 -0
- package/dist/clipboard.cjs.map +1 -0
- package/dist/clipboard.d.ts +60 -0
- package/dist/clipboard.d.ts.map +1 -0
- package/dist/clipboard.mjs +84 -0
- package/dist/clipboard.mjs.map +1 -0
- package/dist/env.cjs +10 -13
- package/dist/env.cjs.map +1 -1
- package/dist/env.d.ts +9 -12
- package/dist/env.d.ts.map +1 -1
- package/dist/env.mjs +10 -13
- package/dist/env.mjs.map +1 -1
- package/dist/storage/enhanced/local/core.cjs +103 -0
- package/dist/storage/enhanced/local/core.cjs.map +1 -0
- package/dist/storage/enhanced/local/core.d.ts +56 -0
- package/dist/storage/enhanced/local/core.d.ts.map +1 -0
- package/dist/storage/enhanced/local/core.mjs +101 -0
- package/dist/storage/enhanced/local/core.mjs.map +1 -0
- package/dist/storage/enhanced/local/index.cjs +10 -0
- package/dist/storage/enhanced/local/index.cjs.map +1 -0
- package/dist/storage/enhanced/local/index.d.ts +3 -0
- package/dist/storage/enhanced/local/index.d.ts.map +1 -0
- package/dist/storage/enhanced/local/index.mjs +3 -0
- package/dist/storage/enhanced/local/index.mjs.map +1 -0
- package/dist/storage/enhanced/local/keyed-store.cjs +39 -0
- package/dist/storage/enhanced/local/keyed-store.cjs.map +1 -0
- package/dist/storage/enhanced/local/keyed-store.d.ts +31 -0
- package/dist/storage/enhanced/local/keyed-store.d.ts.map +1 -0
- package/dist/storage/enhanced/local/keyed-store.mjs +37 -0
- package/dist/storage/enhanced/local/keyed-store.mjs.map +1 -0
- package/dist/storage/enhanced/redis/core.cjs +142 -0
- package/dist/storage/enhanced/redis/core.cjs.map +1 -0
- package/dist/storage/enhanced/redis/core.d.ts +78 -0
- package/dist/storage/enhanced/redis/core.d.ts.map +1 -0
- package/dist/storage/enhanced/redis/core.mjs +140 -0
- package/dist/storage/enhanced/redis/core.mjs.map +1 -0
- package/dist/storage/enhanced/redis/index.cjs +10 -0
- package/dist/storage/enhanced/redis/index.cjs.map +1 -0
- package/dist/storage/enhanced/redis/index.d.ts +3 -0
- package/dist/storage/enhanced/redis/index.d.ts.map +1 -0
- package/dist/storage/enhanced/redis/index.mjs +3 -0
- package/dist/storage/enhanced/redis/index.mjs.map +1 -0
- package/dist/storage/enhanced/redis/keyed-store.cjs +44 -0
- package/dist/storage/enhanced/redis/keyed-store.cjs.map +1 -0
- package/dist/storage/enhanced/redis/keyed-store.d.ts +37 -0
- package/dist/storage/enhanced/redis/keyed-store.d.ts.map +1 -0
- package/dist/storage/enhanced/redis/keyed-store.mjs +42 -0
- package/dist/storage/enhanced/redis/keyed-store.mjs.map +1 -0
- package/dist/storage/lru/keyed-store.cjs +49 -0
- package/dist/storage/lru/keyed-store.cjs.map +1 -0
- package/dist/storage/lru/keyed-store.d.ts +40 -0
- package/dist/storage/lru/keyed-store.d.ts.map +1 -0
- package/dist/storage/lru/keyed-store.mjs +47 -0
- package/dist/storage/lru/keyed-store.mjs.map +1 -0
- package/package.json +31 -17
- package/src/clipboard.ts +85 -0
- package/src/env.ts +10 -13
- package/src/storage/enhanced/local/core.ts +104 -0
- package/src/storage/enhanced/local/index.ts +2 -0
- package/src/storage/enhanced/local/keyed-store.ts +34 -0
- package/src/storage/enhanced/redis/core.ts +149 -0
- package/src/storage/enhanced/redis/index.ts +2 -0
- package/src/storage/enhanced/redis/keyed-store.ts +41 -0
- package/src/storage/lru/keyed-store.ts +48 -0
package/README.md
CHANGED
|
@@ -5,24 +5,25 @@
|
|
|
5
5
|
[![codecov][codecov-src]][codecov-href]
|
|
6
6
|
[![License][license-src]][license-href]
|
|
7
7
|
|
|
8
|
-
A lightweight modular utility library for JavaScript and TypeScript
|
|
8
|
+
A lightweight and modular utility library for modern JavaScript and TypeScript — includes secure hashing, flexible logging, datetime tools, Vue/web helpers, storage abstraction, and more.
|
|
9
9
|
|
|
10
10
|
- [✨ Release Notes](./CHANGELOG.md)
|
|
11
11
|
|
|
12
12
|
## Features
|
|
13
13
|
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
18
|
-
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
- 🧩 Vue 3 utilities
|
|
25
|
-
-
|
|
14
|
+
- 📋 Clipboard utilities — copy text and blobs using the modern Clipboard API (Browser only)
|
|
15
|
+
- 📜 Logging utilities — simple and extensible logging via Consola and Pino
|
|
16
|
+
- 🔒 Crypto utilities — secure hashing functions: MD5, SHA3-224/256/384/512
|
|
17
|
+
- 📅 Datetime utilities — manipulate, format, and offset dates and ranges
|
|
18
|
+
- 🔢 Enum utilities — extract enum keys and values (number or string)
|
|
19
|
+
- 🌱 Environment helpers — safe env var access with error handling (Node only)
|
|
20
|
+
- 📈 Math utilities — rounding, formatting percentages, and ratios
|
|
21
|
+
- 💎 Number utilities — number padding, compact display, currency formatting
|
|
22
|
+
- 🔤 String utilities — casing, trimming, random string generation, etc.
|
|
23
|
+
- 🌐 URL utilities — parse and construct query strings and redirect URLs
|
|
24
|
+
- 🧩 Vue 3 utilities — composables like scroll preservation and key handling
|
|
25
|
+
- 🖥️ Web utilities — browser DOM helpers (e.g. scroll to top, key matching)
|
|
26
|
+
- 🗄️ Storage utilities — enhanced localStorage, Redis, and LRU abstractions
|
|
26
27
|
- 📦 Modular by design — import only what you need via `@kikiutils/shared/<module>`
|
|
27
28
|
|
|
28
29
|
## Requirements
|
|
@@ -66,9 +67,14 @@ logger.info(value);
|
|
|
66
67
|
|
|
67
68
|
Each module file includes function-level comments and usage examples.
|
|
68
69
|
|
|
70
|
+
### [clipboard](./src/clipboard.ts)
|
|
71
|
+
|
|
72
|
+
- `copyBlobToClipboard`
|
|
73
|
+
- `copyTextToClipboard`
|
|
74
|
+
|
|
69
75
|
### [consola](./src/consola.ts)
|
|
70
76
|
|
|
71
|
-
|
|
77
|
+
Console logger integration.
|
|
72
78
|
|
|
73
79
|
### [crypto-hash](./src/crypto-hash.ts)
|
|
74
80
|
|
|
@@ -114,12 +120,28 @@ Each module file includes function-level comments and usage examples.
|
|
|
114
120
|
|
|
115
121
|
### [pino](./src/pino.ts)
|
|
116
122
|
|
|
117
|
-
|
|
123
|
+
Pino logger integration.
|
|
118
124
|
|
|
119
125
|
### [random](./src/random.ts)
|
|
120
126
|
|
|
121
127
|
- `generateWithNestedRandomLength`
|
|
122
128
|
|
|
129
|
+
### storage
|
|
130
|
+
|
|
131
|
+
#### [enhanced-local](./src/storage/enhanced/local/index.ts)
|
|
132
|
+
|
|
133
|
+
- `createKeyedEnhancedLocalStore`
|
|
134
|
+
- `enhancedLocalStorage`
|
|
135
|
+
|
|
136
|
+
#### [enhanced-redis](./src/storage/enhanced/redis/index.ts)
|
|
137
|
+
|
|
138
|
+
- `createEnhancedRedisStorage`
|
|
139
|
+
- `createKeyedEnhancedRedisStore`
|
|
140
|
+
|
|
141
|
+
#### [lru/keyed-store](./src/storage/lru/keyed-store.ts)
|
|
142
|
+
|
|
143
|
+
- `createKeyedLruStore`
|
|
144
|
+
|
|
123
145
|
### [string](./src/string.ts)
|
|
124
146
|
|
|
125
147
|
- `randomString`
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
|
|
5
|
+
*
|
|
6
|
+
* ⚠️ Usage Notes:
|
|
7
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
8
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
9
|
+
* - Not supported in Safari and some older browsers.
|
|
10
|
+
*
|
|
11
|
+
* @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).
|
|
12
|
+
* @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.
|
|
13
|
+
*
|
|
14
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
15
|
+
* - `{ ok: true }` if the copy succeeded
|
|
16
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
21
|
+
* const result = await copyBlobToClipboard(blob);
|
|
22
|
+
* if (result.ok) {
|
|
23
|
+
* console.log('Copied blob!');
|
|
24
|
+
* } else {
|
|
25
|
+
* console.error('Copy failed:', result.error);
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
async function copyBlobToClipboard(blob, options) {
|
|
30
|
+
if (!navigator.clipboard?.write) {
|
|
31
|
+
return {
|
|
32
|
+
error: new Error('Clipboard.write is not supported in this browser.'),
|
|
33
|
+
ok: false,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const item = new ClipboardItem({ [blob.type]: blob }, options);
|
|
38
|
+
await navigator.clipboard.write([item]);
|
|
39
|
+
return { ok: true };
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
return {
|
|
43
|
+
error,
|
|
44
|
+
ok: false,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
|
|
50
|
+
*
|
|
51
|
+
* ⚠️ Usage Notes:
|
|
52
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
53
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
54
|
+
* - Not supported in some older browsers (especially legacy Safari).
|
|
55
|
+
*
|
|
56
|
+
* @param {string} text - The string to be copied to the clipboard.
|
|
57
|
+
*
|
|
58
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
59
|
+
* - `{ ok: true }` if the copy succeeded
|
|
60
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* const result = await copyTextToClipboard('Hello!');
|
|
65
|
+
* if (result.ok) {
|
|
66
|
+
* console.log('Copied!');
|
|
67
|
+
* } else {
|
|
68
|
+
* console.error('Copy failed:', result.error);
|
|
69
|
+
* }
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
async function copyTextToClipboard(text) {
|
|
73
|
+
try {
|
|
74
|
+
await navigator.clipboard.writeText(text);
|
|
75
|
+
return { ok: true };
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
return {
|
|
79
|
+
error,
|
|
80
|
+
ok: false,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
exports.copyBlobToClipboard = copyBlobToClipboard;
|
|
86
|
+
exports.copyTextToClipboard = copyTextToClipboard;
|
|
87
|
+
//# sourceMappingURL=clipboard.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.cjs","sources":["../src/clipboard.ts"],"sourcesContent":["type CopyResult =\n | { error: unknown; ok: false }\n | { ok: true };\n\n/**\n * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in Safari and some older browsers.\n *\n * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).\n * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const blob = new Blob(['Hello world'], { type: 'text/plain' });\n * const result = await copyBlobToClipboard(blob);\n * if (result.ok) {\n * console.log('Copied blob!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult> {\n if (!navigator.clipboard?.write) {\n return {\n error: new Error('Clipboard.write is not supported in this browser.'),\n ok: false,\n };\n }\n\n try {\n const item = new ClipboardItem({ [blob.type]: blob }, options);\n await navigator.clipboard.write([item]);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n\n/**\n * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in some older browsers (especially legacy Safari).\n *\n * @param {string} text - The string to be copied to the clipboard.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const result = await copyTextToClipboard('Hello!');\n * if (result.ok) {\n * console.log('Copied!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyTextToClipboard(text: string): Promise<CopyResult> {\n try {\n await navigator.clipboard.writeText(text);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n"],"names":[],"mappings":";;AAIA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACI,eAAe,mBAAmB,CAAC,IAAU,EAAE,OAA8B,EAAA;AAChF,IAAA,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,KAAK,EAAE;QAC7B,OAAO;AACH,YAAA,KAAK,EAAE,IAAI,KAAK,CAAC,mDAAmD,CAAC;AACrE,YAAA,EAAE,EAAE,KAAK;SACZ;;AAGL,IAAA,IAAI;AACA,QAAA,MAAM,IAAI,GAAG,IAAI,aAAa,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,EAAE,OAAO,CAAC;QAC9D,MAAM,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC;AACvC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;AAEA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACI,eAAe,mBAAmB,CAAC,IAAY,EAAA;AAClD,IAAA,IAAI;QACA,MAAM,SAAS,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC;AACzC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;;;;;"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
type CopyResult = {
|
|
2
|
+
error: unknown;
|
|
3
|
+
ok: false;
|
|
4
|
+
} | {
|
|
5
|
+
ok: true;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
|
|
9
|
+
*
|
|
10
|
+
* ⚠️ Usage Notes:
|
|
11
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
12
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
13
|
+
* - Not supported in Safari and some older browsers.
|
|
14
|
+
*
|
|
15
|
+
* @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).
|
|
16
|
+
* @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.
|
|
17
|
+
*
|
|
18
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
19
|
+
* - `{ ok: true }` if the copy succeeded
|
|
20
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
25
|
+
* const result = await copyBlobToClipboard(blob);
|
|
26
|
+
* if (result.ok) {
|
|
27
|
+
* console.log('Copied blob!');
|
|
28
|
+
* } else {
|
|
29
|
+
* console.error('Copy failed:', result.error);
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult>;
|
|
34
|
+
/**
|
|
35
|
+
* Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
|
|
36
|
+
*
|
|
37
|
+
* ⚠️ Usage Notes:
|
|
38
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
39
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
40
|
+
* - Not supported in some older browsers (especially legacy Safari).
|
|
41
|
+
*
|
|
42
|
+
* @param {string} text - The string to be copied to the clipboard.
|
|
43
|
+
*
|
|
44
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
45
|
+
* - `{ ok: true }` if the copy succeeded
|
|
46
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const result = await copyTextToClipboard('Hello!');
|
|
51
|
+
* if (result.ok) {
|
|
52
|
+
* console.log('Copied!');
|
|
53
|
+
* } else {
|
|
54
|
+
* console.error('Copy failed:', result.error);
|
|
55
|
+
* }
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function copyTextToClipboard(text: string): Promise<CopyResult>;
|
|
59
|
+
export {};
|
|
60
|
+
//# sourceMappingURL=clipboard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.d.ts","sourceRoot":"","sources":["../src/clipboard.ts"],"names":[],"mappings":"AAAA,KAAK,UAAU,GACX;IAAE,KAAK,EAAE,OAAO,CAAC;IAAC,EAAE,EAAE,KAAK,CAAA;CAAE,GAC7B;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,CAAC;AAEjB;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,EAAE,oBAAoB,GAAG,OAAO,CAAC,UAAU,CAAC,CAkBzG;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAsB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAU3E"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.
|
|
3
|
+
*
|
|
4
|
+
* ⚠️ Usage Notes:
|
|
5
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
6
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
7
|
+
* - Not supported in Safari and some older browsers.
|
|
8
|
+
*
|
|
9
|
+
* @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).
|
|
10
|
+
* @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.
|
|
11
|
+
*
|
|
12
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
13
|
+
* - `{ ok: true }` if the copy succeeded
|
|
14
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const blob = new Blob(['Hello world'], { type: 'text/plain' });
|
|
19
|
+
* const result = await copyBlobToClipboard(blob);
|
|
20
|
+
* if (result.ok) {
|
|
21
|
+
* console.log('Copied blob!');
|
|
22
|
+
* } else {
|
|
23
|
+
* console.error('Copy failed:', result.error);
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
async function copyBlobToClipboard(blob, options) {
|
|
28
|
+
if (!navigator.clipboard?.write) {
|
|
29
|
+
return {
|
|
30
|
+
error: new Error('Clipboard.write is not supported in this browser.'),
|
|
31
|
+
ok: false,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
try {
|
|
35
|
+
const item = new ClipboardItem({ [blob.type]: blob }, options);
|
|
36
|
+
await navigator.clipboard.write([item]);
|
|
37
|
+
return { ok: true };
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
return {
|
|
41
|
+
error,
|
|
42
|
+
ok: false,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Attempts to copy the given text to the user's clipboard using the modern Clipboard API.
|
|
48
|
+
*
|
|
49
|
+
* ⚠️ Usage Notes:
|
|
50
|
+
* - Must be called in a **secure context** (HTTPS or localhost).
|
|
51
|
+
* - Must be called **in response to a user interaction** (e.g. click, input).
|
|
52
|
+
* - Not supported in some older browsers (especially legacy Safari).
|
|
53
|
+
*
|
|
54
|
+
* @param {string} text - The string to be copied to the clipboard.
|
|
55
|
+
*
|
|
56
|
+
* @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:
|
|
57
|
+
* - `{ ok: true }` if the copy succeeded
|
|
58
|
+
* - `{ ok: false, error }` if the copy failed, with the error included
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```typescript
|
|
62
|
+
* const result = await copyTextToClipboard('Hello!');
|
|
63
|
+
* if (result.ok) {
|
|
64
|
+
* console.log('Copied!');
|
|
65
|
+
* } else {
|
|
66
|
+
* console.error('Copy failed:', result.error);
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
async function copyTextToClipboard(text) {
|
|
71
|
+
try {
|
|
72
|
+
await navigator.clipboard.writeText(text);
|
|
73
|
+
return { ok: true };
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
return {
|
|
77
|
+
error,
|
|
78
|
+
ok: false,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { copyBlobToClipboard, copyTextToClipboard };
|
|
84
|
+
//# sourceMappingURL=clipboard.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clipboard.mjs","sources":["../src/clipboard.ts"],"sourcesContent":["type CopyResult =\n | { error: unknown; ok: false }\n | { ok: true };\n\n/**\n * Attempts to copy a Blob (e.g. image, plain text, HTML) to the user's clipboard using the ClipboardItem API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in Safari and some older browsers.\n *\n * @param {Blob} blob - The Blob object to copy (e.g. from a File, image, or text content).\n * @param {ClipboardItemOptions} [options] - Optional options passed to the ClipboardItem constructor.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const blob = new Blob(['Hello world'], { type: 'text/plain' });\n * const result = await copyBlobToClipboard(blob);\n * if (result.ok) {\n * console.log('Copied blob!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyBlobToClipboard(blob: Blob, options?: ClipboardItemOptions): Promise<CopyResult> {\n if (!navigator.clipboard?.write) {\n return {\n error: new Error('Clipboard.write is not supported in this browser.'),\n ok: false,\n };\n }\n\n try {\n const item = new ClipboardItem({ [blob.type]: blob }, options);\n await navigator.clipboard.write([item]);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n\n/**\n * Attempts to copy the given text to the user's clipboard using the modern Clipboard API.\n *\n * ⚠️ Usage Notes:\n * - Must be called in a **secure context** (HTTPS or localhost).\n * - Must be called **in response to a user interaction** (e.g. click, input).\n * - Not supported in some older browsers (especially legacy Safari).\n *\n * @param {string} text - The string to be copied to the clipboard.\n *\n * @returns {Promise<CopyResult>} A promise resolving to a `CopyResult`:\n * - `{ ok: true }` if the copy succeeded\n * - `{ ok: false, error }` if the copy failed, with the error included\n *\n * @example\n * ```typescript\n * const result = await copyTextToClipboard('Hello!');\n * if (result.ok) {\n * console.log('Copied!');\n * } else {\n * console.error('Copy failed:', result.error);\n * }\n * ```\n */\nexport async function copyTextToClipboard(text: string): Promise<CopyResult> {\n try {\n await navigator.clipboard.writeText(text);\n return { ok: true };\n } catch (error) {\n return {\n error,\n ok: false,\n };\n }\n}\n"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACI,eAAe,mBAAmB,CAAC,IAAU,EAAE,OAA8B,EAAA;AAChF,IAAA,IAAI,CAAC,SAAS,CAAC,SAAS,EAAE,KAAK,EAAE;QAC7B,OAAO;AACH,YAAA,KAAK,EAAE,IAAI,KAAK,CAAC,mDAAmD,CAAC;AACrE,YAAA,EAAE,EAAE,KAAK;SACZ;;AAGL,IAAA,IAAI;AACA,QAAA,MAAM,IAAI,GAAG,IAAI,aAAa,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,GAAG,IAAI,EAAE,EAAE,OAAO,CAAC;QAC9D,MAAM,SAAS,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,CAAC;AACvC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;AAEA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACI,eAAe,mBAAmB,CAAC,IAAY,EAAA;AAClD,IAAA,IAAI;QACA,MAAM,SAAS,CAAC,SAAS,CAAC,SAAS,CAAC,IAAI,CAAC;AACzC,QAAA,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE;;IACrB,OAAO,KAAK,EAAE;QACZ,OAAO;YACH,KAAK;AACL,YAAA,EAAE,EAAE,KAAK;SACZ;;AAET;;;;"}
|
package/dist/env.cjs
CHANGED
|
@@ -22,9 +22,12 @@ class EnvironmentNotFoundError extends Error {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
|
-
* Retrieves the value of an environment variable, or throws an error if not
|
|
25
|
+
* Retrieves the value of an environment variable, or throws an error if it is not defined.
|
|
26
26
|
*
|
|
27
|
-
*
|
|
27
|
+
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
28
|
+
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} key - The environment variable key to retrieve.
|
|
28
31
|
*
|
|
29
32
|
* @returns {string} The value of the environment variable.
|
|
30
33
|
*
|
|
@@ -32,21 +35,15 @@ class EnvironmentNotFoundError extends Error {
|
|
|
32
35
|
*
|
|
33
36
|
* @example
|
|
34
37
|
* ```typescript
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* // When the environment variable 'API_KEY' is set:
|
|
38
|
-
* console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY
|
|
38
|
+
* process.env.API_KEY = '';
|
|
39
|
+
* checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered "defined")
|
|
39
40
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* const apiKey = checkAndGetEnvValue('API_KEY');
|
|
43
|
-
* } catch (error) {
|
|
44
|
-
* console.error(error); // Missing environment variable: API_KEY
|
|
45
|
-
* }
|
|
41
|
+
* delete process.env.API_KEY;
|
|
42
|
+
* checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError
|
|
46
43
|
* ```
|
|
47
44
|
*/
|
|
48
45
|
function checkAndGetEnvValue(key) {
|
|
49
|
-
if (
|
|
46
|
+
if (process.env[key] === undefined)
|
|
50
47
|
throw new EnvironmentNotFoundError(key);
|
|
51
48
|
return process.env[key];
|
|
52
49
|
}
|
package/dist/env.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.cjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not
|
|
1
|
+
{"version":3,"file":"env.cjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if it is not defined.\n *\n * Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string\n * value like `'0'` or `'false'` is considered a valid (defined) value.\n *\n * @param {string} key - The environment variable key to retrieve.\n *\n * @returns {string} The value of the environment variable.\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * process.env.API_KEY = '';\n * checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered \"defined\")\n *\n * delete process.env.API_KEY;\n * checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (process.env[key] === undefined) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":";;AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC3E,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;;"}
|
package/dist/env.d.ts
CHANGED
|
@@ -15,9 +15,12 @@ export declare class EnvironmentNotFoundError extends Error {
|
|
|
15
15
|
constructor(key: string);
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
|
-
* Retrieves the value of an environment variable, or throws an error if not
|
|
18
|
+
* Retrieves the value of an environment variable, or throws an error if it is not defined.
|
|
19
19
|
*
|
|
20
|
-
*
|
|
20
|
+
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
21
|
+
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} key - The environment variable key to retrieve.
|
|
21
24
|
*
|
|
22
25
|
* @returns {string} The value of the environment variable.
|
|
23
26
|
*
|
|
@@ -25,17 +28,11 @@ export declare class EnvironmentNotFoundError extends Error {
|
|
|
25
28
|
*
|
|
26
29
|
* @example
|
|
27
30
|
* ```typescript
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* // When the environment variable 'API_KEY' is set:
|
|
31
|
-
* console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY
|
|
31
|
+
* process.env.API_KEY = '';
|
|
32
|
+
* checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered "defined")
|
|
32
33
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* const apiKey = checkAndGetEnvValue('API_KEY');
|
|
36
|
-
* } catch (error) {
|
|
37
|
-
* console.error(error); // Missing environment variable: API_KEY
|
|
38
|
-
* }
|
|
34
|
+
* delete process.env.API_KEY;
|
|
35
|
+
* checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError
|
|
39
36
|
* ```
|
|
40
37
|
*/
|
|
41
38
|
export declare function checkAndGetEnvValue(key: string): string;
|
package/dist/env.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;;;OAIG;gBACS,GAAG,EAAE,MAAM;CAM1B;AAED
|
|
1
|
+
{"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;;;OAIG;gBACS,GAAG,EAAE,MAAM;CAM1B;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAGvD"}
|
package/dist/env.mjs
CHANGED
|
@@ -20,9 +20,12 @@ class EnvironmentNotFoundError extends Error {
|
|
|
20
20
|
}
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
|
-
* Retrieves the value of an environment variable, or throws an error if not
|
|
23
|
+
* Retrieves the value of an environment variable, or throws an error if it is not defined.
|
|
24
24
|
*
|
|
25
|
-
*
|
|
25
|
+
* Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string
|
|
26
|
+
* value like `'0'` or `'false'` is considered a valid (defined) value.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} key - The environment variable key to retrieve.
|
|
26
29
|
*
|
|
27
30
|
* @returns {string} The value of the environment variable.
|
|
28
31
|
*
|
|
@@ -30,21 +33,15 @@ class EnvironmentNotFoundError extends Error {
|
|
|
30
33
|
*
|
|
31
34
|
* @example
|
|
32
35
|
* ```typescript
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* // When the environment variable 'API_KEY' is set:
|
|
36
|
-
* console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY
|
|
36
|
+
* process.env.API_KEY = '';
|
|
37
|
+
* checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered "defined")
|
|
37
38
|
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* const apiKey = checkAndGetEnvValue('API_KEY');
|
|
41
|
-
* } catch (error) {
|
|
42
|
-
* console.error(error); // Missing environment variable: API_KEY
|
|
43
|
-
* }
|
|
39
|
+
* delete process.env.API_KEY;
|
|
40
|
+
* checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError
|
|
44
41
|
* ```
|
|
45
42
|
*/
|
|
46
43
|
function checkAndGetEnvValue(key) {
|
|
47
|
-
if (
|
|
44
|
+
if (process.env[key] === undefined)
|
|
48
45
|
throw new EnvironmentNotFoundError(key);
|
|
49
46
|
return process.env[key];
|
|
50
47
|
}
|
package/dist/env.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"env.mjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not
|
|
1
|
+
{"version":3,"file":"env.mjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if it is not defined.\n *\n * Only checks for `process.env[key] === undefined`. An empty string (e.g. '') or any falsy string\n * value like `'0'` or `'false'` is considered a valid (defined) value.\n *\n * @param {string} key - The environment variable key to retrieve.\n *\n * @returns {string} The value of the environment variable.\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * process.env.API_KEY = '';\n * checkAndGetEnvValue('API_KEY'); // ✅ Returns '' (still considered \"defined\")\n *\n * delete process.env.API_KEY;\n * checkAndGetEnvValue('API_KEY'); // ❌ Throws EnvironmentNotFoundError\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (process.env[key] === undefined) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":"AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC3E,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const superjson = require('superjson');
|
|
4
|
+
|
|
5
|
+
var StorageValueEncodingType;
|
|
6
|
+
(function (StorageValueEncodingType) {
|
|
7
|
+
StorageValueEncodingType["Json"] = "0";
|
|
8
|
+
StorageValueEncodingType["String"] = "1";
|
|
9
|
+
})(StorageValueEncodingType || (StorageValueEncodingType = {}));
|
|
10
|
+
const customValueHeader = '';
|
|
11
|
+
const customValueHeaderLength = customValueHeader.length + 1;
|
|
12
|
+
const toCustomValue = (type, payload) => `${customValueHeader}${type}${payload}`;
|
|
13
|
+
/**
|
|
14
|
+
* An enhanced localStorage wrapper that supports storing
|
|
15
|
+
* complex data types (e.g. Dates, Maps, Sets) using SuperJSON encoding.
|
|
16
|
+
*
|
|
17
|
+
* This utility preserves type structure when saving and retrieving values.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* enhancedLocalStorage.setItem('user', { name: 'user', createdAt: new Date() });
|
|
22
|
+
* const user = enhancedLocalStorage.getItem<{ name: string, createdAt: Date }>('user');
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
const enhancedLocalStorage = Object.freeze({
|
|
26
|
+
/**
|
|
27
|
+
* Removes all items from localStorage.
|
|
28
|
+
*/
|
|
29
|
+
clear: () => window.localStorage.clear(),
|
|
30
|
+
/**
|
|
31
|
+
* Retrieves a value by key and decodes it using SuperJSON or raw string.
|
|
32
|
+
*
|
|
33
|
+
* @template T - The expected type of the value.
|
|
34
|
+
*
|
|
35
|
+
* @param {string} key - The key of the value to retrieve.
|
|
36
|
+
*
|
|
37
|
+
* @returns {null | T} The decoded value or null if not found.
|
|
38
|
+
*/
|
|
39
|
+
getItem(key) {
|
|
40
|
+
const rawValue = window.localStorage.getItem(key);
|
|
41
|
+
return rawValue ? decodeStorageValue(rawValue) : null;
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* Checks whether a key exists in localStorage.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} key - The key to check.
|
|
47
|
+
*
|
|
48
|
+
* @returns {boolean} True if the key exists, false otherwise.
|
|
49
|
+
*/
|
|
50
|
+
hasItem: (key) => window.localStorage.getItem(key) !== null,
|
|
51
|
+
/**
|
|
52
|
+
* Returns the number of items stored in localStorage.
|
|
53
|
+
*
|
|
54
|
+
* @returns {number} The number of items stored in localStorage.
|
|
55
|
+
*/
|
|
56
|
+
get length() {
|
|
57
|
+
return window.localStorage.length;
|
|
58
|
+
},
|
|
59
|
+
/**
|
|
60
|
+
* Removes a specific key from localStorage.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} key - The key to remove.
|
|
63
|
+
*/
|
|
64
|
+
removeItem: (key) => window.localStorage.removeItem(key),
|
|
65
|
+
/**
|
|
66
|
+
* Stores a value in localStorage with automatic serialization.
|
|
67
|
+
*
|
|
68
|
+
* @param {string} key - The key to store the value under.
|
|
69
|
+
* @param {any} value - The value to store.
|
|
70
|
+
*/
|
|
71
|
+
setItem: (key, value) => window.localStorage.setItem(key, encodeToStorageValue(value)),
|
|
72
|
+
});
|
|
73
|
+
function decodeStorageValue(data) {
|
|
74
|
+
if (!isCustomFormat(data))
|
|
75
|
+
return data;
|
|
76
|
+
const payload = data.substring(customValueHeaderLength);
|
|
77
|
+
const type = data.charAt(customValueHeader.length);
|
|
78
|
+
switch (type) {
|
|
79
|
+
case StorageValueEncodingType.Json:
|
|
80
|
+
try {
|
|
81
|
+
return superjson.deserialize(JSON.parse(payload));
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
throw new Error('[EnhancedLocalStorage] Failed to parse JSON payload.');
|
|
85
|
+
}
|
|
86
|
+
case StorageValueEncodingType.String: return payload;
|
|
87
|
+
default:
|
|
88
|
+
throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}.`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function encodeToStorageValue(value) {
|
|
92
|
+
if (typeof value === 'string')
|
|
93
|
+
return toCustomValue(StorageValueEncodingType.String, value);
|
|
94
|
+
return toCustomValue(StorageValueEncodingType.Json, JSON.stringify(superjson.serialize(value)));
|
|
95
|
+
}
|
|
96
|
+
function isCustomFormat(data) {
|
|
97
|
+
return (data.length >= customValueHeaderLength
|
|
98
|
+
&& data[0] === customValueHeader[0]
|
|
99
|
+
&& data[1] === customValueHeader[1]);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
exports.enhancedLocalStorage = enhancedLocalStorage;
|
|
103
|
+
//# sourceMappingURL=core.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.cjs","sources":["../../../../src/storage/enhanced/local/core.ts"],"sourcesContent":["import {\n deserialize,\n serialize,\n} from 'superjson';\n\nenum StorageValueEncodingType {\n Json = '0',\n String = '1',\n}\n\nconst customValueHeader = '';\nconst customValueHeaderLength = customValueHeader.length + 1;\nconst toCustomValue = (type: StorageValueEncodingType, payload: string) => `${customValueHeader}${type}${payload}`;\n\n/**\n * An enhanced localStorage wrapper that supports storing\n * complex data types (e.g. Dates, Maps, Sets) using SuperJSON encoding.\n *\n * This utility preserves type structure when saving and retrieving values.\n *\n * @example\n * ```typescript\n * enhancedLocalStorage.setItem('user', { name: 'user', createdAt: new Date() });\n * const user = enhancedLocalStorage.getItem<{ name: string, createdAt: Date }>('user');\n * ```\n */\nexport const enhancedLocalStorage = Object.freeze({\n /**\n * Removes all items from localStorage.\n */\n clear: () => window.localStorage.clear(),\n /**\n * Retrieves a value by key and decodes it using SuperJSON or raw string.\n *\n * @template T - The expected type of the value.\n *\n * @param {string} key - The key of the value to retrieve.\n *\n * @returns {null | T} The decoded value or null if not found.\n */\n getItem<T = unknown>(key: string) {\n const rawValue = window.localStorage.getItem(key);\n return rawValue ? decodeStorageValue(rawValue) as T : null;\n },\n /**\n * Checks whether a key exists in localStorage.\n *\n * @param {string} key - The key to check.\n *\n * @returns {boolean} True if the key exists, false otherwise.\n */\n hasItem: (key: string) => window.localStorage.getItem(key) !== null,\n /**\n * Returns the number of items stored in localStorage.\n *\n * @returns {number} The number of items stored in localStorage.\n */\n get length() {\n return window.localStorage.length;\n },\n /**\n * Removes a specific key from localStorage.\n *\n * @param {string} key - The key to remove.\n */\n removeItem: (key: string) => window.localStorage.removeItem(key),\n /**\n * Stores a value in localStorage with automatic serialization.\n *\n * @param {string} key - The key to store the value under.\n * @param {any} value - The value to store.\n */\n setItem: (key: string, value: any) => window.localStorage.setItem(key, encodeToStorageValue(value)),\n});\n\nfunction decodeStorageValue(data: string) {\n if (!isCustomFormat(data)) return data;\n const payload = data.substring(customValueHeaderLength);\n const type = data.charAt(customValueHeader.length);\n switch (type) {\n case StorageValueEncodingType.Json:\n try {\n return deserialize(JSON.parse(payload));\n } catch {\n throw new Error('[EnhancedLocalStorage] Failed to parse JSON payload.');\n }\n case StorageValueEncodingType.String: return payload;\n default:\n throw new Error(`[EnhancedLocalStorage] Unknown encoding type: ${type}.`);\n }\n}\n\nfunction encodeToStorageValue(value: any) {\n if (typeof value === 'string') return toCustomValue(StorageValueEncodingType.String, value);\n return toCustomValue(StorageValueEncodingType.Json, JSON.stringify(serialize(value)));\n}\n\nfunction isCustomFormat(data: string) {\n return (\n data.length >= customValueHeaderLength\n && data[0] === customValueHeader[0]\n && data[1] === customValueHeader[1]\n );\n}\n"],"names":["deserialize","serialize"],"mappings":";;;;AAKA,IAAK,wBAGJ;AAHD,CAAA,UAAK,wBAAwB,EAAA;AACzB,IAAA,wBAAA,CAAA,MAAA,CAAA,GAAA,GAAU;AACV,IAAA,wBAAA,CAAA,QAAA,CAAA,GAAA,GAAY;AAChB,CAAC,EAHI,wBAAwB,KAAxB,wBAAwB,GAG5B,EAAA,CAAA,CAAA;AAED,MAAM,iBAAiB,GAAG,IAAI;AAC9B,MAAM,uBAAuB,GAAG,iBAAiB,CAAC,MAAM,GAAG,CAAC;AAC5D,MAAM,aAAa,GAAG,CAAC,IAA8B,EAAE,OAAe,KAAK,CAAA,EAAG,iBAAiB,CAAG,EAAA,IAAI,CAAG,EAAA,OAAO,EAAE;AAElH;;;;;;;;;;;AAWG;AACU,MAAA,oBAAoB,GAAG,MAAM,CAAC,MAAM,CAAC;AAC9C;;AAEG;IACH,KAAK,EAAE,MAAM,MAAM,CAAC,YAAY,CAAC,KAAK,EAAE;AACxC;;;;;;;;AAQG;AACH,IAAA,OAAO,CAAc,GAAW,EAAA;QAC5B,MAAM,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC;AACjD,QAAA,OAAO,QAAQ,GAAG,kBAAkB,CAAC,QAAQ,CAAM,GAAG,IAAI;KAC7D;AACD;;;;;;AAMG;AACH,IAAA,OAAO,EAAE,CAAC,GAAW,KAAK,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,IAAI;AACnE;;;;AAIG;AACH,IAAA,IAAI,MAAM,GAAA;AACN,QAAA,OAAO,MAAM,CAAC,YAAY,CAAC,MAAM;KACpC;AACD;;;;AAIG;AACH,IAAA,UAAU,EAAE,CAAC,GAAW,KAAK,MAAM,CAAC,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC;AAChE;;;;;AAKG;IACH,OAAO,EAAE,CAAC,GAAW,EAAE,KAAU,KAAK,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,oBAAoB,CAAC,KAAK,CAAC,CAAC;AACtG,CAAA;AAED,SAAS,kBAAkB,CAAC,IAAY,EAAA;AACpC,IAAA,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC;AAAE,QAAA,OAAO,IAAI;IACtC,MAAM,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,uBAAuB,CAAC;IACvD,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,MAAM,CAAC;IAClD,QAAQ,IAAI;QACR,KAAK,wBAAwB,CAAC,IAAI;AAC9B,YAAA,IAAI;gBACA,OAAOA,qBAAW,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;;AACzC,YAAA,MAAM;AACJ,gBAAA,MAAM,IAAI,KAAK,CAAC,sDAAsD,CAAC;;AAE/E,QAAA,KAAK,wBAAwB,CAAC,MAAM,EAAE,OAAO,OAAO;AACpD,QAAA;AACI,YAAA,MAAM,IAAI,KAAK,CAAC,iDAAiD,IAAI,CAAA,CAAA,CAAG,CAAC;;AAErF;AAEA,SAAS,oBAAoB,CAAC,KAAU,EAAA;IACpC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,aAAa,CAAC,wBAAwB,CAAC,MAAM,EAAE,KAAK,CAAC;AAC3F,IAAA,OAAO,aAAa,CAAC,wBAAwB,CAAC,IAAI,EAAE,IAAI,CAAC,SAAS,CAACC,mBAAS,CAAC,KAAK,CAAC,CAAC,CAAC;AACzF;AAEA,SAAS,cAAc,CAAC,IAAY,EAAA;AAChC,IAAA,QACI,IAAI,CAAC,MAAM,IAAI;AACZ,WAAA,IAAI,CAAC,CAAC,CAAC,KAAK,iBAAiB,CAAC,CAAC;WAC/B,IAAI,CAAC,CAAC,CAAC,KAAK,iBAAiB,CAAC,CAAC,CAAC;AAE3C;;;;"}
|