@ntnyq/utils 0.6.4 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts DELETED
@@ -1,621 +0,0 @@
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
-
12
- /**
13
- * @file is/dom.ts
14
- */
15
- /**
16
- * Check if given value is an HTMLElement
17
- * @param value - The value to check
18
- * @returns True if the value is an HTMLElement, false otherwise
19
- */
20
- declare function isHTMLElement(value: unknown): value is HTMLElement;
21
-
22
- /**
23
- * @file is utils
24
- * @module is
25
- * @copyright {@link https://github.com/sindresorhus/is}
26
- */
27
- type Whitespace = ' ';
28
- type NonEmptyString = string & {
29
- 0: '';
30
- };
31
- declare function getObjectType(value: unknown): string;
32
- declare function isUndefined(value: unknown): value is undefined;
33
- declare function isNull(value: unknown): value is null;
34
- declare function isNil(value: unknown): value is null | undefined;
35
- declare const isNullOrUndefined: typeof isNil;
36
- declare function isString(value: unknown): value is string;
37
- declare function isEmptyString(value: unknown): value is '';
38
- declare function isNonEmptyString(value: unknown): value is NonEmptyString;
39
- declare function isWhitespaceString(value: unknown): value is Whitespace;
40
- declare function isEmptyStringOrWhitespace(value: unknown): value is '' | Whitespace;
41
- declare function isNumbericString(value: unknown): value is `${number}`;
42
- declare function isNumber(value: unknown): value is number;
43
- declare function isZero(value: unknown): value is 0;
44
- declare function isNaN(value: unknown): value is typeof Number.NaN;
45
- declare function isInteger(value: unknown): value is number;
46
- declare function isBigInt(value: unknown): value is bigint;
47
- declare function isBoolean(value: unknown): value is boolean;
48
- declare function isTruthy<T>(value: T | undefined): value is T;
49
- declare function isFunction(value: unknown): value is Function;
50
- declare function isArray(value: unknown): value is unknown[];
51
- declare function isEmptyArray(value: unknown): value is [];
52
- declare function isNonEmptyArray<T = unknown, Item = unknown>(value: T | Item[]): value is [Item, ...Item[]];
53
- declare function isObject(value: unknown): value is object;
54
- declare function isEmptyObject(value: unknown): value is {};
55
- declare function isMap<Key = unknown, Value = unknown>(value: unknown): value is Map<Key, Value>;
56
- declare function isEmptyMap(value: unknown): value is Map<never, never>;
57
- declare function isSet<Value = unknown>(value: unknown): value is Set<Value>;
58
- declare function isEmptySet(value: unknown): value is Set<never>;
59
- declare function isRegExp(value: unknown): value is RegExp;
60
- declare function isError(value: unknown): value is Error;
61
- declare function isNativePromise<T = unknown>(value: unknown): value is Promise<T>;
62
- declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
63
- declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
64
-
65
- /**
66
- * check if two values are deeply equal
67
- */
68
- declare function isDeepEqual(value1: any, value2: any): boolean;
69
-
70
- interface Options extends ScrollIntoViewOptions {
71
- /**
72
- * @default `document.body`
73
- */
74
- parent?: HTMLElement;
75
- }
76
- /**
77
- * Scroll element into view if it is out of view.
78
- *
79
- * @param element - element to scroll
80
- * @param options - scroll options
81
- */
82
- declare function scrollElementIntoView(element: HTMLElement, options?: Options): void;
83
-
84
- type JsonArray = JsonValue[] | readonly JsonValue[];
85
- type JsonObject = {
86
- [Key in string]: JsonValue;
87
- } & {
88
- [Key in string]?: JsonValue | undefined;
89
- };
90
- type JsonPrimitive = boolean | number | string | null;
91
- /**
92
- * @copyright {@link https://github.com/sindresorhus/type-fest/blob/main/source/basic.d.ts}
93
- */
94
- type JsonValue = JsonArray | JsonObject | JsonPrimitive;
95
-
96
- /**
97
- * A literal type that supports custom further strings but preserves autocompletion in IDEs.
98
- *
99
- * @see https://github.com/microsoft/TypeScript/issues/29729#issuecomment-471566609
100
- */
101
- type LiteralUnion<Union extends Base, Base = string> = Union | (Base & {
102
- zz_IGNORE_ME?: never;
103
- });
104
- /**
105
- * Non empty object `{}`
106
- */
107
- type NonEmptyObject<T> = T extends Record<string, never> ? never : T;
108
-
109
- /**
110
- * interop module
111
- */
112
- type InteropModuleDefault<T> = T extends {
113
- default: infer U;
114
- } ? U : T;
115
-
116
- type AnyFn<T = any, R = any> = (...args: T[]) => R;
117
- type Arrayable<T> = T | T[];
118
- type Awaitable<T> = Promise<T> | T;
119
- type Callable<T> = AnyFn<any, T> | T;
120
- type MayBe<T> = T | undefined;
121
- type Nullable<T> = T | null;
122
- /**
123
- * Overwrite some keys type
124
- */
125
- type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
126
- /**
127
- * Prettify object type
128
- */
129
- type Prettify<T> = {
130
- [K in keyof T]: T[K];
131
- } & {};
132
- type PrettifyV2<T> = Omit<T, never>;
133
- type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
134
- /**
135
- * Resolve `boolean | Record<string, any>` to `Record<string, any>`
136
- */
137
- type ResolvedOptions<T> = T extends boolean ? never : NonNullable<T>;
138
-
139
- interface OpenExternalURLOptions {
140
- /**
141
- * open target
142
- *
143
- * @default `_blank`
144
- */
145
- target?: LiteralUnion<'_self' | '_top' | '_blank' | '_parent'>;
146
- }
147
- /**
148
- * Open external url
149
- * @param url - URL to open
150
- * @param options - open options
151
- * @returns window proxy
152
- */
153
- declare function openExternalURL(url: string | URL, options?: OpenExternalURLOptions): WindowProxy | null;
154
-
155
- /**
156
- * Check if element is in viewport
157
- * @param element - checked element
158
- * @param targetWindow - window
159
- * @returns true if element is in viewport, false otherwise
160
- */
161
- declare function isElementVisibleInViewport(element: HTMLElement, targetWindow?: Window): boolean;
162
-
163
- /**
164
- * @file env.ts
165
- */
166
- /**
167
- * Checks if the code is running in a browser
168
- *
169
- * @returns boolean - true if the code is running in a browser
170
- */
171
- declare const isBrowser: () => boolean;
172
-
173
- /**
174
- * Escape html chars
175
- */
176
- declare function escapeHTML(str: string): string;
177
- /**
178
- * Unescape html chars
179
- */
180
- declare function unescapeHTML(str: string): string;
181
-
182
- /**
183
- * @file raf.ts
184
- */
185
- /**
186
- * Request animation frame
187
- *
188
- * @param fn - callback
189
- * @returns id
190
- */
191
- declare function rAF(fn: FrameRequestCallback): number;
192
- /**
193
- * Cancel animation frame
194
- *
195
- * @param id - id
196
- * @returns void
197
- */
198
- declare function cAF(id: number): void;
199
-
200
- /**
201
- * @file time utils
202
- * @module Time
203
- */
204
- declare function seconds(count: number): number;
205
- declare function minutes(count: number): number;
206
- declare function hours(count: number): number;
207
- declare function days(count: number): number;
208
- declare function weeks(count: number): number;
209
-
210
- /**
211
- * Clamps a number between a minimum and maximum value
212
- * @param value - the value to clamp within the given range
213
- * @param min - the minimum value to clamp
214
- * @param max - the maximum value to clamp
215
- * @returns the new value
216
- */
217
- declare function clamp(value: number, min?: number, max?: number): number;
218
-
219
- /**
220
- * Wait for a number of milliseconds
221
- *
222
- * @param ms - millseconds to wait
223
- * @returns a promise that resolves after ms milliseconds
224
- *
225
- * @example
226
- * ```
227
- * import { waitFor } from '@ntnyq/utils'
228
- * await waitFor(3e3)
229
- * // do somthing after 3 seconds
230
- * ```
231
- */
232
- declare function waitFor(ms: number): Promise<void>;
233
-
234
- interface ThrottleDebounceOptions {
235
- /**
236
- * @default false
237
- */
238
- isDebounce?: boolean;
239
- }
240
- /**
241
- * Throttle a function to limit its execution to a maximum of once per a specified time interval.
242
- *
243
- * @param delay - Zero or greater delay in milliseconds
244
- * @param callback - A function to be throttled
245
- * @param options - throttle options
246
- * @returns A throttled function
247
- */
248
- declare function throttle<T extends ((...args: any[]) => undefined | void) | undefined | null>(delay: number, callback: Exclude<T, undefined | null>, options?: ThrottleDebounceOptions): T & {
249
- cancel: () => void;
250
- };
251
- declare function debounce<T extends ((...args: any[]) => undefined | void) | undefined | null>(delay: number, callback: Exclude<T, undefined | null>, options?: ThrottleDebounceOptions): T & {
252
- cancel: () => void;
253
- };
254
-
255
- /**
256
- * Warn message only once
257
- *
258
- * @param message - warning message
259
- */
260
- declare function warnOnce(message: string): void;
261
-
262
- /**
263
- * Get array item by index, negative for backward
264
- * @param array - given array
265
- * @param index - index of item
266
- * @returns undefined if not match, otherwise matched item
267
- */
268
- declare function at<T>(array: readonly T[], index: number): T | undefined;
269
- /**
270
- * Get the last item of given array
271
- * @param array - given array
272
- * @returns undefined if empty array, otherwise last item
273
- */
274
- declare function last<T>(array: readonly T[]): T | undefined;
275
-
276
- /**
277
- * Splits an array into smaller chunks of a given size.
278
- * @param array - The array to split
279
- * @param size - The size of each chunk
280
- * @returns An array of arrays, where each sub-array has `size` elements from the original array.
281
- */
282
- declare function chunk<T>(array: T[], size: number): T[][];
283
-
284
- /**
285
- * Returns a new array with unique values.
286
- * @param array - The array to process.
287
- * @returns The new array.
288
- */
289
- declare function unique<T>(array: T[]): T[];
290
- /**
291
- * Returns a new array with unique values.
292
- * @param array - The array to process.
293
- * @param equalFn - The function to compare values.
294
- * @returns The new array.
295
- */
296
- declare function uniqueBy<T>(array: T[], equalFn: (a: T, b: T) => boolean): T[];
297
-
298
- /**
299
- * Converts a value to an array.
300
- * @param array - The value to convert.
301
- * @returns The array.
302
- */
303
- declare function toArray<T>(array?: Nullable<Arrayable<T>>): T[];
304
-
305
- /**
306
- * Convert `Arrayable<T>` to `Array<T>` and flatten the result
307
- * @param array - given array
308
- * @returns Array<T>
309
- */
310
- declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
311
- /**
312
- * Use rest arguments to merge arrays
313
- * @param args - rest arguments
314
- * @returns Array<T>
315
- */
316
- declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
317
-
318
- /**
319
- * Get intersect items
320
- *
321
- * @returns intersect items
322
- */
323
- declare function intersect<T>(a: T[], b: T[]): T[];
324
-
325
- /**
326
- * Check if values of two arrays are equal
327
- * @param array1 - array 1
328
- * @param array2 - array 2
329
- * @returns `true` if equal
330
- */
331
- declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
332
-
333
- declare class Color {
334
- red: number;
335
- green: number;
336
- blue: number;
337
- alpha: number;
338
- constructor(red?: number, green?: number, blue?: number, alpha?: number);
339
- static fromRGB(red: number, green: number, blue: number): Color;
340
- static fromRGBA(red: number, green: number, blue: number, alpha: number): Color;
341
- static fromHex(hex: string): Color;
342
- get brightness(): number;
343
- get isDark(): boolean;
344
- get isLight(): boolean;
345
- toHexString(isUpperCase?: boolean): string;
346
- toRGBAString(): string;
347
- /**
348
- * add alpha value to {@link Color}
349
- *
350
- * @param alpha - alpha value
351
- * @returns instance of {@link Color}
352
- */
353
- withAlpha(alpha?: number): Color;
354
- /**
355
- * lighten the color by percentage
356
- *
357
- * @param percentage - percentage to lighten
358
- */
359
- lighten(percentage?: number): Color;
360
- /**
361
- * darken the color by percentage
362
- *
363
- * @param percentage - percentage to darken
364
- */
365
- darken(percentage?: number): Color;
366
- }
367
-
368
- /**
369
- * get a random RGB color
370
- * @returns a random RGB color
371
- */
372
- declare function randomRGBColor(): string;
373
- /**
374
- * get a random RGBA color
375
- * @returns a random RGBA color
376
- */
377
- declare function randomRGBAColor(): string;
378
- /**
379
- * get a random hex color
380
- * @returns a random hex color
381
- */
382
- declare function randomHexColor(): string;
383
-
384
- /**
385
- * enhance object
386
- * @module proxy
387
- */
388
- declare function enhance<T extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(module: T, extra: E): T;
389
-
390
- /**
391
- * Interop default export from a module
392
- *
393
- * @param mod - The module
394
- * @returns The default export
395
- *
396
- * @example
397
- *
398
- * ```ts
399
- * import { interopDefault } from '@ntnyq/utils'
400
- *
401
- * const { unindent } = await interopDefault(import('@ntnyq/utils'))
402
- * ```
403
- */
404
- declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefault<T>>;
405
-
406
- /**
407
- * Resolve sub options `boolean | Options` to `Options`
408
- * @param options - core options
409
- * @param key - sub options key
410
- * @returns resolved sub options
411
- *
412
- * @example
413
- *
414
- * ```ts
415
- * import { resolveSubOptions } from '@ntnyq/utils'
416
- *
417
- * interface Options {
418
- * compile?: boolean | {
419
- * include?: string[]
420
- * exclude?: string[]
421
- * }
422
- * }
423
- *
424
- * const options: Options = {
425
- * compile: true
426
- * }
427
- *
428
- * console.log(resolveSubOptions(options, 'compile'))
429
- *
430
- * // => {}
431
- * ```
432
- */
433
- declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
434
-
435
- interface RamdomNumberOptions {
436
- /**
437
- * include max value
438
- *
439
- * @default false
440
- */
441
- includeMax?: boolean;
442
- }
443
- /**
444
- * random an integer by given range
445
- *
446
- * @param min - min value
447
- * @param max - max value
448
- * @returns random integer in range
449
- */
450
- declare function randomNumber(min: number, max?: number, options?: RamdomNumberOptions): number;
451
-
452
- declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
453
-
454
- declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
455
-
456
- interface CleanObjectOptions {
457
- /**
458
- * clean undefined
459
- *
460
- * @default true
461
- */
462
- cleanUndefined?: boolean;
463
- /**
464
- * clean null
465
- *
466
- * @default true
467
- */
468
- cleanNull?: boolean;
469
- /**
470
- * clean zero
471
- *
472
- * @default false
473
- */
474
- cleanZero?: boolean;
475
- /**
476
- * clean NaN
477
- *
478
- * @default true
479
- */
480
- cleanNaN?: boolean;
481
- /**
482
- * clean empty string
483
- *
484
- * @default false
485
- */
486
- cleanEmptyString?: boolean;
487
- /**
488
- * clean empty array
489
- *
490
- * @default false
491
- */
492
- cleanEmptyArray?: boolean;
493
- /**
494
- * clean empty object
495
- *
496
- * @default false
497
- */
498
- cleanEmptyObject?: boolean;
499
- /**
500
- * recursive clean object
501
- *
502
- * @default true
503
- */
504
- recursive?: boolean;
505
- }
506
- /**
507
- * clean undefined, null, zero, empty string, empty array, empty object from object
508
- * @param obj - object to be cleaned
509
- * @param options - clean options
510
- * @returns cleaned object
511
- */
512
- declare function cleanObject<T extends object>(obj: T, options?: CleanObjectOptions): T;
513
-
514
- /**
515
- * check object has a property with given key
516
- * @param object - the object to check
517
- * @param key - the key to check
518
- * @returns true if object has a property with given key, false otherwise
519
- */
520
- declare function hasOwn<T>(object: T, key: PropertyKey): boolean;
521
-
522
- interface SortObjectOptions {
523
- /**
524
- * Recursive sorting
525
- * @default false
526
- */
527
- deep?: boolean;
528
- /**
529
- * Compare function
530
- */
531
- compareFn?: (left: string, right: string) => number;
532
- }
533
- /**
534
- * Sort object properties
535
- */
536
- declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
537
-
538
- interface CreatePadStringOptions {
539
- length: number;
540
- char: string;
541
- }
542
- declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
543
-
544
- type JoinableValue = string | number | null | undefined;
545
- interface JoinOptions {
546
- /**
547
- * @default ''
548
- */
549
- separator?: string;
550
- }
551
- /**
552
- * Joins an array of strings or numbers into a single string.
553
- * @param array - An array of strings or numbers.
554
- * @param options - An object of options.
555
- * @returns A string.
556
- */
557
- declare function join(array: JoinableValue[], options?: JoinOptions): string;
558
-
559
- /**
560
- * Replace backslash to slash
561
- */
562
- declare function slash(input: string): string;
563
-
564
- /**
565
- * randome a string useing given chars
566
- *
567
- * @param length - string length
568
- * @param chars - string chars
569
- * @returns random string
570
- */
571
- declare function randomString(length?: number, chars?: string): string;
572
-
573
- /**
574
- * Default slugify function
575
- */
576
- declare function slugify(str: string): string;
577
-
578
- /**
579
- * Remove common leading whitespace from a template string
580
- * Empty lines at the beginning and end of the template string are also removed.
581
- * @param input - template string
582
- *
583
- * @example
584
- *
585
- * ```ts
586
- * const str = unindent`
587
- * if (foo) {
588
- * bar()
589
- * }
590
- * `
591
- * ```
592
- */
593
- declare function unindent(input: TemplateStringsArray | string): string;
594
-
595
- declare function ensurePrefix(input: string, prefix: string): string;
596
-
597
- declare function ensureSuffix(input: string, suffix: string): string;
598
-
599
- /**
600
- * Special chars
601
- */
602
- declare const SPECIAL_CHAR: {
603
- newline: string;
604
- };
605
-
606
- /**
607
- * 注释正则
608
- *
609
- * 匹配 \<!-- 或 /* 开头的注释,直到 --> 或 *\/ 结尾
610
- */
611
- declare const RE_COMMENTS: RegExp;
612
- /**
613
- * JavaScript line comment
614
- */
615
- declare const RE_LINE_COMMENT: RegExp;
616
- /**
617
- * JavaScript block comment
618
- */
619
- declare const RE_BLOCK_COMMENT: RegExp;
620
-
621
- 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 OpenExternalURLOptions, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, type RamdomNumberOptions, type ResolvedOptions, SPECIAL_CHAR, 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, isHTMLElement, 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, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, resolveSubOptions, scrollElementIntoView, seconds, slash, slugify, sortObject, throttle, toArray, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };