tiny-essentials 1.19.1 → 1.19.2
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/dist/v1/TinyEssentials.js +69 -9
- package/dist/v1/TinyEssentials.min.js +1 -1
- package/dist/v1/TinyLocalStorage.js +70 -10
- package/dist/v1/TinyLocalStorage.min.js +1 -1
- package/dist/v1/TinyUploadClicker.js +69 -9
- package/dist/v1/TinyUploadClicker.min.js +1 -1
- package/dist/v1/libs/TinyLocalStorage.cjs +69 -9
- package/dist/v1/libs/TinyLocalStorage.d.mts +20 -7
- package/dist/v1/libs/TinyLocalStorage.mjs +69 -15
- package/docs/v1/libs/TinyLocalStorage.md +23 -4
- package/package.json +1 -1
|
@@ -18860,6 +18860,22 @@ const customEncoders = new Map();
|
|
|
18860
18860
|
/** @type {Map<any, DecodeFn>} */
|
|
18861
18861
|
const customDecoders = new Map();
|
|
18862
18862
|
|
|
18863
|
+
/** @type {Set<any>} */
|
|
18864
|
+
const customTypesFreezed = new Set([
|
|
18865
|
+
'string',
|
|
18866
|
+
'boolean',
|
|
18867
|
+
'number',
|
|
18868
|
+
'object',
|
|
18869
|
+
'array',
|
|
18870
|
+
String,
|
|
18871
|
+
Boolean,
|
|
18872
|
+
Number,
|
|
18873
|
+
Object,
|
|
18874
|
+
Array,
|
|
18875
|
+
BigInt,
|
|
18876
|
+
Symbol,
|
|
18877
|
+
]);
|
|
18878
|
+
|
|
18863
18879
|
/**
|
|
18864
18880
|
* A function that encodes a value into a serializable JSON-compatible format.
|
|
18865
18881
|
*
|
|
@@ -19131,26 +19147,49 @@ class TinyLocalStorage {
|
|
|
19131
19147
|
|
|
19132
19148
|
///////////////////////////////////////////////////
|
|
19133
19149
|
|
|
19150
|
+
/**
|
|
19151
|
+
* Checks whether a JSON-serializable type is already registered.
|
|
19152
|
+
*
|
|
19153
|
+
* @param {any} type - The type identifier, which can be a string (e.g., `"bigint"`, `"symbol"`)
|
|
19154
|
+
* or a reference to a class/constructor function.
|
|
19155
|
+
* @returns {boolean} True if the type has both an encoder and decoder registered.
|
|
19156
|
+
*/
|
|
19157
|
+
static hasJsonType(type) {
|
|
19158
|
+
return (customEncoders.has(type) && customDecoders.has(type)) || customTypesFreezed.has(type);
|
|
19159
|
+
}
|
|
19160
|
+
|
|
19134
19161
|
/**
|
|
19135
19162
|
* Registers a new JSON-serializable type with its encoder and decoder.
|
|
19136
19163
|
*
|
|
19137
|
-
* @param {any} type - The type
|
|
19138
|
-
*
|
|
19139
|
-
* @param {
|
|
19164
|
+
* @param {any} type - The type identifier, which can be a string (e.g., `"bigint"`, `"symbol"`)
|
|
19165
|
+
* or a reference to a class/constructor function.
|
|
19166
|
+
* @param {EncodeFn} encodeFn - A function that receives a value of this type and returns a JSON-safe representation.
|
|
19167
|
+
* @param {DecodeFn} decodeFn - An object with `check(value)` to identify this type and `decode(value)` to restore it.
|
|
19168
|
+
* @param {boolean} [freezeType=false] - If true, prevents this type from being unregistered later.
|
|
19169
|
+
* @throws {Error} Throws an error if the type is frozen and cannot be registered.
|
|
19140
19170
|
*/
|
|
19141
|
-
static registerJsonType(type, encodeFn, decodeFn) {
|
|
19171
|
+
static registerJsonType(type, encodeFn, decodeFn, freezeType = false) {
|
|
19172
|
+
if (customTypesFreezed.has(type))
|
|
19173
|
+
throw new Error(`Cannot register type "${type}" because it is frozen.`);
|
|
19142
19174
|
customEncoders.set(type, encodeFn);
|
|
19143
19175
|
customDecoders.set(type, decodeFn);
|
|
19176
|
+
if (freezeType) customTypesFreezed.add(type);
|
|
19144
19177
|
}
|
|
19145
19178
|
|
|
19146
19179
|
/**
|
|
19147
|
-
*
|
|
19180
|
+
* Unregisters a previously registered custom type from the encoding/decoding system.
|
|
19148
19181
|
*
|
|
19149
|
-
* @param {
|
|
19182
|
+
* @param {any} type - The primitive name or constructor reference used in the registration.
|
|
19183
|
+
* @returns {boolean} Returns true if the type existed and was removed, or false if it wasn't found.
|
|
19184
|
+
* @throws {Error} Throws an error if the type is frozen and cannot be unregistered.
|
|
19150
19185
|
*/
|
|
19151
19186
|
static deleteJsonType(type) {
|
|
19152
|
-
|
|
19153
|
-
|
|
19187
|
+
if (customTypesFreezed.has(type))
|
|
19188
|
+
throw new Error(`Cannot remove type "${type}" because it is frozen.`);
|
|
19189
|
+
let isDeleted = false;
|
|
19190
|
+
if (customEncoders.delete(type)) isDeleted = true;
|
|
19191
|
+
if (customDecoders.delete(type)) isDeleted = true;
|
|
19192
|
+
return isDeleted;
|
|
19154
19193
|
}
|
|
19155
19194
|
|
|
19156
19195
|
//////////////////////////////////////////////////////
|
|
@@ -19495,7 +19534,13 @@ class TinyLocalStorage {
|
|
|
19495
19534
|
if (typeof data !== 'string') throw new Error('Value must be a string.');
|
|
19496
19535
|
|
|
19497
19536
|
this.emit('setString', name, data);
|
|
19498
|
-
return this.#localStorage.setItem(
|
|
19537
|
+
return this.#localStorage.setItem(
|
|
19538
|
+
name,
|
|
19539
|
+
JSON.stringify({
|
|
19540
|
+
__string__: true,
|
|
19541
|
+
value: data,
|
|
19542
|
+
}),
|
|
19543
|
+
);
|
|
19499
19544
|
}
|
|
19500
19545
|
|
|
19501
19546
|
/**
|
|
@@ -19508,6 +19553,15 @@ class TinyLocalStorage {
|
|
|
19508
19553
|
if (typeof name !== 'string' || !name.length)
|
|
19509
19554
|
throw new Error('Key must be a non-empty string.');
|
|
19510
19555
|
let value = this.#localStorage.getItem(name);
|
|
19556
|
+
try {
|
|
19557
|
+
/** @type {{ value: string; __string__: boolean }} */
|
|
19558
|
+
// @ts-ignore
|
|
19559
|
+
value = JSON.parse(value);
|
|
19560
|
+
if (!isJsonObject(value) || !value.__string__ || typeof value.value !== 'string') return null;
|
|
19561
|
+
value = value.value;
|
|
19562
|
+
} catch {
|
|
19563
|
+
value = null;
|
|
19564
|
+
}
|
|
19511
19565
|
if (typeof value === 'string') return value;
|
|
19512
19566
|
return null;
|
|
19513
19567
|
}
|
|
@@ -19624,6 +19678,7 @@ TinyLocalStorage.registerJsonType(
|
|
|
19624
19678
|
decode: (value, decodeSpecialJson) =>
|
|
19625
19679
|
new Map(value.data.map(([k, v]) => [k, decodeSpecialJson(v)])),
|
|
19626
19680
|
},
|
|
19681
|
+
true,
|
|
19627
19682
|
);
|
|
19628
19683
|
|
|
19629
19684
|
// Set
|
|
@@ -19637,6 +19692,7 @@ TinyLocalStorage.registerJsonType(
|
|
|
19637
19692
|
check: (value) => value.__set__,
|
|
19638
19693
|
decode: (value, decodeSpecialJson) => new Set(value.data.map(decodeSpecialJson)),
|
|
19639
19694
|
},
|
|
19695
|
+
true,
|
|
19640
19696
|
);
|
|
19641
19697
|
|
|
19642
19698
|
// Date
|
|
@@ -19650,6 +19706,7 @@ TinyLocalStorage.registerJsonType(
|
|
|
19650
19706
|
check: (value) => value.__date__,
|
|
19651
19707
|
decode: (value) => new Date(value.value),
|
|
19652
19708
|
},
|
|
19709
|
+
true,
|
|
19653
19710
|
);
|
|
19654
19711
|
|
|
19655
19712
|
// Regex
|
|
@@ -19664,6 +19721,7 @@ TinyLocalStorage.registerJsonType(
|
|
|
19664
19721
|
check: (value) => value.__regexp__,
|
|
19665
19722
|
decode: (value) => new RegExp(value.source, value.flags),
|
|
19666
19723
|
},
|
|
19724
|
+
true,
|
|
19667
19725
|
);
|
|
19668
19726
|
|
|
19669
19727
|
// Big Int
|
|
@@ -19677,6 +19735,7 @@ TinyLocalStorage.registerJsonType(
|
|
|
19677
19735
|
check: (value) => value.__bigint__,
|
|
19678
19736
|
decode: (value) => BigInt(value.value),
|
|
19679
19737
|
},
|
|
19738
|
+
true,
|
|
19680
19739
|
);
|
|
19681
19740
|
|
|
19682
19741
|
// Symbol
|
|
@@ -19693,6 +19752,7 @@ TinyLocalStorage.registerJsonType(
|
|
|
19693
19752
|
return key != null ? Symbol.for(key) : Symbol();
|
|
19694
19753
|
},
|
|
19695
19754
|
},
|
|
19755
|
+
true,
|
|
19696
19756
|
);
|
|
19697
19757
|
|
|
19698
19758
|
/* harmony default export */ const libs_TinyLocalStorage = (TinyLocalStorage);
|