@valkyriestudios/utils 12.37.0 → 12.38.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/array/groupBy.d.ts +3 -3
- package/array/join.d.ts +2 -2
- package/array/split.d.ts +3 -3
- package/caching/LRU.d.ts +16 -10
- package/{array → cjs/array}/groupBy.js +16 -4
- package/{array → cjs/array}/join.js +15 -3
- package/{array → cjs/array}/split.js +16 -7
- package/cjs/caching/LRU.js +123 -0
- package/{caching → cjs/caching}/memoize.js +11 -5
- package/{date → cjs/date}/format.js +4 -1
- package/{date → cjs/date}/isFormat.js +4 -1
- package/esm/array/dedupe.js +52 -0
- package/esm/array/groupBy.js +43 -0
- package/esm/array/index.js +14 -0
- package/esm/array/is.js +4 -0
- package/esm/array/isNotEmpty.js +4 -0
- package/esm/array/join.js +58 -0
- package/esm/array/mapFn.js +22 -0
- package/esm/array/mapFnAsMap.js +21 -0
- package/esm/array/mapKey.js +22 -0
- package/esm/array/mapKeyAsMap.js +22 -0
- package/esm/array/mapPrimitive.js +33 -0
- package/esm/array/shuffle.js +10 -0
- package/esm/array/sort.js +137 -0
- package/esm/array/split.js +43 -0
- package/esm/boolean/index.js +1 -0
- package/esm/boolean/is.js +4 -0
- package/esm/caching/LRU.js +116 -0
- package/esm/caching/index.js +3 -0
- package/esm/caching/memoize.js +37 -0
- package/esm/date/addUTC.js +41 -0
- package/esm/date/convertToDate.js +12 -0
- package/esm/date/diff.js +34 -0
- package/esm/date/endOfUTC.js +50 -0
- package/esm/date/format.js +241 -0
- package/esm/date/index.js +15 -0
- package/esm/date/is.js +4 -0
- package/esm/date/isFormat.js +118 -0
- package/esm/date/isLeap.js +9 -0
- package/esm/date/nowUnix.js +4 -0
- package/esm/date/nowUnixMs.js +4 -0
- package/esm/date/setTimeUTC.js +10 -0
- package/esm/date/startOfUTC.js +50 -0
- package/esm/date/toUTC.js +7 -0
- package/esm/date/toUnix.js +7 -0
- package/esm/deep/freeze.js +21 -0
- package/esm/deep/get.js +84 -0
- package/esm/deep/index.js +8 -0
- package/esm/deep/seal.js +21 -0
- package/esm/deep/set.js +50 -0
- package/esm/equal.js +67 -0
- package/esm/formdata/index.js +2 -0
- package/esm/formdata/is.js +4 -0
- package/esm/formdata/toObject.js +89 -0
- package/esm/function/debounce.js +39 -0
- package/esm/function/index.js +9 -0
- package/esm/function/is.js +4 -0
- package/esm/function/isAsync.js +4 -0
- package/esm/function/noop.js +2 -0
- package/esm/function/noopresolve.js +4 -0
- package/esm/function/noopreturn.js +4 -0
- package/esm/function/sleep.js +6 -0
- package/esm/hash/fnv1A.js +51 -0
- package/esm/hash/guid.js +31 -0
- package/esm/hash/index.js +2 -0
- package/esm/is.js +63 -0
- package/esm/modules/PubSub.js +187 -0
- package/esm/modules/Scheduler.js +343 -0
- package/esm/modules/index.js +2 -0
- package/esm/number/index.js +37 -0
- package/esm/number/is.js +4 -0
- package/esm/number/isAbove.js +4 -0
- package/esm/number/isAboveOrEqual.js +4 -0
- package/esm/number/isBelow.js +4 -0
- package/esm/number/isBelowOrEqual.js +4 -0
- package/esm/number/isBetween.js +7 -0
- package/esm/number/isInteger.js +4 -0
- package/esm/number/isIntegerAbove.js +4 -0
- package/esm/number/isIntegerAboveOrEqual.js +4 -0
- package/esm/number/isIntegerBelow.js +4 -0
- package/esm/number/isIntegerBelowOrEqual.js +4 -0
- package/esm/number/isIntegerBetween.js +7 -0
- package/esm/number/isNumericalNaN.js +4 -0
- package/esm/number/randomBetween.js +7 -0
- package/esm/number/randomIntBetween.js +7 -0
- package/esm/number/round.js +10 -0
- package/esm/number/toPercentage.js +9 -0
- package/esm/object/define.js +7 -0
- package/esm/object/index.js +7 -0
- package/esm/object/is.js +4 -0
- package/esm/object/isNotEmpty.js +9 -0
- package/esm/object/merge.js +31 -0
- package/esm/object/omit.js +30 -0
- package/esm/object/pick.js +33 -0
- package/esm/regexp/index.js +3 -0
- package/esm/regexp/is.js +4 -0
- package/esm/regexp/sanitize.js +7 -0
- package/esm/string/humanizeBytes.js +15 -0
- package/esm/string/humanizeNumber.js +40 -0
- package/esm/string/index.js +7 -0
- package/esm/string/is.js +4 -0
- package/esm/string/isBetween.js +12 -0
- package/esm/string/isNotEmpty.js +20 -0
- package/esm/string/shorten.js +18 -0
- package/index.d.ts +19 -15
- package/package.json +192 -850
- package/caching/LRU.js +0 -72
- /package/{array → cjs/array}/dedupe.js +0 -0
- /package/{array → cjs/array}/index.js +0 -0
- /package/{array → cjs/array}/is.js +0 -0
- /package/{array → cjs/array}/isNotEmpty.js +0 -0
- /package/{array → cjs/array}/mapFn.js +0 -0
- /package/{array → cjs/array}/mapFnAsMap.js +0 -0
- /package/{array → cjs/array}/mapKey.js +0 -0
- /package/{array → cjs/array}/mapKeyAsMap.js +0 -0
- /package/{array → cjs/array}/mapPrimitive.js +0 -0
- /package/{array → cjs/array}/shuffle.js +0 -0
- /package/{array → cjs/array}/sort.js +0 -0
- /package/{boolean → cjs/boolean}/index.js +0 -0
- /package/{boolean → cjs/boolean}/is.js +0 -0
- /package/{caching → cjs/caching}/index.js +0 -0
- /package/{date → cjs/date}/addUTC.js +0 -0
- /package/{date → cjs/date}/convertToDate.js +0 -0
- /package/{date → cjs/date}/diff.js +0 -0
- /package/{date → cjs/date}/endOfUTC.js +0 -0
- /package/{date → cjs/date}/index.js +0 -0
- /package/{date → cjs/date}/is.js +0 -0
- /package/{date → cjs/date}/isLeap.js +0 -0
- /package/{date → cjs/date}/nowUnix.js +0 -0
- /package/{date → cjs/date}/nowUnixMs.js +0 -0
- /package/{date → cjs/date}/setTimeUTC.js +0 -0
- /package/{date → cjs/date}/startOfUTC.js +0 -0
- /package/{date → cjs/date}/toUTC.js +0 -0
- /package/{date → cjs/date}/toUnix.js +0 -0
- /package/{deep → cjs/deep}/freeze.js +0 -0
- /package/{deep → cjs/deep}/get.js +0 -0
- /package/{deep → cjs/deep}/index.js +0 -0
- /package/{deep → cjs/deep}/seal.js +0 -0
- /package/{deep → cjs/deep}/set.js +0 -0
- /package/{equal.js → cjs/equal.js} +0 -0
- /package/{formdata → cjs/formdata}/index.js +0 -0
- /package/{formdata → cjs/formdata}/is.js +0 -0
- /package/{formdata → cjs/formdata}/toObject.js +0 -0
- /package/{function → cjs/function}/debounce.js +0 -0
- /package/{function → cjs/function}/index.js +0 -0
- /package/{function → cjs/function}/is.js +0 -0
- /package/{function → cjs/function}/isAsync.js +0 -0
- /package/{function → cjs/function}/noop.js +0 -0
- /package/{function → cjs/function}/noopresolve.js +0 -0
- /package/{function → cjs/function}/noopreturn.js +0 -0
- /package/{function → cjs/function}/sleep.js +0 -0
- /package/{hash → cjs/hash}/fnv1A.js +0 -0
- /package/{hash → cjs/hash}/guid.js +0 -0
- /package/{hash → cjs/hash}/index.js +0 -0
- /package/{is.js → cjs/is.js} +0 -0
- /package/{modules → cjs/modules}/PubSub.js +0 -0
- /package/{modules → cjs/modules}/Scheduler.js +0 -0
- /package/{modules → cjs/modules}/index.js +0 -0
- /package/{number → cjs/number}/index.js +0 -0
- /package/{number → cjs/number}/is.js +0 -0
- /package/{number → cjs/number}/isAbove.js +0 -0
- /package/{number → cjs/number}/isAboveOrEqual.js +0 -0
- /package/{number → cjs/number}/isBelow.js +0 -0
- /package/{number → cjs/number}/isBelowOrEqual.js +0 -0
- /package/{number → cjs/number}/isBetween.js +0 -0
- /package/{number → cjs/number}/isInteger.js +0 -0
- /package/{number → cjs/number}/isIntegerAbove.js +0 -0
- /package/{number → cjs/number}/isIntegerAboveOrEqual.js +0 -0
- /package/{number → cjs/number}/isIntegerBelow.js +0 -0
- /package/{number → cjs/number}/isIntegerBelowOrEqual.js +0 -0
- /package/{number → cjs/number}/isIntegerBetween.js +0 -0
- /package/{number → cjs/number}/isNumericalNaN.js +0 -0
- /package/{number → cjs/number}/randomBetween.js +0 -0
- /package/{number → cjs/number}/randomIntBetween.js +0 -0
- /package/{number → cjs/number}/round.js +0 -0
- /package/{number → cjs/number}/toPercentage.js +0 -0
- /package/{object → cjs/object}/define.js +0 -0
- /package/{object → cjs/object}/index.js +0 -0
- /package/{object → cjs/object}/is.js +0 -0
- /package/{object → cjs/object}/isNotEmpty.js +0 -0
- /package/{object → cjs/object}/merge.js +0 -0
- /package/{object → cjs/object}/omit.js +0 -0
- /package/{object → cjs/object}/pick.js +0 -0
- /package/{regexp → cjs/regexp}/index.js +0 -0
- /package/{regexp → cjs/regexp}/is.js +0 -0
- /package/{regexp → cjs/regexp}/sanitize.js +0 -0
- /package/{string → cjs/string}/humanizeBytes.js +0 -0
- /package/{string → cjs/string}/humanizeNumber.js +0 -0
- /package/{string → cjs/string}/index.js +0 -0
- /package/{string → cjs/string}/is.js +0 -0
- /package/{string → cjs/string}/isBetween.js +0 -0
- /package/{string → cjs/string}/isNotEmpty.js +0 -0
- /package/{string → cjs/string}/shorten.js +0 -0
package/esm/equal.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
function isArrayEqual(a, b) {
|
|
2
|
+
const a_len = a.length;
|
|
3
|
+
if (a_len !== b.length)
|
|
4
|
+
return false;
|
|
5
|
+
for (let i = a_len - 1; i >= 0; i--) {
|
|
6
|
+
if (!equal(a[i], b[i]))
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
function isObjectEqual(a, b) {
|
|
12
|
+
const a_keys = Object.keys(a);
|
|
13
|
+
const b_keys = Object.keys(b);
|
|
14
|
+
const a_len = a_keys.length;
|
|
15
|
+
if (a_len !== b_keys.length)
|
|
16
|
+
return false;
|
|
17
|
+
for (let i = a_len - 1; i >= 0; i--) {
|
|
18
|
+
const key = a_keys[i];
|
|
19
|
+
if (!equal(a[key], b[key]))
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function isMapEqual(a, b) {
|
|
25
|
+
if (a.size !== b.size)
|
|
26
|
+
return false;
|
|
27
|
+
for (const [key, value] of a) {
|
|
28
|
+
if (!b.has(key) || !equal(value, b.get(key)))
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
function equal(a, b) {
|
|
34
|
+
if (a === b)
|
|
35
|
+
return true;
|
|
36
|
+
switch (typeof a) {
|
|
37
|
+
case 'number':
|
|
38
|
+
return Number.isNaN(b) && Number.isNaN(a);
|
|
39
|
+
case 'object': {
|
|
40
|
+
if (a === null || b === null)
|
|
41
|
+
return false;
|
|
42
|
+
if (Array.isArray(a))
|
|
43
|
+
return Array.isArray(b) && isArrayEqual(a, b);
|
|
44
|
+
const proto_a = Object.prototype.toString.call(a);
|
|
45
|
+
const proto_b = Object.prototype.toString.call(b);
|
|
46
|
+
if (proto_a !== proto_b)
|
|
47
|
+
return false;
|
|
48
|
+
switch (proto_a) {
|
|
49
|
+
case '[object Date]':
|
|
50
|
+
return a.valueOf() === b.valueOf();
|
|
51
|
+
case '[object Object]':
|
|
52
|
+
return isObjectEqual(a, b);
|
|
53
|
+
case '[object Error]':
|
|
54
|
+
return a.name === b.name && a.message === b.message;
|
|
55
|
+
case '[object RegExp]':
|
|
56
|
+
return String(a) === String(b);
|
|
57
|
+
case '[object Map]':
|
|
58
|
+
return isMapEqual(a, b);
|
|
59
|
+
default:
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
default:
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
export { equal, equal as default };
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { isDateFormat } from '../date/isFormat';
|
|
2
|
+
const RGX_TOKENS = /[^.[\]]+/g;
|
|
3
|
+
function assign(acc, rawkey, value, single) {
|
|
4
|
+
let cursor = acc;
|
|
5
|
+
const keys = rawkey.match(RGX_TOKENS);
|
|
6
|
+
const keys_len = keys.length;
|
|
7
|
+
for (let i = 0; i < keys_len; i++) {
|
|
8
|
+
const key = keys[i];
|
|
9
|
+
switch (key) {
|
|
10
|
+
case '__proto__':
|
|
11
|
+
case 'constructor':
|
|
12
|
+
case 'prototype':
|
|
13
|
+
return;
|
|
14
|
+
default: {
|
|
15
|
+
if (i < (keys_len - 1)) {
|
|
16
|
+
const n_key = Array.isArray(cursor) ? Number(key) : key;
|
|
17
|
+
if (!cursor[n_key])
|
|
18
|
+
cursor[n_key] = Number.isInteger(+keys[i + 1]) ? [] : {};
|
|
19
|
+
cursor = cursor[n_key];
|
|
20
|
+
}
|
|
21
|
+
else if (!(key in cursor) || (single && single.has(key))) {
|
|
22
|
+
cursor[key] = value;
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const cursor_val = cursor[key];
|
|
26
|
+
if (Array.isArray(cursor_val)) {
|
|
27
|
+
cursor_val.push(value);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
cursor[key] = [cursor_val, value];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
function toObject(form, config) {
|
|
38
|
+
if (!(form instanceof FormData))
|
|
39
|
+
throw new Error('formdata/toObject: Value is not an instance of FormData');
|
|
40
|
+
const set = config?.raw === true ? null : new Set(Array.isArray(config?.raw) ? config?.raw : []);
|
|
41
|
+
const set_guard = !!(set && set.size > 0);
|
|
42
|
+
const single = Array.isArray(config?.single) ? new Set(config.single) : null;
|
|
43
|
+
const nBool = config?.normalize_bool !== false;
|
|
44
|
+
const nNull = config?.normalize_null !== false;
|
|
45
|
+
const nDate = config?.normalize_date !== false;
|
|
46
|
+
const nNumber = config?.normalize_number !== false;
|
|
47
|
+
const acc = {};
|
|
48
|
+
if (set === null) {
|
|
49
|
+
form.forEach((value, key) => assign(acc, key, value, single));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
form.forEach((value, key) => {
|
|
53
|
+
if (set_guard && set.has(key))
|
|
54
|
+
return assign(acc, key, value, single);
|
|
55
|
+
switch (value) {
|
|
56
|
+
case 'true':
|
|
57
|
+
case 'TRUE':
|
|
58
|
+
case 'True':
|
|
59
|
+
assign(acc, key, nBool ? true : value, single);
|
|
60
|
+
break;
|
|
61
|
+
case 'false':
|
|
62
|
+
case 'FALSE':
|
|
63
|
+
case 'False':
|
|
64
|
+
assign(acc, key, nBool ? false : value, single);
|
|
65
|
+
break;
|
|
66
|
+
case 'null':
|
|
67
|
+
case 'NULL':
|
|
68
|
+
case 'Null':
|
|
69
|
+
assign(acc, key, nNull ? null : value, single);
|
|
70
|
+
break;
|
|
71
|
+
default: {
|
|
72
|
+
if (typeof value === 'string' && value.length) {
|
|
73
|
+
if (nNumber) {
|
|
74
|
+
const nVal = Number(value);
|
|
75
|
+
if (!isNaN(nVal))
|
|
76
|
+
return assign(acc, key, nVal, single);
|
|
77
|
+
}
|
|
78
|
+
if (nDate &&
|
|
79
|
+
isDateFormat(value, 'ISO'))
|
|
80
|
+
return assign(acc, key, new Date(value), single);
|
|
81
|
+
}
|
|
82
|
+
assign(acc, key, value, single);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return acc;
|
|
88
|
+
}
|
|
89
|
+
export { toObject, toObject as default };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { isFunction } from './is';
|
|
2
|
+
import { isIntegerAbove } from '../number/isIntegerAbove';
|
|
3
|
+
function debounce(fn, wait) {
|
|
4
|
+
if (!isFunction(fn))
|
|
5
|
+
throw new Error('functions/debounce: Expected a function');
|
|
6
|
+
if (!isIntegerAbove(wait, 0))
|
|
7
|
+
throw new Error('functions/debounce: Wait should be an integer above 0');
|
|
8
|
+
let timeout;
|
|
9
|
+
let self;
|
|
10
|
+
let current_args;
|
|
11
|
+
let current_rslt;
|
|
12
|
+
const clear = () => {
|
|
13
|
+
if (!timeout)
|
|
14
|
+
return;
|
|
15
|
+
clearTimeout(timeout);
|
|
16
|
+
timeout = null;
|
|
17
|
+
};
|
|
18
|
+
const flush = () => {
|
|
19
|
+
if (!timeout)
|
|
20
|
+
return;
|
|
21
|
+
current_rslt = fn.apply(self, current_args);
|
|
22
|
+
clearTimeout(timeout);
|
|
23
|
+
timeout = null;
|
|
24
|
+
};
|
|
25
|
+
const debouncedFn = function (...args) {
|
|
26
|
+
self = this;
|
|
27
|
+
current_args = args;
|
|
28
|
+
clear();
|
|
29
|
+
timeout = setTimeout(() => {
|
|
30
|
+
timeout = null;
|
|
31
|
+
current_rslt = fn.apply(self, current_args);
|
|
32
|
+
}, wait);
|
|
33
|
+
return current_rslt;
|
|
34
|
+
};
|
|
35
|
+
debouncedFn.cancel = clear;
|
|
36
|
+
debouncedFn.flush = flush;
|
|
37
|
+
return debouncedFn;
|
|
38
|
+
}
|
|
39
|
+
export { debounce, debounce as default };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { debounce } from './debounce';
|
|
2
|
+
export { noop } from './noop';
|
|
3
|
+
export { noopresolve } from './noopresolve';
|
|
4
|
+
export { noopreturn } from './noopreturn';
|
|
5
|
+
export { sleep } from './sleep';
|
|
6
|
+
export { isFunction } from './is';
|
|
7
|
+
export { isFunction as isFn } from './is';
|
|
8
|
+
export { isAsyncFunction } from './isAsync';
|
|
9
|
+
export { isAsyncFunction as isAsyncFn } from './isAsync';
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
export const FNV_32 = 2166136261;
|
|
2
|
+
export const FNV_64 = 1099511628211;
|
|
3
|
+
const REPL_NAN = 'nan';
|
|
4
|
+
const REPL_TRUE = 'true';
|
|
5
|
+
const REPL_FALSE = 'false';
|
|
6
|
+
const REPL_UNDEF = 'undefined';
|
|
7
|
+
const REPL_NULL = 'null';
|
|
8
|
+
function fnv1A(data, offset = FNV_32) {
|
|
9
|
+
let hash = offset;
|
|
10
|
+
let sanitized;
|
|
11
|
+
switch (typeof data) {
|
|
12
|
+
case 'string':
|
|
13
|
+
sanitized = data;
|
|
14
|
+
break;
|
|
15
|
+
case 'number':
|
|
16
|
+
sanitized = Number.isNaN(data) || !Number.isFinite(data) ? REPL_NAN : String(data);
|
|
17
|
+
break;
|
|
18
|
+
case 'boolean':
|
|
19
|
+
sanitized = data ? REPL_TRUE : REPL_FALSE;
|
|
20
|
+
break;
|
|
21
|
+
case 'undefined':
|
|
22
|
+
sanitized = REPL_UNDEF;
|
|
23
|
+
break;
|
|
24
|
+
case 'object':
|
|
25
|
+
if (data === null) {
|
|
26
|
+
sanitized = REPL_NULL;
|
|
27
|
+
}
|
|
28
|
+
else if (Array.isArray(data) || Object.prototype.toString.call(data) === '[object Object]') {
|
|
29
|
+
sanitized = JSON.stringify(data);
|
|
30
|
+
}
|
|
31
|
+
else if (data instanceof RegExp) {
|
|
32
|
+
sanitized = String(data);
|
|
33
|
+
}
|
|
34
|
+
else if (data instanceof Date) {
|
|
35
|
+
sanitized = String(data.getTime());
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
throw new TypeError('An FNV1A Hash could not be calculated for this datatype');
|
|
39
|
+
}
|
|
40
|
+
break;
|
|
41
|
+
default:
|
|
42
|
+
throw new TypeError('An FNV1A Hash could not be calculated for this datatype');
|
|
43
|
+
}
|
|
44
|
+
const len = sanitized.length;
|
|
45
|
+
for (let i = 0; i < len; i++) {
|
|
46
|
+
hash ^= sanitized.charCodeAt(i);
|
|
47
|
+
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
48
|
+
}
|
|
49
|
+
return hash >>> 0;
|
|
50
|
+
}
|
|
51
|
+
export { fnv1A, fnv1A as default };
|
package/esm/hash/guid.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const HEX = [];
|
|
2
|
+
for (let i = 0; i < 256; i++) {
|
|
3
|
+
HEX[i] = (i < 16 ? '0' : '') + i.toString(16);
|
|
4
|
+
}
|
|
5
|
+
function guid() {
|
|
6
|
+
const d0 = (Math.random() * 0xffffffff) | 0;
|
|
7
|
+
const d1 = (Math.random() * 0xffffffff) | 0;
|
|
8
|
+
const d2 = (Math.random() * 0xffffffff) | 0;
|
|
9
|
+
const d3 = (Math.random() * 0xffffffff) | 0;
|
|
10
|
+
return HEX[d0 & 0xff] +
|
|
11
|
+
HEX[(d0 >> 8) & 0xff] +
|
|
12
|
+
HEX[(d0 >> 16) & 0xff] +
|
|
13
|
+
HEX[(d0 >> 24) & 0xff] +
|
|
14
|
+
'-' +
|
|
15
|
+
HEX[d1 & 0xff] +
|
|
16
|
+
HEX[(d1 >> 8) & 0xff] +
|
|
17
|
+
'-' +
|
|
18
|
+
HEX[((d1 >> 16) & 0x0f) | 0x40] +
|
|
19
|
+
HEX[(d1 >> 24) & 0xff] +
|
|
20
|
+
'-' +
|
|
21
|
+
HEX[(d2 & 0x3f) | 0x80] +
|
|
22
|
+
HEX[(d2 >> 8) & 0xff] +
|
|
23
|
+
'-' +
|
|
24
|
+
HEX[(d2 >> 16) & 0xff] +
|
|
25
|
+
HEX[(d2 >> 24) & 0xff] +
|
|
26
|
+
HEX[d3 & 0xff] +
|
|
27
|
+
HEX[(d3 >> 8) & 0xff] +
|
|
28
|
+
HEX[(d3 >> 16) & 0xff] +
|
|
29
|
+
HEX[(d3 >> 24) & 0xff];
|
|
30
|
+
}
|
|
31
|
+
export { guid, guid as default };
|
package/esm/is.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { equal } from './equal';
|
|
2
|
+
import { isArray, isNeArray } from './array';
|
|
3
|
+
import { isBoolean } from './boolean';
|
|
4
|
+
import { isDate } from './date';
|
|
5
|
+
import { isFormData } from './formdata';
|
|
6
|
+
import { isFn, isAsyncFn } from './function';
|
|
7
|
+
import { isRegExp } from './regexp';
|
|
8
|
+
import { isObject, isNeObject } from './object';
|
|
9
|
+
import { isNum, isNumBetween, isNumLt, isNumLte, isNumGt, isNumGte, isInt, isIntBetween, isIntLt, isIntLte, isIntGt, isIntGte, } from './number';
|
|
10
|
+
import { isString, isStringBetween, isNeString, } from './string';
|
|
11
|
+
const Is = Object.freeze({
|
|
12
|
+
Array: isArray,
|
|
13
|
+
NeArray: isNeArray,
|
|
14
|
+
NotEmptyArray: isNeArray,
|
|
15
|
+
Boolean: isBoolean,
|
|
16
|
+
Date: isDate,
|
|
17
|
+
Formdata: isFormData,
|
|
18
|
+
Function: isFn,
|
|
19
|
+
AsyncFunction: isAsyncFn,
|
|
20
|
+
Num: isNum,
|
|
21
|
+
NumBetween: isNumBetween,
|
|
22
|
+
NumAbove: isNumGt,
|
|
23
|
+
NumAboveOrEqual: isNumGte,
|
|
24
|
+
NumBelow: isNumLt,
|
|
25
|
+
NumBelowOrEqual: isNumLte,
|
|
26
|
+
NumGt: isNumGt,
|
|
27
|
+
NumGte: isNumGte,
|
|
28
|
+
NumLt: isNumLt,
|
|
29
|
+
NumLte: isNumLte,
|
|
30
|
+
Number: isNum,
|
|
31
|
+
NumberBetween: isNumBetween,
|
|
32
|
+
NumberAbove: isNumGt,
|
|
33
|
+
NumberAboveOrEqual: isNumGte,
|
|
34
|
+
NumberBelow: isNumLt,
|
|
35
|
+
NumberBelowOrEqual: isNumLte,
|
|
36
|
+
Int: isInt,
|
|
37
|
+
IntBetween: isIntBetween,
|
|
38
|
+
IntAbove: isIntGt,
|
|
39
|
+
IntAboveOrEqual: isIntGte,
|
|
40
|
+
IntBelow: isIntLt,
|
|
41
|
+
IntBelowOrEqual: isIntLte,
|
|
42
|
+
IntGt: isIntGt,
|
|
43
|
+
IntGte: isIntGte,
|
|
44
|
+
IntLt: isIntLt,
|
|
45
|
+
IntLte: isIntLte,
|
|
46
|
+
Integer: isInt,
|
|
47
|
+
IntegerBetween: isIntBetween,
|
|
48
|
+
IntegerBelow: isIntLt,
|
|
49
|
+
IntegerBelowOrEqual: isIntLte,
|
|
50
|
+
IntegerAbove: isIntGt,
|
|
51
|
+
IntegerAboveOrEqual: isIntGte,
|
|
52
|
+
RegExp: isRegExp,
|
|
53
|
+
Object: isObject,
|
|
54
|
+
NeObject: isNeObject,
|
|
55
|
+
NotEmptyObject: isNeObject,
|
|
56
|
+
String: isString,
|
|
57
|
+
StringBetween: isStringBetween,
|
|
58
|
+
NeString: isNeString,
|
|
59
|
+
NotEmptyString: isNeString,
|
|
60
|
+
Equal: equal,
|
|
61
|
+
Eq: equal,
|
|
62
|
+
});
|
|
63
|
+
export { Is, Is as default };
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
12
|
+
var _PubSub_instances, _PubSub_subscriptions, _PubSub_name, _PubSub_log, _PubSub_store_by_default, _PubSub_publishToSubscriber;
|
|
13
|
+
import { isArray } from '../array/is';
|
|
14
|
+
import { isFunction } from '../function/is';
|
|
15
|
+
import { noop } from '../function/noop';
|
|
16
|
+
import { guid } from '../hash/guid';
|
|
17
|
+
import { isObject } from '../object/is';
|
|
18
|
+
import { isNotEmptyObject } from '../object/isNotEmpty';
|
|
19
|
+
import { isNotEmptyString } from '../string/isNotEmpty';
|
|
20
|
+
class PubSub {
|
|
21
|
+
constructor(options = {}) {
|
|
22
|
+
_PubSub_instances.add(this);
|
|
23
|
+
_PubSub_subscriptions.set(this, new Map());
|
|
24
|
+
_PubSub_name.set(this, 'PubSub');
|
|
25
|
+
_PubSub_log.set(this, noop);
|
|
26
|
+
_PubSub_store_by_default.set(this, false);
|
|
27
|
+
if (!isObject(options))
|
|
28
|
+
throw new Error('PubSub@ctor: options should be an object');
|
|
29
|
+
if ('logger' in options) {
|
|
30
|
+
if (!isFunction(options.logger))
|
|
31
|
+
throw new Error('PubSub@ctor: logger should be a function');
|
|
32
|
+
__classPrivateFieldSet(this, _PubSub_log, options.logger, "f");
|
|
33
|
+
}
|
|
34
|
+
if ('name' in options) {
|
|
35
|
+
if (!isNotEmptyString(options.name))
|
|
36
|
+
throw new Error('PubSub@ctor: name should be a non-empty string');
|
|
37
|
+
__classPrivateFieldSet(this, _PubSub_name, options.name.trim(), "f");
|
|
38
|
+
}
|
|
39
|
+
if ('store' in options) {
|
|
40
|
+
if (options.store !== true && options.store !== false)
|
|
41
|
+
throw new Error('PubSub@ctor: store should be a boolean');
|
|
42
|
+
__classPrivateFieldSet(this, _PubSub_store_by_default, !!options.store, "f");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
get name() {
|
|
46
|
+
return __classPrivateFieldGet(this, _PubSub_name, "f");
|
|
47
|
+
}
|
|
48
|
+
publish(event, data, store) {
|
|
49
|
+
if (!isNotEmptyString(event))
|
|
50
|
+
return;
|
|
51
|
+
let entry = __classPrivateFieldGet(this, _PubSub_subscriptions, "f").get(event);
|
|
52
|
+
const shouldStore = store !== undefined ? store : __classPrivateFieldGet(this, _PubSub_store_by_default, "f");
|
|
53
|
+
if (!entry) {
|
|
54
|
+
if (!shouldStore)
|
|
55
|
+
return;
|
|
56
|
+
entry = { value: data, subscribers: new Map() };
|
|
57
|
+
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").set(event, entry);
|
|
58
|
+
}
|
|
59
|
+
else if (shouldStore) {
|
|
60
|
+
entry.value = data;
|
|
61
|
+
}
|
|
62
|
+
const entriesArray = [...entry.subscribers.entries()];
|
|
63
|
+
for (const [client_id, client_handler] of entriesArray) {
|
|
64
|
+
__classPrivateFieldGet(this, _PubSub_instances, "m", _PubSub_publishToSubscriber).call(this, event, entry, client_id, client_handler, data);
|
|
65
|
+
}
|
|
66
|
+
if (!shouldStore && !entry.subscribers.size)
|
|
67
|
+
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").delete(event);
|
|
68
|
+
}
|
|
69
|
+
subscribe(events, client_id, override = true) {
|
|
70
|
+
if (!isNotEmptyObject(events))
|
|
71
|
+
return null;
|
|
72
|
+
const uid = isNotEmptyString(client_id) ? client_id : guid();
|
|
73
|
+
for (const event of Object.keys(events)) {
|
|
74
|
+
const raw_payload = events[event];
|
|
75
|
+
let normalized_payload = null;
|
|
76
|
+
if (isFunction(raw_payload)) {
|
|
77
|
+
normalized_payload = { run: raw_payload, once: false };
|
|
78
|
+
}
|
|
79
|
+
else if (isFunction(raw_payload?.run)) {
|
|
80
|
+
normalized_payload = {
|
|
81
|
+
run: raw_payload.run,
|
|
82
|
+
once: raw_payload.once === true,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (!normalized_payload)
|
|
86
|
+
continue;
|
|
87
|
+
let entry = __classPrivateFieldGet(this, _PubSub_subscriptions, "f").get(event);
|
|
88
|
+
if (!entry) {
|
|
89
|
+
entry = { subscribers: new Map(), value: undefined };
|
|
90
|
+
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").set(event, entry);
|
|
91
|
+
}
|
|
92
|
+
if (override === false && entry.subscribers.has(uid))
|
|
93
|
+
continue;
|
|
94
|
+
entry.subscribers.set(uid, normalized_payload);
|
|
95
|
+
if (entry.value !== undefined) {
|
|
96
|
+
__classPrivateFieldGet(this, _PubSub_instances, "m", _PubSub_publishToSubscriber).call(this, event, entry, uid, normalized_payload, entry.value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return uid;
|
|
100
|
+
}
|
|
101
|
+
unsubscribe(client_id, event) {
|
|
102
|
+
if (!isNotEmptyString(client_id))
|
|
103
|
+
return;
|
|
104
|
+
const events = isNotEmptyString(event)
|
|
105
|
+
? [event]
|
|
106
|
+
: isArray(event)
|
|
107
|
+
? event
|
|
108
|
+
: [...__classPrivateFieldGet(this, _PubSub_subscriptions, "f").keys()];
|
|
109
|
+
for (let i = 0; i < events.length; i++) {
|
|
110
|
+
const ev = events[i];
|
|
111
|
+
if (!isNotEmptyString(ev))
|
|
112
|
+
continue;
|
|
113
|
+
const entry = __classPrivateFieldGet(this, _PubSub_subscriptions, "f").get(ev);
|
|
114
|
+
if (entry) {
|
|
115
|
+
entry.subscribers.delete(client_id);
|
|
116
|
+
if (!entry.subscribers.size) {
|
|
117
|
+
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").delete(ev);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
clientIds(event) {
|
|
123
|
+
const events = isNotEmptyString(event)
|
|
124
|
+
? [event]
|
|
125
|
+
: isArray(event)
|
|
126
|
+
? event
|
|
127
|
+
: [...__classPrivateFieldGet(this, _PubSub_subscriptions, "f").keys()];
|
|
128
|
+
const result = new Set();
|
|
129
|
+
for (let i = 0; i < events.length; i++) {
|
|
130
|
+
const entry = __classPrivateFieldGet(this, _PubSub_subscriptions, "f").get(events[i]);
|
|
131
|
+
if (entry) {
|
|
132
|
+
for (const key of entry.subscribers.keys()) {
|
|
133
|
+
result.add(key);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
}
|
|
139
|
+
clear(event) {
|
|
140
|
+
if (isNotEmptyString(event)) {
|
|
141
|
+
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").delete(event);
|
|
142
|
+
}
|
|
143
|
+
else if (isArray(event)) {
|
|
144
|
+
for (let i = 0; i < event.length; i++) {
|
|
145
|
+
const ev = event[i];
|
|
146
|
+
if (isNotEmptyString(ev)) {
|
|
147
|
+
__classPrivateFieldGet(this, _PubSub_subscriptions, "f").delete(ev);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
__classPrivateFieldSet(this, _PubSub_subscriptions, new Map(), "f");
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
_PubSub_subscriptions = new WeakMap(), _PubSub_name = new WeakMap(), _PubSub_log = new WeakMap(), _PubSub_store_by_default = new WeakMap(), _PubSub_instances = new WeakSet(), _PubSub_publishToSubscriber = function _PubSub_publishToSubscriber(event, map, client_id, sub, data) {
|
|
157
|
+
try {
|
|
158
|
+
const out = sub.run(data);
|
|
159
|
+
if (sub.once)
|
|
160
|
+
map.subscribers.delete(client_id);
|
|
161
|
+
if (isFunction(out?.catch) && isFunction(out?.then)) {
|
|
162
|
+
Promise.resolve(out).catch(err => {
|
|
163
|
+
__classPrivateFieldGet(this, _PubSub_log, "f").call(this, {
|
|
164
|
+
name: __classPrivateFieldGet(this, _PubSub_name, "f"),
|
|
165
|
+
event,
|
|
166
|
+
client_id,
|
|
167
|
+
msg: '[async] ' + __classPrivateFieldGet(this, _PubSub_name, "f") + '@publish: ' + (err?.message || 'Unknown Error'),
|
|
168
|
+
on: new Date(),
|
|
169
|
+
data,
|
|
170
|
+
err: err,
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
catch (err) {
|
|
176
|
+
__classPrivateFieldGet(this, _PubSub_log, "f").call(this, {
|
|
177
|
+
name: __classPrivateFieldGet(this, _PubSub_name, "f"),
|
|
178
|
+
event,
|
|
179
|
+
client_id,
|
|
180
|
+
msg: '[sync] ' + __classPrivateFieldGet(this, _PubSub_name, "f") + '@publish: ' + (err?.message || 'Unknown Error'),
|
|
181
|
+
on: new Date(),
|
|
182
|
+
data,
|
|
183
|
+
err: err,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
export { PubSub, PubSub as default };
|