@jiakun-zhao/utils 1.3.6 → 1.4.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.mts CHANGED
@@ -1,15 +1,72 @@
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
- declare function cleanText(str: string, searchValue: RegExp | string): string;
8
-
9
- declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
10
- declare function from<T>(value: T): {
11
- get: () => T;
12
- to: <R>(fn: (value: T) => R) => ReturnType<typeof from<R>>;
13
- };
14
-
15
- export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
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/natural-compare.d.ts
5
+ declare function naturalCompare<T extends string | number>(a: T, b: T): -1 | 0 | 1;
6
+ //#endregion
7
+ //#region src/add-ons/promise.d.ts
8
+ type Awaitable<T> = T | PromiseLike<T>;
9
+ //#endregion
10
+ //#region src/base/array.d.ts
11
+ type ElementOf<T> = T extends (infer E)[] ? E : never;
12
+ type Arrayable<T> = T | Array<T>;
13
+ declare const isArray: (val: unknown) => val is any[];
14
+ declare const toArray: <T>(val: Arrayable<T>) => T[];
15
+ declare const uniq: <T>(arr: readonly T[]) => T[];
16
+ declare const singleOrNull: <T>(arr: T[]) => T | null;
17
+ declare function createArray<T = number>(len: number, mapFn?: (idx: number) => T): T[];
18
+ declare function shuffle<T>(array: T[]): T[];
19
+ declare function range(stop: number): number[];
20
+ declare function range(start: number, stop: number, step?: number): number[];
21
+ //#endregion
22
+ //#region src/base/boolean.d.ts
23
+ declare const isBoolean: (val: any) => val is boolean;
24
+ //#endregion
25
+ //#region src/base/date.d.ts
26
+ declare const isDate: (val: any) => val is Date;
27
+ declare const timestamp: () => number;
28
+ type Unit = 'year' | 'month' | 'dayOfMonth' | 'hour' | 'minute' | 'second' | 'millisecond' | 'dayOfWeek';
29
+ declare function isSameDay(dateA: Date, dateB?: Date): boolean;
30
+ declare function dateCalculate(date: Date, value: number, unit?: Unit): Date;
31
+ //#endregion
32
+ //#region src/base/function.d.ts
33
+ type Fn<T = void> = () => T;
34
+ declare const isFunction: <T extends Function>(val: any) => val is T;
35
+ declare const noop: () => void;
36
+ declare const transform: <T, R>(val: T, fn: (val: T) => R) => R;
37
+ //#endregion
38
+ //#region src/base/nullable.d.ts
39
+ type Nullable<T> = T | null | undefined;
40
+ declare const isTruthy: <T>(v: T) => v is NonNullable<T>;
41
+ declare const isUndefined: (val: any) => val is undefined;
42
+ declare const notUndefined: <T>(v: T) => v is Exclude<T, undefined>;
43
+ declare const isNull: (val: any) => val is null;
44
+ declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
45
+ declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
46
+ declare const isDefined: <T = any>(val?: T) => val is T;
47
+ //#endregion
48
+ //#region src/base/number.d.ts
49
+ declare const isNumber: (val: any) => val is number;
50
+ declare const clamp: (n: number, min: number, max: number) => number;
51
+ declare function lerp(min: number, max: number, t: number): number;
52
+ //#endregion
53
+ //#region src/base/object.d.ts
54
+ declare const isObject: (val: any) => val is object;
55
+ declare const isKeyOf: <T extends object>(val: T, key: keyof any) => key is keyof T;
56
+ declare function objectFilter<O extends object>(obj: O, fn: (key: keyof O, value: O[keyof O]) => boolean): O;
57
+ declare function objectPick<O extends object, T extends keyof O>(obj: O, ...keys: T[]): Pick<O, T>;
58
+ declare function objectOmit<O extends object, T extends keyof O>(obj: O, ...keys: T[]): Omit<O, T>;
59
+ declare function clearUndefined<O extends object>(obj: O): O;
60
+ //#endregion
61
+ //#region src/base/regexp.d.ts
62
+ declare const isRegExp: (val: any) => val is RegExp;
63
+ //#endregion
64
+ //#region src/base/string.d.ts
65
+ declare const toString: (val: any) => string;
66
+ declare const isString: (val: unknown) => val is string;
67
+ declare const slash: (str: string) => string;
68
+ declare const ensurePrefix: (prefix: string, str: string) => string;
69
+ declare const ensureSuffix: (suffix: string, str: string) => string;
70
+ declare const capitalize: (str: string) => string;
71
+ //#endregion
72
+ export { Arrayable, Awaitable, ElementOf, Fn, Nullable, assert, capitalize, clamp, clearUndefined, createArray, dateCalculate, ensurePrefix, ensureSuffix, isArray, isBoolean, isDate, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, timestamp, toArray, toString, transform, uniq };
package/dist/index.mjs CHANGED
@@ -1,25 +1,154 @@
1
- export * from '@antfu/utils';
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/natural-compare.ts
8
+ function naturalCompare(a, b) {
9
+ var i, codeA, codeB = 1, posA = 0, posB = 0, alphabet = String.alphabet;
10
+ function getCode(str, pos, code) {
11
+ if (code) {
12
+ for (i = pos; code = getCode(str, i), code < 76 && code > 65;) ++i;
13
+ return +str.slice(pos - 1, i);
14
+ }
15
+ code = alphabet && alphabet.indexOf(str.charAt(pos));
16
+ 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;
17
+ }
18
+ if ((a += "") != (b += "")) for (; codeB;) {
19
+ codeA = getCode(a, posA++);
20
+ codeB = getCode(b, posB++);
21
+ if (codeA < 76 && codeB < 76 && codeA > 66 && codeB > 66) {
22
+ codeA = getCode(a, posA, posA);
23
+ codeB = getCode(b, posB, posA = i);
24
+ posB = i;
25
+ }
26
+ if (codeA != codeB) return codeA < codeB ? -1 : 1;
27
+ }
28
+ return 0;
29
+ }
30
+
31
+ //#endregion
32
+ //#region src/base/array.ts
33
+ const isArray = (val) => Array.isArray(val);
34
+ const toArray = (val) => isArray(val) ? val : [val];
35
+ const uniq = (arr) => [...new Set(arr)];
36
+ const singleOrNull = (arr) => arr.length === 1 ? arr[0] : null;
37
+ function createArray(len, mapFn) {
38
+ return Array.from({ length: len }, (_, index) => mapFn?.(index) ?? index);
39
+ }
40
+ function shuffle(array) {
41
+ for (let i = array.length - 1; i > 0; i--) {
42
+ const j = Math.floor(Math.random() * (i + 1));
43
+ [array[i], array[j]] = [array[j], array[i]];
44
+ }
45
+ return array;
46
+ }
47
+ function range(...args) {
48
+ if (args.length === 1) args.unshift(0);
49
+ const [start, stop, step = 1] = args;
50
+ const result = [];
51
+ for (let i = start; i < stop; i += step) result.push(i);
52
+ return result;
53
+ }
2
54
 
