@ntnyq/utils 0.9.2 → 0.10.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 +44 -5
- package/dist/index.js +40 -14
- package/package.json +12 -11
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,22 @@ declare function noop(): void;
|
|
|
9
9
|
declare const NOOP: typeof noop;
|
|
10
10
|
//#endregion
|
|
11
11
|
//#region src/fn/once.d.ts
|
|
12
|
+
/**
|
|
13
|
+
* Creates a function that is restricted to invoking `func` once. Repeat calls to the function return `false`.
|
|
14
|
+
*
|
|
15
|
+
* @param func - The function to restrict.
|
|
16
|
+
* @returns A new function that returns `true` when `func` is invoked for the first time and `false` on subsequent calls.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
*
|
|
20
|
+
* ```ts
|
|
21
|
+
* const initialize = once(() => {
|
|
22
|
+
* console.log('Initialized')
|
|
23
|
+
* })
|
|
24
|
+
*
|
|
25
|
+
* initialize() // Logs: 'Initialized', returns true
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
12
28
|
declare function once<T extends unknown[]>(func: (...args: T) => void): (this: unknown, ...args: T) => boolean;
|
|
13
29
|
//#endregion
|
|
14
30
|
//#region src/is/dom.d.ts
|
|
@@ -104,7 +120,7 @@ type Nullable<T> = T | null;
|
|
|
104
120
|
/**
|
|
105
121
|
* Overwrite some keys type
|
|
106
122
|
*/
|
|
107
|
-
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
|
|
123
|
+
type Overwrite<T, U$1> = Pick<T, Exclude<keyof T, keyof U$1>> & U$1;
|
|
108
124
|
/**
|
|
109
125
|
* Prettify object type
|
|
110
126
|
*/
|
|
@@ -136,7 +152,7 @@ type LiteralUnion<Union extends Base, Base = string> = Union | (Base & {
|
|
|
136
152
|
/**
|
|
137
153
|
* @see {@link TODO:}
|
|
138
154
|
*/
|
|
139
|
-
type Merge<T, U> = keyof T & keyof U extends never ? T & U : Omit<T, keyof T & keyof U> & U;
|
|
155
|
+
type Merge<T, U$1> = keyof T & keyof U$1 extends never ? T & U$1 : Omit<T, keyof T & keyof U$1> & U$1;
|
|
140
156
|
/**
|
|
141
157
|
* Non empty object `{}`
|
|
142
158
|
*/
|
|
@@ -593,7 +609,7 @@ declare function interopDefault<T>(mod: Awaitable<T>): Promise<InteropModuleDefa
|
|
|
593
609
|
* // => {}
|
|
594
610
|
* ```
|
|
595
611
|
*/
|
|
596
|
-
declare function resolveSubOptions<T extends Record<string, any>, K extends keyof T>(options: T, key: K): Partial<ResolvedOptions<T[K]>>;
|
|
612
|
+
declare function resolveSubOptions<T extends Record<string, any>, K$1 extends keyof T>(options: T, key: K$1): Partial<ResolvedOptions<T[K$1]>>;
|
|
597
613
|
//#endregion
|
|
598
614
|
//#region src/number/random.d.ts
|
|
599
615
|
interface RamdomNumberOptions {
|
|
@@ -655,10 +671,10 @@ interface ToIntegerOptions {
|
|
|
655
671
|
declare function toInteger(value: unknown, options?: ToIntegerOptions): number;
|
|
656
672
|
//#endregion
|
|
657
673
|
//#region src/object/omit.d.ts
|
|
658
|
-
declare function omit<T, K extends keyof T>(object: T, ...keys: K[]): Omit<T, K>;
|
|
674
|
+
declare function omit<T, K$1 extends keyof T>(object: T, ...keys: K$1[]): Omit<T, K$1>;
|
|
659
675
|
//#endregion
|
|
660
676
|
//#region src/object/pick.d.ts
|
|
661
|
-
declare function pick<T, K extends keyof T>(object: T, keys: K[]): Pick<T, K>;
|
|
677
|
+
declare function pick<T, K$1 extends keyof T>(object: T, keys: K$1[]): Pick<T, K$1>;
|
|
662
678
|
//#endregion
|
|
663
679
|
//#region src/object/clean.d.ts
|
|
664
680
|
interface CleanObjectOptions {
|
|
@@ -845,7 +861,30 @@ declare function getStringSimilarity(str1: string, str2: string, options?: GetSt
|
|
|
845
861
|
* Special chars
|
|
846
862
|
*/
|
|
847
863
|
declare const SPECIAL_CHAR: {
|
|
864
|
+
/**
|
|
865
|
+
* 中文顿号
|
|
866
|
+
*/
|
|
867
|
+
chineseComma: string;
|
|
868
|
+
/**
|
|
869
|
+
* 英文逗号
|
|
870
|
+
*/
|
|
871
|
+
englishComma: string;
|
|
872
|
+
/**
|
|
873
|
+
* 英文句号
|
|
874
|
+
*/
|
|
875
|
+
englishPeriod: string;
|
|
876
|
+
/**
|
|
877
|
+
* 连接符
|
|
878
|
+
*/
|
|
879
|
+
hyphen: string;
|
|
880
|
+
/**
|
|
881
|
+
* 换行
|
|
882
|
+
*/
|
|
848
883
|
newline: string;
|
|
884
|
+
/**
|
|
885
|
+
* 空格
|
|
886
|
+
*/
|
|
887
|
+
whitespace: string;
|
|
849
888
|
};
|
|
850
889
|
//#endregion
|
|
851
890
|
//#region src/constants/regexp.d.ts
|
package/dist/index.js
CHANGED
|
@@ -10,6 +10,22 @@ const NOOP = noop;
|
|
|
10
10
|
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/fn/once.ts
|
|
13
|
+
/**
|
|
14
|
+
* Creates a function that is restricted to invoking `func` once. Repeat calls to the function return `false`.
|
|
15
|
+
*
|
|
16
|
+
* @param func - The function to restrict.
|
|
17
|
+
* @returns A new function that returns `true` when `func` is invoked for the first time and `false` on subsequent calls.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const initialize = once(() => {
|
|
23
|
+
* console.log('Initialized')
|
|
24
|
+
* })
|
|
25
|
+
*
|
|
26
|
+
* initialize() // Logs: 'Initialized', returns true
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
13
29
|
function once(func) {
|
|
14
30
|
let called = false;
|
|
15
31
|
return function(...args) {
|
|
@@ -164,9 +180,7 @@ function isUrlString(value) {
|
|
|
164
180
|
* check if two values are deeply equal
|
|
165
181
|
*/
|
|
166
182
|
function isDeepEqual(value1, value2) {
|
|
167
|
-
|
|
168
|
-
const type2 = getObjectType(value2);
|
|
169
|
-
if (type1 !== type2) return false;
|
|
183
|
+
if (getObjectType(value1) !== getObjectType(value2)) return false;
|
|
170
184
|
if (isArray(value1) && isArray(value2)) {
|
|
171
185
|
if (value1.length !== value2.length) return false;
|
|
172
186
|
return value1.every((item, index) => isDeepEqual(item, value2[index]));
|
|
@@ -189,7 +203,7 @@ function isDeepEqual(value1, value2) {
|
|
|
189
203
|
*/
|
|
190
204
|
function scrollElementIntoView(element, options = {}) {
|
|
191
205
|
const body = document.body;
|
|
192
|
-
const { parent = body
|
|
206
|
+
const { parent = body, ...scrollIntoViewOptions } = options;
|
|
193
207
|
if (parent === body) {
|
|
194
208
|
parent.scrollIntoView(scrollIntoViewOptions);
|
|
195
209
|
return;
|
|
@@ -291,6 +305,9 @@ function unescapeHTML(str) {
|
|
|
291
305
|
//#endregion
|
|
292
306
|
//#region src/misc/raf.ts
|
|
293
307
|
/**
|
|
308
|
+
* @file raf.ts
|
|
309
|
+
*/
|
|
310
|
+
/**
|
|
294
311
|
* Gets the global root object.
|
|
295
312
|
* @returns the global root object
|
|
296
313
|
*/
|
|
@@ -480,8 +497,7 @@ function convertFromMilliseconds(milliseconds, toUnit = "SECOND") {
|
|
|
480
497
|
* ```
|
|
481
498
|
*/
|
|
482
499
|
function convertTimeUnit(value, fromUnit, toUnit) {
|
|
483
|
-
|
|
484
|
-
return convertFromMilliseconds(milliseconds, toUnit);
|
|
500
|
+
return convertFromMilliseconds(convertToMilliseconds(value, fromUnit), toUnit);
|
|
485
501
|
}
|
|
486
502
|
|
|
487
503
|
//#endregion
|
|
@@ -544,8 +560,7 @@ function convertFromBytes(bytes, toUnit = "MB") {
|
|
|
544
560
|
* ```
|
|
545
561
|
*/
|
|
546
562
|
function convertStorageUnit(value, fromUnit, toUnit) {
|
|
547
|
-
|
|
548
|
-
return convertFromBytes(bytes, toUnit);
|
|
563
|
+
return convertFromBytes(convertToBytes(value, fromUnit), toUnit);
|
|
549
564
|
}
|
|
550
565
|
|
|
551
566
|
//#endregion
|
|
@@ -771,7 +786,10 @@ function toInteger(value, options = {}) {
|
|
|
771
786
|
*/
|
|
772
787
|
function randomString(length = 16, chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") {
|
|
773
788
|
const result = [];
|
|
774
|
-
for (let i = length; i > 0; --i)
|
|
789
|
+
for (let i = length; i > 0; --i) {
|
|
790
|
+
const matchedChar = chars[randomNumber(chars.length)];
|
|
791
|
+
if (matchedChar) result.push(matchedChar);
|
|
792
|
+
}
|
|
775
793
|
return result.join("");
|
|
776
794
|
}
|
|
777
795
|
|
|
@@ -806,7 +824,7 @@ const _RE_FULL_WS = /^\s*$/;
|
|
|
806
824
|
* ```
|
|
807
825
|
*/
|
|
808
826
|
function unindent(input) {
|
|
809
|
-
const lines = (
|
|
827
|
+
const lines = (isString(input) ? input : input[0])?.split("\n") ?? [];
|
|
810
828
|
const whitespaceLines = lines.map((line) => _RE_FULL_WS.test(line));
|
|
811
829
|
const commonIndent = lines.reduce((min, line, idx) => {
|
|
812
830
|
if (whitespaceLines[idx]) return min;
|
|
@@ -1084,9 +1102,10 @@ function hasOwn(object, key) {
|
|
|
1084
1102
|
//#endregion
|
|
1085
1103
|
//#region src/object/pick.ts
|
|
1086
1104
|
function pick(object, keys) {
|
|
1087
|
-
return
|
|
1088
|
-
if (object && hasOwn(object, key))
|
|
1089
|
-
|
|
1105
|
+
return keys.reduce((result, key) => {
|
|
1106
|
+
if (object && hasOwn(object, key)) result[key] = object[key];
|
|
1107
|
+
return result;
|
|
1108
|
+
}, {});
|
|
1090
1109
|
}
|
|
1091
1110
|
|
|
1092
1111
|
//#endregion
|
|
@@ -1157,7 +1176,14 @@ function sortObject(obj, options = {}) {
|
|
|
1157
1176
|
/**
|
|
1158
1177
|
* Special chars
|
|
1159
1178
|
*/
|
|
1160
|
-
const SPECIAL_CHAR = {
|
|
1179
|
+
const SPECIAL_CHAR = {
|
|
1180
|
+
chineseComma: "、",
|
|
1181
|
+
englishComma: ",",
|
|
1182
|
+
englishPeriod: ".",
|
|
1183
|
+
hyphen: "-",
|
|
1184
|
+
newline: "\n",
|
|
1185
|
+
whitespace: " "
|
|
1186
|
+
};
|
|
1161
1187
|
|
|
1162
1188
|
//#endregion
|
|
1163
1189
|
//#region src/constants/regexp.ts
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ntnyq/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.
|
|
5
|
-
"packageManager": "pnpm@10.
|
|
4
|
+
"version": "0.10.0",
|
|
5
|
+
"packageManager": "pnpm@10.26.2",
|
|
6
6
|
"description": "Common used utils.",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"utils"
|
|
@@ -43,20 +43,21 @@
|
|
|
43
43
|
"typecheck": "tsc --noEmit"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@ntnyq/eslint-config": "^5.
|
|
46
|
+
"@ntnyq/eslint-config": "^5.8.0",
|
|
47
47
|
"@ntnyq/prettier-config": "^3.0.1",
|
|
48
|
-
"
|
|
49
|
-
"
|
|
48
|
+
"@ntnyq/tsconfig": "^3.0.0",
|
|
49
|
+
"bumpp": "^10.3.2",
|
|
50
|
+
"eslint": "^9.39.2",
|
|
50
51
|
"husky": "^9.1.7",
|
|
51
|
-
"nano-staged": "^0.
|
|
52
|
+
"nano-staged": "^0.9.0",
|
|
52
53
|
"npm-run-all2": "^8.0.4",
|
|
53
|
-
"prettier": "^3.
|
|
54
|
-
"tsdown": "^0.
|
|
55
|
-
"typescript": "^5.9.
|
|
56
|
-
"vitest": "^
|
|
54
|
+
"prettier": "^3.7.4",
|
|
55
|
+
"tsdown": "^0.18.3",
|
|
56
|
+
"typescript": "^5.9.3",
|
|
57
|
+
"vitest": "^4.0.15"
|
|
57
58
|
},
|
|
58
59
|
"engines": {
|
|
59
|
-
"node": "
|
|
60
|
+
"node": "^20.19.0 || ^22.13.0 || >=24"
|
|
60
61
|
},
|
|
61
62
|
"nano-staged": {
|
|
62
63
|
"*.{js,ts,mjs,cjs,md,yml,yaml,toml,json}": "eslint --fix"
|