@jiakun-zhao/utils 1.3.7 → 1.4.1
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.mts +141 -18
- package/dist/index.mjs +200 -58
- package/package.json +10 -20
- package/dist/index.d.ts +0 -19
- package/dist/natural-compare.d.mts +0 -6
- package/dist/natural-compare.d.ts +0 -6
- package/dist/natural-compare.mjs +0 -74
package/dist/index.d.mts
CHANGED
|
@@ -1,19 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
declare
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
//#region src/add-ons/assert.d.ts
|
|
2
|
+
declare function assert(condition: boolean, message: string): asserts condition;
|
|
3
|
+
//#endregion
|
|
4
|
+
//#region src/add-ons/deep-equal.d.ts
|
|
5
|
+
declare function isDeepEqual(a: any, b: any): boolean;
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/add-ons/http-status-code.d.ts
|
|
8
|
+
declare const status: {
|
|
9
|
+
readonly 100: "Continue";
|
|
10
|
+
readonly 101: "Switching Protocols";
|
|
11
|
+
readonly 102: "Processing";
|
|
12
|
+
readonly 103: "Early Hints";
|
|
13
|
+
readonly 200: "OK";
|
|
14
|
+
readonly 201: "Created";
|
|
15
|
+
readonly 202: "Accepted";
|
|
16
|
+
readonly 203: "Non-Authoritative Information";
|
|
17
|
+
readonly 204: "No Content";
|
|
18
|
+
readonly 205: "Reset Content";
|
|
19
|
+
readonly 206: "Partial Content";
|
|
20
|
+
readonly 207: "Multi-Status";
|
|
21
|
+
readonly 208: "Already Reported";
|
|
22
|
+
readonly 226: "IM Used";
|
|
23
|
+
readonly 300: "Multiple Choices";
|
|
24
|
+
readonly 301: "Moved Permanently";
|
|
25
|
+
readonly 302: "Found";
|
|
26
|
+
readonly 303: "See Other";
|
|
27
|
+
readonly 304: "Not Modified";
|
|
28
|
+
readonly 305: "Use Proxy";
|
|
29
|
+
readonly 307: "Temporary Redirect";
|
|
30
|
+
readonly 308: "Permanent Redirect";
|
|
31
|
+
readonly 400: "Bad Request";
|
|
32
|
+
readonly 401: "Unauthorized";
|
|
33
|
+
readonly 402: "Payment Required";
|
|
34
|
+
readonly 403: "Forbidden";
|
|
35
|
+
readonly 404: "Not Found";
|
|
36
|
+
readonly 405: "Method Not Allowed";
|
|
37
|
+
readonly 406: "Not Acceptable";
|
|
38
|
+
readonly 407: "Proxy Authentication Required";
|
|
39
|
+
readonly 408: "Request Timeout";
|
|
40
|
+
readonly 409: "Conflict";
|
|
41
|
+
readonly 410: "Gone";
|
|
42
|
+
readonly 411: "Length Required";
|
|
43
|
+
readonly 412: "Precondition Failed";
|
|
44
|
+
readonly 413: "Payload Too Large";
|
|
45
|
+
readonly 414: "URI Too Long";
|
|
46
|
+
readonly 415: "Unsupported Media Type";
|
|
47
|
+
readonly 416: "Range Not Satisfiable";
|
|
48
|
+
readonly 417: "Expectation Failed";
|
|
49
|
+
readonly 418: "I'm a Teapot";
|
|
50
|
+
readonly 421: "Misdirected Request";
|
|
51
|
+
readonly 422: "Unprocessable Entity";
|
|
52
|
+
readonly 423: "Locked";
|
|
53
|
+
readonly 424: "Failed Dependency";
|
|
54
|
+
readonly 425: "Too Early";
|
|
55
|
+
readonly 426: "Upgrade Required";
|
|
56
|
+
readonly 428: "Precondition Required";
|
|
57
|
+
readonly 429: "Too Many Requests";
|
|
58
|
+
readonly 431: "Request Header Fields Too Large";
|
|
59
|
+
readonly 451: "Unavailable For Legal Reasons";
|
|
60
|
+
readonly 500: "Internal Server Error";
|
|
61
|
+
readonly 501: "Not Implemented";
|
|
62
|
+
readonly 502: "Bad Gateway";
|
|
63
|
+
readonly 503: "Service Unavailable";
|
|
64
|
+
readonly 504: "Gateway Timeout";
|
|
65
|
+
readonly 505: "HTTP Version Not Supported";
|
|
66
|
+
readonly 506: "Variant Also Negotiates";
|
|
67
|
+
readonly 507: "Insufficient Storage";
|
|
68
|
+
readonly 508: "Loop Detected";
|
|
69
|
+
readonly 509: "Bandwidth Limit Exceeded";
|
|
70
|
+
readonly 510: "Not Extended";
|
|
71
|
+
readonly 511: "Network Authentication Required";
|
|
17
72
|
};
|
|
18
|
-
|
|
19
|
-
|
|
73
|
+
declare function getHttpStatusMessage(code: keyof typeof status): "Continue" | "Switching Protocols" | "Processing" | "Early Hints" | "OK" | "Created" | "Accepted" | "Non-Authoritative Information" | "No Content" | "Reset Content" | "Partial Content" | "Multi-Status" | "Already Reported" | "IM Used" | "Multiple Choices" | "Moved Permanently" | "Found" | "See Other" | "Not Modified" | "Use Proxy" | "Temporary Redirect" | "Permanent Redirect" | "Bad Request" | "Unauthorized" | "Payment Required" | "Forbidden" | "Not Found" | "Method Not Allowed" | "Not Acceptable" | "Proxy Authentication Required" | "Request Timeout" | "Conflict" | "Gone" | "Length Required" | "Precondition Failed" | "Payload Too Large" | "URI Too Long" | "Unsupported Media Type" | "Range Not Satisfiable" | "Expectation Failed" | "I'm a Teapot" | "Misdirected Request" | "Unprocessable Entity" | "Locked" | "Failed Dependency" | "Too Early" | "Upgrade Required" | "Precondition Required" | "Too Many Requests" | "Request Header Fields Too Large" | "Unavailable For Legal Reasons" | "Internal Server Error" | "Not Implemented" | "Bad Gateway" | "Service Unavailable" | "Gateway Timeout" | "HTTP Version Not Supported" | "Variant Also Negotiates" | "Insufficient Storage" | "Loop Detected" | "Bandwidth Limit Exceeded" | "Not Extended" | "Network Authentication Required";
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/add-ons/natural-compare.d.ts
|
|
76
|
+
declare function naturalCompare<T extends string | number>(a: T, b: T): -1 | 0 | 1;
|
|
77
|
+
//#endregion
|
|
78
|
+
//#region src/add-ons/promise.d.ts
|
|
79
|
+
type Awaitable<T> = T | PromiseLike<T>;
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/base/array.d.ts
|
|
82
|
+
type ElementOf<T> = T extends (infer E)[] ? E : never;
|
|
83
|
+
type Arrayable<T> = T | Array<T>;
|
|
84
|
+
declare const isArray: (val: unknown) => val is any[];
|
|
85
|
+
declare const toArray: <T>(val: Arrayable<T>) => T[];
|
|
86
|
+
declare const uniq: <T>(arr: readonly T[]) => T[];
|
|
87
|
+
declare const singleOrNull: <T>(arr: T[]) => T | null;
|
|
88
|
+
declare function createArray<T = number>(len: number, mapFn?: (idx: number) => T): T[];
|
|
89
|
+
declare function shuffle<T>(array: T[]): T[];
|
|
90
|
+
declare function range(stop: number): number[];
|
|
91
|
+
declare function range(start: number, stop: number, step?: number): number[];
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/base/boolean.d.ts
|
|
94
|
+
declare const isBoolean: (val: any) => val is boolean;
|
|
95
|
+
//#endregion
|
|
96
|
+
//#region src/base/date.d.ts
|
|
97
|
+
declare const isDate: (val: any) => val is Date;
|
|
98
|
+
declare const timestamp: () => number;
|
|
99
|
+
declare const isToDay: (date: Date) => boolean;
|
|
100
|
+
declare function isSameDay(dateA: Date, dateB: Date): boolean;
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/base/function.d.ts
|
|
103
|
+
type Fn<T = void> = () => T;
|
|
104
|
+
declare const isFunction: <T extends Function>(val: any) => val is T;
|
|
105
|
+
declare const noop: () => void;
|
|
106
|
+
declare const transform: <T, R>(val: T, fn: (val: T) => R) => R;
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/base/nullable.d.ts
|
|
109
|
+
type Nullable<T> = T | null | undefined;
|
|
110
|
+
declare const isTruthy: <T>(v: T) => v is NonNullable<T>;
|
|
111
|
+
declare const isUndefined: (val: any) => val is undefined;
|
|
112
|
+
declare const notUndefined: <T>(v: T) => v is Exclude<T, undefined>;
|
|
113
|
+
declare const isNull: (val: any) => val is null;
|
|
114
|
+
declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
|
|
115
|
+
declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
|
|
116
|
+
declare const isDefined: <T = any>(val?: T) => val is T;
|
|
117
|
+
//#endregion
|
|
118
|
+
//#region src/base/number.d.ts
|
|
119
|
+
declare const isNumber: (val: any) => val is number;
|
|
120
|
+
declare const clamp: (n: number, min: number, max: number) => number;
|
|
121
|
+
declare function lerp(min: number, max: number, t: number): number;
|
|
122
|
+
//#endregion
|
|
123
|
+
//#region src/base/object.d.ts
|
|
124
|
+
declare const isObject: (val: any) => val is object;
|
|
125
|
+
declare const isKeyOf: <T extends object>(val: T, key: keyof any) => key is keyof T;
|
|
126
|
+
declare function objectFilter<O extends object>(obj: O, fn: (key: keyof O, value: O[keyof O]) => boolean): O;
|
|
127
|
+
declare function objectPick<O extends object, T extends keyof O>(obj: O, ...keys: T[]): Pick<O, T>;
|
|
128
|
+
declare function objectOmit<O extends object, T extends keyof O>(obj: O, ...keys: T[]): Omit<O, T>;
|
|
129
|
+
declare function clearUndefined<O extends object>(obj: O): O;
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/base/regexp.d.ts
|
|
132
|
+
declare const isRegExp: (val: any) => val is RegExp;
|
|
133
|
+
//#endregion
|
|
134
|
+
//#region src/base/string.d.ts
|
|
135
|
+
declare const toString: (val: any) => string;
|
|
136
|
+
declare const isString: (val: unknown) => val is string;
|
|
137
|
+
declare const slash: (str: string) => string;
|
|
138
|
+
declare const ensurePrefix: (prefix: string, str: string) => string;
|
|
139
|
+
declare const ensureSuffix: (suffix: string, str: string) => string;
|
|
140
|
+
declare const capitalize: (str: string) => string;
|
|
141
|
+
//#endregion
|
|
142
|
+
export { Arrayable, Awaitable, ElementOf, Fn, Nullable, assert, capitalize, clamp, clearUndefined, createArray, ensurePrefix, ensureSuffix, getHttpStatusMessage, isArray, isBoolean, isDate, isDeepEqual, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isToDay, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, status, timestamp, toArray, toString, transform, uniq };
|
package/dist/index.mjs
CHANGED
|
@@ -1,70 +1,212 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/add-ons/assert.ts
|
|
2
|
+
function assert(condition, message) {
|
|
3
|
+
if (!condition) throw new Error(message);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
//#endregion
|
|
7
|
+
//#region src/add-ons/deep-equal.ts
|
|
8
|
+
function isDeepEqual(a, b) {
|
|
9
|
+
if (a === b) return true;
|
|
10
|
+
const constructor = a?.constructor;
|
|
11
|
+
if (constructor !== b?.constructor) return false;
|
|
12
|
+
if (constructor === Array) return a.length === b.length && a.every((item, i) => isDeepEqual(item, b[i]));
|
|
13
|
+
if (constructor === Object) {
|
|
14
|
+
const aKeys = Object.keys(a);
|
|
15
|
+
const bKeys = Object.keys(b);
|
|
16
|
+
return aKeys.length === bKeys.length && aKeys.every((key) => isDeepEqual(a[key], b[key]));
|
|
17
|
+
}
|
|
18
|
+
if (constructor === RegExp) return a.toString() === b.toString();
|
|
19
|
+
if (constructor === Date) return a.getTime() === b.getTime();
|
|
20
|
+
return Object.is(a, b);
|
|
21
|
+
}
|
|
2
22
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/add-ons/http-status-code.ts
|
|
25
|
+
const status = {
|
|
26
|
+
100: "Continue",
|
|
27
|
+
101: "Switching Protocols",
|
|
28
|
+
102: "Processing",
|
|
29
|
+
103: "Early Hints",
|
|
30
|
+
200: "OK",
|
|
31
|
+
201: "Created",
|
|
32
|
+
202: "Accepted",
|
|
33
|
+
203: "Non-Authoritative Information",
|
|
34
|
+
204: "No Content",
|
|
35
|
+
205: "Reset Content",
|
|
36
|
+
206: "Partial Content",
|
|
37
|
+
207: "Multi-Status",
|
|
38
|
+
208: "Already Reported",
|
|
39
|
+
226: "IM Used",
|
|
40
|
+
300: "Multiple Choices",
|
|
41
|
+
301: "Moved Permanently",
|
|
42
|
+
302: "Found",
|
|
43
|
+
303: "See Other",
|
|
44
|
+
304: "Not Modified",
|
|
45
|
+
305: "Use Proxy",
|
|
46
|
+
307: "Temporary Redirect",
|
|
47
|
+
308: "Permanent Redirect",
|
|
48
|
+
400: "Bad Request",
|
|
49
|
+
401: "Unauthorized",
|
|
50
|
+
402: "Payment Required",
|
|
51
|
+
403: "Forbidden",
|
|
52
|
+
404: "Not Found",
|
|
53
|
+
405: "Method Not Allowed",
|
|
54
|
+
406: "Not Acceptable",
|
|
55
|
+
407: "Proxy Authentication Required",
|
|
56
|
+
408: "Request Timeout",
|
|
57
|
+
409: "Conflict",
|
|
58
|
+
410: "Gone",
|
|
59
|
+
411: "Length Required",
|
|
60
|
+
412: "Precondition Failed",
|
|
61
|
+
413: "Payload Too Large",
|
|
62
|
+
414: "URI Too Long",
|
|
63
|
+
415: "Unsupported Media Type",
|
|
64
|
+
416: "Range Not Satisfiable",
|
|
65
|
+
417: "Expectation Failed",
|
|
66
|
+
418: "I'm a Teapot",
|
|
67
|
+
421: "Misdirected Request",
|
|
68
|
+
422: "Unprocessable Entity",
|
|
69
|
+
423: "Locked",
|
|
70
|
+
424: "Failed Dependency",
|
|
71
|
+
425: "Too Early",
|
|
72
|
+
426: "Upgrade Required",
|
|
73
|
+
428: "Precondition Required",
|
|
74
|
+
429: "Too Many Requests",
|
|
75
|
+
431: "Request Header Fields Too Large",
|
|
76
|
+
451: "Unavailable For Legal Reasons",
|
|
77
|
+
500: "Internal Server Error",
|
|
78
|
+
501: "Not Implemented",
|
|
79
|
+
502: "Bad Gateway",
|
|
80
|
+
503: "Service Unavailable",
|
|
81
|
+
504: "Gateway Timeout",
|
|
82
|
+
505: "HTTP Version Not Supported",
|
|
83
|
+
506: "Variant Also Negotiates",
|
|
84
|
+
507: "Insufficient Storage",
|
|
85
|
+
508: "Loop Detected",
|
|
86
|
+
509: "Bandwidth Limit Exceeded",
|
|
87
|
+
510: "Not Extended",
|
|
88
|
+
511: "Network Authentication Required"
|
|
89
|
+
};
|
|
90
|
+
function getHttpStatusMessage(code) {
|
|
91
|
+
return status[code];
|
|
6
92
|
}
|
|
7
|
-
|
|
8
|
-
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/add-ons/natural-compare.ts
|
|
96
|
+
function naturalCompare(a, b) {
|
|
97
|
+
var i, codeA, codeB = 1, posA = 0, posB = 0, alphabet = String.alphabet;
|
|
98
|
+
function getCode(str, pos, code) {
|
|
99
|
+
if (code) {
|
|
100
|
+
for (i = pos; code = getCode(str, i), code < 76 && code > 65;) ++i;
|
|
101
|
+
return +str.slice(pos - 1, i);
|
|
102
|
+
}
|
|
103
|
+
code = alphabet && alphabet.indexOf(str.charAt(pos));
|
|
104
|
+
return code > -1 ? code + 76 : (code = str.charCodeAt(pos) || 0, code < 45 || code > 127) ? code : code < 46 ? 65 : code < 48 ? code - 1 : code < 58 ? code + 18 : code < 65 ? code - 11 : code < 91 ? code + 11 : code < 97 ? code - 37 : code < 123 ? code + 5 : code - 63;
|
|
105
|
+
}
|
|
106
|
+
if ((a += "") != (b += "")) for (; codeB;) {
|
|
107
|
+
codeA = getCode(a, posA++);
|
|
108
|
+
codeB = getCode(b, posB++);
|
|
109
|
+
if (codeA < 76 && codeB < 76 && codeA > 66 && codeB > 66) {
|
|
110
|
+
codeA = getCode(a, posA, posA);
|
|
111
|
+
codeB = getCode(b, posB, posA = i);
|
|
112
|
+
posB = i;
|
|
113
|
+
}
|
|
114
|
+
if (codeA != codeB) return codeA < codeB ? -1 : 1;
|
|
115
|
+
}
|
|
116
|
+
return 0;
|
|
9
117
|
}
|
|
10
118
|
|
|
11
|
-
|
|
12
|
-
|
|
119
|
+
//#endregion
|
|
120
|
+
//#region src/base/array.ts
|
|
121
|
+
const isArray = (val) => Array.isArray(val);
|
|
122
|
+
const toArray = (val) => isArray(val) ? val : [val];
|
|
123
|
+
const uniq = (arr) => [...new Set(arr)];
|
|
124
|
+
const singleOrNull = (arr) => arr.length === 1 ? arr[0] : null;
|
|
125
|
+
function createArray(len, mapFn) {
|
|
126
|
+
return Array.from({ length: len }, (_, index) => mapFn?.(index) ?? index);
|
|
13
127
|
}
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
default: {
|
|
50
|
-
return date;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return result;
|
|
128
|
+
function shuffle(array) {
|
|
129
|
+
for (let i = array.length - 1; i > 0; i--) {
|
|
130
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
131
|
+
[array[i], array[j]] = [array[j], array[i]];
|
|
132
|
+
}
|
|
133
|
+
return array;
|
|
134
|
+
}
|
|
135
|
+
function range(...args) {
|
|
136
|
+
if (args.length === 1) args.unshift(0);
|
|
137
|
+
const [start, stop, step = 1] = args;
|
|
138
|
+
const result = [];
|
|
139
|
+
for (let i = start; i < stop; i += step) result.push(i);
|
|
140
|
+
return result;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
//#endregion
|
|
144
|
+
//#region src/base/boolean.ts
|
|
145
|
+
const isBoolean = (val) => typeof val === "boolean";
|
|
146
|
+
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/base/string.ts
|
|
149
|
+
const toString = (val) => Object.prototype.toString.call(val);
|
|
150
|
+
const isString = (val) => typeof val === "string";
|
|
151
|
+
const slash = (str) => str.replace(/\\/g, "/");
|
|
152
|
+
const ensurePrefix = (prefix, str) => str.startsWith(prefix) ? str : prefix + str;
|
|
153
|
+
const ensureSuffix = (suffix, str) => str.endsWith(suffix) ? str : str + suffix;
|
|
154
|
+
const capitalize = (str) => str[0].toUpperCase() + str.slice(1).toLowerCase();
|
|
155
|
+
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/base/date.ts
|
|
158
|
+
const isDate = (val) => toString(val) === "[object Date]";
|
|
159
|
+
const timestamp = () => +Date.now();
|
|
160
|
+
const isToDay = (date) => isSameDay(date, /* @__PURE__ */ new Date());
|
|
161
|
+
function isSameDay(dateA, dateB) {
|
|
162
|
+
return dateA.getDate() === dateB.getDate() && dateA.getMonth() === dateB.getMonth() && dateA.getFullYear() === dateB.getFullYear();
|
|
54
163
|
}
|
|
55
164
|
|
|
56
|
-
|
|
57
|
-
|
|
165
|
+
//#endregion
|
|
166
|
+
//#region src/base/function.ts
|
|
167
|
+
const isFunction = (val) => typeof val === "function";
|
|
168
|
+
const noop = () => {};
|
|
169
|
+
const transform = (val, fn) => fn(val);
|
|
170
|
+
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/base/nullable.ts
|
|
173
|
+
const isTruthy = (v) => Boolean(v);
|
|
174
|
+
const isUndefined = (val) => toString(val) === "[object Undefined]";
|
|
175
|
+
const notUndefined = (v) => v !== void 0;
|
|
176
|
+
const isNull = (val) => toString(val) === "[object Null]";
|
|
177
|
+
const notNull = (v) => v !== null;
|
|
178
|
+
const notNullish = (v) => v != null;
|
|
179
|
+
const isDefined = (val) => typeof val !== "undefined";
|
|
180
|
+
|
|
181
|
+
//#endregion
|
|
182
|
+
//#region src/base/number.ts
|
|
183
|
+
const isNumber = (val) => typeof val === "number";
|
|
184
|
+
const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
|
|
185
|
+
function lerp(min, max, t) {
|
|
186
|
+
const interpolation = clamp(t, 0, 1);
|
|
187
|
+
return min + (max - min) * interpolation;
|
|
58
188
|
}
|
|
59
189
|
|
|
60
|
-
|
|
61
|
-
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/base/object.ts
|
|
192
|
+
const isObject = (val) => toString(val) === "[object Object]";
|
|
193
|
+
const isKeyOf = (val, key) => key in val;
|
|
194
|
+
function objectFilter(obj, fn) {
|
|
195
|
+
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => fn(key, value)));
|
|
62
196
|
}
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
get: () => value,
|
|
66
|
-
to: (fn) => from(fn(value))
|
|
67
|
-
};
|
|
197
|
+
function objectPick(obj, ...keys) {
|
|
198
|
+
return objectFilter(obj, (key) => keys.includes(key));
|
|
68
199
|
}
|
|
200
|
+
function objectOmit(obj, ...keys) {
|
|
201
|
+
return objectFilter(obj, (key) => !keys.includes(key));
|
|
202
|
+
}
|
|
203
|
+
function clearUndefined(obj) {
|
|
204
|
+
return objectFilter(obj, (_, value) => notUndefined(value));
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
//#endregion
|
|
208
|
+
//#region src/base/regexp.ts
|
|
209
|
+
const isRegExp = (val) => toString(val) === "[object RegExp]";
|
|
69
210
|
|
|
70
|
-
|
|
211
|
+
//#endregion
|
|
212
|
+
export { assert, capitalize, clamp, clearUndefined, createArray, ensurePrefix, ensureSuffix, getHttpStatusMessage, isArray, isBoolean, isDate, isDeepEqual, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isToDay, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, status, timestamp, toArray, toString, transform, uniq };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jiakun-zhao/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.4.1",
|
|
5
5
|
"description": "Utils.",
|
|
6
6
|
"author": "Jiakun Zhao <hi@zhaojiakun.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -16,37 +16,27 @@
|
|
|
16
16
|
"exports": {
|
|
17
17
|
".": {
|
|
18
18
|
"import": "./dist/index.mjs",
|
|
19
|
-
"types": "./dist/index.d.
|
|
20
|
-
},
|
|
21
|
-
"./natural-compare": {
|
|
22
|
-
"import": "./dist/natural-compare.mjs",
|
|
23
|
-
"types": "./dist/natural-compare.d.ts"
|
|
19
|
+
"types": "./dist/index.d.mts"
|
|
24
20
|
}
|
|
25
21
|
},
|
|
26
22
|
"main": "./dist/index.mjs",
|
|
27
23
|
"module": "./dist/index.mjs",
|
|
28
|
-
"types": "./dist/index.d.
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
29
25
|
"files": [
|
|
30
26
|
"dist"
|
|
31
27
|
],
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"@antfu/utils": "^9.2.0"
|
|
34
|
-
},
|
|
35
28
|
"devDependencies": {
|
|
36
29
|
"@jiakun-zhao/eslint-config": "^4.1.8",
|
|
37
|
-
"@types/
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"unbuild": "^3.5.0",
|
|
44
|
-
"vitest": "^3.2.4"
|
|
30
|
+
"@types/node": "^25.0.3",
|
|
31
|
+
"bumpp": "^10.3.2",
|
|
32
|
+
"eslint": "^9.39.2",
|
|
33
|
+
"tsdown": "^0.18.1",
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"vitest": "^4.0.16"
|
|
45
36
|
},
|
|
46
37
|
"scripts": {
|
|
47
|
-
"build": "
|
|
38
|
+
"build": "tsdown",
|
|
48
39
|
"release": "bumpp && pnpm publish && npx cnpm sync @jiakun-zhao/utils",
|
|
49
|
-
"stub": "unbuild --stub",
|
|
50
40
|
"test": "vitest"
|
|
51
41
|
}
|
|
52
42
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export * from '@antfu/utils';
|
|
2
|
-
|
|
3
|
-
declare const isArray: (arg: any) => arg is any[];
|
|
4
|
-
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
5
|
-
declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
|
|
6
|
-
|
|
7
|
-
type Unit = 'year' | 'month' | 'dayOfMonth' | 'hour' | 'minute' | 'second' | 'millisecond' | 'dayOfWeek';
|
|
8
|
-
declare function isSameDay(date: Date, anotherDate?: Date): boolean;
|
|
9
|
-
declare function dateCalculate(date: Date, value: number, unit?: Unit): Date;
|
|
10
|
-
|
|
11
|
-
declare function cleanText(str: string, searchValue: RegExp | string): string;
|
|
12
|
-
|
|
13
|
-
declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
|
|
14
|
-
declare function from<T>(value: T): {
|
|
15
|
-
get: () => T;
|
|
16
|
-
to: <R>(fn: (value: T) => R) => ReturnType<typeof from<R>>;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export { cleanText, createArray, dateCalculate, from, getValueOrUndefined, isArray, isSameDay, singleOrNull };
|
package/dist/natural-compare.mjs
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
function getDefaultExportFromCjs (x) {
|
|
2
|
-
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;
|
|
3
|
-
}
|
|
4
|
-
|
|
5
|
-
var naturalCompare$1 = {exports: {}};
|
|
6
|
-
|
|
7
|
-
var hasRequiredNaturalCompare;
|
|
8
|
-
|
|
9
|
-
function requireNaturalCompare () {
|
|
10
|
-
if (hasRequiredNaturalCompare) return naturalCompare$1.exports;
|
|
11
|
-
hasRequiredNaturalCompare = 1;
|
|
12
|
-
/*
|
|
13
|
-
* @version 1.4.0
|
|
14
|
-
* @date 2015-10-26
|
|
15
|
-
* @stability 3 - Stable
|
|
16
|
-
* @author Lauri Rooden (https://github.com/litejs/natural-compare-lite)
|
|
17
|
-
* @license MIT License
|
|
18
|
-
*/
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
var naturalCompare = function(a, b) {
|
|
22
|
-
var i, codeA
|
|
23
|
-
, codeB = 1
|
|
24
|
-
, posA = 0
|
|
25
|
-
, posB = 0
|
|
26
|
-
, alphabet = String.alphabet;
|
|
27
|
-
|
|
28
|
-
function getCode(str, pos, code) {
|
|
29
|
-
if (code) {
|
|
30
|
-
for (i = pos; code = getCode(str, i), code < 76 && code > 65;) ++i;
|
|
31
|
-
return +str.slice(pos - 1, i)
|
|
32
|
-
}
|
|
33
|
-
code = alphabet && alphabet.indexOf(str.charAt(pos));
|
|
34
|
-
return code > -1 ? code + 76 : ((code = str.charCodeAt(pos) || 0), code < 45 || code > 127) ? code
|
|
35
|
-
: code < 46 ? 65 // -
|
|
36
|
-
: code < 48 ? code - 1
|
|
37
|
-
: code < 58 ? code + 18 // 0-9
|
|
38
|
-
: code < 65 ? code - 11
|
|
39
|
-
: code < 91 ? code + 11 // A-Z
|
|
40
|
-
: code < 97 ? code - 37
|
|
41
|
-
: code < 123 ? code + 5 // a-z
|
|
42
|
-
: code - 63
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if ((a+="") != (b+="")) for (;codeB;) {
|
|
47
|
-
codeA = getCode(a, posA++);
|
|
48
|
-
codeB = getCode(b, posB++);
|
|
49
|
-
|
|
50
|
-
if (codeA < 76 && codeB < 76 && codeA > 66 && codeB > 66) {
|
|
51
|
-
codeA = getCode(a, posA, posA);
|
|
52
|
-
codeB = getCode(b, posB, posA = i);
|
|
53
|
-
posB = i;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (codeA != codeB) return (codeA < codeB) ? -1 : 1
|
|
57
|
-
}
|
|
58
|
-
return 0
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
try {
|
|
62
|
-
naturalCompare$1.exports = naturalCompare;
|
|
63
|
-
} catch (e) {
|
|
64
|
-
String.naturalCompare = naturalCompare;
|
|
65
|
-
}
|
|
66
|
-
return naturalCompare$1.exports;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
var naturalCompareExports = requireNaturalCompare();
|
|
70
|
-
const defaultNaturalCompare = /*@__PURE__*/getDefaultExportFromCjs(naturalCompareExports);
|
|
71
|
-
|
|
72
|
-
const naturalCompare = defaultNaturalCompare;
|
|
73
|
-
|
|
74
|
-
export { naturalCompare as default, naturalCompare };
|