3
- const isArray = Array.isArray;
4
- function singleOrNull(arr) {
5
- return arr.length === 1 ? arr[0] : null;
55
+ //#endregion
56
+ //#region src/base/boolean.ts
57
+ const isBoolean = (val) => typeof val === "boolean";
58
+
59
+ //#endregion
60
+ //#region src/base/string.ts
61
+ const toString = (val) => Object.prototype.toString.call(val);
62
+ const isString = (val) => typeof val === "string";
63
+ const slash = (str) => str.replace(/\\/g, "/");
64
+ const ensurePrefix = (prefix, str) => str.startsWith(prefix) ? str : prefix + str;
65
+ const ensureSuffix = (suffix, str) => str.endsWith(suffix) ? str : str + suffix;
66
+ const capitalize = (str) => str[0].toUpperCase() + str.slice(1).toLowerCase();
67
+
68
+ //#endregion
69
+ //#region src/base/date.ts
70
+ const isDate = (val) => toString(val) === "[object Date]";
71
+ const timestamp = () => +Date.now();
72
+ function isSameDay(dateA, dateB = /* @__PURE__ */ new Date()) {
73
+ return dateA.getDate() === dateB.getDate() && dateA.getMonth() === dateB.getMonth() && dateA.getFullYear() === dateB.getFullYear();
6
74
  }
7
- function createArray(length, mapFn) {
8
- return Array.from({ length }, (_, index) => mapFn?.(index) ?? index);
75
+ function dateCalculate(date, value, unit = "dayOfMonth") {
76
+ const result = new Date(date);
77
+ switch (unit) {
78
+ case "year":
79
+ result.setFullYear(date.getFullYear() + value);
80
+ break;
81
+ case "month":
82
+ result.setMonth(date.getMonth() + value);
83
+ break;
84
+ case "dayOfMonth":
85
+ result.setDate(date.getDate() + value);
86
+ break;
87
+ case "dayOfWeek":
88
+ result.setDate(date.getDay() + value);
89
+ break;
90
+ case "hour":
91
+ result.setHours(date.getHours() + value);
92
+ break;
93
+ case "minute":
94
+ result.setMinutes(date.getMinutes() + value);
95
+ break;
96
+ case "second":
97
+ result.setSeconds(date.getSeconds() + value);
98
+ break;
99
+ case "millisecond":
100
+ result.setMilliseconds(date.getMilliseconds() + value);
101
+ break;
102
+ default: return date;
103
+ }
104
+ return result;
9
105
  }
10
106
 
11
- function cleanText(str, searchValue) {
12
- return str.replace(searchValue, "");
107
+ //#endregion
108
+ //#region src/base/function.ts
109
+ const isFunction = (val) => typeof val === "function";
110
+ const noop = () => {};
111
+ const transform = (val, fn) => fn(val);
112
+
113
+ //#endregion
114
+ //#region src/base/nullable.ts
115
+ const isTruthy = (v) => Boolean(v);
116
+ const isUndefined = (val) => toString(val) === "[object Undefined]";
117
+ const notUndefined = (v) => v !== void 0;
118
+ const isNull = (val) => toString(val) === "[object Null]";
119
+ const notNull = (v) => v !== null;
120
+ const notNullish = (v) => v != null;
121
+ const isDefined = (val) => typeof val !== "undefined";
122
+
123
+ //#endregion
124
+ //#region src/base/number.ts
125
+ const isNumber = (val) => typeof val === "number";
126
+ const clamp = (n, min, max) => Math.min(max, Math.max(min, n));
127
+ function lerp(min, max, t) {
128
+ const interpolation = clamp(t, 0, 1);
129
+ return min + (max - min) * interpolation;
13
130
  }
14
131
 
15
- function getValueOrUndefined(condition, value) {
16
- return condition ? value : void 0;
132
+ //#endregion
133
+ //#region src/base/object.ts
134
+ const isObject = (val) => toString(val) === "[object Object]";
135
+ const isKeyOf = (val, key) => key in val;
136
+ function objectFilter(obj, fn) {
137
+ return Object.fromEntries(Object.entries(obj).filter(([key, value]) => fn(key, value)));
17
138
  }
18
- function from(value) {
19
- return {
20
- get: () => value,
21
- to: (fn) => from(fn(value))
22
- };
139
+ function objectPick(obj, ...keys) {
140
+ return objectFilter(obj, (key) => keys.includes(key));
23
141
  }
142
+ function objectOmit(obj, ...keys) {
143
+ return objectFilter(obj, (key) => !keys.includes(key));
144
+ }
145
+ function clearUndefined(obj) {
146
+ return objectFilter(obj, (_, value) => notUndefined(value));
147
+ }
148
+
149
+ //#endregion
150
+ //#region src/base/regexp.ts
151
+ const isRegExp = (val) => toString(val) === "[object RegExp]";
24
152
 
25
- export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
153
+ //#endregion
154
+ export { assert, capitalize, clamp, clearUndefined, createArray, dateCalculate, ensurePrefix, ensureSuffix, isArray, isBoolean, isDate, isDefined, isFunction, isKeyOf, isNull, isNumber, isObject, isRegExp, isSameDay, isString, isTruthy, isUndefined, lerp, naturalCompare, noop, notNull, notNullish, notUndefined, objectFilter, objectOmit, objectPick, range, shuffle, singleOrNull, slash, 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.3.6",
4
+ "version": "1.4.0",
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.ts"
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.ts",
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/natural-compare": "^1.4.3",
38
- "@types/node": "^24.0.3",
39
- "bumpp": "^10.2.0",
40
- "eslint": "^9.29.0",
41
- "natural-compare": "^1.4.0",
42
- "typescript": "^5.8.3",
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": "unbuild",
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,15 +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
- declare function cleanText(str: string, searchValue: RegExp | string): string;
8
-
9
- declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
10
- declare function from<T>(value: T): {
11
- get: () => T;
12
- to: <R>(fn: (value: T) => R) => ReturnType<typeof from<R>>;
13
- };
14
-
15
- export { cleanText, createArray, from, getValueOrUndefined, isArray, singleOrNull };
@@ -1,6 +0,0 @@
1
- declare const naturalCompare: {
2
- (a: string, b: string): -1 | 0 | 1;
3
- (a: number, b: number): -1 | 0 | 1;
4
- };
5
-
6
- export { naturalCompare as default, naturalCompare };
@@ -1,6 +0,0 @@
1
- declare const naturalCompare: {
2
- (a: string, b: string): -1 | 0 | 1;
3
- (a: number, b: number): -1 | 0 | 1;
4
- };
5
-
6
- export { naturalCompare as default, naturalCompare };
@@ -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 };