@ntnyq/utils 0.2.0 → 0.3.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 +102 -0
- package/dist/index.d.cts +56 -1
- package/dist/index.d.ts +56 -1
- package/dist/index.js +89 -0
- package/package.json +9 -10
package/dist/index.cjs
CHANGED
|
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
NOOP: () => NOOP,
|
|
24
|
+
at: () => at,
|
|
24
25
|
cAF: () => cAF,
|
|
25
26
|
camelCase: () => camelCase,
|
|
26
27
|
capitalize: () => capitalize,
|
|
@@ -31,6 +32,7 @@ __export(src_exports, {
|
|
|
31
32
|
ensurePrefix: () => ensurePrefix,
|
|
32
33
|
ensureSuffix: () => ensureSuffix,
|
|
33
34
|
flatCase: () => flatCase,
|
|
35
|
+
flattenArrayable: () => flattenArrayable,
|
|
34
36
|
getObjectType: () => getObjectType,
|
|
35
37
|
hasOwn: () => hasOwn,
|
|
36
38
|
hours: () => hours,
|
|
@@ -38,18 +40,28 @@ __export(src_exports, {
|
|
|
38
40
|
isArrayEqual: () => isArrayEqual,
|
|
39
41
|
isBoolean: () => isBoolean,
|
|
40
42
|
isBrowser: () => isBrowser,
|
|
43
|
+
isEmptyString: () => isEmptyString,
|
|
44
|
+
isEmptyStringOrWhitespace: () => isEmptyStringOrWhitespace,
|
|
41
45
|
isFunction: () => isFunction,
|
|
42
46
|
isInteger: () => isInteger,
|
|
43
47
|
isNativePromise: () => isNativePromise,
|
|
48
|
+
isNil: () => isNil,
|
|
44
49
|
isNull: () => isNull,
|
|
45
50
|
isNumber: () => isNumber,
|
|
51
|
+
isNumbericString: () => isNumbericString,
|
|
52
|
+
isObject: () => isObject,
|
|
46
53
|
isPromise: () => isPromise,
|
|
54
|
+
isRegExp: () => isRegExp,
|
|
55
|
+
isSet: () => isSet,
|
|
47
56
|
isString: () => isString,
|
|
48
57
|
isUndefined: () => isUndefined,
|
|
49
58
|
isUppercase: () => isUppercase,
|
|
59
|
+
isWhitespaceString: () => isWhitespaceString,
|
|
50
60
|
join: () => join,
|
|
51
61
|
kebabCase: () => kebabCase,
|
|
62
|
+
last: () => last,
|
|
52
63
|
lowerFirst: () => lowerFirst,
|
|
64
|
+
mergeArrayable: () => mergeArrayable,
|
|
53
65
|
minutes: () => minutes,
|
|
54
66
|
noop: () => noop,
|
|
55
67
|
omit: () => omit,
|
|
@@ -60,6 +72,7 @@ __export(src_exports, {
|
|
|
60
72
|
seconds: () => seconds,
|
|
61
73
|
slash: () => slash,
|
|
62
74
|
snakeCase: () => snakeCase,
|
|
75
|
+
sortObject: () => sortObject,
|
|
63
76
|
splitByCase: () => splitByCase,
|
|
64
77
|
throttle: () => throttle,
|
|
65
78
|
titleCase: () => titleCase,
|
|
@@ -85,6 +98,18 @@ function isString(value) {
|
|
|
85
98
|
function isNumber(value) {
|
|
86
99
|
return typeof value === "number";
|
|
87
100
|
}
|
|
101
|
+
function isEmptyString(value) {
|
|
102
|
+
return isString(value) && value.length === 0;
|
|
103
|
+
}
|
|
104
|
+
function isWhitespaceString(value) {
|
|
105
|
+
return isString(value) && /^\s*$/.test(value);
|
|
106
|
+
}
|
|
107
|
+
function isEmptyStringOrWhitespace(value) {
|
|
108
|
+
return isEmptyString(value) || isWhitespaceString(value);
|
|
109
|
+
}
|
|
110
|
+
function isNumbericString(value) {
|
|
111
|
+
return isString(value) && !isEmptyStringOrWhitespace(value) && !Number.isNaN(Number(value));
|
|
112
|
+
}
|
|
88
113
|
function isInteger(value) {
|
|
89
114
|
return Number.isInteger(value);
|
|
90
115
|
}
|
|
@@ -103,6 +128,18 @@ function isUndefined(value) {
|
|
|
103
128
|
function isNull(value) {
|
|
104
129
|
return value === null;
|
|
105
130
|
}
|
|
131
|
+
function isNil(value) {
|
|
132
|
+
return isNull(value) || isUndefined(value);
|
|
133
|
+
}
|
|
134
|
+
function isObject(value) {
|
|
135
|
+
return getObjectType(value) === "Object";
|
|
136
|
+
}
|
|
137
|
+
function isRegExp(value) {
|
|
138
|
+
return getObjectType(value) === "RegExp";
|
|
139
|
+
}
|
|
140
|
+
function isSet(value) {
|
|
141
|
+
return getObjectType(value) === "Set";
|
|
142
|
+
}
|
|
106
143
|
function isNativePromise(value) {
|
|
107
144
|
return getObjectType(value) === "Promise";
|
|
108
145
|
}
|
|
@@ -325,6 +362,19 @@ var warnOnce = (message) => {
|
|
|
325
362
|
console.warn(message);
|
|
326
363
|
};
|
|
327
364
|
|
|
365
|
+
// src/array/at.ts
|
|
366
|
+
function at(array, index) {
|
|
367
|
+
const length = array.length;
|
|
368
|
+
if (!length) return void 0;
|
|
369
|
+
if (index < 0) {
|
|
370
|
+
index += length;
|
|
371
|
+
}
|
|
372
|
+
return array[index];
|
|
373
|
+
}
|
|
374
|
+
function last(array) {
|
|
375
|
+
return at(array, -1);
|
|
376
|
+
}
|
|
377
|
+
|
|
328
378
|
// src/array/chunk.ts
|
|
329
379
|
function chunk(array, size) {
|
|
330
380
|
const result = [];
|
|
@@ -354,6 +404,14 @@ function toArray(array) {
|
|
|
354
404
|
return Array.isArray(array) ? array : [array];
|
|
355
405
|
}
|
|
356
406
|
|
|
407
|
+
// src/array/arrayable.ts
|
|
408
|
+
function flattenArrayable(array) {
|
|
409
|
+
return toArray(array).flat(1);
|
|
410
|
+
}
|
|
411
|
+
function mergeArrayable(...args) {
|
|
412
|
+
return args.flatMap((i) => toArray(i));
|
|
413
|
+
}
|
|
414
|
+
|
|
357
415
|
// src/array/isArrayEqual.ts
|
|
358
416
|
function isArrayEqual(array1, array2) {
|
|
359
417
|
if (array1.length !== array2.length) {
|
|
@@ -429,9 +487,41 @@ function pick(object, keys) {
|
|
|
429
487
|
})
|
|
430
488
|
);
|
|
431
489
|
}
|
|
490
|
+
|
|
491
|
+
// src/object/isPlainObject.ts
|
|
492
|
+
function isPlainObject(value) {
|
|
493
|
+
if (!isObject(value)) return false;
|
|
494
|
+
const prototype = Object.getPrototypeOf(value);
|
|
495
|
+
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
// src/object/sortObject.ts
|
|
499
|
+
function sortObject(obj, options = {}) {
|
|
500
|
+
const { compareFn = (a, b) => a.localeCompare(b) } = options;
|
|
501
|
+
function sortKeys(obj2) {
|
|
502
|
+
const sortedKeys = Object.keys(obj2).sort(compareFn);
|
|
503
|
+
const result = {};
|
|
504
|
+
for (const key of sortedKeys) {
|
|
505
|
+
const value = obj2[key];
|
|
506
|
+
let newValue;
|
|
507
|
+
if (options.deep && isPlainObject(value)) {
|
|
508
|
+
newValue = sortKeys(value);
|
|
509
|
+
} else {
|
|
510
|
+
newValue = value;
|
|
511
|
+
}
|
|
512
|
+
Object.defineProperty(result, key, {
|
|
513
|
+
...Object.getOwnPropertyDescriptor(obj2, key),
|
|
514
|
+
value: newValue
|
|
515
|
+
});
|
|
516
|
+
}
|
|
517
|
+
return result;
|
|
518
|
+
}
|
|
519
|
+
return sortKeys(obj);
|
|
520
|
+
}
|
|
432
521
|
// Annotate the CommonJS export names for ESM import in node:
|
|
433
522
|
0 && (module.exports = {
|
|
434
523
|
NOOP,
|
|
524
|
+
at,
|
|
435
525
|
cAF,
|
|
436
526
|
camelCase,
|
|
437
527
|
capitalize,
|
|
@@ -442,6 +532,7 @@ function pick(object, keys) {
|
|
|
442
532
|
ensurePrefix,
|
|
443
533
|
ensureSuffix,
|
|
444
534
|
flatCase,
|
|
535
|
+
flattenArrayable,
|
|
445
536
|
getObjectType,
|
|
446
537
|
hasOwn,
|
|
447
538
|
hours,
|
|
@@ -449,18 +540,28 @@ function pick(object, keys) {
|
|
|
449
540
|
isArrayEqual,
|
|
450
541
|
isBoolean,
|
|
451
542
|
isBrowser,
|
|
543
|
+
isEmptyString,
|
|
544
|
+
isEmptyStringOrWhitespace,
|
|
452
545
|
isFunction,
|
|
453
546
|
isInteger,
|
|
454
547
|
isNativePromise,
|
|
548
|
+
isNil,
|
|
455
549
|
isNull,
|
|
456
550
|
isNumber,
|
|
551
|
+
isNumbericString,
|
|
552
|
+
isObject,
|
|
457
553
|
isPromise,
|
|
554
|
+
isRegExp,
|
|
555
|
+
isSet,
|
|
458
556
|
isString,
|
|
459
557
|
isUndefined,
|
|
460
558
|
isUppercase,
|
|
559
|
+
isWhitespaceString,
|
|
461
560
|
join,
|
|
462
561
|
kebabCase,
|
|
562
|
+
last,
|
|
463
563
|
lowerFirst,
|
|
564
|
+
mergeArrayable,
|
|
464
565
|
minutes,
|
|
465
566
|
noop,
|
|
466
567
|
omit,
|
|
@@ -471,6 +572,7 @@ function pick(object, keys) {
|
|
|
471
572
|
seconds,
|
|
472
573
|
slash,
|
|
473
574
|
snakeCase,
|
|
575
|
+
sortObject,
|
|
474
576
|
splitByCase,
|
|
475
577
|
throttle,
|
|
476
578
|
titleCase,
|
package/dist/index.d.cts
CHANGED
|
@@ -9,12 +9,24 @@ export * from 'scule';
|
|
|
9
9
|
declare function getObjectType(value: unknown): string;
|
|
10
10
|
declare function isString(value: unknown): value is string;
|
|
11
11
|
declare function isNumber(value: unknown): value is number;
|
|
12
|
+
declare function isEmptyString(value: unknown): value is '';
|
|
13
|
+
type Whitespace = ' ';
|
|
14
|
+
type NonEmptyString = string & {
|
|
15
|
+
0: '';
|
|
16
|
+
};
|
|
17
|
+
declare function isWhitespaceString(value: unknown): value is Whitespace;
|
|
18
|
+
declare function isEmptyStringOrWhitespace(value: unknown): value is '' | Whitespace;
|
|
19
|
+
declare function isNumbericString(value: unknown): value is `${number}`;
|
|
12
20
|
declare function isInteger(value: unknown): value is number;
|
|
13
21
|
declare function isBoolean(value: unknown): value is boolean;
|
|
14
22
|
declare function isFunction(value: unknown): value is Function;
|
|
15
23
|
declare function isArray(value: unknown): value is unknown[];
|
|
16
24
|
declare function isUndefined(value: unknown): value is undefined;
|
|
17
25
|
declare function isNull(value: unknown): value is null;
|
|
26
|
+
declare function isNil(value: unknown): value is null | undefined;
|
|
27
|
+
declare function isObject(value: unknown): value is object;
|
|
28
|
+
declare function isRegExp(value: unknown): value is RegExp;
|
|
29
|
+
declare function isSet<Value = unknown>(value: unknown): value is Set<Value>;
|
|
18
30
|
declare function isNativePromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
19
31
|
declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
20
32
|
|
|
@@ -123,6 +135,20 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
|
|
|
123
135
|
|
|
124
136
|
declare const warnOnce: (message: string) => void;
|
|
125
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Get array item by index, negative for backward
|
|
140
|
+
* @param array - given array
|
|
141
|
+
* @param index - index of item
|
|
142
|
+
* @returns undefined if not match, otherwise matched item
|
|
143
|
+
*/
|
|
144
|
+
declare function at<T>(array: readonly T[], index: number): T | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Get the last item of given array
|
|
147
|
+
* @param array - given array
|
|
148
|
+
* @returns undefined if empty array, otherwise last item
|
|
149
|
+
*/
|
|
150
|
+
declare function last<T>(array: readonly T[]): T | undefined;
|
|
151
|
+
|
|
126
152
|
/**
|
|
127
153
|
* Splits an array into smaller chunks of a given size.
|
|
128
154
|
* @param array The array to split
|
|
@@ -163,6 +189,19 @@ type PrimitiveType = number | bigint | string | boolean | symbol | null | undefi
|
|
|
163
189
|
*/
|
|
164
190
|
declare function toArray<T>(array?: Nullable<Arrayable<T>>): T[];
|
|
165
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Convert `Arrayable<T>` to `Array<T>` and flatten the result
|
|
194
|
+
* @param array - given array
|
|
195
|
+
* @returns Array<T>
|
|
196
|
+
*/
|
|
197
|
+
declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
|
|
198
|
+
/**
|
|
199
|
+
* Use rest arguments to merge arrays
|
|
200
|
+
* @param args - rest arguments
|
|
201
|
+
* @returns Array<T>
|
|
202
|
+
*/
|
|
203
|
+
declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
204
|
+
|
|
166
205
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
167
206
|
|
|
168
207
|
type JoinableValue = string | number | null | undefined;
|
|
@@ -212,4 +251,20 @@ declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
|
|
|
212
251
|
|
|
213
252
|
declare function hasOwn<T, K extends keyof T>(object: T, key: K): boolean;
|
|
214
253
|
|
|
215
|
-
|
|
254
|
+
interface SortObjectOptions {
|
|
255
|
+
/**
|
|
256
|
+
* Recursive sorting
|
|
257
|
+
* @default false
|
|
258
|
+
*/
|
|
259
|
+
deep?: boolean;
|
|
260
|
+
/**
|
|
261
|
+
* Compare function
|
|
262
|
+
*/
|
|
263
|
+
compareFn?: (left: string, right: string) => number;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Sort object properties
|
|
267
|
+
*/
|
|
268
|
+
declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
|
|
269
|
+
|
|
270
|
+
export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type NonEmptyString, type Nullable, type Prettify, type PrettifyV2, type PrimitiveType, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, days, debounce, ensurePrefix, ensureSuffix, flattenArrayable, getObjectType, hasOwn, hours, isArray, isArrayEqual, isBoolean, isBrowser, isEmptyString, isEmptyStringOrWhitespace, isFunction, isInteger, isNativePromise, isNil, isNull, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.d.ts
CHANGED
|
@@ -9,12 +9,24 @@ export * from 'scule';
|
|
|
9
9
|
declare function getObjectType(value: unknown): string;
|
|
10
10
|
declare function isString(value: unknown): value is string;
|
|
11
11
|
declare function isNumber(value: unknown): value is number;
|
|
12
|
+
declare function isEmptyString(value: unknown): value is '';
|
|
13
|
+
type Whitespace = ' ';
|
|
14
|
+
type NonEmptyString = string & {
|
|
15
|
+
0: '';
|
|
16
|
+
};
|
|
17
|
+
declare function isWhitespaceString(value: unknown): value is Whitespace;
|
|
18
|
+
declare function isEmptyStringOrWhitespace(value: unknown): value is '' | Whitespace;
|
|
19
|
+
declare function isNumbericString(value: unknown): value is `${number}`;
|
|
12
20
|
declare function isInteger(value: unknown): value is number;
|
|
13
21
|
declare function isBoolean(value: unknown): value is boolean;
|
|
14
22
|
declare function isFunction(value: unknown): value is Function;
|
|
15
23
|
declare function isArray(value: unknown): value is unknown[];
|
|
16
24
|
declare function isUndefined(value: unknown): value is undefined;
|
|
17
25
|
declare function isNull(value: unknown): value is null;
|
|
26
|
+
declare function isNil(value: unknown): value is null | undefined;
|
|
27
|
+
declare function isObject(value: unknown): value is object;
|
|
28
|
+
declare function isRegExp(value: unknown): value is RegExp;
|
|
29
|
+
declare function isSet<Value = unknown>(value: unknown): value is Set<Value>;
|
|
18
30
|
declare function isNativePromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
19
31
|
declare function isPromise<T = unknown>(value: unknown): value is Promise<T>;
|
|
20
32
|
|
|
@@ -123,6 +135,20 @@ declare function debounce<T extends ((...args: any[]) => undefined | void) | und
|
|
|
123
135
|
|
|
124
136
|
declare const warnOnce: (message: string) => void;
|
|
125
137
|
|
|
138
|
+
/**
|
|
139
|
+
* Get array item by index, negative for backward
|
|
140
|
+
* @param array - given array
|
|
141
|
+
* @param index - index of item
|
|
142
|
+
* @returns undefined if not match, otherwise matched item
|
|
143
|
+
*/
|
|
144
|
+
declare function at<T>(array: readonly T[], index: number): T | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Get the last item of given array
|
|
147
|
+
* @param array - given array
|
|
148
|
+
* @returns undefined if empty array, otherwise last item
|
|
149
|
+
*/
|
|
150
|
+
declare function last<T>(array: readonly T[]): T | undefined;
|
|
151
|
+
|
|
126
152
|
/**
|
|
127
153
|
* Splits an array into smaller chunks of a given size.
|
|
128
154
|
* @param array The array to split
|
|
@@ -163,6 +189,19 @@ type PrimitiveType = number | bigint | string | boolean | symbol | null | undefi
|
|
|
163
189
|
*/
|
|
164
190
|
declare function toArray<T>(array?: Nullable<Arrayable<T>>): T[];
|
|
165
191
|
|
|
192
|
+
/**
|
|
193
|
+
* Convert `Arrayable<T>` to `Array<T>` and flatten the result
|
|
194
|
+
* @param array - given array
|
|
195
|
+
* @returns Array<T>
|
|
196
|
+
*/
|
|
197
|
+
declare function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T>;
|
|
198
|
+
/**
|
|
199
|
+
* Use rest arguments to merge arrays
|
|
200
|
+
* @param args - rest arguments
|
|
201
|
+
* @returns Array<T>
|
|
202
|
+
*/
|
|
203
|
+
declare function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T>;
|
|
204
|
+
|
|
166
205
|
declare function isArrayEqual(array1: unknown[], array2: unknown[]): boolean;
|
|
167
206
|
|
|
168
207
|
type JoinableValue = string | number | null | undefined;
|
|
@@ -212,4 +251,20 @@ declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
|
|
|
212
251
|
|
|
213
252
|
declare function hasOwn<T, K extends keyof T>(object: T, key: K): boolean;
|
|
214
253
|
|
|
215
|
-
|
|
254
|
+
interface SortObjectOptions {
|
|
255
|
+
/**
|
|
256
|
+
* Recursive sorting
|
|
257
|
+
* @default false
|
|
258
|
+
*/
|
|
259
|
+
deep?: boolean;
|
|
260
|
+
/**
|
|
261
|
+
* Compare function
|
|
262
|
+
*/
|
|
263
|
+
compareFn?: (left: string, right: string) => number;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Sort object properties
|
|
267
|
+
*/
|
|
268
|
+
declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
|
|
269
|
+
|
|
270
|
+
export { type AnyFn, type Arrayable, type Awaitable, type MayBe, NOOP, type NonEmptyString, type Nullable, type Prettify, type PrettifyV2, type PrimitiveType, type SortObjectOptions, type ThrottleDebounceOptions, type Whitespace, at, cAF, capitalize, chunk, clamp, days, debounce, ensurePrefix, ensureSuffix, flattenArrayable, getObjectType, hasOwn, hours, isArray, isArrayEqual, isBoolean, isBrowser, isEmptyString, isEmptyStringOrWhitespace, isFunction, isInteger, isNativePromise, isNil, isNull, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isUndefined, isWhitespaceString, join, last, mergeArrayable, minutes, noop, omit, once, pick, rAF, seconds, slash, sortObject, throttle, toArray, unindent, unique, uniqueBy, waitFor, warnOnce, weeks };
|
package/dist/index.js
CHANGED
|
@@ -8,6 +8,18 @@ function isString(value) {
|
|
|
8
8
|
function isNumber(value) {
|
|
9
9
|
return typeof value === "number";
|
|
10
10
|
}
|
|
11
|
+
function isEmptyString(value) {
|
|
12
|
+
return isString(value) && value.length === 0;
|
|
13
|
+
}
|
|
14
|
+
function isWhitespaceString(value) {
|
|
15
|
+
return isString(value) && /^\s*$/.test(value);
|
|
16
|
+
}
|
|
17
|
+
function isEmptyStringOrWhitespace(value) {
|
|
18
|
+
return isEmptyString(value) || isWhitespaceString(value);
|
|
19
|
+
}
|
|
20
|
+
function isNumbericString(value) {
|
|
21
|
+
return isString(value) && !isEmptyStringOrWhitespace(value) && !Number.isNaN(Number(value));
|
|
22
|
+
}
|
|
11
23
|
function isInteger(value) {
|
|
12
24
|
return Number.isInteger(value);
|
|
13
25
|
}
|
|
@@ -26,6 +38,18 @@ function isUndefined(value) {
|
|
|
26
38
|
function isNull(value) {
|
|
27
39
|
return value === null;
|
|
28
40
|
}
|
|
41
|
+
function isNil(value) {
|
|
42
|
+
return isNull(value) || isUndefined(value);
|
|
43
|
+
}
|
|
44
|
+
function isObject(value) {
|
|
45
|
+
return getObjectType(value) === "Object";
|
|
46
|
+
}
|
|
47
|
+
function isRegExp(value) {
|
|
48
|
+
return getObjectType(value) === "RegExp";
|
|
49
|
+
}
|
|
50
|
+
function isSet(value) {
|
|
51
|
+
return getObjectType(value) === "Set";
|
|
52
|
+
}
|
|
29
53
|
function isNativePromise(value) {
|
|
30
54
|
return getObjectType(value) === "Promise";
|
|
31
55
|
}
|
|
@@ -248,6 +272,19 @@ var warnOnce = (message) => {
|
|
|
248
272
|
console.warn(message);
|
|
249
273
|
};
|
|
250
274
|
|
|
275
|
+
// src/array/at.ts
|
|
276
|
+
function at(array, index) {
|
|
277
|
+
const length = array.length;
|
|
278
|
+
if (!length) return void 0;
|
|
279
|
+
if (index < 0) {
|
|
280
|
+
index += length;
|
|
281
|
+
}
|
|
282
|
+
return array[index];
|
|
283
|
+
}
|
|
284
|
+
function last(array) {
|
|
285
|
+
return at(array, -1);
|
|
286
|
+
}
|
|
287
|
+
|
|
251
288
|
// src/array/chunk.ts
|
|
252
289
|
function chunk(array, size) {
|
|
253
290
|
const result = [];
|
|
@@ -277,6 +314,14 @@ function toArray(array) {
|
|
|
277
314
|
return Array.isArray(array) ? array : [array];
|
|
278
315
|
}
|
|
279
316
|
|
|
317
|
+
// src/array/arrayable.ts
|
|
318
|
+
function flattenArrayable(array) {
|
|
319
|
+
return toArray(array).flat(1);
|
|
320
|
+
}
|
|
321
|
+
function mergeArrayable(...args) {
|
|
322
|
+
return args.flatMap((i) => toArray(i));
|
|
323
|
+
}
|
|
324
|
+
|
|
280
325
|
// src/array/isArrayEqual.ts
|
|
281
326
|
function isArrayEqual(array1, array2) {
|
|
282
327
|
if (array1.length !== array2.length) {
|
|
@@ -352,8 +397,40 @@ function pick(object, keys) {
|
|
|
352
397
|
})
|
|
353
398
|
);
|
|
354
399
|
}
|
|
400
|
+
|
|
401
|
+
// src/object/isPlainObject.ts
|
|
402
|
+
function isPlainObject(value) {
|
|
403
|
+
if (!isObject(value)) return false;
|
|
404
|
+
const prototype = Object.getPrototypeOf(value);
|
|
405
|
+
return (prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null) && !(Symbol.toStringTag in value) && !(Symbol.iterator in value);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// src/object/sortObject.ts
|
|
409
|
+
function sortObject(obj, options = {}) {
|
|
410
|
+
const { compareFn = (a, b) => a.localeCompare(b) } = options;
|
|
411
|
+
function sortKeys(obj2) {
|
|
412
|
+
const sortedKeys = Object.keys(obj2).sort(compareFn);
|
|
413
|
+
const result = {};
|
|
414
|
+
for (const key of sortedKeys) {
|
|
415
|
+
const value = obj2[key];
|
|
416
|
+
let newValue;
|
|
417
|
+
if (options.deep && isPlainObject(value)) {
|
|
418
|
+
newValue = sortKeys(value);
|
|
419
|
+
} else {
|
|
420
|
+
newValue = value;
|
|
421
|
+
}
|
|
422
|
+
Object.defineProperty(result, key, {
|
|
423
|
+
...Object.getOwnPropertyDescriptor(obj2, key),
|
|
424
|
+
value: newValue
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
return result;
|
|
428
|
+
}
|
|
429
|
+
return sortKeys(obj);
|
|
430
|
+
}
|
|
355
431
|
export {
|
|
356
432
|
NOOP,
|
|
433
|
+
at,
|
|
357
434
|
cAF,
|
|
358
435
|
camelCase,
|
|
359
436
|
capitalize,
|
|
@@ -364,6 +441,7 @@ export {
|
|
|
364
441
|
ensurePrefix,
|
|
365
442
|
ensureSuffix,
|
|
366
443
|
flatCase,
|
|
444
|
+
flattenArrayable,
|
|
367
445
|
getObjectType,
|
|
368
446
|
hasOwn,
|
|
369
447
|
hours,
|
|
@@ -371,18 +449,28 @@ export {
|
|
|
371
449
|
isArrayEqual,
|
|
372
450
|
isBoolean,
|
|
373
451
|
isBrowser,
|
|
452
|
+
isEmptyString,
|
|
453
|
+
isEmptyStringOrWhitespace,
|
|
374
454
|
isFunction,
|
|
375
455
|
isInteger,
|
|
376
456
|
isNativePromise,
|
|
457
|
+
isNil,
|
|
377
458
|
isNull,
|
|
378
459
|
isNumber,
|
|
460
|
+
isNumbericString,
|
|
461
|
+
isObject,
|
|
379
462
|
isPromise,
|
|
463
|
+
isRegExp,
|
|
464
|
+
isSet,
|
|
380
465
|
isString,
|
|
381
466
|
isUndefined,
|
|
382
467
|
isUppercase,
|
|
468
|
+
isWhitespaceString,
|
|
383
469
|
join,
|
|
384
470
|
kebabCase,
|
|
471
|
+
last,
|
|
385
472
|
lowerFirst,
|
|
473
|
+
mergeArrayable,
|
|
386
474
|
minutes,
|
|
387
475
|
noop,
|
|
388
476
|
omit,
|
|
@@ -393,6 +481,7 @@ export {
|
|
|
393
481
|
seconds,
|
|
394
482
|
slash,
|
|
395
483
|
snakeCase,
|
|
484
|
+
sortObject,
|
|
396
485
|
splitByCase,
|
|
397
486
|
throttle,
|
|
398
487
|
titleCase,
|
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.3.0",
|
|
5
5
|
"description": "Common used utils.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"utils"
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"email": "ntnyq13@gmail.com"
|
|
13
13
|
},
|
|
14
14
|
"homepage": "https://github.com/ntnyq/utils#readme",
|
|
15
|
+
"repository": "ntnyq/utils",
|
|
15
16
|
"bugs": {
|
|
16
17
|
"url": "https://github.com/ntnyq/utils/issues"
|
|
17
18
|
},
|
|
18
|
-
"repository": "ntnyq/utils",
|
|
19
19
|
"exports": {
|
|
20
20
|
"./package.json": "./package.json",
|
|
21
21
|
".": {
|
|
@@ -40,19 +40,18 @@
|
|
|
40
40
|
"scule": "^1.3.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@ntnyq/eslint-config": "^3.
|
|
43
|
+
"@ntnyq/eslint-config": "^3.2.2",
|
|
44
44
|
"@ntnyq/prettier-config": "^1.21.3",
|
|
45
|
-
"@vitest/coverage-v8": "^2.1.
|
|
46
|
-
"bumpp": "^9.
|
|
47
|
-
"eslint": "^9.
|
|
45
|
+
"@vitest/coverage-v8": "^2.1.5",
|
|
46
|
+
"bumpp": "^9.8.1",
|
|
47
|
+
"eslint": "^9.14.0",
|
|
48
48
|
"husky": "^9.1.6",
|
|
49
49
|
"nano-staged": "^0.8.0",
|
|
50
|
-
"npm-run-all2": "^
|
|
51
|
-
"pnpm": "^9.12.1",
|
|
50
|
+
"npm-run-all2": "^7.0.1",
|
|
52
51
|
"prettier": "^3.3.3",
|
|
53
|
-
"tsup": "^8.3.
|
|
52
|
+
"tsup": "^8.3.5",
|
|
54
53
|
"typescript": "^5.6.3",
|
|
55
|
-
"vitest": "^2.1.
|
|
54
|
+
"vitest": "^2.1.5"
|
|
56
55
|
},
|
|
57
56
|
"engines": {
|
|
58
57
|
"node": ">=18.18.0"
|