@ntnyq/utils 0.4.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +90 -18
- package/dist/index.d.cts +38 -10
- package/dist/index.d.ts +38 -10
- package/dist/index.js +81 -18
- package/package.json +10 -10
package/dist/index.cjs
CHANGED
|
@@ -28,6 +28,7 @@ __export(index_exports, {
|
|
|
28
28
|
chunk: () => chunk,
|
|
29
29
|
clamp: () => clamp,
|
|
30
30
|
cleanObject: () => cleanObject,
|
|
31
|
+
createPadString: () => createPadString,
|
|
31
32
|
days: () => days,
|
|
32
33
|
debounce: () => debounce,
|
|
33
34
|
ensurePrefix: () => ensurePrefix,
|
|
@@ -39,6 +40,7 @@ __export(index_exports, {
|
|
|
39
40
|
hasOwn: () => hasOwn,
|
|
40
41
|
hours: () => hours,
|
|
41
42
|
interopDefault: () => interopDefault,
|
|
43
|
+
intersect: () => intersect,
|
|
42
44
|
isArray: () => isArray,
|
|
43
45
|
isArrayEqual: () => isArrayEqual,
|
|
44
46
|
isBigInt: () => isBigInt,
|
|
@@ -46,17 +48,23 @@ __export(index_exports, {
|
|
|
46
48
|
isBrowser: () => isBrowser,
|
|
47
49
|
isDeepEqual: () => isDeepEqual,
|
|
48
50
|
isEmptyArray: () => isEmptyArray,
|
|
51
|
+
isEmptyMap: () => isEmptyMap,
|
|
49
52
|
isEmptyObject: () => isEmptyObject,
|
|
53
|
+
isEmptySet: () => isEmptySet,
|
|
50
54
|
isEmptyString: () => isEmptyString,
|
|
51
55
|
isEmptyStringOrWhitespace: () => isEmptyStringOrWhitespace,
|
|
52
56
|
isError: () => isError,
|
|
53
57
|
isFunction: () => isFunction,
|
|
54
58
|
isInteger: () => isInteger,
|
|
59
|
+
isIterable: () => isIterable,
|
|
60
|
+
isMap: () => isMap,
|
|
55
61
|
isNaN: () => isNaN,
|
|
56
62
|
isNativePromise: () => isNativePromise,
|
|
57
63
|
isNil: () => isNil,
|
|
64
|
+
isNonEmptyArray: () => isNonEmptyArray,
|
|
58
65
|
isNonEmptyString: () => isNonEmptyString,
|
|
59
66
|
isNull: () => isNull,
|
|
67
|
+
isNullOrUndefined: () => isNullOrUndefined,
|
|
60
68
|
isNumber: () => isNumber,
|
|
61
69
|
isNumbericString: () => isNumbericString,
|
|
62
70
|
isObject: () => isObject,
|
|
@@ -80,6 +88,7 @@ __export(index_exports, {
|
|
|
80
88
|
pascalCase: () => pascalCase,
|
|
81
89
|
pick: () => pick,
|
|
82
90
|
rAF: () => rAF,
|
|
91
|
+
randomString: () => randomString,
|
|
83
92
|
resolveSubOptions: () => resolveSubOptions,
|
|
84
93
|
seconds: () => seconds,
|
|
85
94
|
slash: () => slash,
|
|
@@ -118,7 +127,9 @@ function isDeepEqual(value1, value2) {
|
|
|
118
127
|
if (keys.length !== Object.keys(value2).length) {
|
|
119
128
|
return false;
|
|
120
129
|
}
|
|
121
|
-
return keys.every(
|
|
130
|
+
return keys.every(
|
|
131
|
+
(key) => isDeepEqual(value1[key], value2[key])
|
|
132
|
+
);
|
|
122
133
|
}
|
|
123
134
|
return Object.is(value1, value2);
|
|
124
135
|
}
|
|
@@ -136,18 +147,10 @@ function isNull(value) {
|
|
|
136
147
|
function isNil(value) {
|
|
137
148
|
return isNull(value) || isUndefined(value);
|
|
138
149
|
}
|
|
150
|
+
var isNullOrUndefined = isNil;
|
|
139
151
|
function isString(value) {
|
|
140
152
|
return typeof value === "string";
|
|
141
153
|
}
|
|
142
|
-
function isNumber(value) {
|
|
143
|
-
return typeof value === "number";
|
|
144
|
-
}
|
|
145
|
-
function isZero(value) {
|
|
146
|
-
return value === 0;
|
|
147
|
-
}
|
|
148
|
-
function isNaN(value) {
|
|
149
|
-
return Number.isNaN(value);
|
|
150
|
-
}
|
|
151
154
|
function isEmptyString(value) {
|
|
152
155
|
return isString(value) && value.length === 0;
|
|
153
156
|
}
|
|
@@ -163,6 +166,15 @@ function isEmptyStringOrWhitespace(value) {
|
|
|
163
166
|
function isNumbericString(value) {
|
|
164
167
|
return isString(value) && !isEmptyStringOrWhitespace(value) && !Number.isNaN(Number(value));
|
|
165
168
|
}
|
|
169
|
+
function isNumber(value) {
|
|
170
|
+
return typeof value === "number";
|
|
171
|
+
}
|
|
172
|
+
function isZero(value) {
|
|
173
|
+
return value === 0;
|
|
174
|
+
}
|
|
175
|
+
function isNaN(value) {
|
|
176
|
+
return Number.isNaN(value);
|
|
177
|
+
}
|
|
166
178
|
function isInteger(value) {
|
|
167
179
|
return Number.isInteger(value);
|
|
168
180
|
}
|
|
@@ -181,11 +193,26 @@ function isArray(value) {
|
|
|
181
193
|
function isEmptyArray(value) {
|
|
182
194
|
return isArray(value) && value.length === 0;
|
|
183
195
|
}
|
|
196
|
+
function isNonEmptyArray(value) {
|
|
197
|
+
return isArray(value) && value.length > 0;
|
|
198
|
+
}
|
|
184
199
|
function isObject(value) {
|
|
185
|
-
return
|
|
200
|
+
return (typeof value === "object" || isFunction(value)) && !isNull(value);
|
|
186
201
|
}
|
|
187
202
|
function isEmptyObject(value) {
|
|
188
|
-
return isObject(value) && Object.keys(value).length === 0;
|
|
203
|
+
return isObject(value) && !isMap(value) && !isSet(value) && Object.keys(value).length === 0;
|
|
204
|
+
}
|
|
205
|
+
function isMap(value) {
|
|
206
|
+
return getObjectType(value) === "Map";
|
|
207
|
+
}
|
|
208
|
+
function isEmptyMap(value) {
|
|
209
|
+
return isMap(value) && value.size === 0;
|
|
210
|
+
}
|
|
211
|
+
function isSet(value) {
|
|
212
|
+
return getObjectType(value) === "Set";
|
|
213
|
+
}
|
|
214
|
+
function isEmptySet(value) {
|
|
215
|
+
return isSet(value) && value.size === 0;
|
|
189
216
|
}
|
|
190
217
|
function isRegExp(value) {
|
|
191
218
|
return getObjectType(value) === "RegExp";
|
|
@@ -193,18 +220,18 @@ function isRegExp(value) {
|
|
|
193
220
|
function isError(value) {
|
|
194
221
|
return getObjectType(value) === "Error";
|
|
195
222
|
}
|
|
196
|
-
function
|
|
197
|
-
return
|
|
223
|
+
function hasPromiseApi(value) {
|
|
224
|
+
return isFunction(value?.then) && isFunction(value?.catch);
|
|
198
225
|
}
|
|
199
226
|
function isNativePromise(value) {
|
|
200
227
|
return getObjectType(value) === "Promise";
|
|
201
228
|
}
|
|
202
|
-
function hasPromiseApi(value) {
|
|
203
|
-
return isFunction(value?.then) && isFunction(value?.catch);
|
|
204
|
-
}
|
|
205
229
|
function isPromise(value) {
|
|
206
230
|
return isNativePromise(value) || hasPromiseApi(value);
|
|
207
231
|
}
|
|
232
|
+
function isIterable(value) {
|
|
233
|
+
return isFunction(value?.[Symbol.iterator]);
|
|
234
|
+
}
|
|
208
235
|
|
|
209
236
|
// src/fn/noop.ts
|
|
210
237
|
var noop = () => {
|
|
@@ -312,7 +339,10 @@ function throttle(delay, callback, options = {}) {
|
|
|
312
339
|
if (!isDebounce && elapsed > delay) {
|
|
313
340
|
exec(now);
|
|
314
341
|
} else {
|
|
315
|
-
timeoutId = setTimeout(
|
|
342
|
+
timeoutId = setTimeout(
|
|
343
|
+
isDebounce ? clear : exec,
|
|
344
|
+
isDebounce ? delay : delay - elapsed
|
|
345
|
+
);
|
|
316
346
|
}
|
|
317
347
|
}
|
|
318
348
|
wrapper.cancel = cancel;
|
|
@@ -390,6 +420,11 @@ function mergeArrayable(...args) {
|
|
|
390
420
|
return args.flatMap((i) => toArray(i));
|
|
391
421
|
}
|
|
392
422
|
|
|
423
|
+
// src/array/intersect.ts
|
|
424
|
+
function intersect(a, b) {
|
|
425
|
+
return a.filter((item) => b.includes(item));
|
|
426
|
+
}
|
|
427
|
+
|
|
393
428
|
// src/array/isArrayEqual.ts
|
|
394
429
|
function isArrayEqual(array1, array2) {
|
|
395
430
|
if (array1.length !== array2.length) {
|
|
@@ -398,6 +433,12 @@ function isArrayEqual(array1, array2) {
|
|
|
398
433
|
return array1.every((item, idx) => item === array2[idx]);
|
|
399
434
|
}
|
|
400
435
|
|
|
436
|
+
// src/string/pad.ts
|
|
437
|
+
function createPadString(options) {
|
|
438
|
+
const { length, char } = options;
|
|
439
|
+
return (value) => (char.repeat(length) + value).slice(-length);
|
|
440
|
+
}
|
|
441
|
+
|
|
401
442
|
// src/string/join.ts
|
|
402
443
|
function join(array, options = {}) {
|
|
403
444
|
const { separator = "" } = options;
|
|
@@ -410,6 +451,28 @@ function slash(input) {
|
|
|
410
451
|
return input.replace(/\\/g, "/");
|
|
411
452
|
}
|
|
412
453
|
|
|
454
|
+
// src/number/random.ts
|
|
455
|
+
function randomNumber(min, max = 0) {
|
|
456
|
+
if (max === 0) {
|
|
457
|
+
max = min;
|
|
458
|
+
min = 0;
|
|
459
|
+
}
|
|
460
|
+
if (min > max) {
|
|
461
|
+
;
|
|
462
|
+
[min, max] = [max, min];
|
|
463
|
+
}
|
|
464
|
+
return Math.trunc(Math.random() * (max - min + 1) + min);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// src/string/random.ts
|
|
468
|
+
function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
|
|
469
|
+
const result = [];
|
|
470
|
+
for (let i = length; i > 0; --i) {
|
|
471
|
+
result.push(chars[randomNumber(chars.length)]);
|
|
472
|
+
}
|
|
473
|
+
return result.join("");
|
|
474
|
+
}
|
|
475
|
+
|
|
413
476
|
// src/string/unindent.ts
|
|
414
477
|
var _RE_FULL_WS = /^\s*$/;
|
|
415
478
|
function unindent(input) {
|
|
@@ -645,6 +708,7 @@ function resolveSubOptions(options, key) {
|
|
|
645
708
|
chunk,
|
|
646
709
|
clamp,
|
|
647
710
|
cleanObject,
|
|
711
|
+
createPadString,
|
|
648
712
|
days,
|
|
649
713
|
debounce,
|
|
650
714
|
ensurePrefix,
|
|
@@ -656,6 +720,7 @@ function resolveSubOptions(options, key) {
|
|
|
656
720
|
hasOwn,
|
|
657
721
|
hours,
|
|
658
722
|
interopDefault,
|
|
723
|
+
intersect,
|
|
659
724
|
isArray,
|
|
660
725
|
isArrayEqual,
|
|
661
726
|
isBigInt,
|
|
@@ -663,17 +728,23 @@ function resolveSubOptions(options, key) {
|
|
|
663
728
|
isBrowser,
|
|
664
729
|
isDeepEqual,
|
|
665
730
|
isEmptyArray,
|
|
731
|
+
isEmptyMap,
|
|
666
732
|
isEmptyObject,
|
|
733
|
+
isEmptySet,
|
|
667
734
|
isEmptyString,
|
|
668
735
|
isEmptyStringOrWhitespace,
|
|
669
736
|
isError,
|
|
670
737
|
isFunction,
|
|
671
738
|
isInteger,
|
|
739
|
+
isIterable,
|
|
740
|
+
isMap,
|
|
672
741
|
isNaN,
|
|
673
742
|
isNativePromise,
|
|
674
743
|
isNil,
|
|
744
|
+
isNonEmptyArray,
|
|
675
745
|
isNonEmptyString,
|
|
676
746
|
isNull,
|
|
747
|
+
isNullOrUndefined,
|
|
677
748
|
isNumber,
|
|
678
749
|
isNumbericString,
|
|
679
750
|
isObject,
|
|
@@ -697,6 +768,7 @@ function resolveSubOptions(options, key) {
|
|
|
697
768
|
pascalCase,
|
|
698
769
|
pick,
|
|
699
770
|
rAF,
|
|
771
|
+
randomString,
|
|
700
772
|
resolveSubOptions,
|
|
701
773
|
seconds,
|
|
702
774
|
slash,
|
package/dist/index.d.cts
CHANGED
|
@@ -19,28 +19,34 @@ declare function getObjectType(value: unknown): string;
|
|
|
19
19
|
declare function isUndefined(value: unknown): value is undefined;
|
|
20
20
|
declare function isNull(value: unknown): value is null;
|
|
21
21
|
declare function isNil(value: unknown): value is null | undefined;
|
|
22
|
+
declare const isNullOrUndefined: typeof isNil;
|
|
22
23
|
declare function isString(value: unknown): value is string;
|
|
23
|
-
declare function isNumber(value: unknown): value is number;
|
|
24
|
-
declare function isZero(value: unknown): value is 0;
|
|
25
|
-
declare function isNaN(value: unknown): value is typeof Number.NaN;
|
|
26
24
|
declare function isEmptyString(value: unknown): value is '';
|
|
27
25
|
declare function isNonEmptyString(value: unknown): value is NonEmptyString;
|
|
28
26
|
declare function isWhitespaceString(value: unknown): value is Whitespace;
|
|
29
27
|
declare function isEmptyStringOrWhitespace(value: unknown): value is '' | Whitespace;
|
|
30
28
|
declare function isNumbericString(value: unknown): value is `${number}`;
|
|
29
|
+
declare function isNumber(value: unknown): value is number;
|
|
30
|
+
declare function isZero(value: unknown): value is 0;
|
|
31
|
+
declare function isNaN(value: unknown): value is typeof Number.NaN;
|
|
31
32
|
declare function isInteger(value: unknown): value is number;
|
|
32
33
|
declare function isBigInt(value: unknown): value is bigint;
|
|
33
34
|
declare function isBoolean(value: unknown): value is boolean;
|
|
34
35
|
declare function isFunction(value: unknown): value is Function;
|
|
35
36
|
declare function isArray(value: unknown): value is unknown[];
|
|
36
37
|
declare function isEmptyArray(value: unknown): value is [];
|
|
38
|
+
declare function isNonEmptyArray<T = unknown, Item = unknown>(value: T | Item[]): value is [Item, ...Item[]];
|
|
37
39
|
declare function isObject(value: unknown): value is object;
|
|
38
40
|
declare function isEmptyObject(value: unknown): value is {};
|
|
41
|
+
declare function isMap<Key = unknown, Value = unknown>(value: unknown): value is Map<Key, Value>;
|
|
42
|
+
declare function isEmptyMap(value: unknown): value is Map<never, never>;
|
|
43
|
+
declare function isSet<Value = unknown>(value: unknown): value is Set<Value>;
|
|
44
|
+
declare function isEmptySet(value: unknown): value is Set<never>;
|
|
39
45
|
declare function isRegExp(value: unknown): value is RegExp;
|
|
40
46
|
declare function isError(value: unknown): value is Error;
|
|
41
|
-
declare function isSet<Value = unknown>(value: unknown): value is Set<Value>;
|
|
42
47
|
declare function isNativePromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
43
48
|
declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
49
|
+
declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
|
|
44
50
|
|
|
45
51
|
/**
|
|
46
52
|
* A function that does nothing.
|
|
@@ -201,7 +207,10 @@ type Awaitable<T> = Promise<T> | T;
|
|
|
201
207
|
type Callable<T> = AnyFn<any, T> | T;
|
|
202
208
|
type MayBe<T> = T | undefined;
|
|
203
209
|
type Nullable<T> = T | null;
|
|
204
|
-
|
|
210
|
+
/**
|
|
211
|
+
* Overwrite some keys type
|
|
212
|
+
*/
|
|
213
|
+
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
205
214
|
/**
|
|
206
215
|
* Prettify object type
|
|
207
216
|
*/
|
|
@@ -209,10 +218,7 @@ type Prettify<T> = {
|
|
|
209
218
|
[K in keyof T]: T[K];
|
|
210
219
|
} & {};
|
|
211
220
|
type PrettifyV2<T> = Omit<T, never>;
|
|
212
|
-
|
|
213
|
-
* Overwrite some keys type
|
|
214
|
-
*/
|
|
215
|
-
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
221
|
+
type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
|
|
216
222
|
/**
|
|
217
223
|
* Resolve `boolean | Record<string, any>` to `Record<string, any>`
|
|
218
224
|
*/
|
|
@@ -238,8 +244,21 @@ declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>):
|
|
|
238
244
|
*/
|
|
239
245
|
declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
240
246
|
|
|
247
|
+
/**
|
|
248
|
+
* Get intersect items
|
|
249
|
+
*
|
|
250
|
+
* @returns intersect items
|
|
251
|
+
*/
|
|
252
|
+
declare function intersect<T>(a: T[], b: T[]): T[];
|
|
253
|
+
|
|
241
254
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
242
255
|
|
|
256
|
+
interface CreatePadStringOptions {
|
|
257
|
+
length: number;
|
|
258
|
+
char: string;
|
|
259
|
+
}
|
|
260
|
+
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
261
|
+
|
|
243
262
|
type JoinableValue = string | number | null | undefined;
|
|
244
263
|
interface JoinOptions {
|
|
245
264
|
/**
|
|
@@ -260,6 +279,15 @@ declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
|
260
279
|
*/
|
|
261
280
|
declare function slash(input: string): string;
|
|
262
281
|
|
|
282
|
+
/**
|
|
283
|
+
* randome a string useing given chars
|
|
284
|
+
*
|
|
285
|
+
* @param length - string length
|
|
286
|
+
* @param chars - string chars
|
|
287
|
+
* @returns random string
|
|
288
|
+
*/
|
|
289
|
+
declare function randomString(length?: number, chars?: string): string;
|
|
290
|
+
|
|
263
291
|
/**
|
|
264
292
|
* Remove common leading whitespace from a template string
|
|
265
293
|
* Empty lines at the beginning and end of the template string are also removed.
|
|
@@ -422,4 +450,4 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
|
|
|
422
450
|
*/
|
|
423
451
|
declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
|
|
424
452
|
|
|
425
|
-
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, days, debounce, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isEmptyArray, isEmptyObject, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isNaN, isNativePromise, isNil, isNonEmptyString, isNull, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
|
453
|
+
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, createPadString, days, debounce, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, 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, randomString, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.d.ts
CHANGED
|
@@ -19,28 +19,34 @@ declare function getObjectType(value: unknown): string;
|
|
|
19
19
|
declare function isUndefined(value: unknown): value is undefined;
|
|
20
20
|
declare function isNull(value: unknown): value is null;
|
|
21
21
|
declare function isNil(value: unknown): value is null | undefined;
|
|
22
|
+
declare const isNullOrUndefined: typeof isNil;
|
|
22
23
|
declare function isString(value: unknown): value is string;
|
|
23
|
-
declare function isNumber(value: unknown): value is number;
|
|
24
|
-
declare function isZero(value: unknown): value is 0;
|
|
25
|
-
declare function isNaN(value: unknown): value is typeof Number.NaN;
|
|
26
24
|
declare function isEmptyString(value: unknown): value is '';
|
|
27
25
|
declare function isNonEmptyString(value: unknown): value is NonEmptyString;
|
|
28
26
|
declare function isWhitespaceString(value: unknown): value is Whitespace;
|
|
29
27
|
declare function isEmptyStringOrWhitespace(value: unknown): value is '' | Whitespace;
|
|
30
28
|
declare function isNumbericString(value: unknown): value is `${number}`;
|
|
29
|
+
declare function isNumber(value: unknown): value is number;
|
|
30
|
+
declare function isZero(value: unknown): value is 0;
|
|
31
|
+
declare function isNaN(value: unknown): value is typeof Number.NaN;
|
|
31
32
|
declare function isInteger(value: unknown): value is number;
|
|
32
33
|
declare function isBigInt(value: unknown): value is bigint;
|
|
33
34
|
declare function isBoolean(value: unknown): value is boolean;
|
|
34
35
|
declare function isFunction(value: unknown): value is Function;
|
|
35
36
|
declare function isArray(value: unknown): value is unknown[];
|
|
36
37
|
declare function isEmptyArray(value: unknown): value is [];
|
|
38
|
+
declare function isNonEmptyArray<T = unknown, Item = unknown>(value: T | Item[]): value is [Item, ...Item[]];
|
|
37
39
|
declare function isObject(value: unknown): value is object;
|
|
38
40
|
declare function isEmptyObject(value: unknown): value is {};
|
|
41
|
+
declare function isMap<Key = unknown, Value = unknown>(value: unknown): value is Map<Key, Value>;
|
|
42
|
+
declare function isEmptyMap(value: unknown): value is Map<never, never>;
|
|
43
|
+
declare function isSet<Value = unknown>(value: unknown): value is Set<Value>;
|
|
44
|
+
declare function isEmptySet(value: unknown): value is Set<never>;
|
|
39
45
|
declare function isRegExp(value: unknown): value is RegExp;
|
|
40
46
|
declare function isError(value: unknown): value is Error;
|
|
41
|
-
declare function isSet<Value = unknown>(value: unknown): value is Set<Value>;
|
|
42
47
|
declare function isNativePromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
43
48
|
declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
49
|
+
declare function isIterable<T = unknown>(value: unknown): value is Iterable<T>;
|
|
44
50
|
|
|
45
51
|
/**
|
|
46
52
|
* A function that does nothing.
|
|
@@ -201,7 +207,10 @@ type Awaitable<T> = Promise<T> | T;
|
|
|
201
207
|
type Callable<T> = AnyFn<any, T> | T;
|
|
202
208
|
type MayBe<T> = T | undefined;
|
|
203
209
|
type Nullable<T> = T | null;
|
|
204
|
-
|
|
210
|
+
/**
|
|
211
|
+
* Overwrite some keys type
|
|
212
|
+
*/
|
|
213
|
+
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
205
214
|
/**
|
|
206
215
|
* Prettify object type
|
|
207
216
|
*/
|
|
@@ -209,10 +218,7 @@ type Prettify<T> = {
|
|
|
209
218
|
[K in keyof T]: T[K];
|
|
210
219
|
} & {};
|
|
211
220
|
type PrettifyV2<T> = Omit<T, never>;
|
|
212
|
-
|
|
213
|
-
* Overwrite some keys type
|
|
214
|
-
*/
|
|
215
|
-
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
221
|
+
type PrimitiveType = bigint | boolean | number | string | symbol | null | undefined;
|
|
216
222
|
/**
|
|
217
223
|
* Resolve `boolean | Record<string, any>` to `Record<string, any>`
|
|
218
224
|
*/
|
|
@@ -238,8 +244,21 @@ declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>):
|
|
|
238
244
|
*/
|
|
239
245
|
declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
240
246
|
|
|
247
|
+
/**
|
|
248
|
+
* Get intersect items
|
|
249
|
+
*
|
|
250
|
+
* @returns intersect items
|
|
251
|
+
*/
|
|
252
|
+
declare function intersect<T>(a: T[], b: T[]): T[];
|
|
253
|
+
|
|
241
254
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
242
255
|
|
|
256
|
+
interface CreatePadStringOptions {
|
|
257
|
+
length: number;
|
|
258
|
+
char: string;
|
|
259
|
+
}
|
|
260
|
+
declare function createPadString(options: CreatePadStringOptions): (value: string) => string;
|
|
261
|
+
|
|
243
262
|
type JoinableValue = string | number | null | undefined;
|
|
244
263
|
interface JoinOptions {
|
|
245
264
|
/**
|
|
@@ -260,6 +279,15 @@ declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
|
260
279
|
*/
|
|
261
280
|
declare function slash(input: string): string;
|
|
262
281
|
|
|
282
|
+
/**
|
|
283
|
+
* randome a string useing given chars
|
|
284
|
+
*
|
|
285
|
+
* @param length - string length
|
|
286
|
+
* @param chars - string chars
|
|
287
|
+
* @returns random string
|
|
288
|
+
*/
|
|
289
|
+
declare function randomString(length?: number, chars?: string): string;
|
|
290
|
+
|
|
263
291
|
/**
|
|
264
292
|
* Remove common leading whitespace from a template string
|
|
265
293
|
* Empty lines at the beginning and end of the template string are also removed.
|
|
@@ -422,4 +450,4 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
|
|
|
422
450
|
*/
|
|
423
451
|
declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
|
|
424
452
|
|
|
425
|
-
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, days, debounce, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, isEmptyArray, isEmptyObject, isEmptyString, isEmptyStringOrWhitespace, isError, isFunction, isInteger, isNaN, isNativePromise, isNil, isNonEmptyString, isNull, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, isZero, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
|
453
|
+
export { type AnyFn, type Arrayable, type Awaitable, type Callable, type CleanObjectOptions, type CreatePadStringOptions, type InteropModuleDefault, type JsonArray, type JsonObject, type JsonPrimitive, type JsonValue, type MayBe, NOOP, type NonEmptyString, type Nullable, type Overwrite, type Prettify, type PrettifyV2, type PrimitiveType, type ResolvedOptions, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, cleanObject, createPadString, days, debounce, ensurePrefix, ensureSuffix, escapeHtml, flattenArrayable, getObjectType, hasOwn, hours, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBoolean, isBrowser, isDeepEqual, 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, randomString, resolveSubOptions, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.js
CHANGED
|
@@ -16,7 +16,9 @@ function isDeepEqual(value1, value2) {
|
|
|
16
16
|
if (keys.length !== Object.keys(value2).length) {
|
|
17
17
|
return false;
|
|
18
18
|
}
|
|
19
|
-
return keys.every(
|
|
19
|
+
return keys.every(
|
|
20
|
+
(key) => isDeepEqual(value1[key], value2[key])
|
|
21
|
+
);
|
|
20
22
|
}
|
|
21
23
|
return Object.is(value1, value2);
|
|
22
24
|
}
|
|
@@ -34,18 +36,10 @@ function isNull(value) {
|
|
|
34
36
|
function isNil(value) {
|
|
35
37
|
return isNull(value) || isUndefined(value);
|
|
36
38
|
}
|
|
39
|
+
var isNullOrUndefined = isNil;
|
|
37
40
|
function isString(value) {
|
|
38
41
|
return typeof value === "string";
|
|
39
42
|
}
|
|
40
|
-
function isNumber(value) {
|
|
41
|
-
return typeof value === "number";
|
|
42
|
-
}
|
|
43
|
-
function isZero(value) {
|
|
44
|
-
return value === 0;
|
|
45
|
-
}
|
|
46
|
-
function isNaN(value) {
|
|
47
|
-
return Number.isNaN(value);
|
|
48
|
-
}
|
|
49
43
|
function isEmptyString(value) {
|
|
50
44
|
return isString(value) && value.length === 0;
|
|
51
45
|
}
|
|
@@ -61,6 +55,15 @@ function isEmptyStringOrWhitespace(value) {
|
|
|
61
55
|
function isNumbericString(value) {
|
|
62
56
|
return isString(value) && !isEmptyStringOrWhitespace(value) && !Number.isNaN(Number(value));
|
|
63
57
|
}
|
|
58
|
+
function isNumber(value) {
|
|
59
|
+
return typeof value === "number";
|
|
60
|
+
}
|
|
61
|
+
function isZero(value) {
|
|
62
|
+
return value === 0;
|
|
63
|
+
}
|
|
64
|
+
function isNaN(value) {
|
|
65
|
+
return Number.isNaN(value);
|
|
66
|
+
}
|
|
64
67
|
function isInteger(value) {
|
|
65
68
|
return Number.isInteger(value);
|
|
66
69
|
}
|
|
@@ -79,11 +82,26 @@ function isArray(value) {
|
|
|
79
82
|
function isEmptyArray(value) {
|
|
80
83
|
return isArray(value) && value.length === 0;
|
|
81
84
|
}
|
|
85
|
+
function isNonEmptyArray(value) {
|
|
86
|
+
return isArray(value) && value.length > 0;
|
|
87
|
+
}
|
|
82
88
|
function isObject(value) {
|
|
83
|
-
return
|
|
89
|
+
return (typeof value === "object" || isFunction(value)) && !isNull(value);
|
|
84
90
|
}
|
|
85
91
|
function isEmptyObject(value) {
|
|
86
|
-
return isObject(value) && Object.keys(value).length === 0;
|
|
92
|
+
return isObject(value) && !isMap(value) && !isSet(value) && Object.keys(value).length === 0;
|
|
93
|
+
}
|
|
94
|
+
function isMap(value) {
|
|
95
|
+
return getObjectType(value) === "Map";
|
|
96
|
+
}
|
|
97
|
+
function isEmptyMap(value) {
|
|
98
|
+
return isMap(value) && value.size === 0;
|
|
99
|
+
}
|
|
100
|
+
function isSet(value) {
|
|
101
|
+
return getObjectType(value) === "Set";
|
|
102
|
+
}
|
|
103
|
+
function isEmptySet(value) {
|
|
104
|
+
return isSet(value) && value.size === 0;
|
|
87
105
|
}
|
|
88
106
|
function isRegExp(value) {
|
|
89
107
|
return getObjectType(value) === "RegExp";
|
|
@@ -91,18 +109,18 @@ function isRegExp(value) {
|
|
|
91
109
|
function isError(value) {
|
|
92
110
|
return getObjectType(value) === "Error";
|
|
93
111
|
}
|
|
94
|
-
function
|
|
95
|
-
return
|
|
112
|
+
function hasPromiseApi(value) {
|
|
113
|
+
return isFunction(value?.then) && isFunction(value?.catch);
|
|
96
114
|
}
|
|
97
115
|
function isNativePromise(value) {
|
|
98
116
|
return getObjectType(value) === "Promise";
|
|
99
117
|
}
|
|
100
|
-
function hasPromiseApi(value) {
|
|
101
|
-
return isFunction(value?.then) && isFunction(value?.catch);
|
|
102
|
-
}
|
|
103
118
|
function isPromise(value) {
|
|
104
119
|
return isNativePromise(value) || hasPromiseApi(value);
|
|
105
120
|
}
|
|
121
|
+
function isIterable(value) {
|
|
122
|
+
return isFunction(value?.[Symbol.iterator]);
|
|
123
|
+
}
|
|
106
124
|
|
|
107
125
|
// src/fn/noop.ts
|
|
108
126
|
var noop = () => {
|
|
@@ -210,7 +228,10 @@ function throttle(delay, callback, options = {}) {
|
|
|
210
228
|
if (!isDebounce && elapsed > delay) {
|
|
211
229
|
exec(now);
|
|
212
230
|
} else {
|
|
213
|
-
timeoutId = setTimeout(
|
|
231
|
+
timeoutId = setTimeout(
|
|
232
|
+
isDebounce ? clear : exec,
|
|
233
|
+
isDebounce ? delay : delay - elapsed
|
|
234
|
+
);
|
|
214
235
|
}
|
|
215
236
|
}
|
|
216
237
|
wrapper.cancel = cancel;
|
|
@@ -288,6 +309,11 @@ function mergeArrayable(...args) {
|
|
|
288
309
|
return args.flatMap((i) => toArray(i));
|
|
289
310
|
}
|
|
290
311
|
|
|
312
|
+
// src/array/intersect.ts
|
|
313
|
+
function intersect(a, b) {
|
|
314
|
+
return a.filter((item) => b.includes(item));
|
|
315
|
+
}
|
|
316
|
+
|
|
291
317
|
// src/array/isArrayEqual.ts
|
|
292
318
|
function isArrayEqual(array1, array2) {
|
|
293
319
|
if (array1.length !== array2.length) {
|
|
@@ -296,6 +322,12 @@ function isArrayEqual(array1, array2) {
|
|
|
296
322
|
return array1.every((item, idx) => item === array2[idx]);
|
|
297
323
|
}
|
|
298
324
|
|
|
325
|
+
// src/string/pad.ts
|
|
326
|
+
function createPadString(options) {
|
|
327
|
+
const { length, char } = options;
|
|
328
|
+
return (value) => (char.repeat(length) + value).slice(-length);
|
|
329
|
+
}
|
|
330
|
+
|
|
299
331
|
// src/string/join.ts
|
|
300
332
|
function join(array, options = {}) {
|
|
301
333
|
const { separator = "" } = options;
|
|
@@ -308,6 +340,28 @@ function slash(input) {
|
|
|
308
340
|
return input.replace(/\\/g, "/");
|
|
309
341
|
}
|
|
310
342
|
|
|
343
|
+
// src/number/random.ts
|
|
344
|
+
function randomNumber(min, max = 0) {
|
|
345
|
+
if (max === 0) {
|
|
346
|
+
max = min;
|
|
347
|
+
min = 0;
|
|
348
|
+
}
|
|
349
|
+
if (min > max) {
|
|
350
|
+
;
|
|
351
|
+
[min, max] = [max, min];
|
|
352
|
+
}
|
|
353
|
+
return Math.trunc(Math.random() * (max - min + 1) + min);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// src/string/random.ts
|
|
357
|
+
function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
|
|
358
|
+
const result = [];
|
|
359
|
+
for (let i = length; i > 0; --i) {
|
|
360
|
+
result.push(chars[randomNumber(chars.length)]);
|
|
361
|
+
}
|
|
362
|
+
return result.join("");
|
|
363
|
+
}
|
|
364
|
+
|
|
311
365
|
// src/string/unindent.ts
|
|
312
366
|
var _RE_FULL_WS = /^\s*$/;
|
|
313
367
|
function unindent(input) {
|
|
@@ -542,6 +596,7 @@ export {
|
|
|
542
596
|
chunk,
|
|
543
597
|
clamp,
|
|
544
598
|
cleanObject,
|
|
599
|
+
createPadString,
|
|
545
600
|
days,
|
|
546
601
|
debounce,
|
|
547
602
|
ensurePrefix,
|
|
@@ -553,6 +608,7 @@ export {
|
|
|
553
608
|
hasOwn,
|
|
554
609
|
hours,
|
|
555
610
|
interopDefault,
|
|
611
|
+
intersect,
|
|
556
612
|
isArray,
|
|
557
613
|
isArrayEqual,
|
|
558
614
|
isBigInt,
|
|
@@ -560,17 +616,23 @@ export {
|
|
|
560
616
|
isBrowser,
|
|
561
617
|
isDeepEqual,
|
|
562
618
|
isEmptyArray,
|
|
619
|
+
isEmptyMap,
|
|
563
620
|
isEmptyObject,
|
|
621
|
+
isEmptySet,
|
|
564
622
|
isEmptyString,
|
|
565
623
|
isEmptyStringOrWhitespace,
|
|
566
624
|
isError,
|
|
567
625
|
isFunction,
|
|
568
626
|
isInteger,
|
|
627
|
+
isIterable,
|
|
628
|
+
isMap,
|
|
569
629
|
isNaN,
|
|
570
630
|
isNativePromise,
|
|
571
631
|
isNil,
|
|
632
|
+
isNonEmptyArray,
|
|
572
633
|
isNonEmptyString,
|
|
573
634
|
isNull,
|
|
635
|
+
isNullOrUndefined,
|
|
574
636
|
isNumber,
|
|
575
637
|
isNumbericString,
|
|
576
638
|
isObject,
|
|
@@ -594,6 +656,7 @@ export {
|
|
|
594
656
|
pascalCase,
|
|
595
657
|
pick,
|
|
596
658
|
rAF,
|
|
659
|
+
randomString,
|
|
597
660
|
resolveSubOptions,
|
|
598
661
|
seconds,
|
|
599
662
|
slash,
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ntnyq/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.0",
|
|
5
5
|
"description": "Common used utils.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"utils"
|
|
@@ -40,18 +40,18 @@
|
|
|
40
40
|
"scule": "^1.3.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@ntnyq/eslint-config": "^
|
|
44
|
-
"@ntnyq/prettier-config": "^
|
|
45
|
-
"@vitest/coverage-v8": "^3.0.
|
|
46
|
-
"bumpp": "^
|
|
47
|
-
"eslint": "^9.
|
|
43
|
+
"@ntnyq/eslint-config": "^4.0.0-beta.3",
|
|
44
|
+
"@ntnyq/prettier-config": "^2.0.0-beta.2",
|
|
45
|
+
"@vitest/coverage-v8": "^3.0.5",
|
|
46
|
+
"bumpp": "^10.0.2",
|
|
47
|
+
"eslint": "^9.20.0",
|
|
48
48
|
"husky": "^9.1.7",
|
|
49
49
|
"nano-staged": "^0.8.0",
|
|
50
50
|
"npm-run-all2": "^7.0.2",
|
|
51
|
-
"prettier": "^3.
|
|
52
|
-
"tsup": "^8.3.
|
|
53
|
-
"typescript": "^5.7.
|
|
54
|
-
"vitest": "^3.0.
|
|
51
|
+
"prettier": "^3.5.0",
|
|
52
|
+
"tsup": "^8.3.6",
|
|
53
|
+
"typescript": "^5.7.3",
|
|
54
|
+
"vitest": "^3.0.5"
|
|
55
55
|
},
|
|
56
56
|
"engines": {
|
|
57
57
|
"node": ">=18.18.0"
|