@ntnyq/utils 0.6.1 → 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 +84 -77
- package/dist/index.d.cts +136 -119
- package/dist/index.d.ts +136 -119
- package/dist/index.js +82 -76
- package/package.json +11 -10
package/dist/index.cjs
CHANGED
|
@@ -75,6 +75,7 @@ __export(index_exports, {
|
|
|
75
75
|
isRegExp: () => isRegExp,
|
|
76
76
|
isSet: () => isSet,
|
|
77
77
|
isString: () => isString,
|
|
78
|
+
isTruthy: () => isTruthy,
|
|
78
79
|
isUndefined: () => isUndefined,
|
|
79
80
|
isWhitespaceString: () => isWhitespaceString,
|
|
80
81
|
isZero: () => isZero,
|
|
@@ -93,7 +94,7 @@ __export(index_exports, {
|
|
|
93
94
|
randomRGBColor: () => randomRGBColor,
|
|
94
95
|
randomString: () => randomString,
|
|
95
96
|
resolveSubOptions: () => resolveSubOptions,
|
|
96
|
-
|
|
97
|
+
scrollElementIntoView: () => scrollElementIntoView,
|
|
97
98
|
seconds: () => seconds,
|
|
98
99
|
slash: () => slash,
|
|
99
100
|
slugify: () => slugify,
|
|
@@ -110,6 +111,24 @@ __export(index_exports, {
|
|
|
110
111
|
});
|
|
111
112
|
module.exports = __toCommonJS(index_exports);
|
|
112
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
|
+
|
|
113
132
|
// src/is/isDeepEqual.ts
|
|
114
133
|
function isDeepEqual(value1, value2) {
|
|
115
134
|
const type1 = getObjectType(value1);
|
|
@@ -188,6 +207,9 @@ function isBigInt(value) {
|
|
|
188
207
|
function isBoolean(value) {
|
|
189
208
|
return typeof value === "boolean";
|
|
190
209
|
}
|
|
210
|
+
function isTruthy(value) {
|
|
211
|
+
return Boolean(value);
|
|
212
|
+
}
|
|
191
213
|
function isFunction(value) {
|
|
192
214
|
return typeof value === "function";
|
|
193
215
|
}
|
|
@@ -240,35 +262,12 @@ function isIterable(value) {
|
|
|
240
262
|
return isFunction(value?.[Symbol.iterator]);
|
|
241
263
|
}
|
|
242
264
|
|
|
243
|
-
// src/fn/noop.ts
|
|
244
|
-
function noop() {
|
|
245
|
-
}
|
|
246
|
-
var NOOP = noop;
|
|
247
|
-
|
|
248
|
-
// src/fn/once.ts
|
|
249
|
-
function once(func) {
|
|
250
|
-
let called = false;
|
|
251
|
-
return function(...args) {
|
|
252
|
-
if (called) {
|
|
253
|
-
return false;
|
|
254
|
-
}
|
|
255
|
-
called = true;
|
|
256
|
-
func.apply(this, args);
|
|
257
|
-
return true;
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
// src/env/isBrowser.ts
|
|
262
|
-
var isBrowser = () => typeof document !== "undefined";
|
|
263
|
-
|
|
264
265
|
// src/dom/scrollIntoView.ts
|
|
265
|
-
function
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
if (parent === document.body) {
|
|
271
|
-
parent.scrollIntoView(options);
|
|
266
|
+
function scrollElementIntoView(element, options = {}) {
|
|
267
|
+
const body = document.body;
|
|
268
|
+
const { parent = body, ...scrollIntoViewOptions } = options;
|
|
269
|
+
if (parent === body) {
|
|
270
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
272
271
|
return;
|
|
273
272
|
}
|
|
274
273
|
const parentRect = parent.getBoundingClientRect();
|
|
@@ -276,7 +275,7 @@ function scrollIntoView(element, parent, options = {
|
|
|
276
275
|
const isHorizontal = parent.scrollWidth > parent.scrollHeight;
|
|
277
276
|
const isOutOfView = isHorizontal ? elementRect.left < parentRect.left || elementRect.right > parentRect.right : elementRect.top < parentRect.top || elementRect.bottom > parentRect.bottom;
|
|
278
277
|
if (isOutOfView) {
|
|
279
|
-
parent.scrollIntoView(
|
|
278
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
280
279
|
}
|
|
281
280
|
}
|
|
282
281
|
|
|
@@ -287,6 +286,44 @@ function isElementVisibleInViewport(element, targetWindow = window) {
|
|
|
287
286
|
return (top >= 0 && top <= innerHeight || bottom >= 0 && bottom <= innerHeight) && (left >= 0 && left <= innerWidth || right >= 0 && right <= innerWidth);
|
|
288
287
|
}
|
|
289
288
|
|
|
289
|
+
// src/env/isBrowser.ts
|
|
290
|
+
var isBrowser = () => typeof document !== "undefined";
|
|
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
|
+
|
|
290
327
|
// src/misc/raf.ts
|
|
291
328
|
var root = isBrowser() ? window : globalThis;
|
|
292
329
|
var prev = Date.now();
|
|
@@ -398,41 +435,6 @@ function warnOnce(message) {
|
|
|
398
435
|
console.warn(message);
|
|
399
436
|
}
|
|
400
437
|
|
|
401
|
-
// src/html/escape.ts
|
|
402
|
-
var htmlEscapeMap = {
|
|
403
|
-
"&": "&",
|
|
404
|
-
"<": "<",
|
|
405
|
-
">": ">",
|
|
406
|
-
"'": "'",
|
|
407
|
-
'"': """
|
|
408
|
-
};
|
|
409
|
-
var htmlEscapeRegexp = /[&<>'"]/g;
|
|
410
|
-
function escapeHTML(str) {
|
|
411
|
-
return str.replace(
|
|
412
|
-
htmlEscapeRegexp,
|
|
413
|
-
(char) => htmlEscapeMap[char]
|
|
414
|
-
);
|
|
415
|
-
}
|
|
416
|
-
var htmlUnescapeMap = {
|
|
417
|
-
"&": "&",
|
|
418
|
-
"&": "&",
|
|
419
|
-
"<": "<",
|
|
420
|
-
"<": "<",
|
|
421
|
-
">": ">",
|
|
422
|
-
">": ">",
|
|
423
|
-
"'": "'",
|
|
424
|
-
"'": "'",
|
|
425
|
-
""": '"',
|
|
426
|
-
""": '"'
|
|
427
|
-
};
|
|
428
|
-
var htmlUnescapeRegexp = /&(amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g;
|
|
429
|
-
function unescapeHTML(str) {
|
|
430
|
-
return str.replace(
|
|
431
|
-
htmlUnescapeRegexp,
|
|
432
|
-
(char) => htmlUnescapeMap[char]
|
|
433
|
-
);
|
|
434
|
-
}
|
|
435
|
-
|
|
436
438
|
// src/array/at.ts
|
|
437
439
|
function at(array, index) {
|
|
438
440
|
const length = array.length;
|
|
@@ -591,6 +593,10 @@ function normalizeHexString(hex) {
|
|
|
591
593
|
return hex.length === 6 ? hex : hex.replace(/./g, "$&$&");
|
|
592
594
|
}
|
|
593
595
|
var Color = class _Color {
|
|
596
|
+
red = 0;
|
|
597
|
+
green = 0;
|
|
598
|
+
blue = 0;
|
|
599
|
+
alpha = 1;
|
|
594
600
|
constructor(red = 0, green = 0, blue = 0, alpha = 1) {
|
|
595
601
|
this.red = red;
|
|
596
602
|
this.green = green;
|
|
@@ -692,6 +698,17 @@ function enhance(module2, extra) {
|
|
|
692
698
|
});
|
|
693
699
|
}
|
|
694
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
|
+
|
|
695
712
|
// src/object/omit.ts
|
|
696
713
|
function omit(object, ...keys) {
|
|
697
714
|
keys.forEach((key) => delete object[key]);
|
|
@@ -791,17 +808,6 @@ function sortObject(obj, options = {}) {
|
|
|
791
808
|
return sortKeys(obj);
|
|
792
809
|
}
|
|
793
810
|
|
|
794
|
-
// src/module/interopDefault.ts
|
|
795
|
-
async function interopDefault(mod) {
|
|
796
|
-
const resolved = await mod;
|
|
797
|
-
return resolved.default || resolved;
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
// src/module/resolveSubOptions.ts
|
|
801
|
-
function resolveSubOptions(options, key) {
|
|
802
|
-
return typeof options[key] === "boolean" ? {} : options[key] || {};
|
|
803
|
-
}
|
|
804
|
-
|
|
805
811
|
// src/constants/regexp.ts
|
|
806
812
|
var RE_COMMENTS = /(?:<!--|\/\*)([\s\S]*?)(?:-->|\*\/)/g;
|
|
807
813
|
var RE_LINE_COMMENT = /\/\/.*/;
|
|
@@ -863,6 +869,7 @@ var RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
|
|
|
863
869
|
isRegExp,
|
|
864
870
|
isSet,
|
|
865
871
|
isString,
|
|
872
|
+
isTruthy,
|
|
866
873
|
isUndefined,
|
|
867
874
|
isWhitespaceString,
|
|
868
875
|
isZero,
|
|
@@ -881,7 +888,7 @@ var RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
|
|
|
881
888
|
randomRGBColor,
|
|
882
889
|
randomString,
|
|
883
890
|
resolveSubOptions,
|
|
884
|
-
|
|
891
|
+
scrollElementIntoView,
|
|
885
892
|
seconds,
|
|
886
893
|
slash,
|
|
887
894
|
slugify,
|
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
|
|
@@ -67,15 +84,13 @@ declare function once<T extends unknown[]>(func: (...args: T) => void): (this: u
|
|
|
67
84
|
declare const isBrowser: () => boolean;
|
|
68
85
|
|
|
69
86
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param element - element to scroll
|
|
73
|
-
* @param parent - parent element
|
|
74
|
-
* @param options - scroll options
|
|
87
|
+
* Escape html chars
|
|
75
88
|
*/
|
|
76
|
-
declare function
|
|
77
|
-
|
|
78
|
-
|
|
89
|
+
declare function escapeHTML(str: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* Unescape html chars
|
|
92
|
+
*/
|
|
93
|
+
declare function unescapeHTML(str: string): string;
|
|
79
94
|
|
|
80
95
|
/**
|
|
81
96
|
* @file raf.ts
|
|
@@ -150,16 +165,12 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
|
|
|
150
165
|
cancel: () => void;
|
|
151
166
|
};
|
|
152
167
|
|
|
153
|
-
declare function warnOnce(message: string): void;
|
|
154
|
-
|
|
155
168
|
/**
|
|
156
|
-
*
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Unescape html chars
|
|
169
|
+
* Warn message only once
|
|
170
|
+
*
|
|
171
|
+
* @param message - warning message
|
|
161
172
|
*/
|
|
162
|
-
declare function
|
|
173
|
+
declare function warnOnce(message: string): void;
|
|
163
174
|
|
|
164
175
|
/**
|
|
165
176
|
* Get array item by index, negative for backward
|
|
@@ -279,6 +290,12 @@ declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
|
279
290
|
*/
|
|
280
291
|
declare function intersect<T>(a: T[], b: T[]): T[];
|
|
281
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
|
+
*/
|
|
282
299
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
283
300
|
|
|
284
301
|
declare class Color {
|
|
@@ -338,66 +355,67 @@ declare function randomHexColor(): string;
|
|
|
338
355
|
*/
|
|
339
356
|
declare function enhance<T extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(module: T, extra: E): T;
|
|
340
357
|
|
|
341
|
-
interface CreatePadStringOptions {
|
|
342
|
-
length: number;
|
|
343
|
-
char: string;
|
|
344
|
-
}
|
|
345
|
-
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
346
|
-
|
|
347
|
-
type JoinableValue = string | number | null | undefined;
|
|
348
|
-
interface JoinOptions {
|
|
349
|
-
/**
|
|
350
|
-
* @default ''
|
|
351
|
-
*/
|
|
352
|
-
separator?: string;
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Joins an array of strings or numbers into a single string.
|
|
356
|
-
* @param array - An array of strings or numbers.
|
|
357
|
-
* @param options - An object of options.
|
|
358
|
-
* @returns A string.
|
|
359
|
-
*/
|
|
360
|
-
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Replace backslash to slash
|
|
364
|
-
*/
|
|
365
|
-
declare function slash(input: string): string;
|
|
366
|
-
|
|
367
358
|
/**
|
|
368
|
-
*
|
|
359
|
+
* Interop default export from a module
|
|
369
360
|
*
|
|
370
|
-
* @param
|
|
371
|
-
* @
|
|
372
|
-
*
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
*
|
|
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
|
+
* ```
|
|
378
371
|
*/
|
|
379
|
-
declare function
|
|
372
|
+
declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefault<T>>;
|
|
380
373
|
|
|
381
374
|
/**
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
* @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
|
|
385
379
|
*
|
|
386
380
|
* @example
|
|
387
381
|
*
|
|
388
382
|
* ```ts
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
383
|
+
* import { resolveSubOptions } from '@ntnyq/utils'
|
|
384
|
+
*
|
|
385
|
+
* interface Options {
|
|
386
|
+
* compile?: boolean | {
|
|
387
|
+
* include?: string[]
|
|
388
|
+
* exclude?: string[]
|
|
392
389
|
* }
|
|
393
|
-
*
|
|
390
|
+
* }
|
|
391
|
+
*
|
|
392
|
+
* const options: Options = {
|
|
393
|
+
* compile: true
|
|
394
|
+
* }
|
|
395
|
+
*
|
|
396
|
+
* console.log(resolveSubOptions(options, 'compile'))
|
|
397
|
+
*
|
|
398
|
+
* // => {}
|
|
394
399
|
* ```
|
|
395
400
|
*/
|
|
396
|
-
declare function
|
|
397
|
-
|
|
398
|
-
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]>>;
|
|
399
402
|
|
|
400
|
-
|
|
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;
|
|
401
419
|
|
|
402
420
|
declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
|
|
403
421
|
|
|
@@ -485,67 +503,66 @@ interface SortObjectOptions {
|
|
|
485
503
|
*/
|
|
486
504
|
declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
|
|
487
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
|
+
}
|
|
488
519
|
/**
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
* @param
|
|
492
|
-
* @returns
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
*
|
|
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
|
|
498
534
|
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
535
|
+
* @param length - string length
|
|
536
|
+
* @param chars - string chars
|
|
537
|
+
* @returns random string
|
|
501
538
|
*/
|
|
502
|
-
declare function
|
|
539
|
+
declare function randomString(length?: number, chars?: string): string;
|
|
503
540
|
|
|
504
541
|
/**
|
|
505
|
-
*
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
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
|
|
509
550
|
*
|
|
510
551
|
* @example
|
|
511
552
|
*
|
|
512
553
|
* ```ts
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
* compile?: boolean | {
|
|
517
|
-
* include?: string[]
|
|
518
|
-
* exclude?: string[]
|
|
554
|
+
* const str = unindent`
|
|
555
|
+
* if (foo) {
|
|
556
|
+
* bar()
|
|
519
557
|
* }
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
* const options: Options = {
|
|
523
|
-
* compile: true
|
|
524
|
-
* }
|
|
525
|
-
*
|
|
526
|
-
* console.log(resolveSubOptions(options, 'compile'))
|
|
527
|
-
*
|
|
528
|
-
* // => {}
|
|
558
|
+
* `
|
|
529
559
|
* ```
|
|
530
560
|
*/
|
|
531
|
-
declare function
|
|
561
|
+
declare function unindent(input: TemplateStringsArray | string): string;
|
|
532
562
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
*
|
|
537
|
-
* @default false
|
|
538
|
-
*/
|
|
539
|
-
includeMax?: boolean;
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* random an integer by given range
|
|
543
|
-
*
|
|
544
|
-
* @param min - min value
|
|
545
|
-
* @param max - max value
|
|
546
|
-
* @returns random integer in range
|
|
547
|
-
*/
|
|
548
|
-
declare function randomNumber(min: number, max?: number, options?: RamdomNumberOptions): number;
|
|
563
|
+
declare function ensurePrefix(input: string, prefix: string): string;
|
|
564
|
+
|
|
565
|
+
declare function ensureSuffix(input: string, suffix: string): string;
|
|
549
566
|
|
|
550
567
|
/**
|
|
551
568
|
* 注释正则
|
|
@@ -562,4 +579,4 @@ declare const RE_LINE_COMMENT: RegExp;
|
|
|
562
579
|
*/
|
|
563
580
|
declare const RE_BLOCK_COMMENT: RegExp;
|
|
564
581
|
|
|
565
|
-
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, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions,
|
|
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
|
|
@@ -67,15 +84,13 @@ declare function once<T extends unknown[]>(func: (...args: T) => void): (this: u
|
|
|
67
84
|
declare const isBrowser: () => boolean;
|
|
68
85
|
|
|
69
86
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param element - element to scroll
|
|
73
|
-
* @param parent - parent element
|
|
74
|
-
* @param options - scroll options
|
|
87
|
+
* Escape html chars
|
|
75
88
|
*/
|
|
76
|
-
declare function
|
|
77
|
-
|
|
78
|
-
|
|
89
|
+
declare function escapeHTML(str: string): string;
|
|
90
|
+
/**
|
|
91
|
+
* Unescape html chars
|
|
92
|
+
*/
|
|
93
|
+
declare function unescapeHTML(str: string): string;
|
|
79
94
|
|
|
80
95
|
/**
|
|
81
96
|
* @file raf.ts
|
|
@@ -150,16 +165,12 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
|
|
|
150
165
|
cancel: () => void;
|
|
151
166
|
};
|
|
152
167
|
|
|
153
|
-
declare function warnOnce(message: string): void;
|
|
154
|
-
|
|
155
168
|
/**
|
|
156
|
-
*
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Unescape html chars
|
|
169
|
+
* Warn message only once
|
|
170
|
+
*
|
|
171
|
+
* @param message - warning message
|
|
161
172
|
*/
|
|
162
|
-
declare function
|
|
173
|
+
declare function warnOnce(message: string): void;
|
|
163
174
|
|
|
164
175
|
/**
|
|
165
176
|
* Get array item by index, negative for backward
|
|
@@ -279,6 +290,12 @@ declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
|
279
290
|
*/
|
|
280
291
|
declare function intersect<T>(a: T[], b: T[]): T[];
|
|
281
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
|
+
*/
|
|
282
299
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
283
300
|
|
|
284
301
|
declare class Color {
|
|
@@ -338,66 +355,67 @@ declare function randomHexColor(): string;
|
|
|
338
355
|
*/
|
|
339
356
|
declare function enhance<T extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(module: T, extra: E): T;
|
|
340
357
|
|
|
341
|
-
interface CreatePadStringOptions {
|
|
342
|
-
length: number;
|
|
343
|
-
char: string;
|
|
344
|
-
}
|
|
345
|
-
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
346
|
-
|
|
347
|
-
type JoinableValue = string | number | null | undefined;
|
|
348
|
-
interface JoinOptions {
|
|
349
|
-
/**
|
|
350
|
-
* @default ''
|
|
351
|
-
*/
|
|
352
|
-
separator?: string;
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Joins an array of strings or numbers into a single string.
|
|
356
|
-
* @param array - An array of strings or numbers.
|
|
357
|
-
* @param options - An object of options.
|
|
358
|
-
* @returns A string.
|
|
359
|
-
*/
|
|
360
|
-
declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Replace backslash to slash
|
|
364
|
-
*/
|
|
365
|
-
declare function slash(input: string): string;
|
|
366
|
-
|
|
367
358
|
/**
|
|
368
|
-
*
|
|
359
|
+
* Interop default export from a module
|
|
369
360
|
*
|
|
370
|
-
* @param
|
|
371
|
-
* @
|
|
372
|
-
*
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
*
|
|
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
|
+
* ```
|
|
378
371
|
*/
|
|
379
|
-
declare function
|
|
372
|
+
declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefault<T>>;
|
|
380
373
|
|
|
381
374
|
/**
|
|
382
|
-
*
|
|
383
|
-
*
|
|
384
|
-
* @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
|
|
385
379
|
*
|
|
386
380
|
* @example
|
|
387
381
|
*
|
|
388
382
|
* ```ts
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
383
|
+
* import { resolveSubOptions } from '@ntnyq/utils'
|
|
384
|
+
*
|
|
385
|
+
* interface Options {
|
|
386
|
+
* compile?: boolean | {
|
|
387
|
+
* include?: string[]
|
|
388
|
+
* exclude?: string[]
|
|
392
389
|
* }
|
|
393
|
-
*
|
|
390
|
+
* }
|
|
391
|
+
*
|
|
392
|
+
* const options: Options = {
|
|
393
|
+
* compile: true
|
|
394
|
+
* }
|
|
395
|
+
*
|
|
396
|
+
* console.log(resolveSubOptions(options, 'compile'))
|
|
397
|
+
*
|
|
398
|
+
* // => {}
|
|
394
399
|
* ```
|
|
395
400
|
*/
|
|
396
|
-
declare function
|
|
397
|
-
|
|
398
|
-
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]>>;
|
|
399
402
|
|
|
400
|
-
|
|
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;
|
|
401
419
|
|
|
402
420
|
declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
|
|
403
421
|
|
|
@@ -485,67 +503,66 @@ interface SortObjectOptions {
|
|
|
485
503
|
*/
|
|
486
504
|
declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
|
|
487
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
|
+
}
|
|
488
519
|
/**
|
|
489
|
-
*
|
|
490
|
-
*
|
|
491
|
-
* @param
|
|
492
|
-
* @returns
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
*
|
|
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
|
|
498
534
|
*
|
|
499
|
-
*
|
|
500
|
-
*
|
|
535
|
+
* @param length - string length
|
|
536
|
+
* @param chars - string chars
|
|
537
|
+
* @returns random string
|
|
501
538
|
*/
|
|
502
|
-
declare function
|
|
539
|
+
declare function randomString(length?: number, chars?: string): string;
|
|
503
540
|
|
|
504
541
|
/**
|
|
505
|
-
*
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
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
|
|
509
550
|
*
|
|
510
551
|
* @example
|
|
511
552
|
*
|
|
512
553
|
* ```ts
|
|
513
|
-
*
|
|
514
|
-
*
|
|
515
|
-
*
|
|
516
|
-
* compile?: boolean | {
|
|
517
|
-
* include?: string[]
|
|
518
|
-
* exclude?: string[]
|
|
554
|
+
* const str = unindent`
|
|
555
|
+
* if (foo) {
|
|
556
|
+
* bar()
|
|
519
557
|
* }
|
|
520
|
-
*
|
|
521
|
-
*
|
|
522
|
-
* const options: Options = {
|
|
523
|
-
* compile: true
|
|
524
|
-
* }
|
|
525
|
-
*
|
|
526
|
-
* console.log(resolveSubOptions(options, 'compile'))
|
|
527
|
-
*
|
|
528
|
-
* // => {}
|
|
558
|
+
* `
|
|
529
559
|
* ```
|
|
530
560
|
*/
|
|
531
|
-
declare function
|
|
561
|
+
declare function unindent(input: TemplateStringsArray | string): string;
|
|
532
562
|
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
*
|
|
537
|
-
* @default false
|
|
538
|
-
*/
|
|
539
|
-
includeMax?: boolean;
|
|
540
|
-
}
|
|
541
|
-
/**
|
|
542
|
-
* random an integer by given range
|
|
543
|
-
*
|
|
544
|
-
* @param min - min value
|
|
545
|
-
* @param max - max value
|
|
546
|
-
* @returns random integer in range
|
|
547
|
-
*/
|
|
548
|
-
declare function randomNumber(min: number, max?: number, options?: RamdomNumberOptions): number;
|
|
563
|
+
declare function ensurePrefix(input: string, prefix: string): string;
|
|
564
|
+
|
|
565
|
+
declare function ensureSuffix(input: string, suffix: string): string;
|
|
549
566
|
|
|
550
567
|
/**
|
|
551
568
|
* 注释正则
|
|
@@ -562,4 +579,4 @@ declare const RE_LINE_COMMENT: RegExp;
|
|
|
562
579
|
*/
|
|
563
580
|
declare const RE_BLOCK_COMMENT: RegExp;
|
|
564
581
|
|
|
565
|
-
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, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions,
|
|
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,35 +149,12 @@ function isIterable(value) {
|
|
|
128
149
|
return isFunction(value?.[Symbol.iterator]);
|
|
129
150
|
}
|
|
130
151
|
|
|
131
|
-
// src/fn/noop.ts
|
|
132
|
-
function noop() {
|
|
133
|
-
}
|
|
134
|
-
var NOOP = noop;
|
|
135
|
-
|
|
136
|
-
// src/fn/once.ts
|
|
137
|
-
function once(func) {
|
|
138
|
-
let called = false;
|
|
139
|
-
return function(...args) {
|
|
140
|
-
if (called) {
|
|
141
|
-
return false;
|
|
142
|
-
}
|
|
143
|
-
called = true;
|
|
144
|
-
func.apply(this, args);
|
|
145
|
-
return true;
|
|
146
|
-
};
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// src/env/isBrowser.ts
|
|
150
|
-
var isBrowser = () => typeof document !== "undefined";
|
|
151
|
-
|
|
152
152
|
// src/dom/scrollIntoView.ts
|
|
153
|
-
function
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (parent === document.body) {
|
|
159
|
-
parent.scrollIntoView(options);
|
|
153
|
+
function scrollElementIntoView(element, options = {}) {
|
|
154
|
+
const body = document.body;
|
|
155
|
+
const { parent = body, ...scrollIntoViewOptions } = options;
|
|
156
|
+
if (parent === body) {
|
|
157
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
160
158
|
return;
|
|
161
159
|
}
|
|
162
160
|
const parentRect = parent.getBoundingClientRect();
|
|
@@ -164,7 +162,7 @@ function scrollIntoView(element, parent, options = {
|
|
|
164
162
|
const isHorizontal = parent.scrollWidth > parent.scrollHeight;
|
|
165
163
|
const isOutOfView = isHorizontal ? elementRect.left < parentRect.left || elementRect.right > parentRect.right : elementRect.top < parentRect.top || elementRect.bottom > parentRect.bottom;
|
|
166
164
|
if (isOutOfView) {
|
|
167
|
-
parent.scrollIntoView(
|
|
165
|
+
parent.scrollIntoView(scrollIntoViewOptions);
|
|
168
166
|
}
|
|
169
167
|
}
|
|
170
168
|
|
|
@@ -175,6 +173,44 @@ function isElementVisibleInViewport(element, targetWindow = window) {
|
|
|
175
173
|
return (top >= 0 && top <= innerHeight || bottom >= 0 && bottom <= innerHeight) && (left >= 0 && left <= innerWidth || right >= 0 && right <= innerWidth);
|
|
176
174
|
}
|
|
177
175
|
|
|
176
|
+
// src/env/isBrowser.ts
|
|
177
|
+
var isBrowser = () => typeof document !== "undefined";
|
|
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
|
+
|
|
178
214
|
// src/misc/raf.ts
|
|
179
215
|
var root = isBrowser() ? window : globalThis;
|
|
180
216
|
var prev = Date.now();
|
|
@@ -286,41 +322,6 @@ function warnOnce(message) {
|
|
|
286
322
|
console.warn(message);
|
|
287
323
|
}
|
|
288
324
|
|
|
289
|
-
// src/html/escape.ts
|
|
290
|
-
var htmlEscapeMap = {
|
|
291
|
-
"&": "&",
|
|
292
|
-
"<": "<",
|
|
293
|
-
">": ">",
|
|
294
|
-
"'": "'",
|
|
295
|
-
'"': """
|
|
296
|
-
};
|
|
297
|
-
var htmlEscapeRegexp = /[&<>'"]/g;
|
|
298
|
-
function escapeHTML(str) {
|
|
299
|
-
return str.replace(
|
|
300
|
-
htmlEscapeRegexp,
|
|
301
|
-
(char) => htmlEscapeMap[char]
|
|
302
|
-
);
|
|
303
|
-
}
|
|
304
|
-
var htmlUnescapeMap = {
|
|
305
|
-
"&": "&",
|
|
306
|
-
"&": "&",
|
|
307
|
-
"<": "<",
|
|
308
|
-
"<": "<",
|
|
309
|
-
">": ">",
|
|
310
|
-
">": ">",
|
|
311
|
-
"'": "'",
|
|
312
|
-
"'": "'",
|
|
313
|
-
""": '"',
|
|
314
|
-
""": '"'
|
|
315
|
-
};
|
|
316
|
-
var htmlUnescapeRegexp = /&(amp|#38|lt|#60|gt|#62|apos|#39|quot|#34);/g;
|
|
317
|
-
function unescapeHTML(str) {
|
|
318
|
-
return str.replace(
|
|
319
|
-
htmlUnescapeRegexp,
|
|
320
|
-
(char) => htmlUnescapeMap[char]
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
|
|
324
325
|
// src/array/at.ts
|
|
325
326
|
function at(array, index) {
|
|
326
327
|
const length = array.length;
|
|
@@ -479,6 +480,10 @@ function normalizeHexString(hex) {
|
|
|
479
480
|
return hex.length === 6 ? hex : hex.replace(/./g, "$&$&");
|
|
480
481
|
}
|
|
481
482
|
var Color = class _Color {
|
|
483
|
+
red = 0;
|
|
484
|
+
green = 0;
|
|
485
|
+
blue = 0;
|
|
486
|
+
alpha = 1;
|
|
482
487
|
constructor(red = 0, green = 0, blue = 0, alpha = 1) {
|
|
483
488
|
this.red = red;
|
|
484
489
|
this.green = green;
|
|
@@ -580,6 +585,17 @@ function enhance(module, extra) {
|
|
|
580
585
|
});
|
|
581
586
|
}
|
|
582
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
|
+
|
|
583
599
|
// src/object/omit.ts
|
|
584
600
|
function omit(object, ...keys) {
|
|
585
601
|
keys.forEach((key) => delete object[key]);
|
|
@@ -679,17 +695,6 @@ function sortObject(obj, options = {}) {
|
|
|
679
695
|
return sortKeys(obj);
|
|
680
696
|
}
|
|
681
697
|
|
|
682
|
-
// src/module/interopDefault.ts
|
|
683
|
-
async function interopDefault(mod) {
|
|
684
|
-
const resolved = await mod;
|
|
685
|
-
return resolved.default || resolved;
|
|
686
|
-
}
|
|
687
|
-
|
|
688
|
-
// src/module/resolveSubOptions.ts
|
|
689
|
-
function resolveSubOptions(options, key) {
|
|
690
|
-
return typeof options[key] === "boolean" ? {} : options[key] || {};
|
|
691
|
-
}
|
|
692
|
-
|
|
693
698
|
// src/constants/regexp.ts
|
|
694
699
|
var RE_COMMENTS = /(?:<!--|\/\*)([\s\S]*?)(?:-->|\*\/)/g;
|
|
695
700
|
var RE_LINE_COMMENT = /\/\/.*/;
|
|
@@ -750,6 +755,7 @@ export {
|
|
|
750
755
|
isRegExp,
|
|
751
756
|
isSet,
|
|
752
757
|
isString,
|
|
758
|
+
isTruthy,
|
|
753
759
|
isUndefined,
|
|
754
760
|
isWhitespaceString,
|
|
755
761
|
isZero,
|
|
@@ -768,7 +774,7 @@ export {
|
|
|
768
774
|
randomRGBColor,
|
|
769
775
|
randomString,
|
|
770
776
|
resolveSubOptions,
|
|
771
|
-
|
|
777
|
+
scrollElementIntoView,
|
|
772
778
|
seconds,
|
|
773
779
|
slash,
|
|
774
780
|
slugify,
|
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.
|
|
48
|
+
"prettier": "^3.5.3",
|
|
49
49
|
"tsup": "^8.4.0",
|
|
50
50
|
"typescript": "^5.8.2",
|
|
51
|
-
"vitest": "^3.0.
|
|
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
|
}
|