@ntnyq/utils 0.6.0 → 0.6.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/index.cjs +122 -32
- package/dist/index.d.cts +152 -97
- package/dist/index.d.ts +152 -97
- package/dist/index.js +113 -31
- package/package.json +13 -12
package/dist/index.cjs
CHANGED
|
@@ -22,6 +22,9 @@ var index_exports = {};
|
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
Color: () => Color,
|
|
24
24
|
NOOP: () => NOOP,
|
|
25
|
+
RE_BLOCK_COMMENT: () => RE_BLOCK_COMMENT,
|
|
26
|
+
RE_COMMENTS: () => RE_COMMENTS,
|
|
27
|
+
RE_LINE_COMMENT: () => RE_LINE_COMMENT,
|
|
25
28
|
at: () => at,
|
|
26
29
|
cAF: () => cAF,
|
|
27
30
|
chunk: () => chunk,
|
|
@@ -33,7 +36,7 @@ __export(index_exports, {
|
|
|
33
36
|
enhance: () => enhance,
|
|
34
37
|
ensurePrefix: () => ensurePrefix,
|
|
35
38
|
ensureSuffix: () => ensureSuffix,
|
|
36
|
-
|
|
39
|
+
escapeHTML: () => escapeHTML,
|
|
37
40
|
flattenArrayable: () => flattenArrayable,
|
|
38
41
|
getObjectType: () => getObjectType,
|
|
39
42
|
hasOwn: () => hasOwn,
|
|
@@ -46,6 +49,7 @@ __export(index_exports, {
|
|
|
46
49
|
isBoolean: () => isBoolean,
|
|
47
50
|
isBrowser: () => isBrowser,
|
|
48
51
|
isDeepEqual: () => isDeepEqual,
|
|
52
|
+
isElementVisibleInViewport: () => isElementVisibleInViewport,
|
|
49
53
|
isEmptyArray: () => isEmptyArray,
|
|
50
54
|
isEmptyMap: () => isEmptyMap,
|
|
51
55
|
isEmptyObject: () => isEmptyObject,
|
|
@@ -71,6 +75,7 @@ __export(index_exports, {
|
|
|
71
75
|
isRegExp: () => isRegExp,
|
|
72
76
|
isSet: () => isSet,
|
|
73
77
|
isString: () => isString,
|
|
78
|
+
isTruthy: () => isTruthy,
|
|
74
79
|
isUndefined: () => isUndefined,
|
|
75
80
|
isWhitespaceString: () => isWhitespaceString,
|
|
76
81
|
isZero: () => isZero,
|
|
@@ -89,11 +94,14 @@ __export(index_exports, {
|
|
|
89
94
|
randomRGBColor: () => randomRGBColor,
|
|
90
95
|
randomString: () => randomString,
|
|
91
96
|
resolveSubOptions: () => resolveSubOptions,
|
|
97
|
+
scrollElementIntoView: () => scrollElementIntoView,
|
|
92
98
|
seconds: () => seconds,
|
|
93
99
|
slash: () => slash,
|
|
100
|
+
slugify: () => slugify,
|
|
94
101
|
sortObject: () => sortObject,
|
|
95
102
|
throttle: () => throttle,
|
|
96
103
|
toArray: () => toArray,
|
|
104
|
+
unescapeHTML: () => unescapeHTML,
|
|
97
105
|
unindent: () => unindent,
|
|
98
106
|
unique: () => unique,
|
|
99
107
|
uniqueBy: () => uniqueBy,
|
|
@@ -103,6 +111,24 @@ __export(index_exports, {
|
|
|
103
111
|
});
|
|
104
112
|
module.exports = __toCommonJS(index_exports);
|
|
105
113
|
|
|
114
|
+
// src/fn/noop.ts
|
|
115
|
+
function noop() {
|
|
116
|
+
}
|
|
117
|
+
var NOOP = noop;
|
|
118
|
+
|
|
119
|
+
// src/fn/once.ts
|
|
120
|
+
function once(func) {
|
|
121
|
+
let called = false;
|
|
122
|
+
return function(...args) {
|
|
123
|
+
if (called) {
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
called = true;
|
|
127
|
+
func.apply(this, args);
|
|
128
|
+
return true;
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
106
132
|
// src/is/isDeepEqual.ts
|
|
107
133
|
function isDeepEqual(value1, value2) {
|
|
108
134
|
const type1 = getObjectType(value1);
|
|
@@ -181,6 +207,9 @@ function isBigInt(value) {
|
|
|
181
207
|
function isBoolean(value) {
|
|
182
208
|
return typeof value === "boolean";
|
|
183
209
|
}
|
|
210
|
+
function isTruthy(value) {
|
|
211
|
+
return Boolean(value);
|
|
212
|
+
}
|
|
184
213
|
function isFunction(value) {
|
|
185
214
|
return typeof value === "function";
|
|
186
215
|
}
|
|
@@ -233,27 +262,68 @@ function isIterable(value) {
|
|
|
233
262
|
return isFunction(value?.[Symbol.iterator]);
|
|
234
263
|
}
|
|
235
264
|
|
|
236
|
-
// src/
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
265
|
+
// src/dom/scrollIntoView.ts
|
|
266
|
+
function scrollElementIntoView(element, options = {}) {
|
|
267
|
+
const body = document.body;
|
|
268
|
+
const { parent = body, ...scrollIntoViewOptions } = options;
|
|
269
|
+
if (parent === body) {
|
|
270
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const parentRect = parent.getBoundingClientRect();
|
|
274
|
+
const elementRect = element.getBoundingClientRect();
|
|
275
|
+
const isHorizontal = parent.scrollWidth > parent.scrollHeight;
|
|
276
|
+
const isOutOfView = isHorizontal ? elementRect.left < parentRect.left || elementRect.right > parentRect.right : elementRect.top < parentRect.top || elementRect.bottom > parentRect.bottom;
|
|
277
|
+
if (isOutOfView) {
|
|
278
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
240
281
|
|
|
241
|
-
// src/
|
|
242
|
-
function
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
return false;
|
|
247
|
-
}
|
|
248
|
-
called = true;
|
|
249
|
-
func.apply(this, args);
|
|
250
|
-
return true;
|
|
251
|
-
};
|
|
282
|
+
// src/dom/isVisibleInViewport.ts
|
|
283
|
+
function isElementVisibleInViewport(element, targetWindow = window) {
|
|
284
|
+
const { top, left, bottom, right } = element.getBoundingClientRect();
|
|
285
|
+
const { innerWidth, innerHeight } = targetWindow;
|
|
286
|
+
return (top >= 0 && top <= innerHeight || bottom >= 0 && bottom <= innerHeight) && (left >= 0 && left <= innerWidth || right >= 0 && right <= innerWidth);
|
|
252
287
|
}
|
|
253
288
|
|
|
254
289
|
// src/env/isBrowser.ts
|
|
255
290
|
var isBrowser = () => typeof document !== "undefined";
|
|
256
291
|
|
|
292
|
+
// src/html/escape.ts
|
|
293
|
+
var htmlEscapeMap = {
|
|
294
|
+
"&": "&",
|
|
295
|
+
"<": "<",
|
|
296
|
+
">": ">",
|
|
297
|
+
"'": "'",
|
|
298
|
+
'"': """
|
|
299
|
+
};
|
|
300
|
+
var htmlEscapeRegexp = /[&<>'"]/g;
|
|
301
|
+
function escapeHTML(str) {
|
|
302
|
+
return str.replace(
|
|
303
|
+
htmlEscapeRegexp,
|
|
304
|
+
(char) => htmlEscapeMap[char]
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
var htmlUnescapeMap = {
|
|
308
|
+
"&": "&",
|
|
309
|
+
"&": "&",
|
|
310
|
+
"<": "<",
|
|
311
|
+
"<": "<",
|
|
312
|
+
">": ">",
|
|
313
|
+
">": ">",
|
|
314
|
+
"'": "'",
|
|
315
|
+
"'": "'",
|
|
316
|
+
""": '"',
|
|
317
|
+
""": '"'
|
|
318
|
+
};
|
|
319
|
+
var htmlUnescapeRegexp = /&(amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g;
|
|
320
|
+
function unescapeHTML(str) {
|
|
321
|
+
return str.replace(
|
|
322
|
+
htmlUnescapeRegexp,
|
|
323
|
+
(char) => htmlUnescapeMap[char]
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
257
327
|
// src/misc/raf.ts
|
|
258
328
|
var root = isBrowser() ? window : globalThis;
|
|
259
329
|
var prev = Date.now();
|
|
@@ -365,11 +435,6 @@ function warnOnce(message) {
|
|
|
365
435
|
console.warn(message);
|
|
366
436
|
}
|
|
367
437
|
|
|
368
|
-
// src/html/escapeHtml.ts
|
|
369
|
-
function escapeHtml(unsafe) {
|
|
370
|
-
return unsafe.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
371
|
-
}
|
|
372
|
-
|
|
373
438
|
// src/array/at.ts
|
|
374
439
|
function at(array, index) {
|
|
375
440
|
const length = array.length;
|
|
@@ -475,6 +540,14 @@ function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyz
|
|
|
475
540
|
return result.join("");
|
|
476
541
|
}
|
|
477
542
|
|
|
543
|
+
// src/string/slugify.ts
|
|
544
|
+
var rControl = /[\u0000-\u001F]/g;
|
|
545
|
+
var rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'“”‘’<>,.?/]+/g;
|
|
546
|
+
var rCombining = /[\u0300-\u036F]/g;
|
|
547
|
+
function slugify(str) {
|
|
548
|
+
return str.normalize("NFKD").replace(rCombining, "").replace(rControl, "").replace(rSpecial, "-").replace(/-{2,}/g, "-").replace(/^-+|-+$/g, "").replace(/^(\d)/, "_$1").toLowerCase();
|
|
549
|
+
}
|
|
550
|
+
|
|
478
551
|
// src/string/unindent.ts
|
|
479
552
|
var _RE_FULL_WS = /^\s*$/;
|
|
480
553
|
function unindent(input) {
|
|
@@ -520,6 +593,10 @@ function normalizeHexString(hex) {
|
|
|
520
593
|
return hex.length === 6 ? hex : hex.replace(/./g, "$&$&");
|
|
521
594
|
}
|
|
522
595
|
var Color = class _Color {
|
|
596
|
+
red = 0;
|
|
597
|
+
green = 0;
|
|
598
|
+
blue = 0;
|
|
599
|
+
alpha = 1;
|
|
523
600
|
constructor(red = 0, green = 0, blue = 0, alpha = 1) {
|
|
524
601
|
this.red = red;
|
|
525
602
|
this.green = green;
|
|
@@ -621,6 +698,17 @@ function enhance(module2, extra) {
|
|
|
621
698
|
});
|
|
622
699
|
}
|
|
623
700
|
|
|
701
|
+
// src/module/interopDefault.ts
|
|
702
|
+
async function interopDefault(mod) {
|
|
703
|
+
const resolved = await mod;
|
|
704
|
+
return resolved.default || resolved;
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
// src/module/resolveSubOptions.ts
|
|
708
|
+
function resolveSubOptions(options, key) {
|
|
709
|
+
return typeof options[key] === "boolean" ? {} : options[key] || {};
|
|
710
|
+
}
|
|
711
|
+
|
|
624
712
|
// src/object/omit.ts
|
|
625
713
|
function omit(object, ...keys) {
|
|
626
714
|
keys.forEach((key) => delete object[key]);
|
|
@@ -720,20 +808,17 @@ function sortObject(obj, options = {}) {
|
|
|
720
808
|
return sortKeys(obj);
|
|
721
809
|
}
|
|
722
810
|
|
|
723
|
-
// src/
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
// src/module/resolveSubOptions.ts
|
|
730
|
-
function resolveSubOptions(options, key) {
|
|
731
|
-
return typeof options[key] === "boolean" ? {} : options[key] || {};
|
|
732
|
-
}
|
|
811
|
+
// src/constants/regexp.ts
|
|
812
|
+
var RE_COMMENTS = /(?:<!--|\/\*)([\s\S]*?)(?:-->|\*\/)/g;
|
|
813
|
+
var RE_LINE_COMMENT = /\/\/.*/;
|
|
814
|
+
var RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
|
|
733
815
|
// Annotate the CommonJS export names for ESM import in node:
|
|
734
816
|
0 && (module.exports = {
|
|
735
817
|
Color,
|
|
736
818
|
NOOP,
|
|
819
|
+
RE_BLOCK_COMMENT,
|
|
820
|
+
RE_COMMENTS,
|
|
821
|
+
RE_LINE_COMMENT,
|
|
737
822
|
at,
|
|
738
823
|
cAF,
|
|
739
824
|
chunk,
|
|
@@ -745,7 +830,7 @@ function resolveSubOptions(options, key) {
|
|
|
745
830
|
enhance,
|
|
746
831
|
ensurePrefix,
|
|
747
832
|
ensureSuffix,
|
|
748
|
-
|
|
833
|
+
escapeHTML,
|
|
749
834
|
flattenArrayable,
|
|
750
835
|
getObjectType,
|
|
751
836
|
hasOwn,
|
|
@@ -758,6 +843,7 @@ function resolveSubOptions(options, key) {
|
|
|
758
843
|
isBoolean,
|
|
759
844
|
isBrowser,
|
|
760
845
|
isDeepEqual,
|
|
846
|
+
isElementVisibleInViewport,
|
|
761
847
|
isEmptyArray,
|
|
762
848
|
isEmptyMap,
|
|
763
849
|
isEmptyObject,
|
|
@@ -783,6 +869,7 @@ function resolveSubOptions(options, key) {
|
|
|
783
869
|
isRegExp,
|
|
784
870
|
isSet,
|
|
785
871
|
isString,
|
|
872
|
+
isTruthy,
|
|
786
873
|
isUndefined,
|
|
787
874
|
isWhitespaceString,
|
|
788
875
|
isZero,
|
|
@@ -801,11 +888,14 @@ function resolveSubOptions(options, key) {
|
|
|
801
888
|
randomRGBColor,
|
|
802
889
|
randomString,
|
|
803
890
|
resolveSubOptions,
|
|
891
|
+
scrollElementIntoView,
|
|
804
892
|
seconds,
|
|
805
893
|
slash,
|
|
894
|
+
slugify,
|
|
806
895
|
sortObject,
|
|
807
896
|
throttle,
|
|
808
897
|
toArray,
|
|
898
|
+
unescapeHTML,
|
|
809
899
|
unindent,
|
|
810
900
|
unique,
|
|
811
901
|
uniqueBy,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that does nothing.
|
|
3
|
+
*/
|
|
4
|
+
declare function noop(): void;
|
|
5
|
+
/**
|
|
6
|
+
* Alias of {@link noop}.
|
|
7
|
+
*/
|
|
8
|
+
declare const NOOP: typeof noop;
|
|
9
|
+
|
|
10
|
+
declare function once<T extends unknown[]>(func: (...args: T) => void): (this: unknown, ...args: T) => boolean;
|
|
11
|
+
|
|
1
12
|
/**
|
|
2
13
|
* check if two values are deeply equal
|
|
3
14
|
*/
|
|
@@ -29,6 +40,7 @@ declare function isNaN(value: unknown): value is typeof Number.NaN;
|
|
|
29
40
|
declare function isInteger(value: unknown): value is number;
|
|
30
41
|
declare function isBigInt(value: unknown): value is bigint;
|
|
31
42
|
declare function isBoolean(value: unknown): value is boolean;
|
|
43
|
+
declare function isTruthy<T>(value: T | undefined): value is T;
|
|
32
44
|
declare function isFunction(value: unknown): value is Function;
|
|
33
45
|
declare function isArray(value: unknown): value is unknown[];
|
|
34
46
|
declare function isEmptyArray(value: unknown): value is [];
|
|
@@ -45,16 +57,21 @@ declare function isNativePromise<T = unknown>(value: unknown): value is Promise<
|
|
|
45
57
|
declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
46
58
|
declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
|
|
47
59
|
|
|
60
|
+
interface Options extends ScrollIntoViewOptions {
|
|
61
|
+
/**
|
|
62
|
+
* @default `document.body`
|
|
63
|
+
*/
|
|
64
|
+
parent?: HTMLElement;
|
|
65
|
+
}
|
|
48
66
|
/**
|
|
49
|
-
*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* Alias of {@link noop}.
|
|
67
|
+
* Scroll element into view if it is out of view.
|
|
68
|
+
*
|
|
69
|
+
* @param element - element to scroll
|
|
70
|
+
* @param options - scroll options
|
|
54
71
|
*/
|
|
55
|
-
declare
|
|
72
|
+
declare function scrollElementIntoView(element: HTMLElement, options?: Options): void;
|
|
56
73
|
|
|
57
|
-
declare function
|
|
74
|
+
declare function isElementVisibleInViewport(element: HTMLElement, targetWindow?: Window): boolean;
|
|
58
75
|
|
|
59
76
|
/**
|
|
60
77
|
* @file env.ts
|
|
@@ -66,6 +83,15 @@ declare function once<T extends unknown[]>(func: (...args: T) => void): (this: u
|
|
|
66
83
|
*/
|
|
67
84
|
declare const isBrowser: () => boolean;
|
|
68
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Escape html chars
|
|
88
|
+
*/
|
|
89
|
+
declare function escapeHTML(str: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* Unescape html chars
|
|
92
|
+
*/
|
|
93
|
+
declare function unescapeHTML(str: string): string;
|
|
94
|
+
|
|
69
95
|
/**
|
|
70
96
|
* @file raf.ts
|
|
71
97
|
*/
|
|
@@ -139,10 +165,13 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
|
|
|
139
165
|
cancel: () => void;
|
|
140
166
|
};
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Warn message only once
|
|
170
|
+
*
|
|
171
|
+
* @param message - warning message
|
|
172
|
+
*/
|
|
142
173
|
declare function warnOnce(message: string): void;
|
|
143
174
|
|
|
144
|
-
declare function escapeHtml(unsafe: string): string;
|
|
145
|
-
|
|
146
175
|
/**
|
|
147
176
|
* Get array item by index, negative for backward
|
|
148
177
|
* @param array - given array
|
|
@@ -261,6 +290,12 @@ declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
|
261
290
|
*/
|
|
262
291
|
declare function intersect<T>(a: T[], b: T[]): T[];
|
|
263
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Check if values of two arrays are equal
|
|
295
|
+
* @param array1 - array 1
|
|
296
|
+
* @param array2 - array 2
|
|
297
|
+
* @returns `true` if equal
|
|
298
|
+
*/
|
|
264
299
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
265
300
|
|
|
266
301
|
declare class Color {
|
|
@@ -320,61 +355,67 @@ declare function randomHexColor(): string;
|
|
|
320
355
|
*/
|
|
321
356
|
declare function enhance<T extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(module: T, extra: E): T;
|
|
322
357
|
|
|
323
|
-
interface CreatePadStringOptions {
|
|
324
|
-
length: number;
|
|
325
|
-
char: string;
|
|
326
|
-
}
|
|
327
|
-
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
328
|
-
|
|
329
|
-
type JoinableValue = string | number | null | undefined;
|
|
330
|
-
interface JoinOptions {
|
|
331
|
-
/**
|
|
332
|
-
* @default ''
|
|
333
|
-
*/
|
|
334
|
-
separator?: string;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Joins an array of strings or numbers into a single string.
|
|
338
|
-
* @param array - An array of strings or numbers.
|
|
339
|
-
* @param options - An object of options.
|
|
340
|
-
* @returns A string.
|
|
341
|
-
*/
|
|
342
|
-
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Replace backslash to slash
|
|
346
|
-
*/
|
|
347
|
-
declare function slash(input: string): string;
|
|
348
|
-
|
|
349
358
|
/**
|
|
350
|
-
*
|
|
359
|
+
* Interop default export from a module
|
|
351
360
|
*
|
|
352
|
-
* @param
|
|
353
|
-
* @
|
|
354
|
-
*
|
|
361
|
+
* @param mod - The module
|
|
362
|
+
* @returns The default export
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
*
|
|
366
|
+
* ```ts
|
|
367
|
+
* import { interopDefault } from '@ntnyq/utils'
|
|
368
|
+
*
|
|
369
|
+
* const { unindent } = await interopDefault(import('@ntnyq/utils'))
|
|
370
|
+
* ```
|
|
355
371
|
*/
|
|
356
|
-
declare function
|
|
372
|
+
declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefault<T>>;
|
|
357
373
|
|
|
358
374
|
/**
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* @param
|
|
375
|
+
* Resolve sub options `boolean | Options` to `Options`
|
|
376
|
+
* @param options - core options
|
|
377
|
+
* @param key - sub options key
|
|
378
|
+
* @returns resolved sub options
|
|
362
379
|
*
|
|
363
380
|
* @example
|
|
364
381
|
*
|
|
365
382
|
* ```ts
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
383
|
+
* import { resolveSubOptions } from '@ntnyq/utils'
|
|
384
|
+
*
|
|
385
|
+
* interface Options {
|
|
386
|
+
* compile?: boolean | {
|
|
387
|
+
* include?: string[]
|
|
388
|
+
* exclude?: string[]
|
|
369
389
|
* }
|
|
370
|
-
*
|
|
390
|
+
* }
|
|
391
|
+
*
|
|
392
|
+
* const options: Options = {
|
|
393
|
+
* compile: true
|
|
394
|
+
* }
|
|
395
|
+
*
|
|
396
|
+
* console.log(resolveSubOptions(options, 'compile'))
|
|
397
|
+
*
|
|
398
|
+
* // => {}
|
|
371
399
|
* ```
|
|
372
400
|
*/
|
|
373
|
-
declare function
|
|
374
|
-
|
|
375
|
-
declare function ensurePrefix(input: string, prefix: string): string;
|
|
401
|
+
declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
|
|
376
402
|
|
|
377
|
-
|
|
403
|
+
interface RamdomNumberOptions {
|
|
404
|
+
/**
|
|
405
|
+
* include max value
|
|
406
|
+
*
|
|
407
|
+
* @default false
|
|
408
|
+
*/
|
|
409
|
+
includeMax?: boolean;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* random an integer by given range
|
|
413
|
+
*
|
|
414
|
+
* @param min - min value
|
|
415
|
+
* @param max - max value
|
|
416
|
+
* @returns random integer in range
|
|
417
|
+
*/
|
|
418
|
+
declare function randomNumber(min: number, max?: number, options?: RamdomNumberOptions): number;
|
|
378
419
|
|
|
379
420
|
declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
|
|
380
421
|
|
|
@@ -462,66 +503,80 @@ interface SortObjectOptions {
|
|
|
462
503
|
*/
|
|
463
504
|
declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
|
|
464
505
|
|
|
506
|
+
interface CreatePadStringOptions {
|
|
507
|
+
length: number;
|
|
508
|
+
char: string;
|
|
509
|
+
}
|
|
510
|
+
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
511
|
+
|
|
512
|
+
type JoinableValue = string | number | null | undefined;
|
|
513
|
+
interface JoinOptions {
|
|
514
|
+
/**
|
|
515
|
+
* @default ''
|
|
516
|
+
*/
|
|
517
|
+
separator?: string;
|
|
518
|
+
}
|
|
465
519
|
/**
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
* @param
|
|
469
|
-
* @returns
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
*
|
|
520
|
+
* Joins an array of strings or numbers into a single string.
|
|
521
|
+
* @param array - An array of strings or numbers.
|
|
522
|
+
* @param options - An object of options.
|
|
523
|
+
* @returns A string.
|
|
524
|
+
*/
|
|
525
|
+
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Replace backslash to slash
|
|
529
|
+
*/
|
|
530
|
+
declare function slash(input: string): string;
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* randome a string useing given chars
|
|
475
534
|
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
535
|
+
* @param length - string length
|
|
536
|
+
* @param chars - string chars
|
|
537
|
+
* @returns random string
|
|
478
538
|
*/
|
|
479
|
-
declare function
|
|
539
|
+
declare function randomString(length?: number, chars?: string): string;
|
|
480
540
|
|
|
481
541
|
/**
|
|
482
|
-
*
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
542
|
+
* Default slugify function
|
|
543
|
+
*/
|
|
544
|
+
declare function slugify(str: string): string;
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Remove common leading whitespace from a template string
|
|
548
|
+
* Empty lines at the beginning and end of the template string are also removed.
|
|
549
|
+
* @param input - template string
|
|
486
550
|
*
|
|
487
551
|
* @example
|
|
488
552
|
*
|
|
489
553
|
* ```ts
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
* compile?: boolean | {
|
|
494
|
-
* include?: string[]
|
|
495
|
-
* exclude?: string[]
|
|
554
|
+
* const str = unindent`
|
|
555
|
+
* if (foo) {
|
|
556
|
+
* bar()
|
|
496
557
|
* }
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
* const options: Options = {
|
|
500
|
-
* compile: true
|
|
501
|
-
* }
|
|
502
|
-
*
|
|
503
|
-
* console.log(resolveSubOptions(options, 'compile'))
|
|
504
|
-
*
|
|
505
|
-
* // => {}
|
|
558
|
+
* `
|
|
506
559
|
* ```
|
|
507
560
|
*/
|
|
508
|
-
declare function
|
|
561
|
+
declare function unindent(input: TemplateStringsArray | string): string;
|
|
562
|
+
|
|
563
|
+
declare function ensurePrefix(input: string, prefix: string): string;
|
|
564
|
+
|
|
565
|
+
declare function ensureSuffix(input: string, suffix: string): string;
|
|
509
566
|
|
|
510
|
-
interface RamdomNumberOptions {
|
|
511
|
-
/**
|
|
512
|
-
* include max value
|
|
513
|
-
*
|
|
514
|
-
* @default false
|
|
515
|
-
*/
|
|
516
|
-
includeMax?: boolean;
|
|
517
|
-
}
|
|
518
567
|
/**
|
|
519
|
-
*
|
|
568
|
+
* 注释正则
|
|
520
569
|
*
|
|
521
|
-
*
|
|
522
|
-
* @param max - max value
|
|
523
|
-
* @returns random integer in range
|
|
570
|
+
* 匹配 \<!-- 或 /* 开头的注释,直到 --> 或 *\/ 结尾
|
|
524
571
|
*/
|
|
525
|
-
declare
|
|
572
|
+
declare const RE_COMMENTS: RegExp;
|
|
573
|
+
/**
|
|
574
|
+
* JavaScript line comment
|
|
575
|
+
*/
|
|
576
|
+
declare const RE_LINE_COMMENT: RegExp;
|
|
577
|
+
/**
|
|
578
|
+
* JavaScript block comment
|
|
579
|
+
*/
|
|
580
|
+
declare const RE_BLOCK_COMMENT: RegExp;
|
|
526
581
|
|
|
527
|
-
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix,
|
|
582
|
+
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that does nothing.
|
|
3
|
+
*/
|
|
4
|
+
declare function noop(): void;
|
|
5
|
+
/**
|
|
6
|
+
* Alias of {@link noop}.
|
|
7
|
+
*/
|
|
8
|
+
declare const NOOP: typeof noop;
|
|
9
|
+
|
|
10
|
+
declare function once<T extends unknown[]>(func: (...args: T) => void): (this: unknown, ...args: T) => boolean;
|
|
11
|
+
|
|
1
12
|
/**
|
|
2
13
|
* check if two values are deeply equal
|
|
3
14
|
*/
|
|
@@ -29,6 +40,7 @@ declare function isNaN(value: unknown): value is typeof Number.NaN;
|
|
|
29
40
|
declare function isInteger(value: unknown): value is number;
|
|
30
41
|
declare function isBigInt(value: unknown): value is bigint;
|
|
31
42
|
declare function isBoolean(value: unknown): value is boolean;
|
|
43
|
+
declare function isTruthy<T>(value: T | undefined): value is T;
|
|
32
44
|
declare function isFunction(value: unknown): value is Function;
|
|
33
45
|
declare function isArray(value: unknown): value is unknown[];
|
|
34
46
|
declare function isEmptyArray(value: unknown): value is [];
|
|
@@ -45,16 +57,21 @@ declare function isNativePromise<T = unknown>(value: unknown): value is Promise<
|
|
|
45
57
|
declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
46
58
|
declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
|
|
47
59
|
|
|
60
|
+
interface Options extends ScrollIntoViewOptions {
|
|
61
|
+
/**
|
|
62
|
+
* @default `document.body`
|
|
63
|
+
*/
|
|
64
|
+
parent?: HTMLElement;
|
|
65
|
+
}
|
|
48
66
|
/**
|
|
49
|
-
*
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
* Alias of {@link noop}.
|
|
67
|
+
* Scroll element into view if it is out of view.
|
|
68
|
+
*
|
|
69
|
+
* @param element - element to scroll
|
|
70
|
+
* @param options - scroll options
|
|
54
71
|
*/
|
|
55
|
-
declare
|
|
72
|
+
declare function scrollElementIntoView(element: HTMLElement, options?: Options): void;
|
|
56
73
|
|
|
57
|
-
declare function
|
|
74
|
+
declare function isElementVisibleInViewport(element: HTMLElement, targetWindow?: Window): boolean;
|
|
58
75
|
|
|
59
76
|
/**
|
|
60
77
|
* @file env.ts
|
|
@@ -66,6 +83,15 @@ declare function once<T extends unknown[]>(func: (...args: T) => void): (this: u
|
|
|
66
83
|
*/
|
|
67
84
|
declare const isBrowser: () => boolean;
|
|
68
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Escape html chars
|
|
88
|
+
*/
|
|
89
|
+
declare function escapeHTML(str: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* Unescape html chars
|
|
92
|
+
*/
|
|
93
|
+
declare function unescapeHTML(str: string): string;
|
|
94
|
+
|
|
69
95
|
/**
|
|
70
96
|
* @file raf.ts
|
|
71
97
|
*/
|
|
@@ -139,10 +165,13 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
|
|
|
139
165
|
cancel: () => void;
|
|
140
166
|
};
|
|
141
167
|
|
|
168
|
+
/**
|
|
169
|
+
* Warn message only once
|
|
170
|
+
*
|
|
171
|
+
* @param message - warning message
|
|
172
|
+
*/
|
|
142
173
|
declare function warnOnce(message: string): void;
|
|
143
174
|
|
|
144
|
-
declare function escapeHtml(unsafe: string): string;
|
|
145
|
-
|
|
146
175
|
/**
|
|
147
176
|
* Get array item by index, negative for backward
|
|
148
177
|
* @param array - given array
|
|
@@ -261,6 +290,12 @@ declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
|
261
290
|
*/
|
|
262
291
|
declare function intersect<T>(a: T[], b: T[]): T[];
|
|
263
292
|
|
|
293
|
+
/**
|
|
294
|
+
* Check if values of two arrays are equal
|
|
295
|
+
* @param array1 - array 1
|
|
296
|
+
* @param array2 - array 2
|
|
297
|
+
* @returns `true` if equal
|
|
298
|
+
*/
|
|
264
299
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
265
300
|
|
|
266
301
|
declare class Color {
|
|
@@ -320,61 +355,67 @@ declare function randomHexColor(): string;
|
|
|
320
355
|
*/
|
|
321
356
|
declare function enhance<T extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(module: T, extra: E): T;
|
|
322
357
|
|
|
323
|
-
interface CreatePadStringOptions {
|
|
324
|
-
length: number;
|
|
325
|
-
char: string;
|
|
326
|
-
}
|
|
327
|
-
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
328
|
-
|
|
329
|
-
type JoinableValue = string | number | null | undefined;
|
|
330
|
-
interface JoinOptions {
|
|
331
|
-
/**
|
|
332
|
-
* @default ''
|
|
333
|
-
*/
|
|
334
|
-
separator?: string;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Joins an array of strings or numbers into a single string.
|
|
338
|
-
* @param array - An array of strings or numbers.
|
|
339
|
-
* @param options - An object of options.
|
|
340
|
-
* @returns A string.
|
|
341
|
-
*/
|
|
342
|
-
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Replace backslash to slash
|
|
346
|
-
*/
|
|
347
|
-
declare function slash(input: string): string;
|
|
348
|
-
|
|
349
358
|
/**
|
|
350
|
-
*
|
|
359
|
+
* Interop default export from a module
|
|
351
360
|
*
|
|
352
|
-
* @param
|
|
353
|
-
* @
|
|
354
|
-
*
|
|
361
|
+
* @param mod - The module
|
|
362
|
+
* @returns The default export
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
*
|
|
366
|
+
* ```ts
|
|
367
|
+
* import { interopDefault } from '@ntnyq/utils'
|
|
368
|
+
*
|
|
369
|
+
* const { unindent } = await interopDefault(import('@ntnyq/utils'))
|
|
370
|
+
* ```
|
|
355
371
|
*/
|
|
356
|
-
declare function
|
|
372
|
+
declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefault<T>>;
|
|
357
373
|
|
|
358
374
|
/**
|
|
359
|
-
*
|
|
360
|
-
*
|
|
361
|
-
* @param
|
|
375
|
+
* Resolve sub options `boolean | Options` to `Options`
|
|
376
|
+
* @param options - core options
|
|
377
|
+
* @param key - sub options key
|
|
378
|
+
* @returns resolved sub options
|
|
362
379
|
*
|
|
363
380
|
* @example
|
|
364
381
|
*
|
|
365
382
|
* ```ts
|
|
366
|
-
*
|
|
367
|
-
*
|
|
368
|
-
*
|
|
383
|
+
* import { resolveSubOptions } from '@ntnyq/utils'
|
|
384
|
+
*
|
|
385
|
+
* interface Options {
|
|
386
|
+
* compile?: boolean | {
|
|
387
|
+
* include?: string[]
|
|
388
|
+
* exclude?: string[]
|
|
369
389
|
* }
|
|
370
|
-
*
|
|
390
|
+
* }
|
|
391
|
+
*
|
|
392
|
+
* const options: Options = {
|
|
393
|
+
* compile: true
|
|
394
|
+
* }
|
|
395
|
+
*
|
|
396
|
+
* console.log(resolveSubOptions(options, 'compile'))
|
|
397
|
+
*
|
|
398
|
+
* // => {}
|
|
371
399
|
* ```
|
|
372
400
|
*/
|
|
373
|
-
declare function
|
|
374
|
-
|
|
375
|
-
declare function ensurePrefix(input: string, prefix: string): string;
|
|
401
|
+
declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
|
|
376
402
|
|
|
377
|
-
|
|
403
|
+
interface RamdomNumberOptions {
|
|
404
|
+
/**
|
|
405
|
+
* include max value
|
|
406
|
+
*
|
|
407
|
+
* @default false
|
|
408
|
+
*/
|
|
409
|
+
includeMax?: boolean;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* random an integer by given range
|
|
413
|
+
*
|
|
414
|
+
* @param min - min value
|
|
415
|
+
* @param max - max value
|
|
416
|
+
* @returns random integer in range
|
|
417
|
+
*/
|
|
418
|
+
declare function randomNumber(min: number, max?: number, options?: RamdomNumberOptions): number;
|
|
378
419
|
|
|
379
420
|
declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
|
|
380
421
|
|
|
@@ -462,66 +503,80 @@ interface SortObjectOptions {
|
|
|
462
503
|
*/
|
|
463
504
|
declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
|
|
464
505
|
|
|
506
|
+
interface CreatePadStringOptions {
|
|
507
|
+
length: number;
|
|
508
|
+
char: string;
|
|
509
|
+
}
|
|
510
|
+
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
511
|
+
|
|
512
|
+
type JoinableValue = string | number | null | undefined;
|
|
513
|
+
interface JoinOptions {
|
|
514
|
+
/**
|
|
515
|
+
* @default ''
|
|
516
|
+
*/
|
|
517
|
+
separator?: string;
|
|
518
|
+
}
|
|
465
519
|
/**
|
|
466
|
-
*
|
|
467
|
-
*
|
|
468
|
-
* @param
|
|
469
|
-
* @returns
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
*
|
|
520
|
+
* Joins an array of strings or numbers into a single string.
|
|
521
|
+
* @param array - An array of strings or numbers.
|
|
522
|
+
* @param options - An object of options.
|
|
523
|
+
* @returns A string.
|
|
524
|
+
*/
|
|
525
|
+
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Replace backslash to slash
|
|
529
|
+
*/
|
|
530
|
+
declare function slash(input: string): string;
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* randome a string useing given chars
|
|
475
534
|
*
|
|
476
|
-
*
|
|
477
|
-
*
|
|
535
|
+
* @param length - string length
|
|
536
|
+
* @param chars - string chars
|
|
537
|
+
* @returns random string
|
|
478
538
|
*/
|
|
479
|
-
declare function
|
|
539
|
+
declare function randomString(length?: number, chars?: string): string;
|
|
480
540
|
|
|
481
541
|
/**
|
|
482
|
-
*
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
542
|
+
* Default slugify function
|
|
543
|
+
*/
|
|
544
|
+
declare function slugify(str: string): string;
|
|
545
|
+
|
|
546
|
+
/**
|
|
547
|
+
* Remove common leading whitespace from a template string
|
|
548
|
+
* Empty lines at the beginning and end of the template string are also removed.
|
|
549
|
+
* @param input - template string
|
|
486
550
|
*
|
|
487
551
|
* @example
|
|
488
552
|
*
|
|
489
553
|
* ```ts
|
|
490
|
-
*
|
|
491
|
-
*
|
|
492
|
-
*
|
|
493
|
-
* compile?: boolean | {
|
|
494
|
-
* include?: string[]
|
|
495
|
-
* exclude?: string[]
|
|
554
|
+
* const str = unindent`
|
|
555
|
+
* if (foo) {
|
|
556
|
+
* bar()
|
|
496
557
|
* }
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
* const options: Options = {
|
|
500
|
-
* compile: true
|
|
501
|
-
* }
|
|
502
|
-
*
|
|
503
|
-
* console.log(resolveSubOptions(options, 'compile'))
|
|
504
|
-
*
|
|
505
|
-
* // => {}
|
|
558
|
+
* `
|
|
506
559
|
* ```
|
|
507
560
|
*/
|
|
508
|
-
declare function
|
|
561
|
+
declare function unindent(input: TemplateStringsArray | string): string;
|
|
562
|
+
|
|
563
|
+
declare function ensurePrefix(input: string, prefix: string): string;
|
|
564
|
+
|
|
565
|
+
declare function ensureSuffix(input: string, suffix: string): string;
|
|
509
566
|
|
|
510
|
-
interface RamdomNumberOptions {
|
|
511
|
-
/**
|
|
512
|
-
* include max value
|
|
513
|
-
*
|
|
514
|
-
* @default false
|
|
515
|
-
*/
|
|
516
|
-
includeMax?: boolean;
|
|
517
|
-
}
|
|
518
567
|
/**
|
|
519
|
-
*
|
|
568
|
+
* 注释正则
|
|
520
569
|
*
|
|
521
|
-
*
|
|
522
|
-
* @param max - max value
|
|
523
|
-
* @returns random integer in range
|
|
570
|
+
* 匹配 \<!-- 或 /* 开头的注释,直到 --> 或 *\/ 结尾
|
|
524
571
|
*/
|
|
525
|
-
declare
|
|
572
|
+
declare const RE_COMMENTS: RegExp;
|
|
573
|
+
/**
|
|
574
|
+
* JavaScript line comment
|
|
575
|
+
*/
|
|
576
|
+
declare const RE_LINE_COMMENT: RegExp;
|
|
577
|
+
/**
|
|
578
|
+
* JavaScript block comment
|
|
579
|
+
*/
|
|
580
|
+
declare const RE_BLOCK_COMMENT: RegExp;
|
|
526
581
|
|
|
527
|
-
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix,
|
|
582
|
+
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, Color, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type LiteralUnion, type MayBe, NOOP, type NonEmptyObject, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, type RamdomNumberOptions, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, chunk, clamp, cleanObject, createPadString, days, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
// src/fn/noop.ts
|
|
2
|
+
function noop() {
|
|
3
|
+
}
|
|
4
|
+
var NOOP = noop;
|
|
5
|
+
|
|
6
|
+
// src/fn/once.ts
|
|
7
|
+
function once(func) {
|
|
8
|
+
let called = false;
|
|
9
|
+
return function(...args) {
|
|
10
|
+
if (called) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
called = true;
|
|
14
|
+
func.apply(this, args);
|
|
15
|
+
return true;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
1
19
|
// src/is/isDeepEqual.ts
|
|
2
20
|
function isDeepEqual(value1, value2) {
|
|
3
21
|
const type1 = getObjectType(value1);
|
|
@@ -76,6 +94,9 @@ function isBigInt(value) {
|
|
|
76
94
|
function isBoolean(value) {
|
|
77
95
|
return typeof value === "boolean";
|
|
78
96
|
}
|
|
97
|
+
function isTruthy(value) {
|
|
98
|
+
return Boolean(value);
|
|
99
|
+
}
|
|
79
100
|
function isFunction(value) {
|
|
80
101
|
return typeof value === "function";
|
|
81
102
|
}
|
|
@@ -128,27 +149,68 @@ function isIterable(value) {
|
|
|
128
149
|
return isFunction(value?.[Symbol.iterator]);
|
|
129
150
|
}
|
|
130
151
|
|
|
131
|
-
// src/
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
152
|
+
// src/dom/scrollIntoView.ts
|
|
153
|
+
function scrollElementIntoView(element, options = {}) {
|
|
154
|
+
const body = document.body;
|
|
155
|
+
const { parent = body, ...scrollIntoViewOptions } = options;
|
|
156
|
+
if (parent === body) {
|
|
157
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
const parentRect = parent.getBoundingClientRect();
|
|
161
|
+
const elementRect = element.getBoundingClientRect();
|
|
162
|
+
const isHorizontal = parent.scrollWidth > parent.scrollHeight;
|
|
163
|
+
const isOutOfView = isHorizontal ? elementRect.left < parentRect.left || elementRect.right > parentRect.right : elementRect.top < parentRect.top || elementRect.bottom > parentRect.bottom;
|
|
164
|
+
if (isOutOfView) {
|
|
165
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
135
168
|
|
|
136
|
-
// src/
|
|
137
|
-
function
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
called = true;
|
|
144
|
-
func.apply(this, args);
|
|
145
|
-
return true;
|
|
146
|
-
};
|
|
169
|
+
// src/dom/isVisibleInViewport.ts
|
|
170
|
+
function isElementVisibleInViewport(element, targetWindow = window) {
|
|
171
|
+
const { top, left, bottom, right } = element.getBoundingClientRect();
|
|
172
|
+
const { innerWidth, innerHeight } = targetWindow;
|
|
173
|
+
return (top >= 0 && top <= innerHeight || bottom >= 0 && bottom <= innerHeight) && (left >= 0 && left <= innerWidth || right >= 0 && right <= innerWidth);
|
|
147
174
|
}
|
|
148
175
|
|
|
149
176
|
// src/env/isBrowser.ts
|
|
150
177
|
var isBrowser = () => typeof document !== "undefined";
|
|
151
178
|
|
|
179
|
+
// src/html/escape.ts
|
|
180
|
+
var htmlEscapeMap = {
|
|
181
|
+
"&": "&",
|
|
182
|
+
"<": "<",
|
|
183
|
+
">": ">",
|
|
184
|
+
"'": "'",
|
|
185
|
+
'"': """
|
|
186
|
+
};
|
|
187
|
+
var htmlEscapeRegexp = /[&<>'"]/g;
|
|
188
|
+
function escapeHTML(str) {
|
|
189
|
+
return str.replace(
|
|
190
|
+
htmlEscapeRegexp,
|
|
191
|
+
(char) => htmlEscapeMap[char]
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
var htmlUnescapeMap = {
|
|
195
|
+
"&": "&",
|
|
196
|
+
"&": "&",
|
|
197
|
+
"<": "<",
|
|
198
|
+
"<": "<",
|
|
199
|
+
">": ">",
|
|
200
|
+
">": ">",
|
|
201
|
+
"'": "'",
|
|
202
|
+
"'": "'",
|
|
203
|
+
""": '"',
|
|
204
|
+
""": '"'
|
|
205
|
+
};
|
|
206
|
+
var htmlUnescapeRegexp = /&(amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g;
|
|
207
|
+
function unescapeHTML(str) {
|
|
208
|
+
return str.replace(
|
|
209
|
+
htmlUnescapeRegexp,
|
|
210
|
+
(char) => htmlUnescapeMap[char]
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
|
|
152
214
|
// src/misc/raf.ts
|
|
153
215
|
var root = isBrowser() ? window : globalThis;
|
|
154
216
|
var prev = Date.now();
|
|
@@ -260,11 +322,6 @@ function warnOnce(message) {
|
|
|
260
322
|
console.warn(message);
|
|
261
323
|
}
|
|
262
324
|
|
|
263
|
-
// src/html/escapeHtml.ts
|
|
264
|
-
function escapeHtml(unsafe) {
|
|
265
|
-
return unsafe.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
266
|
-
}
|
|
267
|
-
|
|
268
325
|
// src/array/at.ts
|
|
269
326
|
function at(array, index) {
|
|
270
327
|
const length = array.length;
|
|
@@ -370,6 +427,14 @@ function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyz
|
|
|
370
427
|
return result.join("");
|
|
371
428
|
}
|
|
372
429
|
|
|
430
|
+
// src/string/slugify.ts
|
|
431
|
+
var rControl = /[\u0000-\u001F]/g;
|
|
432
|
+
var rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'“”‘’<>,.?/]+/g;
|
|
433
|
+
var rCombining = /[\u0300-\u036F]/g;
|
|
434
|
+
function slugify(str) {
|
|
435
|
+
return str.normalize("NFKD").replace(rCombining, "").replace(rControl, "").replace(rSpecial, "-").replace(/-{2,}/g, "-").replace(/^-+|-+$/g, "").replace(/^(\d)/, "_$1").toLowerCase();
|
|
436
|
+
}
|
|
437
|
+
|
|
373
438
|
// src/string/unindent.ts
|
|
374
439
|
var _RE_FULL_WS = /^\s*$/;
|
|
375
440
|
function unindent(input) {
|
|
@@ -415,6 +480,10 @@ function normalizeHexString(hex) {
|
|
|
415
480
|
return hex.length === 6 ? hex : hex.replace(/./g, "$&$&");
|
|
416
481
|
}
|
|
417
482
|
var Color = class _Color {
|
|
483
|
+
red = 0;
|
|
484
|
+
green = 0;
|
|
485
|
+
blue = 0;
|
|
486
|
+
alpha = 1;
|
|
418
487
|
constructor(red = 0, green = 0, blue = 0, alpha = 1) {
|
|
419
488
|
this.red = red;
|
|
420
489
|
this.green = green;
|
|
@@ -516,6 +585,17 @@ function enhance(module, extra) {
|
|
|
516
585
|
});
|
|
517
586
|
}
|
|
518
587
|
|
|
588
|
+
// src/module/interopDefault.ts
|
|
589
|
+
async function interopDefault(mod) {
|
|
590
|
+
const resolved = await mod;
|
|
591
|
+
return resolved.default || resolved;
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// src/module/resolveSubOptions.ts
|
|
595
|
+
function resolveSubOptions(options, key) {
|
|
596
|
+
return typeof options[key] === "boolean" ? {} : options[key] || {};
|
|
597
|
+
}
|
|
598
|
+
|
|
519
599
|
// src/object/omit.ts
|
|
520
600
|
function omit(object, ...keys) {
|
|
521
601
|
keys.forEach((key) => delete object[key]);
|
|
@@ -615,19 +695,16 @@ function sortObject(obj, options = {}) {
|
|
|
615
695
|
return sortKeys(obj);
|
|
616
696
|
}
|
|
617
697
|
|
|
618
|
-
// src/
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
// src/module/resolveSubOptions.ts
|
|
625
|
-
function resolveSubOptions(options, key) {
|
|
626
|
-
return typeof options[key] === "boolean" ? {} : options[key] || {};
|
|
627
|
-
}
|
|
698
|
+
// src/constants/regexp.ts
|
|
699
|
+
var RE_COMMENTS = /(?:<!--|\/\*)([\s\S]*?)(?:-->|\*\/)/g;
|
|
700
|
+
var RE_LINE_COMMENT = /\/\/.*/;
|
|
701
|
+
var RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
|
|
628
702
|
export {
|
|
629
703
|
Color,
|
|
630
704
|
NOOP,
|
|
705
|
+
RE_BLOCK_COMMENT,
|
|
706
|
+
RE_COMMENTS,
|
|
707
|
+
RE_LINE_COMMENT,
|
|
631
708
|
at,
|
|
632
709
|
cAF,
|
|
633
710
|
chunk,
|
|
@@ -639,7 +716,7 @@ export {
|
|
|
639
716
|
enhance,
|
|
640
717
|
ensurePrefix,
|
|
641
718
|
ensureSuffix,
|
|
642
|
-
|
|
719
|
+
escapeHTML,
|
|
643
720
|
flattenArrayable,
|
|
644
721
|
getObjectType,
|
|
645
722
|
hasOwn,
|
|
@@ -652,6 +729,7 @@ export {
|
|
|
652
729
|
isBoolean,
|
|
653
730
|
isBrowser,
|
|
654
731
|
isDeepEqual,
|
|
732
|
+
isElementVisibleInViewport,
|
|
655
733
|
isEmptyArray,
|
|
656
734
|
isEmptyMap,
|
|
657
735
|
isEmptyObject,
|
|
@@ -677,6 +755,7 @@ export {
|
|
|
677
755
|
isRegExp,
|
|
678
756
|
isSet,
|
|
679
757
|
isString,
|
|
758
|
+
isTruthy,
|
|
680
759
|
isUndefined,
|
|
681
760
|
isWhitespaceString,
|
|
682
761
|
isZero,
|
|
@@ -695,11 +774,14 @@ export {
|
|
|
695
774
|
randomRGBColor,
|
|
696
775
|
randomString,
|
|
697
776
|
resolveSubOptions,
|
|
777
|
+
scrollElementIntoView,
|
|
698
778
|
seconds,
|
|
699
779
|
slash,
|
|
780
|
+
slugify,
|
|
700
781
|
sortObject,
|
|
701
782
|
throttle,
|
|
702
783
|
toArray,
|
|
784
|
+
unescapeHTML,
|
|
703
785
|
unindent,
|
|
704
786
|
unique,
|
|
705
787
|
uniqueBy,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ntnyq/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.6.
|
|
4
|
+
"version": "0.6.2",
|
|
5
5
|
"description": "Common used utils.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"utils"
|
|
@@ -37,18 +37,18 @@
|
|
|
37
37
|
],
|
|
38
38
|
"sideEffects": false,
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@ntnyq/eslint-config": "^4.0.
|
|
40
|
+
"@ntnyq/eslint-config": "^4.0.2",
|
|
41
41
|
"@ntnyq/prettier-config": "^2.0.0",
|
|
42
|
-
"@vitest/coverage-v8": "^3.0.
|
|
43
|
-
"bumpp": "^10.0
|
|
44
|
-
"eslint": "^9.
|
|
42
|
+
"@vitest/coverage-v8": "^3.0.9",
|
|
43
|
+
"bumpp": "^10.1.0",
|
|
44
|
+
"eslint": "^9.23.0",
|
|
45
45
|
"husky": "^9.1.7",
|
|
46
46
|
"nano-staged": "^0.8.0",
|
|
47
47
|
"npm-run-all2": "^7.0.2",
|
|
48
|
-
"prettier": "^3.5.
|
|
49
|
-
"tsup": "^8.
|
|
50
|
-
"typescript": "^5.
|
|
51
|
-
"vitest": "^3.0.
|
|
48
|
+
"prettier": "^3.5.3",
|
|
49
|
+
"tsup": "^8.4.0",
|
|
50
|
+
"typescript": "^5.8.2",
|
|
51
|
+
"vitest": "^3.0.9"
|
|
52
52
|
},
|
|
53
53
|
"engines": {
|
|
54
54
|
"node": ">=18.18.0"
|
|
@@ -60,10 +60,11 @@
|
|
|
60
60
|
"build": "tsup",
|
|
61
61
|
"coverage": "vitest --coverage",
|
|
62
62
|
"dev": "tsup --watch src",
|
|
63
|
-
"lint": "eslint
|
|
64
|
-
"release": "run-s release:check release:publish",
|
|
63
|
+
"lint": "eslint",
|
|
64
|
+
"release": "run-s release:check release:version release:publish",
|
|
65
65
|
"release:check": "run-s lint typecheck test",
|
|
66
|
-
"release:publish": "
|
|
66
|
+
"release:publish": "pnpm publish",
|
|
67
|
+
"release:version": "bumpp",
|
|
67
68
|
"test": "vitest",
|
|
68
69
|
"typecheck": "tsc --noEmit"
|
|
69
70
|
}
|