@zenstackhq/common-helpers 3.5.6 → 3.6.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/dist/index.cjs +114 -181
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -12
- package/dist/{index.d.ts → index.d.mts} +24 -12
- package/dist/index.mjs +128 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +10 -8
- package/dist/index.js +0 -171
- package/dist/index.js.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,206 +1,139 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
-
|
|
21
|
-
// src/index.ts
|
|
22
|
-
var src_exports = {};
|
|
23
|
-
__export(src_exports, {
|
|
24
|
-
clone: () => clone,
|
|
25
|
-
enumerate: () => enumerate,
|
|
26
|
-
invariant: () => invariant,
|
|
27
|
-
isPlainObject: () => isPlainObject,
|
|
28
|
-
lowerCaseFirst: () => lowerCaseFirst,
|
|
29
|
-
paramCase: () => paramCase,
|
|
30
|
-
safeJSONStringify: () => safeJSONStringify,
|
|
31
|
-
singleDebounce: () => singleDebounce,
|
|
32
|
-
sleep: () => sleep,
|
|
33
|
-
upperCaseFirst: () => upperCaseFirst,
|
|
34
|
-
zip: () => zip
|
|
35
|
-
});
|
|
36
|
-
module.exports = __toCommonJS(src_exports);
|
|
37
|
-
|
|
38
|
-
// src/is-plain-object.ts
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/is-plain-object.ts
|
|
39
3
|
function isObject(o) {
|
|
40
|
-
|
|
4
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
41
5
|
}
|
|
42
|
-
__name(isObject, "isObject");
|
|
43
6
|
function isPlainObject(o) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
return true;
|
|
7
|
+
if (isObject(o) === false) return false;
|
|
8
|
+
const ctor = o.constructor;
|
|
9
|
+
if (ctor === void 0) return true;
|
|
10
|
+
const prot = ctor.prototype;
|
|
11
|
+
if (isObject(prot) === false) return false;
|
|
12
|
+
if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) return false;
|
|
13
|
+
return true;
|
|
53
14
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/clone.ts
|
|
17
|
+
/**
|
|
18
|
+
* Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.
|
|
19
|
+
*/
|
|
57
20
|
function clone(value) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
for (const key of Object.keys(value)) {
|
|
67
|
-
result[key] = clone(value[key]);
|
|
68
|
-
}
|
|
69
|
-
return result;
|
|
70
|
-
}
|
|
71
|
-
return value;
|
|
21
|
+
if (Array.isArray(value)) return value.map((v) => clone(v));
|
|
22
|
+
if (typeof value === "object") {
|
|
23
|
+
if (!value || !isPlainObject(value)) return value;
|
|
24
|
+
const result = {};
|
|
25
|
+
for (const key of Object.keys(value)) result[key] = clone(value[key]);
|
|
26
|
+
return result;
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
72
29
|
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/enumerable.ts
|
|
32
|
+
/**
|
|
33
|
+
* Uniformly enumerates an array or scalar.
|
|
34
|
+
*/
|
|
76
35
|
function enumerate(x) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
return x;
|
|
81
|
-
} else {
|
|
82
|
-
return [
|
|
83
|
-
x
|
|
84
|
-
];
|
|
85
|
-
}
|
|
36
|
+
if (x === null || x === void 0) return [];
|
|
37
|
+
else if (Array.isArray(x)) return x;
|
|
38
|
+
else return [x];
|
|
86
39
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// src/lower-case-first.ts
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/lower-case-first.ts
|
|
90
42
|
function lowerCaseFirst(input) {
|
|
91
|
-
|
|
43
|
+
return input.charAt(0).toLowerCase() + input.slice(1);
|
|
92
44
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/param-case.ts
|
|
47
|
+
const DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;
|
|
48
|
+
const DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;
|
|
49
|
+
const DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
|
|
99
50
|
function paramCase(input) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
51
|
+
const result = input.replace(DEFAULT_SPLIT_REGEXP_1, "$1\0$2").replace(DEFAULT_SPLIT_REGEXP_2, "$1\0$2").replace(DEFAULT_STRIP_REGEXP, "\0");
|
|
52
|
+
let start = 0;
|
|
53
|
+
let end = result.length;
|
|
54
|
+
while (result.charAt(start) === "\0") start++;
|
|
55
|
+
while (result.charAt(end - 1) === "\0") end--;
|
|
56
|
+
return result.slice(start, end).split("\0").map((str) => str.toLowerCase()).join("-");
|
|
106
57
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/safe-json-stringify.ts
|
|
60
|
+
/**
|
|
61
|
+
* A safe JSON stringify that handles bigint values.
|
|
62
|
+
*/
|
|
110
63
|
function safeJSONStringify(value) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
return v;
|
|
116
|
-
}
|
|
117
|
-
});
|
|
64
|
+
return JSON.stringify(value, (_, v) => {
|
|
65
|
+
if (typeof v === "bigint") return v.toString();
|
|
66
|
+
else return v;
|
|
67
|
+
});
|
|
118
68
|
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
// src/single-debounce.ts
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/single-debounce.ts
|
|
122
71
|
function singleDebounce(cb, debounceMc, reRunOnInProgressCall = false) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return () => {
|
|
145
|
-
clearTimeout(timeout);
|
|
146
|
-
timeout = setTimeout(run, debounceMc);
|
|
147
|
-
};
|
|
72
|
+
let timeout;
|
|
73
|
+
let inProgress = false;
|
|
74
|
+
let pendingInProgress = false;
|
|
75
|
+
const run = async () => {
|
|
76
|
+
if (inProgress) {
|
|
77
|
+
if (reRunOnInProgressCall) pendingInProgress = true;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
inProgress = true;
|
|
81
|
+
pendingInProgress = false;
|
|
82
|
+
try {
|
|
83
|
+
await cb();
|
|
84
|
+
} finally {
|
|
85
|
+
inProgress = false;
|
|
86
|
+
if (pendingInProgress) await run();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
return () => {
|
|
90
|
+
clearTimeout(timeout);
|
|
91
|
+
timeout = setTimeout(run, debounceMc);
|
|
92
|
+
};
|
|
148
93
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
// src/sleep.ts
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/sleep.ts
|
|
152
96
|
function sleep(timeout) {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
97
|
+
return new Promise((resolve) => {
|
|
98
|
+
setTimeout(() => resolve(), timeout);
|
|
99
|
+
});
|
|
156
100
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
var prefix = "Invariant failed";
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/tiny-invariant.ts
|
|
103
|
+
const isProduction = typeof process !== "undefined" && process.env["NODE_ENV"] === "production";
|
|
104
|
+
const prefix = "Invariant failed";
|
|
162
105
|
function invariant(condition, message) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
if (isProduction) {
|
|
167
|
-
throw new Error(prefix);
|
|
168
|
-
}
|
|
169
|
-
throw new Error(message ? `${prefix}: ${message}` : prefix);
|
|
106
|
+
if (condition) return;
|
|
107
|
+
if (isProduction) throw new Error(prefix);
|
|
108
|
+
throw new Error(message ? `${prefix}: ${message}` : prefix);
|
|
170
109
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
// src/upper-case-first.ts
|
|
110
|
+
//#endregion
|
|
111
|
+
//#region src/upper-case-first.ts
|
|
174
112
|
function upperCaseFirst(input) {
|
|
175
|
-
|
|
113
|
+
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
176
114
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
115
|
+
//#endregion
|
|
116
|
+
//#region src/zip.ts
|
|
117
|
+
/**
|
|
118
|
+
* Zips two arrays into an array of tuples.
|
|
119
|
+
*/
|
|
180
120
|
function zip(arr1, arr2) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
arr1[i],
|
|
186
|
-
arr2[i]
|
|
187
|
-
]);
|
|
188
|
-
}
|
|
189
|
-
return result;
|
|
121
|
+
const length = Math.min(arr1.length, arr2.length);
|
|
122
|
+
const result = [];
|
|
123
|
+
for (let i = 0; i < length; i++) result.push([arr1[i], arr2[i]]);
|
|
124
|
+
return result;
|
|
190
125
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
zip
|
|
205
|
-
});
|
|
126
|
+
//#endregion
|
|
127
|
+
exports.clone = clone;
|
|
128
|
+
exports.enumerate = enumerate;
|
|
129
|
+
exports.invariant = invariant;
|
|
130
|
+
exports.isPlainObject = isPlainObject;
|
|
131
|
+
exports.lowerCaseFirst = lowerCaseFirst;
|
|
132
|
+
exports.paramCase = paramCase;
|
|
133
|
+
exports.safeJSONStringify = safeJSONStringify;
|
|
134
|
+
exports.singleDebounce = singleDebounce;
|
|
135
|
+
exports.sleep = sleep;
|
|
136
|
+
exports.upperCaseFirst = upperCaseFirst;
|
|
137
|
+
exports.zip = zip;
|
|
138
|
+
|
|
206
139
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"index.cjs","names":[],"sources":["../src/is-plain-object.ts","../src/clone.ts","../src/enumerable.ts","../src/lower-case-first.ts","../src/param-case.ts","../src/safe-json-stringify.ts","../src/single-debounce.ts","../src/sleep.ts","../src/tiny-invariant.ts","../src/upper-case-first.ts","../src/zip.ts"],"sourcesContent":["function isObject(o: unknown) {\n return Object.prototype.toString.call(o) === '[object Object]';\n}\n\nexport function isPlainObject(o: unknown) {\n if (isObject(o) === false) return false;\n\n // If has modified constructor\n const ctor = (o as { constructor: unknown }).constructor;\n if (ctor === undefined) return true;\n\n // If has modified prototype\n const prot = (ctor as { prototype: unknown }).prototype;\n if (isObject(prot) === false) return false;\n\n // If constructor does not have an Object-specific method\n if (Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf') === false) {\n return false;\n }\n\n // Most likely a plain Object\n return true;\n}\n","import { isPlainObject } from './is-plain-object';\n\n/**\n * Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.\n */\nexport function clone<T>(value: T): T {\n if (Array.isArray(value)) {\n return value.map((v) => clone(v)) as T;\n }\n\n if (typeof value === 'object') {\n if (!value || !isPlainObject(value)) {\n return value;\n }\n\n const result: any = {};\n for (const key of Object.keys(value)) {\n result[key] = clone(value[key as keyof T]);\n }\n return result;\n }\n\n return value;\n}\n","/**\n * Array or scalar\n */\nexport type Enumerable<T> = T | Array<T>;\n\n/**\n * Uniformly enumerates an array or scalar.\n */\nexport function enumerate<T>(x: Enumerable<T>) {\n if (x === null || x === undefined) {\n return [];\n } else if (Array.isArray(x)) {\n return x;\n } else {\n return [x];\n }\n}\n","export function lowerCaseFirst(input: string) {\n return input.charAt(0).toLowerCase() + input.slice(1);\n}\n","const DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;\nconst DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;\nconst DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;\n\nexport function paramCase(input: string) {\n const result = input\n .replace(DEFAULT_SPLIT_REGEXP_1, '$1\\0$2')\n .replace(DEFAULT_SPLIT_REGEXP_2, '$1\\0$2')\n .replace(DEFAULT_STRIP_REGEXP, '\\0');\n\n let start = 0;\n let end = result.length;\n\n while (result.charAt(start) === '\\0') start++;\n while (result.charAt(end - 1) === '\\0') end--;\n\n return result\n .slice(start, end)\n .split('\\0')\n .map((str) => str.toLowerCase())\n .join('-');\n}\n","/**\n * A safe JSON stringify that handles bigint values.\n */\nexport function safeJSONStringify(value: unknown) {\n return JSON.stringify(value, (_, v) => {\n if (typeof v === 'bigint') {\n return v.toString();\n } else {\n return v;\n }\n });\n}\n","export function singleDebounce(cb: () => void | PromiseLike<void>, debounceMc: number, reRunOnInProgressCall: boolean = false) {\n let timeout: ReturnType<typeof setTimeout> | undefined;\n let inProgress = false;\n let pendingInProgress = false;\n\n const run = async () => {\n if (inProgress) {\n if (reRunOnInProgressCall) {\n pendingInProgress = true;\n }\n\n return;\n }\n\n inProgress = true;\n pendingInProgress = false;\n\n try {\n await cb();\n } finally {\n inProgress = false;\n\n if (pendingInProgress) {\n await run();\n }\n }\n };\n\n return () => {\n clearTimeout(timeout);\n\n timeout = setTimeout(run, debounceMc);\n }\n}\n","export function sleep(timeout: number) {\n return new Promise<void>((resolve) => {\n setTimeout(() => resolve(), timeout);\n });\n}\n","const isProduction = typeof process !== 'undefined' && process.env['NODE_ENV'] === 'production';\nconst prefix = 'Invariant failed';\n\nexport function invariant(condition: unknown, message?: string): asserts condition {\n if (condition) {\n return;\n }\n\n if (isProduction) {\n throw new Error(prefix);\n }\n\n throw new Error(message ? `${prefix}: ${message}` : prefix);\n}\n","export function upperCaseFirst(input: string) {\n return input.charAt(0).toUpperCase() + input.slice(1);\n}\n","/**\n * Zips two arrays into an array of tuples.\n */\nexport function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]> {\n const length = Math.min(arr1.length, arr2.length);\n const result: Array<[T, U]> = [];\n for (let i = 0; i < length; i++) {\n result.push([arr1[i]!, arr2[i]!]);\n }\n return result;\n}\n"],"mappings":";;AAAA,SAAS,SAAS,GAAY;AAC1B,QAAO,OAAO,UAAU,SAAS,KAAK,EAAE,KAAK;;AAGjD,SAAgB,cAAc,GAAY;AACtC,KAAI,SAAS,EAAE,KAAK,MAAO,QAAO;CAGlC,MAAM,OAAQ,EAA+B;AAC7C,KAAI,SAAS,KAAA,EAAW,QAAO;CAG/B,MAAM,OAAQ,KAAgC;AAC9C,KAAI,SAAS,KAAK,KAAK,MAAO,QAAO;AAGrC,KAAI,OAAO,UAAU,eAAe,KAAK,MAAM,gBAAgB,KAAK,MAChE,QAAO;AAIX,QAAO;;;;;;;AChBX,SAAgB,MAAS,OAAa;AAClC,KAAI,MAAM,QAAQ,MAAM,CACpB,QAAO,MAAM,KAAK,MAAM,MAAM,EAAE,CAAC;AAGrC,KAAI,OAAO,UAAU,UAAU;AAC3B,MAAI,CAAC,SAAS,CAAC,cAAc,MAAM,CAC/B,QAAO;EAGX,MAAM,SAAc,EAAE;AACtB,OAAK,MAAM,OAAO,OAAO,KAAK,MAAM,CAChC,QAAO,OAAO,MAAM,MAAM,KAAgB;AAE9C,SAAO;;AAGX,QAAO;;;;;;;ACdX,SAAgB,UAAa,GAAkB;AAC3C,KAAI,MAAM,QAAQ,MAAM,KAAA,EACpB,QAAO,EAAE;UACF,MAAM,QAAQ,EAAE,CACvB,QAAO;KAEP,QAAO,CAAC,EAAE;;;;ACdlB,SAAgB,eAAe,OAAe;AAC1C,QAAO,MAAM,OAAO,EAAE,CAAC,aAAa,GAAG,MAAM,MAAM,EAAE;;;;ACDzD,MAAM,yBAAyB;AAC/B,MAAM,yBAAyB;AAC/B,MAAM,uBAAuB;AAE7B,SAAgB,UAAU,OAAe;CACrC,MAAM,SAAS,MACV,QAAQ,wBAAwB,SAAS,CACzC,QAA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ACEzD,SAAgB,IAAU,MAAoB,MAAmC;CAC7E,MAAM,SAAS,KAAK,IAAI,KAAK,QAAQ,KAAK,OAAO;CACjD,MAAM,SAAwB,EAAE;AAChC,MAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,IACxB,QAAO,KAAK,CAAC,KAAK,IAAK,KAAK,GAAI,CAAC;AAErC,QAAO"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
//#region src/clone.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.
|
|
3
4
|
*/
|
|
4
5
|
declare function clone<T>(value: T): T;
|
|
5
|
-
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/enumerable.d.ts
|
|
6
8
|
/**
|
|
7
9
|
* Array or scalar
|
|
8
10
|
*/
|
|
@@ -11,29 +13,39 @@ type Enumerable<T> = T | Array<T>;
|
|
|
11
13
|
* Uniformly enumerates an array or scalar.
|
|
12
14
|
*/
|
|
13
15
|
declare function enumerate<T>(x: Enumerable<T>): T[];
|
|
14
|
-
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/is-plain-object.d.ts
|
|
15
18
|
declare function isPlainObject(o: unknown): boolean;
|
|
16
|
-
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/lower-case-first.d.ts
|
|
17
21
|
declare function lowerCaseFirst(input: string): string;
|
|
18
|
-
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/param-case.d.ts
|
|
19
24
|
declare function paramCase(input: string): string;
|
|
20
|
-
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/safe-json-stringify.d.ts
|
|
21
27
|
/**
|
|
22
28
|
* A safe JSON stringify that handles bigint values.
|
|
23
29
|
*/
|
|
24
30
|
declare function safeJSONStringify(value: unknown): string;
|
|
25
|
-
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/single-debounce.d.ts
|
|
26
33
|
declare function singleDebounce(cb: () => void | PromiseLike<void>, debounceMc: number, reRunOnInProgressCall?: boolean): () => void;
|
|
27
|
-
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/sleep.d.ts
|
|
28
36
|
declare function sleep(timeout: number): Promise<void>;
|
|
29
|
-
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/tiny-invariant.d.ts
|
|
30
39
|
declare function invariant(condition: unknown, message?: string): asserts condition;
|
|
31
|
-
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/upper-case-first.d.ts
|
|
32
42
|
declare function upperCaseFirst(input: string): string;
|
|
33
|
-
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/zip.d.ts
|
|
34
45
|
/**
|
|
35
46
|
* Zips two arrays into an array of tuples.
|
|
36
47
|
*/
|
|
37
48
|
declare function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]>;
|
|
38
|
-
|
|
39
|
-
export {
|
|
49
|
+
//#endregion
|
|
50
|
+
export { Enumerable, clone, enumerate, invariant, isPlainObject, lowerCaseFirst, paramCase, safeJSONStringify, singleDebounce, sleep, upperCaseFirst, zip };
|
|
51
|
+
//# sourceMappingURL=index.d.cts.map
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
+
//#region src/clone.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.
|
|
3
4
|
*/
|
|
4
5
|
declare function clone<T>(value: T): T;
|
|
5
|
-
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/enumerable.d.ts
|
|
6
8
|
/**
|
|
7
9
|
* Array or scalar
|
|
8
10
|
*/
|
|
@@ -11,29 +13,39 @@ type Enumerable<T> = T | Array<T>;
|
|
|
11
13
|
* Uniformly enumerates an array or scalar.
|
|
12
14
|
*/
|
|
13
15
|
declare function enumerate<T>(x: Enumerable<T>): T[];
|
|
14
|
-
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region src/is-plain-object.d.ts
|
|
15
18
|
declare function isPlainObject(o: unknown): boolean;
|
|
16
|
-
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/lower-case-first.d.ts
|
|
17
21
|
declare function lowerCaseFirst(input: string): string;
|
|
18
|
-
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/param-case.d.ts
|
|
19
24
|
declare function paramCase(input: string): string;
|
|
20
|
-
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/safe-json-stringify.d.ts
|
|
21
27
|
/**
|
|
22
28
|
* A safe JSON stringify that handles bigint values.
|
|
23
29
|
*/
|
|
24
30
|
declare function safeJSONStringify(value: unknown): string;
|
|
25
|
-
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/single-debounce.d.ts
|
|
26
33
|
declare function singleDebounce(cb: () => void | PromiseLike<void>, debounceMc: number, reRunOnInProgressCall?: boolean): () => void;
|
|
27
|
-
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/sleep.d.ts
|
|
28
36
|
declare function sleep(timeout: number): Promise<void>;
|
|
29
|
-
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/tiny-invariant.d.ts
|
|
30
39
|
declare function invariant(condition: unknown, message?: string): asserts condition;
|
|
31
|
-
|
|
40
|
+
//#endregion
|
|
41
|
+
//#region src/upper-case-first.d.ts
|
|
32
42
|
declare function upperCaseFirst(input: string): string;
|
|
33
|
-
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/zip.d.ts
|
|
34
45
|
/**
|
|
35
46
|
* Zips two arrays into an array of tuples.
|
|
36
47
|
*/
|
|
37
48
|
declare function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]>;
|
|
38
|
-
|
|
39
|
-
export {
|
|
49
|
+
//#endregion
|
|
50
|
+
export { Enumerable, clone, enumerate, invariant, isPlainObject, lowerCaseFirst, paramCase, safeJSONStringify, singleDebounce, sleep, upperCaseFirst, zip };
|
|
51
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
//#region src/is-plain-object.ts
|
|
2
|
+
function isObject(o) {
|
|
3
|
+
return Object.prototype.toString.call(o) === "[object Object]";
|
|
4
|
+
}
|
|
5
|
+
function isPlainObject(o) {
|
|
6
|
+
if (isObject(o) === false) return false;
|
|
7
|
+
const ctor = o.constructor;
|
|
8
|
+
if (ctor === void 0) return true;
|
|
9
|
+
const prot = ctor.prototype;
|
|
10
|
+
if (isObject(prot) === false) return false;
|
|
11
|
+
if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) return false;
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region src/clone.ts
|
|
16
|
+
/**
|
|
17
|
+
* Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.
|
|
18
|
+
*/
|
|
19
|
+
function clone(value) {
|
|
20
|
+
if (Array.isArray(value)) return value.map((v) => clone(v));
|
|
21
|
+
if (typeof value === "object") {
|
|
22
|
+
if (!value || !isPlainObject(value)) return value;
|
|
23
|
+
const result = {};
|
|
24
|
+
for (const key of Object.keys(value)) result[key] = clone(value[key]);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
return value;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/enumerable.ts
|
|
31
|
+
/**
|
|
32
|
+
* Uniformly enumerates an array or scalar.
|
|
33
|
+
*/
|
|
34
|
+
function enumerate(x) {
|
|
35
|
+
if (x === null || x === void 0) return [];
|
|
36
|
+
else if (Array.isArray(x)) return x;
|
|
37
|
+
else return [x];
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/lower-case-first.ts
|
|
41
|
+
function lowerCaseFirst(input) {
|
|
42
|
+
return input.charAt(0).toLowerCase() + input.slice(1);
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/param-case.ts
|
|
46
|
+
const DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;
|
|
47
|
+
const DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;
|
|
48
|
+
const DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
|
|
49
|
+
function paramCase(input) {
|
|
50
|
+
const result = input.replace(DEFAULT_SPLIT_REGEXP_1, "$1\0$2").replace(DEFAULT_SPLIT_REGEXP_2, "$1\0$2").replace(DEFAULT_STRIP_REGEXP, "\0");
|
|
51
|
+
let start = 0;
|
|
52
|
+
let end = result.length;
|
|
53
|
+
while (result.charAt(start) === "\0") start++;
|
|
54
|
+
while (result.charAt(end - 1) === "\0") end--;
|
|
55
|
+
return result.slice(start, end).split("\0").map((str) => str.toLowerCase()).join("-");
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/safe-json-stringify.ts
|
|
59
|
+
/**
|
|
60
|
+
* A safe JSON stringify that handles bigint values.
|
|
61
|
+
*/
|
|
62
|
+
function safeJSONStringify(value) {
|
|
63
|
+
return JSON.stringify(value, (_, v) => {
|
|
64
|
+
if (typeof v === "bigint") return v.toString();
|
|
65
|
+
else return v;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region src/single-debounce.ts
|
|
70
|
+
function singleDebounce(cb, debounceMc, reRunOnInProgressCall = false) {
|
|
71
|
+
let timeout;
|
|
72
|
+
let inProgress = false;
|
|
73
|
+
let pendingInProgress = false;
|
|
74
|
+
const run = async () => {
|
|
75
|
+
if (inProgress) {
|
|
76
|
+
if (reRunOnInProgressCall) pendingInProgress = true;
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
inProgress = true;
|
|
80
|
+
pendingInProgress = false;
|
|
81
|
+
try {
|
|
82
|
+
await cb();
|
|
83
|
+
} finally {
|
|
84
|
+
inProgress = false;
|
|
85
|
+
if (pendingInProgress) await run();
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
return () => {
|
|
89
|
+
clearTimeout(timeout);
|
|
90
|
+
timeout = setTimeout(run, debounceMc);
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/sleep.ts
|
|
95
|
+
function sleep(timeout) {
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
setTimeout(() => resolve(), timeout);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/tiny-invariant.ts
|
|
102
|
+
const isProduction = typeof process !== "undefined" && process.env["NODE_ENV"] === "production";
|
|
103
|
+
const prefix = "Invariant failed";
|
|
104
|
+
function invariant(condition, message) {
|
|
105
|
+
if (condition) return;
|
|
106
|
+
if (isProduction) throw new Error(prefix);
|
|
107
|
+
throw new Error(message ? `${prefix}: ${message}` : prefix);
|
|
108
|
+
}
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/upper-case-first.ts
|
|
111
|
+
function upperCaseFirst(input) {
|
|
112
|
+
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
113
|
+
}
|
|
114
|
+
//#endregion
|
|
115
|
+
//#region src/zip.ts
|
|
116
|
+
/**
|
|
117
|
+
* Zips two arrays into an array of tuples.
|
|
118
|
+
*/
|
|
119
|
+
function zip(arr1, arr2) {
|
|
120
|
+
const length = Math.min(arr1.length, arr2.length);
|
|
121
|
+
const result = [];
|
|
122
|
+
for (let i = 0; i < length; i++) result.push([arr1[i], arr2[i]]);
|
|
123
|
+
return result;
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
export { clone, enumerate, invariant, isPlainObject, lowerCaseFirst, paramCase, safeJSONStringify, singleDebounce, sleep, upperCaseFirst, zip };
|
|
127
|
+
|
|
128
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/is-plain-object.ts","../src/clone.ts","../src/enumerable.ts","../src/lower-case-first.ts","../src/param-case.ts","../src/safe-json-stringify.ts","../src/single-debounce.ts","../src/sleep.ts","../src/tiny-invariant.ts","../src/upper-case-first.ts","../src/zip.ts"],"sourcesContent":["function isObject(o: unknown) {\n return Object.prototype.toString.call(o) === '[object Object]';\n}\n\nexport function isPlainObject(o: unknown) {\n if (isObject(o) === false) return false;\n\n // If has modified constructor\n const ctor = (o as { constructor: unknown }).constructor;\n if (ctor === undefined) return true;\n\n // If has modified prototype\n const prot = (ctor as { prototype: unknown }).prototype;\n if (isObject(prot) === false) return false;\n\n // If constructor does not have an Object-specific method\n if (Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf') === false) {\n return false;\n }\n\n // Most likely a plain Object\n return true;\n}\n","import { isPlainObject } from './is-plain-object';\n\n/**\n * Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.\n */\nexport function clone<T>(value: T): T {\n if (Array.isArray(value)) {\n return value.map((v) => clone(v)) as T;\n }\n\n if (typeof value === 'object') {\n if (!value || !isPlainObject(value)) {\n return value;\n }\n\n const result: any = {};\n for (const key of Object.keys(value)) {\n result[key] = clone(value[key as keyof T]);\n }\n return result;\n }\n\n return value;\n}\n","/**\n * Array or scalar\n */\nexport type Enumerable<T> = T | Array<T>;\n\n/**\n * Uniformly enumerates an array or scalar.\n */\nexport function enumerate<T>(x: Enumerable<T>) {\n if (x === null || x === undefined) {\n return [];\n } else if (Array.isArray(x)) {\n return x;\n } else {\n return [x];\n }\n}\n","export function lowerCaseFirst(input: string) {\n return input.charAt(0).toLowerCase() + input.slice(1);\n}\n","const DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;\nconst DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;\nconst DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;\n\nexport function paramCase(input: string) {\n const result = input\n .replace(DEFAULT_SPLIT_REGEXP_1, '$1\\0$2')\n .replace(DEFAULT_SPLIT_REGEXP_2, '$1\\0$2')\n .replace(DEFAULT_STRIP_REGEXP, '\\0');\n\n let start = 0;\n let end = result.length;\n\n while (result.charAt(start) === '\\0') start++;\n while (result.charAt(end - 1) === '\\0') end--;\n\n return result\n .slice(start, end)\n .split('\\0')\n .map((str) => str.toLowerCase())\n .join('-');\n}\n","/**\n * A safe JSON stringify that handles bigint values.\n */\nexport function safeJSONStringify(value: unknown) {\n return JSON.stringify(value, (_, v) => {\n if (typeof v === 'bigint') {\n return v.toString();\n } else {\n return v;\n }\n });\n}\n","export function singleDebounce(cb: () => void | PromiseLike<void>, debounceMc: number, reRunOnInProgressCall: boolean = false) {\n let timeout: ReturnType<typeof setTimeout> | undefined;\n let inProgress = false;\n let pendingInProgress = false;\n\n const run = async () => {\n if (inProgress) {\n if (reRunOnInProgressCall) {\n pendingInProgress = true;\n }\n\n return;\n }\n\n inProgress = true;\n pendingInProgress = false;\n\n try {\n await cb();\n } finally {\n inProgress = false;\n\n if (pendingInProgress) {\n await run();\n }\n }\n };\n\n return () => {\n clearTimeout(timeout);\n\n timeout = setTimeout(run, debounceMc);\n }\n}\n","export function sleep(timeout: number) {\n return new Promise<void>((resolve) => {\n setTimeout(() => resolve(), timeout);\n });\n}\n","const isProduction = typeof process !== 'undefined' && process.env['NODE_ENV'] === 'production';\nconst prefix = 'Invariant failed';\n\nexport function invariant(condition: unknown, message?: string): asserts condition {\n if (condition) {\n return;\n }\n\n if (isProduction) {\n throw new Error(prefix);\n }\n\n throw new Error(message ? `${prefix}: ${message}` : prefix);\n}\n","export function upperCaseFirst(input: string) {\n return input.charAt(0).toUpperCase() + input.slice(1);\n}\n","/**\n * Zips two arrays into an array of tuples.\n */\nexport function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]> {\n const length = Math.min(arr1.length, arr2.length);\n const result: Array<[T, U]> = [];\n for (let i = 0; i < length; i++) {\n result.push([arr1[i]!, arr2[i]!]);\n }\n return result;\n}\n"],"mappings":";AAAA,SAAS,SAAS,GAAY;AAC1B,QAAO,OAAO,UAAU,SAAS,KAAK,EAAE,KAAK;;AAGjD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}
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@zenstackhq/common-helpers",
|
|
3
3
|
"displayName": "ZenStack Common Helpers",
|
|
4
4
|
"description": "ZenStack Common Helpers",
|
|
5
|
-
"version": "3.
|
|
5
|
+
"version": "3.6.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"author": {
|
|
8
8
|
"name": "ZenStack Team",
|
|
@@ -21,8 +21,8 @@
|
|
|
21
21
|
"exports": {
|
|
22
22
|
".": {
|
|
23
23
|
"import": {
|
|
24
|
-
"types": "./dist/index.d.
|
|
25
|
-
"default": "./dist/index.
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"default": "./dist/index.mjs"
|
|
26
26
|
},
|
|
27
27
|
"require": {
|
|
28
28
|
"types": "./dist/index.d.cts",
|
|
@@ -31,14 +31,16 @@
|
|
|
31
31
|
}
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@
|
|
35
|
-
"@zenstackhq/eslint-config": "3.
|
|
36
|
-
"@zenstackhq/
|
|
34
|
+
"@types/node": "^20.19.0",
|
|
35
|
+
"@zenstackhq/eslint-config": "3.6.0",
|
|
36
|
+
"@zenstackhq/typescript-config": "3.6.0",
|
|
37
|
+
"@zenstackhq/tsdown-config": "3.6.0",
|
|
38
|
+
"@zenstackhq/vitest-config": "3.6.0"
|
|
37
39
|
},
|
|
38
40
|
"funding": "https://github.com/sponsors/zenstackhq",
|
|
39
41
|
"scripts": {
|
|
40
|
-
"build": "tsc --noEmit &&
|
|
41
|
-
"watch": "
|
|
42
|
+
"build": "tsc --noEmit && tsdown",
|
|
43
|
+
"watch": "tsdown --watch",
|
|
42
44
|
"lint": "eslint src --ext ts",
|
|
43
45
|
"test": "vitest run",
|
|
44
46
|
"pack": "pnpm pack"
|
package/dist/index.js
DELETED
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
|
|
4
|
-
// src/is-plain-object.ts
|
|
5
|
-
function isObject(o) {
|
|
6
|
-
return Object.prototype.toString.call(o) === "[object Object]";
|
|
7
|
-
}
|
|
8
|
-
__name(isObject, "isObject");
|
|
9
|
-
function isPlainObject(o) {
|
|
10
|
-
if (isObject(o) === false) return false;
|
|
11
|
-
const ctor = o.constructor;
|
|
12
|
-
if (ctor === void 0) return true;
|
|
13
|
-
const prot = ctor.prototype;
|
|
14
|
-
if (isObject(prot) === false) return false;
|
|
15
|
-
if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
return true;
|
|
19
|
-
}
|
|
20
|
-
__name(isPlainObject, "isPlainObject");
|
|
21
|
-
|
|
22
|
-
// src/clone.ts
|
|
23
|
-
function clone(value) {
|
|
24
|
-
if (Array.isArray(value)) {
|
|
25
|
-
return value.map((v) => clone(v));
|
|
26
|
-
}
|
|
27
|
-
if (typeof value === "object") {
|
|
28
|
-
if (!value || !isPlainObject(value)) {
|
|
29
|
-
return value;
|
|
30
|
-
}
|
|
31
|
-
const result = {};
|
|
32
|
-
for (const key of Object.keys(value)) {
|
|
33
|
-
result[key] = clone(value[key]);
|
|
34
|
-
}
|
|
35
|
-
return result;
|
|
36
|
-
}
|
|
37
|
-
return value;
|
|
38
|
-
}
|
|
39
|
-
__name(clone, "clone");
|
|
40
|
-
|
|
41
|
-
// src/enumerable.ts
|
|
42
|
-
function enumerate(x) {
|
|
43
|
-
if (x === null || x === void 0) {
|
|
44
|
-
return [];
|
|
45
|
-
} else if (Array.isArray(x)) {
|
|
46
|
-
return x;
|
|
47
|
-
} else {
|
|
48
|
-
return [
|
|
49
|
-
x
|
|
50
|
-
];
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
__name(enumerate, "enumerate");
|
|
54
|
-
|
|
55
|
-
// src/lower-case-first.ts
|
|
56
|
-
function lowerCaseFirst(input) {
|
|
57
|
-
return input.charAt(0).toLowerCase() + input.slice(1);
|
|
58
|
-
}
|
|
59
|
-
__name(lowerCaseFirst, "lowerCaseFirst");
|
|
60
|
-
|
|
61
|
-
// src/param-case.ts
|
|
62
|
-
var DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;
|
|
63
|
-
var DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;
|
|
64
|
-
var DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;
|
|
65
|
-
function paramCase(input) {
|
|
66
|
-
const result = input.replace(DEFAULT_SPLIT_REGEXP_1, "$1\0$2").replace(DEFAULT_SPLIT_REGEXP_2, "$1\0$2").replace(DEFAULT_STRIP_REGEXP, "\0");
|
|
67
|
-
let start = 0;
|
|
68
|
-
let end = result.length;
|
|
69
|
-
while (result.charAt(start) === "\0") start++;
|
|
70
|
-
while (result.charAt(end - 1) === "\0") end--;
|
|
71
|
-
return result.slice(start, end).split("\0").map((str) => str.toLowerCase()).join("-");
|
|
72
|
-
}
|
|
73
|
-
__name(paramCase, "paramCase");
|
|
74
|
-
|
|
75
|
-
// src/safe-json-stringify.ts
|
|
76
|
-
function safeJSONStringify(value) {
|
|
77
|
-
return JSON.stringify(value, (_, v) => {
|
|
78
|
-
if (typeof v === "bigint") {
|
|
79
|
-
return v.toString();
|
|
80
|
-
} else {
|
|
81
|
-
return v;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
__name(safeJSONStringify, "safeJSONStringify");
|
|
86
|
-
|
|
87
|
-
// src/single-debounce.ts
|
|
88
|
-
function singleDebounce(cb, debounceMc, reRunOnInProgressCall = false) {
|
|
89
|
-
let timeout;
|
|
90
|
-
let inProgress = false;
|
|
91
|
-
let pendingInProgress = false;
|
|
92
|
-
const run = /* @__PURE__ */ __name(async () => {
|
|
93
|
-
if (inProgress) {
|
|
94
|
-
if (reRunOnInProgressCall) {
|
|
95
|
-
pendingInProgress = true;
|
|
96
|
-
}
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
inProgress = true;
|
|
100
|
-
pendingInProgress = false;
|
|
101
|
-
try {
|
|
102
|
-
await cb();
|
|
103
|
-
} finally {
|
|
104
|
-
inProgress = false;
|
|
105
|
-
if (pendingInProgress) {
|
|
106
|
-
await run();
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
}, "run");
|
|
110
|
-
return () => {
|
|
111
|
-
clearTimeout(timeout);
|
|
112
|
-
timeout = setTimeout(run, debounceMc);
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
__name(singleDebounce, "singleDebounce");
|
|
116
|
-
|
|
117
|
-
// src/sleep.ts
|
|
118
|
-
function sleep(timeout) {
|
|
119
|
-
return new Promise((resolve) => {
|
|
120
|
-
setTimeout(() => resolve(), timeout);
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
__name(sleep, "sleep");
|
|
124
|
-
|
|
125
|
-
// src/tiny-invariant.ts
|
|
126
|
-
var isProduction = typeof process !== "undefined" && process.env["NODE_ENV"] === "production";
|
|
127
|
-
var prefix = "Invariant failed";
|
|
128
|
-
function invariant(condition, message) {
|
|
129
|
-
if (condition) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
if (isProduction) {
|
|
133
|
-
throw new Error(prefix);
|
|
134
|
-
}
|
|
135
|
-
throw new Error(message ? `${prefix}: ${message}` : prefix);
|
|
136
|
-
}
|
|
137
|
-
__name(invariant, "invariant");
|
|
138
|
-
|
|
139
|
-
// src/upper-case-first.ts
|
|
140
|
-
function upperCaseFirst(input) {
|
|
141
|
-
return input.charAt(0).toUpperCase() + input.slice(1);
|
|
142
|
-
}
|
|
143
|
-
__name(upperCaseFirst, "upperCaseFirst");
|
|
144
|
-
|
|
145
|
-
// src/zip.ts
|
|
146
|
-
function zip(arr1, arr2) {
|
|
147
|
-
const length = Math.min(arr1.length, arr2.length);
|
|
148
|
-
const result = [];
|
|
149
|
-
for (let i = 0; i < length; i++) {
|
|
150
|
-
result.push([
|
|
151
|
-
arr1[i],
|
|
152
|
-
arr2[i]
|
|
153
|
-
]);
|
|
154
|
-
}
|
|
155
|
-
return result;
|
|
156
|
-
}
|
|
157
|
-
__name(zip, "zip");
|
|
158
|
-
export {
|
|
159
|
-
clone,
|
|
160
|
-
enumerate,
|
|
161
|
-
invariant,
|
|
162
|
-
isPlainObject,
|
|
163
|
-
lowerCaseFirst,
|
|
164
|
-
paramCase,
|
|
165
|
-
safeJSONStringify,
|
|
166
|
-
singleDebounce,
|
|
167
|
-
sleep,
|
|
168
|
-
upperCaseFirst,
|
|
169
|
-
zip
|
|
170
|
-
};
|
|
171
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/is-plain-object.ts","../src/clone.ts","../src/enumerable.ts","../src/lower-case-first.ts","../src/param-case.ts","../src/safe-json-stringify.ts","../src/single-debounce.ts","../src/sleep.ts","../src/tiny-invariant.ts","../src/upper-case-first.ts","../src/zip.ts"],"sourcesContent":["function isObject(o: unknown) {\n return Object.prototype.toString.call(o) === '[object Object]';\n}\n\nexport function isPlainObject(o: unknown) {\n if (isObject(o) === false) return false;\n\n // If has modified constructor\n const ctor = (o as { constructor: unknown }).constructor;\n if (ctor === undefined) return true;\n\n // If has modified prototype\n const prot = (ctor as { prototype: unknown }).prototype;\n if (isObject(prot) === false) return false;\n\n // If constructor does not have an Object-specific method\n if (Object.prototype.hasOwnProperty.call(prot, 'isPrototypeOf') === false) {\n return false;\n }\n\n // Most likely a plain Object\n return true;\n}\n","import { isPlainObject } from './is-plain-object';\n\n/**\n * Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.\n */\nexport function clone<T>(value: T): T {\n if (Array.isArray(value)) {\n return value.map((v) => clone(v)) as T;\n }\n\n if (typeof value === 'object') {\n if (!value || !isPlainObject(value)) {\n return value;\n }\n\n const result: any = {};\n for (const key of Object.keys(value)) {\n result[key] = clone(value[key as keyof T]);\n }\n return result;\n }\n\n return value;\n}\n","/**\n * Array or scalar\n */\nexport type Enumerable<T> = T | Array<T>;\n\n/**\n * Uniformly enumerates an array or scalar.\n */\nexport function enumerate<T>(x: Enumerable<T>) {\n if (x === null || x === undefined) {\n return [];\n } else if (Array.isArray(x)) {\n return x;\n } else {\n return [x];\n }\n}\n","export function lowerCaseFirst(input: string) {\n return input.charAt(0).toLowerCase() + input.slice(1);\n}\n","const DEFAULT_SPLIT_REGEXP_1 = /([a-z0-9])([A-Z])/g;\nconst DEFAULT_SPLIT_REGEXP_2 = /([A-Z])([A-Z][a-z])/g;\nconst DEFAULT_STRIP_REGEXP = /[^A-Z0-9]+/gi;\n\nexport function paramCase(input: string) {\n const result = input\n .replace(DEFAULT_SPLIT_REGEXP_1, '$1\\0$2')\n .replace(DEFAULT_SPLIT_REGEXP_2, '$1\\0$2')\n .replace(DEFAULT_STRIP_REGEXP, '\\0');\n\n let start = 0;\n let end = result.length;\n\n while (result.charAt(start) === '\\0') start++;\n while (result.charAt(end - 1) === '\\0') end--;\n\n return result\n .slice(start, end)\n .split('\\0')\n .map((str) => str.toLowerCase())\n .join('-');\n}\n","/**\n * A safe JSON stringify that handles bigint values.\n */\nexport function safeJSONStringify(value: unknown) {\n return JSON.stringify(value, (_, v) => {\n if (typeof v === 'bigint') {\n return v.toString();\n } else {\n return v;\n }\n });\n}\n","export function singleDebounce(cb: () => void | PromiseLike<void>, debounceMc: number, reRunOnInProgressCall: boolean = false) {\n let timeout: ReturnType<typeof setTimeout> | undefined;\n let inProgress = false;\n let pendingInProgress = false;\n\n const run = async () => {\n if (inProgress) {\n if (reRunOnInProgressCall) {\n pendingInProgress = true;\n }\n\n return;\n }\n\n inProgress = true;\n pendingInProgress = false;\n\n try {\n await cb();\n } finally {\n inProgress = false;\n\n if (pendingInProgress) {\n await run();\n }\n }\n };\n\n return () => {\n clearTimeout(timeout);\n\n timeout = setTimeout(run, debounceMc);\n }\n}\n","export function sleep(timeout: number) {\n return new Promise<void>((resolve) => {\n setTimeout(() => resolve(), timeout);\n });\n}\n","const isProduction = typeof process !== 'undefined' && process.env['NODE_ENV'] === 'production';\nconst prefix = 'Invariant failed';\n\nexport function invariant(condition: unknown, message?: string): asserts condition {\n if (condition) {\n return;\n }\n\n if (isProduction) {\n throw new Error(prefix);\n }\n\n throw new Error(message ? `${prefix}: ${message}` : prefix);\n}\n","export function upperCaseFirst(input: string) {\n return input.charAt(0).toUpperCase() + input.slice(1);\n}\n","/**\n * Zips two arrays into an array of tuples.\n */\nexport function zip<T, U>(arr1: readonly T[], arr2: readonly U[]): Array<[T, U]> {\n const length = Math.min(arr1.length, arr2.length);\n const result: Array<[T, U]> = [];\n for (let i = 0; i < length; i++) {\n result.push([arr1[i]!, arr2[i]!]);\n }\n return result;\n}\n"],"mappings":";;;;AAAA,SAASA,SAASC,GAAU;AACxB,SAAOC,OAAOC,UAAUC,SAASC,KAAKJ,CAAAA,MAAO;AACjD;AAFSD;AAIF,SAASM,cAAcL,GAAU;AACpC,MAAID,SAASC,CAAAA,MAAO,MAAO,QAAO;AAGlC,QAAMM,OAAQN,EAA+B;AAC7C,MAAIM,SAASC,OAAW,QAAO;AAG/B,QAAMC,OAAQF,KAAgCJ;AAC9C,MAAIH,SAASS,IAAAA,MAAU,MAAO,QAAO;AAGrC,MAAIP,OAAOC,UAAUO,eAAeL,KAAKI,MAAM,eAAA,MAAqB,OAAO;AACvE,WAAO;EACX;AAGA,SAAO;AACX;AAlBgBH;;;ACCT,SAASK,MAASC,OAAQ;AAC7B,MAAIC,MAAMC,QAAQF,KAAAA,GAAQ;AACtB,WAAOA,MAAMG,IAAI,CAACC,MAAML,MAAMK,CAAAA,CAAAA;EAClC;AAEA,MAAI,OAAOJ,UAAU,UAAU;AAC3B,QAAI,CAACA,SAAS,CAACK,cAAcL,KAAAA,GAAQ;AACjC,aAAOA;IACX;AAEA,UAAMM,SAAc,CAAC;AACrB,eAAWC,OAAO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efined","prot","hasOwnProperty","clone","value","Array","isArray","map","v","isPlainObject","result","key","Object","keys","enumerate","x","undefined","Array","isArray","lowerCaseFirst","input","charAt","toLowerCase","slice","DEFAULT_SPLIT_REGEXP_1","DEFAULT_SPLIT_REGEXP_2","DEFAULT_STRIP_REGEXP","paramCase","input","result","replace","start","end","length","charAt","slice","split","map","str","toLowerCase","join","safeJSONStringify","value","JSON","stringify","_","v","toString","singleDebounce","cb","debounceMc","reRunOnInProgressCall","timeout","inProgress","pendingInProgress","run","clearTimeout","setTimeout","sleep","timeout","Promise","resolve","setTimeout","isProduction","process","env","prefix","invariant","condition","message","Error","upperCaseFirst","input","charAt","toUpperCase","slice","zip","arr1","arr2","length","Math","min","result","i","push"]}
|