@ntnyq/utils 0.10.0 → 0.11.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.ts +53 -9
- package/dist/index.js +143 -79
- package/package.json +15 -18
package/dist/index.d.ts
CHANGED
|
@@ -29,9 +29,6 @@ declare function once<T extends unknown[]>(func: (...args: T) => void): (this: u
|
|
|
29
29
|
//#endregion
|
|
30
30
|
//#region src/is/dom.d.ts
|
|
31
31
|
/**
|
|
32
|
-
* @file is/dom.ts
|
|
33
|
-
*/
|
|
34
|
-
/**
|
|
35
32
|
* Check if given value is an HTMLElement
|
|
36
33
|
* @param value - The value to check
|
|
37
34
|
* @returns True if the value is an HTMLElement, false otherwise
|
|
@@ -206,16 +203,13 @@ declare function isElementVisibleInViewport(element: HTMLElement, targetWindow?:
|
|
|
206
203
|
//#endregion
|
|
207
204
|
//#region src/env/isBrowser.d.ts
|
|
208
205
|
/**
|
|
209
|
-
* @file env.ts
|
|
210
|
-
*/
|
|
211
|
-
/**
|
|
212
206
|
* Checks if the code is running in a browser
|
|
213
207
|
*
|
|
214
|
-
* @returns
|
|
208
|
+
* @returns true if the code is running in a browser
|
|
215
209
|
*/
|
|
216
210
|
declare function isBrowser(): boolean;
|
|
217
211
|
//#endregion
|
|
218
|
-
//#region src/file/
|
|
212
|
+
//#region src/file/extension.d.ts
|
|
219
213
|
/**
|
|
220
214
|
* Removes the file extension from a filename.
|
|
221
215
|
*
|
|
@@ -223,6 +217,12 @@ declare function isBrowser(): boolean;
|
|
|
223
217
|
* @returns The filename without the extension.
|
|
224
218
|
*/
|
|
225
219
|
declare function removeFileExtension(filename: string): string;
|
|
220
|
+
/**
|
|
221
|
+
* Gets the file extension from a filename.
|
|
222
|
+
* @param filePath - The filePath to get the extension from.
|
|
223
|
+
* @returns The file extension, or undefined if there is none.
|
|
224
|
+
*/
|
|
225
|
+
declare function getFileExtension(filePath?: string): string | undefined;
|
|
226
226
|
//#endregion
|
|
227
227
|
//#region src/html/escape.d.ts
|
|
228
228
|
/**
|
|
@@ -450,6 +450,15 @@ declare function last<T>(array: readonly T[]): T | undefined;
|
|
|
450
450
|
*/
|
|
451
451
|
declare function chunk<T>(array: T[], size: number): T[][];
|
|
452
452
|
//#endregion
|
|
453
|
+
//#region src/array/remove.d.ts
|
|
454
|
+
/**
|
|
455
|
+
* Remove given item from an array
|
|
456
|
+
* @param array - given array
|
|
457
|
+
* @param value - item to be removed
|
|
458
|
+
* @returns true if item was removed, otherwise false
|
|
459
|
+
*/
|
|
460
|
+
declare function remove<T>(array: T[], value: T): boolean;
|
|
461
|
+
//#endregion
|
|
453
462
|
//#region src/array/unique.d.ts
|
|
454
463
|
/**
|
|
455
464
|
* Returns a new array with unique values.
|
|
@@ -465,6 +474,15 @@ declare function unique<T>(array: T[]): T[];
|
|
|
465
474
|
*/
|
|
466
475
|
declare function uniqueBy<T>(array: T[], equalFn: (a: T, b: T) => boolean): T[];
|
|
467
476
|
//#endregion
|
|
477
|
+
//#region src/array/shuffle.d.ts
|
|
478
|
+
/**
|
|
479
|
+
* Fisher–Yates shuffle
|
|
480
|
+
*
|
|
481
|
+
* @param array - array to shuffle
|
|
482
|
+
* @returns shuffled array
|
|
483
|
+
*/
|
|
484
|
+
declare function shuffle<T>(array: T[]): T[];
|
|
485
|
+
//#endregion
|
|
468
486
|
//#region src/array/toArray.d.ts
|
|
469
487
|
/**
|
|
470
488
|
* Converts a value to an array.
|
|
@@ -744,6 +762,17 @@ declare function cleanObject<T extends object>(obj: T, options?: CleanObjectOpti
|
|
|
744
762
|
*/
|
|
745
763
|
declare function hasOwn<T>(object: T, key: PropertyKey): boolean;
|
|
746
764
|
//#endregion
|
|
765
|
+
//#region src/object/isKeyOf.d.ts
|
|
766
|
+
/**
|
|
767
|
+
* Type guard for any key, `k`
|
|
768
|
+
* marks `k` as a key of `T` if `k` is a key of `T`
|
|
769
|
+
*
|
|
770
|
+
* @param obj - object to query for key
|
|
771
|
+
* @param k - key to check for
|
|
772
|
+
* @returns true if `k` is a key of `T`
|
|
773
|
+
*/
|
|
774
|
+
declare function isKeyOf<T extends object>(obj: T, k: keyof T): k is keyof T;
|
|
775
|
+
//#endregion
|
|
747
776
|
//#region src/object/sortObject.d.ts
|
|
748
777
|
interface SortObjectOptions {
|
|
749
778
|
/**
|
|
@@ -761,6 +790,15 @@ interface SortObjectOptions {
|
|
|
761
790
|
*/
|
|
762
791
|
declare function sortObject<T extends Record<string, any>>(obj: T, options?: SortObjectOptions): T;
|
|
763
792
|
//#endregion
|
|
793
|
+
//#region src/object/isPlainObject.d.ts
|
|
794
|
+
/**
|
|
795
|
+
* Check if a value is a plain object (not an array, Date, RegExp, Map, Set, etc.)
|
|
796
|
+
*
|
|
797
|
+
* @param value - Checked value
|
|
798
|
+
* @copyright {@link https://github.com/sindresorhus/is/blob/main/source/index.ts}
|
|
799
|
+
*/
|
|
800
|
+
declare function isPlainObject<Value = unknown>(value: unknown): value is Record<PropertyKey, Value>;
|
|
801
|
+
//#endregion
|
|
764
802
|
//#region src/string/pad.d.ts
|
|
765
803
|
interface CreatePadStringOptions {
|
|
766
804
|
length: number;
|
|
@@ -790,6 +828,12 @@ declare function join(array: JoinableValue[], options?: JoinOptions): string;
|
|
|
790
828
|
*/
|
|
791
829
|
declare function slash(input: string): string;
|
|
792
830
|
//#endregion
|
|
831
|
+
//#region src/string/escape.d.ts
|
|
832
|
+
/**
|
|
833
|
+
* @copyright {@link https://github.com/sindresorhus/escape-string-regexp}
|
|
834
|
+
*/
|
|
835
|
+
declare function escapeStringRegexp(value: string): string;
|
|
836
|
+
//#endregion
|
|
793
837
|
//#region src/string/random.d.ts
|
|
794
838
|
/**
|
|
795
839
|
* randome a string useing given chars
|
|
@@ -903,4 +947,4 @@ declare const RE_LINE_COMMENT: RegExp;
|
|
|
903
947
|
*/
|
|
904
948
|
declare const RE_BLOCK_COMMENT: RegExp;
|
|
905
949
|
//#endregion
|
|
906
|
-
export { AnyFn, Arrayable, Awaitable, Callable, CleanObjectOptions, Color, CreatePadStringOptions, DeepRequired, ElementOf, GetStringSimilarityOptions, InteropModuleDefault, JsonArray, JsonObject, JsonPrimitive, JsonValue, LiteralUnion, MayBe, Merge, NOOP, NonEmptyObject, NonEmptyString, Nullable, OpenExternalURLOptions, Overwrite, Prettify, PrettifyV2, Primitive, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, RamdomNumberOptions, ResolvedOptions, SPECIAL_CHAR, STORAGE_UNITS, SortObjectOptions, StorageUnit, TIME_UNITS, ThrottleDebounceOptions, TimeUnit, ToIntegerOptions, UrlString, ValueOf, Whitespace, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertFromMilliseconds, convertStorageUnit, convertTimeUnit, convertToBytes, convertToMilliseconds, createPadString, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBlob, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFile, isFormData, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isUrlString, isWhitespaceString, isZero, join, last, mergeArrayable, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, removeFileExtension, resolveSubOptions, scrollElementIntoView, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce };
|
|
950
|
+
export { AnyFn, Arrayable, Awaitable, Callable, CleanObjectOptions, Color, CreatePadStringOptions, DeepRequired, ElementOf, GetStringSimilarityOptions, InteropModuleDefault, JsonArray, JsonObject, JsonPrimitive, JsonValue, LiteralUnion, MayBe, Merge, NOOP, NonEmptyObject, NonEmptyString, Nullable, OpenExternalURLOptions, Overwrite, Prettify, PrettifyV2, Primitive, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, RamdomNumberOptions, ResolvedOptions, SPECIAL_CHAR, STORAGE_UNITS, SortObjectOptions, StorageUnit, TIME_UNITS, ThrottleDebounceOptions, TimeUnit, ToIntegerOptions, UrlString, ValueOf, Whitespace, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertFromMilliseconds, convertStorageUnit, convertTimeUnit, convertToBytes, convertToMilliseconds, createPadString, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, escapeStringRegexp, flattenArrayable, getFileExtension, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBlob, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFile, isFormData, isFunction, isHTMLElement, isInteger, isIterable, isKeyOf, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPlainObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isUrlString, isWhitespaceString, isZero, join, last, mergeArrayable, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, remove, removeFileExtension, resolveSubOptions, scrollElementIntoView, shuffle, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce };
|
package/dist/index.js
CHANGED
|
@@ -36,6 +36,17 @@ function once(func) {
|
|
|
36
36
|
};
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/env/isBrowser.ts
|
|
41
|
+
/**
|
|
42
|
+
* Checks if the code is running in a browser
|
|
43
|
+
*
|
|
44
|
+
* @returns true if the code is running in a browser
|
|
45
|
+
*/
|
|
46
|
+
function isBrowser() {
|
|
47
|
+
return typeof document !== "undefined" && typeof window !== "undefined" && typeof navigator !== "undefined" && window === self;
|
|
48
|
+
}
|
|
49
|
+
|
|
39
50
|
//#endregion
|
|
40
51
|
//#region src/is/dom.ts
|
|
41
52
|
/**
|
|
@@ -47,6 +58,7 @@ function once(func) {
|
|
|
47
58
|
* @returns True if the value is an HTMLElement, false otherwise
|
|
48
59
|
*/
|
|
49
60
|
function isHTMLElement(value) {
|
|
61
|
+
if (!isBrowser()) return false;
|
|
50
62
|
return typeof value === "object" && value !== null && "nodeType" in value && value.nodeType === Node.ELEMENT_NODE && value instanceof HTMLElement;
|
|
51
63
|
}
|
|
52
64
|
|
|
@@ -241,21 +253,7 @@ function isElementVisibleInViewport(element, targetWindow = window) {
|
|
|
241
253
|
}
|
|
242
254
|
|
|
243
255
|
//#endregion
|
|
244
|
-
//#region src/
|
|
245
|
-
/**
|
|
246
|
-
* @file env.ts
|
|
247
|
-
*/
|
|
248
|
-
/**
|
|
249
|
-
* Checks if the code is running in a browser
|
|
250
|
-
*
|
|
251
|
-
* @returns boolean - true if the code is running in a browser
|
|
252
|
-
*/
|
|
253
|
-
function isBrowser() {
|
|
254
|
-
return typeof document !== "undefined";
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
//#endregion
|
|
258
|
-
//#region src/file/removeExtension.ts
|
|
256
|
+
//#region src/file/extension.ts
|
|
259
257
|
/**
|
|
260
258
|
* Removes the file extension from a filename.
|
|
261
259
|
*
|
|
@@ -265,6 +263,15 @@ function isBrowser() {
|
|
|
265
263
|
function removeFileExtension(filename) {
|
|
266
264
|
return filename.replace(/\.[^/.]+$/, "");
|
|
267
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* Gets the file extension from a filename.
|
|
268
|
+
* @param filePath - The filePath to get the extension from.
|
|
269
|
+
* @returns The file extension, or undefined if there is none.
|
|
270
|
+
*/
|
|
271
|
+
function getFileExtension(filePath) {
|
|
272
|
+
if (!filePath) return;
|
|
273
|
+
return filePath.match(/\.([^.]+)$/)?.[1];
|
|
274
|
+
}
|
|
268
275
|
|
|
269
276
|
//#endregion
|
|
270
277
|
//#region src/html/escape.ts
|
|
@@ -600,6 +607,24 @@ function chunk(array, size) {
|
|
|
600
607
|
return result;
|
|
601
608
|
}
|
|
602
609
|
|
|
610
|
+
//#endregion
|
|
611
|
+
//#region src/array/remove.ts
|
|
612
|
+
/**
|
|
613
|
+
* Remove given item from an array
|
|
614
|
+
* @param array - given array
|
|
615
|
+
* @param value - item to be removed
|
|
616
|
+
* @returns true if item was removed, otherwise false
|
|
617
|
+
*/
|
|
618
|
+
function remove(array, value) {
|
|
619
|
+
if (!array) return false;
|
|
620
|
+
const index = array.indexOf(value);
|
|
621
|
+
if (index !== -1) {
|
|
622
|
+
array.splice(index, 1);
|
|
623
|
+
return true;
|
|
624
|
+
}
|
|
625
|
+
return false;
|
|
626
|
+
}
|
|
627
|
+
|
|
603
628
|
//#endregion
|
|
604
629
|
//#region src/array/unique.ts
|
|
605
630
|
/**
|
|
@@ -623,6 +648,89 @@ function uniqueBy(array, equalFn) {
|
|
|
623
648
|
}, []);
|
|
624
649
|
}
|
|
625
650
|
|
|
651
|
+
//#endregion
|
|
652
|
+
//#region src/number/random.ts
|
|
653
|
+
/**
|
|
654
|
+
* random an integer by given range
|
|
655
|
+
*
|
|
656
|
+
* @param min - min value
|
|
657
|
+
* @param max - max value
|
|
658
|
+
* @returns random integer in range
|
|
659
|
+
*/
|
|
660
|
+
function randomNumber(min, max = 0, options = {}) {
|
|
661
|
+
if (max === 0) {
|
|
662
|
+
max = min;
|
|
663
|
+
min = 0;
|
|
664
|
+
}
|
|
665
|
+
if (min > max) [min, max] = [max, min];
|
|
666
|
+
return Math.trunc(Math.random() * (max - min + (options.includeMax ? 1 : 0)) + min);
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
//#endregion
|
|
670
|
+
//#region src/number/toInteger.ts
|
|
671
|
+
/**
|
|
672
|
+
* Transforms a value to an integer.
|
|
673
|
+
* @param value - The value to convert to an integer.
|
|
674
|
+
* @param options - Options for the conversion.
|
|
675
|
+
* @returns The converted integer.
|
|
676
|
+
*/
|
|
677
|
+
function toInteger(value, options = {}) {
|
|
678
|
+
const { defaultValue = 0, allowDecimal = false, allowNaN = false, onError = "useDefault", min, max, outOfRange = "clamp" } = options;
|
|
679
|
+
let numberValue;
|
|
680
|
+
let result;
|
|
681
|
+
if (isNumber(value)) numberValue = value;
|
|
682
|
+
else if (isString(value)) {
|
|
683
|
+
const trimmed = value.trim();
|
|
684
|
+
if (isEmptyString(trimmed)) {
|
|
685
|
+
if (onError === "throwError") throw new TypeError("Cannot convert empty string to an integer");
|
|
686
|
+
return onError === "returnOriginal" ? value : defaultValue;
|
|
687
|
+
}
|
|
688
|
+
numberValue = Number(trimmed);
|
|
689
|
+
} else if (isNullOrUndefined(value)) {
|
|
690
|
+
if (onError === "throwError") throw new TypeError(`Cannot convert ${value} to an integer`);
|
|
691
|
+
return onError === "useDefault" ? value : defaultValue;
|
|
692
|
+
} else numberValue = Number(value);
|
|
693
|
+
if (isNaN(numberValue)) {
|
|
694
|
+
if (allowNaN) return numberValue;
|
|
695
|
+
if (onError === "throwError") throw new TypeError(`Cannot convert NaN to an integer`);
|
|
696
|
+
return onError === "returnOriginal" ? value : defaultValue;
|
|
697
|
+
}
|
|
698
|
+
if (allowDecimal) result = numberValue > 0 ? Math.floor(numberValue) : Math.ceil(numberValue);
|
|
699
|
+
else {
|
|
700
|
+
if (numberValue % 1 !== 0) {
|
|
701
|
+
if (onError === "throwError") throw new Error("Decimal values are not allowed");
|
|
702
|
+
return onError === "returnOriginal" ? value : defaultValue;
|
|
703
|
+
}
|
|
704
|
+
result = numberValue;
|
|
705
|
+
}
|
|
706
|
+
if (!isUndefined(min) || !isUndefined(max)) {
|
|
707
|
+
const minVal = min ?? -Infinity;
|
|
708
|
+
const maxVal = max ?? Infinity;
|
|
709
|
+
if (result < minVal || result > maxVal) {
|
|
710
|
+
if (outOfRange === "throwError") throw new RangeError(`Value ${result} is out of range [${minVal}, ${maxVal}]`);
|
|
711
|
+
if (outOfRange === "useDefault") return defaultValue;
|
|
712
|
+
if (outOfRange === "clamp") result = Math.max(minVal, Math.min(maxVal, numberValue));
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return result;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
//#endregion
|
|
719
|
+
//#region src/array/shuffle.ts
|
|
720
|
+
/**
|
|
721
|
+
* Fisher–Yates shuffle
|
|
722
|
+
*
|
|
723
|
+
* @param array - array to shuffle
|
|
724
|
+
* @returns shuffled array
|
|
725
|
+
*/
|
|
726
|
+
function shuffle(array) {
|
|
727
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
728
|
+
const j = randomNumber(0, i, { includeMax: true });
|
|
729
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
730
|
+
}
|
|
731
|
+
return array;
|
|
732
|
+
}
|
|
733
|
+
|
|
626
734
|
//#endregion
|
|
627
735
|
//#region src/array/toArray.ts
|
|
628
736
|
/**
|
|
@@ -709,70 +817,12 @@ function slash(input) {
|
|
|
709
817
|
}
|
|
710
818
|
|
|
711
819
|
//#endregion
|
|
712
|
-
//#region src/
|
|
820
|
+
//#region src/string/escape.ts
|
|
713
821
|
/**
|
|
714
|
-
*
|
|
715
|
-
*
|
|
716
|
-
* @param min - min value
|
|
717
|
-
* @param max - max value
|
|
718
|
-
* @returns random integer in range
|
|
822
|
+
* @copyright {@link https://github.com/sindresorhus/escape-string-regexp}
|
|
719
823
|
*/
|
|
720
|
-
function
|
|
721
|
-
|
|
722
|
-
max = min;
|
|
723
|
-
min = 0;
|
|
724
|
-
}
|
|
725
|
-
if (min > max) [min, max] = [max, min];
|
|
726
|
-
return Math.trunc(Math.random() * (max - min + (options.includeMax ? 1 : 0)) + min);
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
//#endregion
|
|
730
|
-
//#region src/number/toInteger.ts
|
|
731
|
-
/**
|
|
732
|
-
* Transforms a value to an integer.
|
|
733
|
-
* @param value - The value to convert to an integer.
|
|
734
|
-
* @param options - Options for the conversion.
|
|
735
|
-
* @returns The converted integer.
|
|
736
|
-
*/
|
|
737
|
-
function toInteger(value, options = {}) {
|
|
738
|
-
const { defaultValue = 0, allowDecimal = false, allowNaN = false, onError = "useDefault", min, max, outOfRange = "clamp" } = options;
|
|
739
|
-
let numberValue;
|
|
740
|
-
let result;
|
|
741
|
-
if (isNumber(value)) numberValue = value;
|
|
742
|
-
else if (isString(value)) {
|
|
743
|
-
const trimmed = value.trim();
|
|
744
|
-
if (isEmptyString(trimmed)) {
|
|
745
|
-
if (onError === "throwError") throw new TypeError("Cannot convert empty string to an integer");
|
|
746
|
-
return onError === "returnOriginal" ? value : defaultValue;
|
|
747
|
-
}
|
|
748
|
-
numberValue = Number(trimmed);
|
|
749
|
-
} else if (isNullOrUndefined(value)) {
|
|
750
|
-
if (onError === "throwError") throw new TypeError(`Cannot convert ${value} to an integer`);
|
|
751
|
-
return onError === "useDefault" ? value : defaultValue;
|
|
752
|
-
} else numberValue = Number(value);
|
|
753
|
-
if (isNaN(numberValue)) {
|
|
754
|
-
if (allowNaN) return numberValue;
|
|
755
|
-
if (onError === "throwError") throw new TypeError(`Cannot convert NaN to an integer`);
|
|
756
|
-
return onError === "returnOriginal" ? value : defaultValue;
|
|
757
|
-
}
|
|
758
|
-
if (allowDecimal) result = numberValue > 0 ? Math.floor(numberValue) : Math.ceil(numberValue);
|
|
759
|
-
else {
|
|
760
|
-
if (numberValue % 1 !== 0) {
|
|
761
|
-
if (onError === "throwError") throw new Error("Decimal values are not allowed");
|
|
762
|
-
return onError === "returnOriginal" ? value : defaultValue;
|
|
763
|
-
}
|
|
764
|
-
result = numberValue;
|
|
765
|
-
}
|
|
766
|
-
if (!isUndefined(min) || !isUndefined(max)) {
|
|
767
|
-
const minVal = min ?? -Infinity;
|
|
768
|
-
const maxVal = max ?? Infinity;
|
|
769
|
-
if (result < minVal || result > maxVal) {
|
|
770
|
-
if (outOfRange === "throwError") throw new RangeError(`Value ${result} is out of range [${minVal}, ${maxVal}]`);
|
|
771
|
-
if (outOfRange === "useDefault") return defaultValue;
|
|
772
|
-
if (outOfRange === "clamp") result = Math.max(minVal, Math.min(maxVal, numberValue));
|
|
773
|
-
}
|
|
774
|
-
}
|
|
775
|
-
return result;
|
|
824
|
+
function escapeStringRegexp(value) {
|
|
825
|
+
return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&").replace(/-/g, "\\x2d");
|
|
776
826
|
}
|
|
777
827
|
|
|
778
828
|
//#endregion
|
|
@@ -1123,7 +1173,7 @@ function cleanObject(obj, options = {}) {
|
|
|
1123
1173
|
if (cleanUndefined && isUndefined(v)) delete obj[key];
|
|
1124
1174
|
if (cleanNull && isNull(v)) delete obj[key];
|
|
1125
1175
|
if (cleanZero && isZero(v)) delete obj[key];
|
|
1126
|
-
if (cleanNaN &&
|
|
1176
|
+
if (cleanNaN && isNaN(v)) delete obj[key];
|
|
1127
1177
|
if (cleanEmptyString && isEmptyString(v)) delete obj[key];
|
|
1128
1178
|
if (cleanEmptyArray && isEmptyArray(v)) delete obj[key];
|
|
1129
1179
|
if (cleanEmptyObject && isEmptyObject(v)) delete obj[key];
|
|
@@ -1132,6 +1182,20 @@ function cleanObject(obj, options = {}) {
|
|
|
1132
1182
|
return obj;
|
|
1133
1183
|
}
|
|
1134
1184
|
|
|
1185
|
+
//#endregion
|
|
1186
|
+
//#region src/object/isKeyOf.ts
|
|
1187
|
+
/**
|
|
1188
|
+
* Type guard for any key, `k`
|
|
1189
|
+
* marks `k` as a key of `T` if `k` is a key of `T`
|
|
1190
|
+
*
|
|
1191
|
+
* @param obj - object to query for key
|
|
1192
|
+
* @param k - key to check for
|
|
1193
|
+
* @returns true if `k` is a key of `T`
|
|
1194
|
+
*/
|
|
1195
|
+
function isKeyOf(obj, k) {
|
|
1196
|
+
return k in obj;
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1135
1199
|
//#endregion
|
|
1136
1200
|
//#region src/object/isPlainObject.ts
|
|
1137
1201
|
/**
|
|
@@ -1203,4 +1267,4 @@ const RE_LINE_COMMENT = /\/\/.*/;
|
|
|
1203
1267
|
const RE_BLOCK_COMMENT = /\/\*[\s\S]*?\*\//g;
|
|
1204
1268
|
|
|
1205
1269
|
//#endregion
|
|
1206
|
-
export { Color, NOOP, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, SPECIAL_CHAR, STORAGE_UNITS, TIME_UNITS, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertFromMilliseconds, convertStorageUnit, convertTimeUnit, convertToBytes, convertToMilliseconds, createPadString, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, flattenArrayable, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBlob, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFile, isFormData, isFunction, isHTMLElement, isInteger, isIterable, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isUrlString, isWhitespaceString, isZero, join, last, mergeArrayable, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, removeFileExtension, resolveSubOptions, scrollElementIntoView, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce };
|
|
1270
|
+
export { Color, NOOP, RE_BLOCK_COMMENT, RE_COMMENTS, RE_LINE_COMMENT, SPECIAL_CHAR, STORAGE_UNITS, TIME_UNITS, at, cAF, chunk, clamp, cleanObject, convertFromBytes, convertFromMilliseconds, convertStorageUnit, convertTimeUnit, convertToBytes, convertToMilliseconds, createPadString, debounce, enhance, ensurePrefix, ensureSuffix, escapeHTML, escapeStringRegexp, flattenArrayable, getFileExtension, getObjectType, getRoot, getStringLength, getStringSimilarity, hasOwn, interopDefault, intersect, isArray, isArrayEqual, isBigInt, isBlob, isBoolean, isBrowser, isDeepEqual, isElementVisibleInViewport, isEmptyArray, isEmptyMap, isEmptyObject, isEmptySet, isEmptyString, isEmptyStringOrWhitespace, isError, isFile, isFormData, isFunction, isHTMLElement, isInteger, isIterable, isKeyOf, isMap, isNaN, isNativePromise, isNil, isNonEmptyArray, isNonEmptyString, isNull, isNullOrUndefined, isNumber, isNumbericString, isObject, isPlainObject, isPromise, isRegExp, isSet, isString, isTruthy, isUndefined, isUrlString, isWhitespaceString, isZero, join, last, mergeArrayable, noop, omit, once, openExternalURL, pick, rAF, randomHexColor, randomNumber, randomRGBAColor, randomRGBColor, randomString, remove, removeFileExtension, resolveSubOptions, scrollElementIntoView, shuffle, slash, slugify, sortObject, throttle, toArray, toInteger, unescapeHTML, unindent, unique, uniqueBy, waitFor, warnOnce };
|
package/package.json
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ntnyq/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@10.26.2",
|
|
4
|
+
"version": "0.11.0",
|
|
6
5
|
"description": "Common used utils.",
|
|
7
6
|
"keywords": [
|
|
8
7
|
"utils"
|
|
@@ -30,22 +29,10 @@
|
|
|
30
29
|
"dist"
|
|
31
30
|
],
|
|
32
31
|
"sideEffects": false,
|
|
33
|
-
"scripts": {
|
|
34
|
-
"build": "tsdown",
|
|
35
|
-
"dev": "tsdown --watch src",
|
|
36
|
-
"lint": "eslint",
|
|
37
|
-
"prepare": "husky",
|
|
38
|
-
"prepublishOnly": "pnpm run build",
|
|
39
|
-
"release": "run-s release:check release:version",
|
|
40
|
-
"release:check": "run-s lint typecheck test",
|
|
41
|
-
"release:version": "bumpp",
|
|
42
|
-
"test": "vitest",
|
|
43
|
-
"typecheck": "tsc --noEmit"
|
|
44
|
-
},
|
|
45
32
|
"devDependencies": {
|
|
46
|
-
"@ntnyq/eslint-config": "^5.
|
|
33
|
+
"@ntnyq/eslint-config": "^5.9.0",
|
|
47
34
|
"@ntnyq/prettier-config": "^3.0.1",
|
|
48
|
-
"@ntnyq/tsconfig": "^3.
|
|
35
|
+
"@ntnyq/tsconfig": "^3.1.0",
|
|
49
36
|
"bumpp": "^10.3.2",
|
|
50
37
|
"eslint": "^9.39.2",
|
|
51
38
|
"husky": "^9.1.7",
|
|
@@ -54,12 +41,22 @@
|
|
|
54
41
|
"prettier": "^3.7.4",
|
|
55
42
|
"tsdown": "^0.18.3",
|
|
56
43
|
"typescript": "^5.9.3",
|
|
57
|
-
"vitest": "^4.0.
|
|
44
|
+
"vitest": "^4.0.16"
|
|
58
45
|
},
|
|
59
46
|
"engines": {
|
|
60
47
|
"node": "^20.19.0 || ^22.13.0 || >=24"
|
|
61
48
|
},
|
|
62
49
|
"nano-staged": {
|
|
63
50
|
"*.{js,ts,mjs,cjs,md,yml,yaml,toml,json}": "eslint --fix"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"build": "tsdown",
|
|
54
|
+
"dev": "tsdown --watch src",
|
|
55
|
+
"lint": "eslint",
|
|
56
|
+
"release": "run-s release:check release:version",
|
|
57
|
+
"release:check": "run-s lint typecheck test",
|
|
58
|
+
"release:version": "bumpp",
|
|
59
|
+
"test": "vitest",
|
|
60
|
+
"typecheck": "tsc --noEmit"
|
|
64
61
|
}
|
|
65
|
-
}
|
|
62
|
+
}
|