@jiakun-zhao/utils 1.3.0 → 1.3.2
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 +4 -79
- package/dist/index.d.cts +3 -84
- package/dist/index.d.mts +3 -84
- package/dist/index.d.ts +3 -84
- package/dist/index.mjs +4 -77
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -6,10 +6,10 @@ const isArray = Array.isArray;
|
|
|
6
6
|
function singleOrNull(arr) {
|
|
7
7
|
return arr.length === 1 ? arr[0] : null;
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
return fn(value);
|
|
9
|
+
function createArray(length, mapFn) {
|
|
10
|
+
return Array.from({ length }, (_, index) => mapFn?.(index) ?? index);
|
|
12
11
|
}
|
|
12
|
+
|
|
13
13
|
function safe(fn) {
|
|
14
14
|
try {
|
|
15
15
|
const res = fn();
|
|
@@ -23,71 +23,6 @@ function random(max, min = 0) {
|
|
|
23
23
|
return Math.floor(Math.random() * max) + min;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
const colors = {
|
|
27
|
-
/** 重置颜色 */
|
|
28
|
-
reset: "\x1B[0m",
|
|
29
|
-
/** 亮色(加粗) */
|
|
30
|
-
bright: "\x1B[1m",
|
|
31
|
-
/** 斜体 */
|
|
32
|
-
italic: "\x1B[3m",
|
|
33
|
-
/** 下划线 */
|
|
34
|
-
underline: "\x1B[4m",
|
|
35
|
-
/** 反向 */
|
|
36
|
-
reverse: "\x1B[7m",
|
|
37
|
-
/** 隐藏 */
|
|
38
|
-
hidden: "\x1B[8m",
|
|
39
|
-
/** 灰色 */
|
|
40
|
-
grey: "\x1B[2m",
|
|
41
|
-
/** 黑色 */
|
|
42
|
-
black: "\x1B[30m",
|
|
43
|
-
/** 红色 */
|
|
44
|
-
red: "\x1B[31m",
|
|
45
|
-
/** 绿色 */
|
|
46
|
-
green: "\x1B[32m",
|
|
47
|
-
/** 黄色 */
|
|
48
|
-
yellow: "\x1B[33m",
|
|
49
|
-
/** 蓝色 */
|
|
50
|
-
blue: "\x1B[34m",
|
|
51
|
-
/** 品红 */
|
|
52
|
-
magenta: "\x1B[35m",
|
|
53
|
-
/** 青色 */
|
|
54
|
-
cyan: "\x1B[36m",
|
|
55
|
-
/** 白色 */
|
|
56
|
-
white: "\x1B[37m",
|
|
57
|
-
/** 背景黑色 */
|
|
58
|
-
bgBlack: "\x1B[40m",
|
|
59
|
-
/** 背景红色 */
|
|
60
|
-
bgRed: "\x1B[41m",
|
|
61
|
-
/** 背景绿色 */
|
|
62
|
-
bgGreen: "\x1B[42m",
|
|
63
|
-
/** 背景黄色 */
|
|
64
|
-
bgYellow: "\x1B[43m",
|
|
65
|
-
/** 背景蓝色 */
|
|
66
|
-
bgBlue: "\x1B[44m",
|
|
67
|
-
/** 背景品红 */
|
|
68
|
-
bgMagenta: "\x1B[45m",
|
|
69
|
-
/** 背景青色 */
|
|
70
|
-
bgCyan: "\x1B[46m",
|
|
71
|
-
/** 背景白色 */
|
|
72
|
-
bgWhite: "\x1B[47m"
|
|
73
|
-
};
|
|
74
|
-
const messages = {
|
|
75
|
-
info: { color: colors.blue, symbol: "\u{1D4F2}" },
|
|
76
|
-
warn: { color: colors.yellow, symbol: "\u{1D4F2}" },
|
|
77
|
-
success: { color: colors.green, symbol: "\u2714\uFE0E" },
|
|
78
|
-
error: { color: colors.red, symbol: "\u2718" }
|
|
79
|
-
};
|
|
80
|
-
function print(...args) {
|
|
81
|
-
if (!args.length)
|
|
82
|
-
return;
|
|
83
|
-
if (utils.isString(args[0])) {
|
|
84
|
-
const [msg, type = "info"] = args;
|
|
85
|
-
console.log(`${messages[type].color}${messages[type].symbol} ${colors.grey}${msg}${colors.reset}`);
|
|
86
|
-
}
|
|
87
|
-
let text = args.map((it) => `${it.color ? colors[it.color] : ""}${it.value}`).join("");
|
|
88
|
-
console.log(`${text}${colors.reset}`);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
26
|
function cleanText(str, searchValue) {
|
|
92
27
|
return str.replace(searchValue, "");
|
|
93
28
|
}
|
|
@@ -95,24 +30,14 @@ function cleanText(str, searchValue) {
|
|
|
95
30
|
function getValueOrUndefined(condition, value) {
|
|
96
31
|
return condition ? value : void 0;
|
|
97
32
|
}
|
|
98
|
-
function setFromObject(entry, obj, format) {
|
|
99
|
-
Object.entries(obj).forEach(([name, value], index) => {
|
|
100
|
-
const [_name, _value] = format?.(name, value, index) ?? [name, value];
|
|
101
|
-
entry.set(_name, _value);
|
|
102
|
-
});
|
|
103
|
-
return entry;
|
|
104
|
-
}
|
|
105
33
|
|
|
106
34
|
exports.cleanText = cleanText;
|
|
107
|
-
exports.
|
|
35
|
+
exports.createArray = createArray;
|
|
108
36
|
exports.getValueOrUndefined = getValueOrUndefined;
|
|
109
37
|
exports.isArray = isArray;
|
|
110
|
-
exports.print = print;
|
|
111
38
|
exports.random = random;
|
|
112
39
|
exports.safe = safe;
|
|
113
|
-
exports.setFromObject = setFromObject;
|
|
114
40
|
exports.singleOrNull = singleOrNull;
|
|
115
|
-
exports.transform = transform;
|
|
116
41
|
Object.prototype.hasOwnProperty.call(utils, '__proto__') &&
|
|
117
42
|
!Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
|
|
118
43
|
Object.defineProperty(exports, '__proto__', {
|
package/dist/index.d.cts
CHANGED
|
@@ -2,8 +2,8 @@ export * from '@antfu/utils';
|
|
|
2
2
|
|
|
3
3
|
declare const isArray: (arg: any) => arg is any[];
|
|
4
4
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
5
|
+
declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
|
|
5
6
|
|
|
6
|
-
declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
7
7
|
declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -11,89 +11,8 @@ declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T>
|
|
|
11
11
|
*/
|
|
12
12
|
declare function random(max: number, min?: number): number;
|
|
13
13
|
|
|
14
|
-
declare const colors: {
|
|
15
|
-
/** 重置颜色 */
|
|
16
|
-
readonly reset: "\u001B[0m";
|
|
17
|
-
/** 亮色(加粗) */
|
|
18
|
-
readonly bright: "\u001B[1m";
|
|
19
|
-
/** 斜体 */
|
|
20
|
-
readonly italic: "\u001B[3m";
|
|
21
|
-
/** 下划线 */
|
|
22
|
-
readonly underline: "\u001B[4m";
|
|
23
|
-
/** 反向 */
|
|
24
|
-
readonly reverse: "\u001B[7m";
|
|
25
|
-
/** 隐藏 */
|
|
26
|
-
readonly hidden: "\u001B[8m";
|
|
27
|
-
/** 灰色 */
|
|
28
|
-
readonly grey: "\u001B[2m";
|
|
29
|
-
/** 黑色 */
|
|
30
|
-
readonly black: "\u001B[30m";
|
|
31
|
-
/** 红色 */
|
|
32
|
-
readonly red: "\u001B[31m";
|
|
33
|
-
/** 绿色 */
|
|
34
|
-
readonly green: "\u001B[32m";
|
|
35
|
-
/** 黄色 */
|
|
36
|
-
readonly yellow: "\u001B[33m";
|
|
37
|
-
/** 蓝色 */
|
|
38
|
-
readonly blue: "\u001B[34m";
|
|
39
|
-
/** 品红 */
|
|
40
|
-
readonly magenta: "\u001B[35m";
|
|
41
|
-
/** 青色 */
|
|
42
|
-
readonly cyan: "\u001B[36m";
|
|
43
|
-
/** 白色 */
|
|
44
|
-
readonly white: "\u001B[37m";
|
|
45
|
-
/** 背景黑色 */
|
|
46
|
-
readonly bgBlack: "\u001B[40m";
|
|
47
|
-
/** 背景红色 */
|
|
48
|
-
readonly bgRed: "\u001B[41m";
|
|
49
|
-
/** 背景绿色 */
|
|
50
|
-
readonly bgGreen: "\u001B[42m";
|
|
51
|
-
/** 背景黄色 */
|
|
52
|
-
readonly bgYellow: "\u001B[43m";
|
|
53
|
-
/** 背景蓝色 */
|
|
54
|
-
readonly bgBlue: "\u001B[44m";
|
|
55
|
-
/** 背景品红 */
|
|
56
|
-
readonly bgMagenta: "\u001B[45m";
|
|
57
|
-
/** 背景青色 */
|
|
58
|
-
readonly bgCyan: "\u001B[46m";
|
|
59
|
-
/** 背景白色 */
|
|
60
|
-
readonly bgWhite: "\u001B[47m";
|
|
61
|
-
};
|
|
62
|
-
declare const messages: {
|
|
63
|
-
readonly info: {
|
|
64
|
-
readonly color: "\u001B[34m";
|
|
65
|
-
readonly symbol: "𝓲";
|
|
66
|
-
};
|
|
67
|
-
readonly warn: {
|
|
68
|
-
readonly color: "\u001B[33m";
|
|
69
|
-
readonly symbol: "𝓲";
|
|
70
|
-
};
|
|
71
|
-
readonly success: {
|
|
72
|
-
readonly color: "\u001B[32m";
|
|
73
|
-
readonly symbol: "✔︎";
|
|
74
|
-
};
|
|
75
|
-
readonly error: {
|
|
76
|
-
readonly color: "\u001B[31m";
|
|
77
|
-
readonly symbol: "✘";
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* process.stdout.write('msg')
|
|
82
|
-
* process.stdout.clearLine(0)
|
|
83
|
-
*/
|
|
84
|
-
type ColorType = keyof typeof colors;
|
|
85
|
-
type MessageType = keyof typeof messages;
|
|
86
|
-
declare function print(msg: string, type?: MessageType): void;
|
|
87
|
-
declare function print(...msg: {
|
|
88
|
-
color?: ColorType;
|
|
89
|
-
value: string;
|
|
90
|
-
}[]): void;
|
|
91
|
-
|
|
92
14
|
declare function cleanText(str: string, searchValue: RegExp | string): string;
|
|
93
15
|
|
|
94
|
-
declare function getValueOrUndefined<T>(condition:
|
|
95
|
-
declare function setFromObject<Value, Entry extends {
|
|
96
|
-
set: (name: string, value: Value) => any;
|
|
97
|
-
}>(entry: Entry, obj: Record<string, Value>, format?: (name: string, value: Value, index: number) => [string, Value]): Entry;
|
|
16
|
+
declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
|
|
98
17
|
|
|
99
|
-
export { cleanText,
|
|
18
|
+
export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
|
package/dist/index.d.mts
CHANGED
|
@@ -2,8 +2,8 @@ export * from '@antfu/utils';
|
|
|
2
2
|
|
|
3
3
|
declare const isArray: (arg: any) => arg is any[];
|
|
4
4
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
5
|
+
declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
|
|
5
6
|
|
|
6
|
-
declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
7
7
|
declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -11,89 +11,8 @@ declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T>
|
|
|
11
11
|
*/
|
|
12
12
|
declare function random(max: number, min?: number): number;
|
|
13
13
|
|
|
14
|
-
declare const colors: {
|
|
15
|
-
/** 重置颜色 */
|
|
16
|
-
readonly reset: "\u001B[0m";
|
|
17
|
-
/** 亮色(加粗) */
|
|
18
|
-
readonly bright: "\u001B[1m";
|
|
19
|
-
/** 斜体 */
|
|
20
|
-
readonly italic: "\u001B[3m";
|
|
21
|
-
/** 下划线 */
|
|
22
|
-
readonly underline: "\u001B[4m";
|
|
23
|
-
/** 反向 */
|
|
24
|
-
readonly reverse: "\u001B[7m";
|
|
25
|
-
/** 隐藏 */
|
|
26
|
-
readonly hidden: "\u001B[8m";
|
|
27
|
-
/** 灰色 */
|
|
28
|
-
readonly grey: "\u001B[2m";
|
|
29
|
-
/** 黑色 */
|
|
30
|
-
readonly black: "\u001B[30m";
|
|
31
|
-
/** 红色 */
|
|
32
|
-
readonly red: "\u001B[31m";
|
|
33
|
-
/** 绿色 */
|
|
34
|
-
readonly green: "\u001B[32m";
|
|
35
|
-
/** 黄色 */
|
|
36
|
-
readonly yellow: "\u001B[33m";
|
|
37
|
-
/** 蓝色 */
|
|
38
|
-
readonly blue: "\u001B[34m";
|
|
39
|
-
/** 品红 */
|
|
40
|
-
readonly magenta: "\u001B[35m";
|
|
41
|
-
/** 青色 */
|
|
42
|
-
readonly cyan: "\u001B[36m";
|
|
43
|
-
/** 白色 */
|
|
44
|
-
readonly white: "\u001B[37m";
|
|
45
|
-
/** 背景黑色 */
|
|
46
|
-
readonly bgBlack: "\u001B[40m";
|
|
47
|
-
/** 背景红色 */
|
|
48
|
-
readonly bgRed: "\u001B[41m";
|
|
49
|
-
/** 背景绿色 */
|
|
50
|
-
readonly bgGreen: "\u001B[42m";
|
|
51
|
-
/** 背景黄色 */
|
|
52
|
-
readonly bgYellow: "\u001B[43m";
|
|
53
|
-
/** 背景蓝色 */
|
|
54
|
-
readonly bgBlue: "\u001B[44m";
|
|
55
|
-
/** 背景品红 */
|
|
56
|
-
readonly bgMagenta: "\u001B[45m";
|
|
57
|
-
/** 背景青色 */
|
|
58
|
-
readonly bgCyan: "\u001B[46m";
|
|
59
|
-
/** 背景白色 */
|
|
60
|
-
readonly bgWhite: "\u001B[47m";
|
|
61
|
-
};
|
|
62
|
-
declare const messages: {
|
|
63
|
-
readonly info: {
|
|
64
|
-
readonly color: "\u001B[34m";
|
|
65
|
-
readonly symbol: "𝓲";
|
|
66
|
-
};
|
|
67
|
-
readonly warn: {
|
|
68
|
-
readonly color: "\u001B[33m";
|
|
69
|
-
readonly symbol: "𝓲";
|
|
70
|
-
};
|
|
71
|
-
readonly success: {
|
|
72
|
-
readonly color: "\u001B[32m";
|
|
73
|
-
readonly symbol: "✔︎";
|
|
74
|
-
};
|
|
75
|
-
readonly error: {
|
|
76
|
-
readonly color: "\u001B[31m";
|
|
77
|
-
readonly symbol: "✘";
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* process.stdout.write('msg')
|
|
82
|
-
* process.stdout.clearLine(0)
|
|
83
|
-
*/
|
|
84
|
-
type ColorType = keyof typeof colors;
|
|
85
|
-
type MessageType = keyof typeof messages;
|
|
86
|
-
declare function print(msg: string, type?: MessageType): void;
|
|
87
|
-
declare function print(...msg: {
|
|
88
|
-
color?: ColorType;
|
|
89
|
-
value: string;
|
|
90
|
-
}[]): void;
|
|
91
|
-
|
|
92
14
|
declare function cleanText(str: string, searchValue: RegExp | string): string;
|
|
93
15
|
|
|
94
|
-
declare function getValueOrUndefined<T>(condition:
|
|
95
|
-
declare function setFromObject<Value, Entry extends {
|
|
96
|
-
set: (name: string, value: Value) => any;
|
|
97
|
-
}>(entry: Entry, obj: Record<string, Value>, format?: (name: string, value: Value, index: number) => [string, Value]): Entry;
|
|
16
|
+
declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
|
|
98
17
|
|
|
99
|
-
export { cleanText,
|
|
18
|
+
export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ export * from '@antfu/utils';
|
|
|
2
2
|
|
|
3
3
|
declare const isArray: (arg: any) => arg is any[];
|
|
4
4
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
5
|
+
declare function createArray<T = number>(length: number, mapFn?: (index: number) => T): T[];
|
|
5
6
|
|
|
6
|
-
declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
7
7
|
declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
|
|
8
8
|
|
|
9
9
|
/**
|
|
@@ -11,89 +11,8 @@ declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T>
|
|
|
11
11
|
*/
|
|
12
12
|
declare function random(max: number, min?: number): number;
|
|
13
13
|
|
|
14
|
-
declare const colors: {
|
|
15
|
-
/** 重置颜色 */
|
|
16
|
-
readonly reset: "\u001B[0m";
|
|
17
|
-
/** 亮色(加粗) */
|
|
18
|
-
readonly bright: "\u001B[1m";
|
|
19
|
-
/** 斜体 */
|
|
20
|
-
readonly italic: "\u001B[3m";
|
|
21
|
-
/** 下划线 */
|
|
22
|
-
readonly underline: "\u001B[4m";
|
|
23
|
-
/** 反向 */
|
|
24
|
-
readonly reverse: "\u001B[7m";
|
|
25
|
-
/** 隐藏 */
|
|
26
|
-
readonly hidden: "\u001B[8m";
|
|
27
|
-
/** 灰色 */
|
|
28
|
-
readonly grey: "\u001B[2m";
|
|
29
|
-
/** 黑色 */
|
|
30
|
-
readonly black: "\u001B[30m";
|
|
31
|
-
/** 红色 */
|
|
32
|
-
readonly red: "\u001B[31m";
|
|
33
|
-
/** 绿色 */
|
|
34
|
-
readonly green: "\u001B[32m";
|
|
35
|
-
/** 黄色 */
|
|
36
|
-
readonly yellow: "\u001B[33m";
|
|
37
|
-
/** 蓝色 */
|
|
38
|
-
readonly blue: "\u001B[34m";
|
|
39
|
-
/** 品红 */
|
|
40
|
-
readonly magenta: "\u001B[35m";
|
|
41
|
-
/** 青色 */
|
|
42
|
-
readonly cyan: "\u001B[36m";
|
|
43
|
-
/** 白色 */
|
|
44
|
-
readonly white: "\u001B[37m";
|
|
45
|
-
/** 背景黑色 */
|
|
46
|
-
readonly bgBlack: "\u001B[40m";
|
|
47
|
-
/** 背景红色 */
|
|
48
|
-
readonly bgRed: "\u001B[41m";
|
|
49
|
-
/** 背景绿色 */
|
|
50
|
-
readonly bgGreen: "\u001B[42m";
|
|
51
|
-
/** 背景黄色 */
|
|
52
|
-
readonly bgYellow: "\u001B[43m";
|
|
53
|
-
/** 背景蓝色 */
|
|
54
|
-
readonly bgBlue: "\u001B[44m";
|
|
55
|
-
/** 背景品红 */
|
|
56
|
-
readonly bgMagenta: "\u001B[45m";
|
|
57
|
-
/** 背景青色 */
|
|
58
|
-
readonly bgCyan: "\u001B[46m";
|
|
59
|
-
/** 背景白色 */
|
|
60
|
-
readonly bgWhite: "\u001B[47m";
|
|
61
|
-
};
|
|
62
|
-
declare const messages: {
|
|
63
|
-
readonly info: {
|
|
64
|
-
readonly color: "\u001B[34m";
|
|
65
|
-
readonly symbol: "𝓲";
|
|
66
|
-
};
|
|
67
|
-
readonly warn: {
|
|
68
|
-
readonly color: "\u001B[33m";
|
|
69
|
-
readonly symbol: "𝓲";
|
|
70
|
-
};
|
|
71
|
-
readonly success: {
|
|
72
|
-
readonly color: "\u001B[32m";
|
|
73
|
-
readonly symbol: "✔︎";
|
|
74
|
-
};
|
|
75
|
-
readonly error: {
|
|
76
|
-
readonly color: "\u001B[31m";
|
|
77
|
-
readonly symbol: "✘";
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* process.stdout.write('msg')
|
|
82
|
-
* process.stdout.clearLine(0)
|
|
83
|
-
*/
|
|
84
|
-
type ColorType = keyof typeof colors;
|
|
85
|
-
type MessageType = keyof typeof messages;
|
|
86
|
-
declare function print(msg: string, type?: MessageType): void;
|
|
87
|
-
declare function print(...msg: {
|
|
88
|
-
color?: ColorType;
|
|
89
|
-
value: string;
|
|
90
|
-
}[]): void;
|
|
91
|
-
|
|
92
14
|
declare function cleanText(str: string, searchValue: RegExp | string): string;
|
|
93
15
|
|
|
94
|
-
declare function getValueOrUndefined<T>(condition:
|
|
95
|
-
declare function setFromObject<Value, Entry extends {
|
|
96
|
-
set: (name: string, value: Value) => any;
|
|
97
|
-
}>(entry: Entry, obj: Record<string, Value>, format?: (name: string, value: Value, index: number) => [string, Value]): Entry;
|
|
16
|
+
declare function getValueOrUndefined<T>(condition: any, value: T): T | undefined;
|
|
98
17
|
|
|
99
|
-
export { cleanText,
|
|
18
|
+
export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
|
package/dist/index.mjs
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { isString } from '@antfu/utils';
|
|
2
1
|
export * from '@antfu/utils';
|
|
3
2
|
|
|
4
3
|
const isArray = Array.isArray;
|
|
5
4
|
function singleOrNull(arr) {
|
|
6
5
|
return arr.length === 1 ? arr[0] : null;
|
|
7
6
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return fn(value);
|
|
7
|
+
function createArray(length, mapFn) {
|
|
8
|
+
return Array.from({ length }, (_, index) => mapFn?.(index) ?? index);
|
|
11
9
|
}
|
|
10
|
+
|
|
12
11
|
function safe(fn) {
|
|
13
12
|
try {
|
|
14
13
|
const res = fn();
|
|
@@ -22,71 +21,6 @@ function random(max, min = 0) {
|
|
|
22
21
|
return Math.floor(Math.random() * max) + min;
|
|
23
22
|
}
|
|
24
23
|
|
|
25
|
-
const colors = {
|
|
26
|
-
/** 重置颜色 */
|
|
27
|
-
reset: "\x1B[0m",
|
|
28
|
-
/** 亮色(加粗) */
|
|
29
|
-
bright: "\x1B[1m",
|
|
30
|
-
/** 斜体 */
|
|
31
|
-
italic: "\x1B[3m",
|
|
32
|
-
/** 下划线 */
|
|
33
|
-
underline: "\x1B[4m",
|
|
34
|
-
/** 反向 */
|
|
35
|
-
reverse: "\x1B[7m",
|
|
36
|
-
/** 隐藏 */
|
|
37
|
-
hidden: "\x1B[8m",
|
|
38
|
-
/** 灰色 */
|
|
39
|
-
grey: "\x1B[2m",
|
|
40
|
-
/** 黑色 */
|
|
41
|
-
black: "\x1B[30m",
|
|
42
|
-
/** 红色 */
|
|
43
|
-
red: "\x1B[31m",
|
|
44
|
-
/** 绿色 */
|
|
45
|
-
green: "\x1B[32m",
|
|
46
|
-
/** 黄色 */
|
|
47
|
-
yellow: "\x1B[33m",
|
|
48
|
-
/** 蓝色 */
|
|
49
|
-
blue: "\x1B[34m",
|
|
50
|
-
/** 品红 */
|
|
51
|
-
magenta: "\x1B[35m",
|
|
52
|
-
/** 青色 */
|
|
53
|
-
cyan: "\x1B[36m",
|
|
54
|
-
/** 白色 */
|
|
55
|
-
white: "\x1B[37m",
|
|
56
|
-
/** 背景黑色 */
|
|
57
|
-
bgBlack: "\x1B[40m",
|
|
58
|
-
/** 背景红色 */
|
|
59
|
-
bgRed: "\x1B[41m",
|
|
60
|
-
/** 背景绿色 */
|
|
61
|
-
bgGreen: "\x1B[42m",
|
|
62
|
-
/** 背景黄色 */
|
|
63
|
-
bgYellow: "\x1B[43m",
|
|
64
|
-
/** 背景蓝色 */
|
|
65
|
-
bgBlue: "\x1B[44m",
|
|
66
|
-
/** 背景品红 */
|
|
67
|
-
bgMagenta: "\x1B[45m",
|
|
68
|
-
/** 背景青色 */
|
|
69
|
-
bgCyan: "\x1B[46m",
|
|
70
|
-
/** 背景白色 */
|
|
71
|
-
bgWhite: "\x1B[47m"
|
|
72
|
-
};
|
|
73
|
-
const messages = {
|
|
74
|
-
info: { color: colors.blue, symbol: "\u{1D4F2}" },
|
|
75
|
-
warn: { color: colors.yellow, symbol: "\u{1D4F2}" },
|
|
76
|
-
success: { color: colors.green, symbol: "\u2714\uFE0E" },
|
|
77
|
-
error: { color: colors.red, symbol: "\u2718" }
|
|
78
|
-
};
|
|
79
|
-
function print(...args) {
|
|
80
|
-
if (!args.length)
|
|
81
|
-
return;
|
|
82
|
-
if (isString(args[0])) {
|
|
83
|
-
const [msg, type = "info"] = args;
|
|
84
|
-
console.log(`${messages[type].color}${messages[type].symbol} ${colors.grey}${msg}${colors.reset}`);
|
|
85
|
-
}
|
|
86
|
-
let text = args.map((it) => `${it.color ? colors[it.color] : ""}${it.value}`).join("");
|
|
87
|
-
console.log(`${text}${colors.reset}`);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
24
|
function cleanText(str, searchValue) {
|
|
91
25
|
return str.replace(searchValue, "");
|
|
92
26
|
}
|
|
@@ -94,12 +28,5 @@ function cleanText(str, searchValue) {
|
|
|
94
28
|
function getValueOrUndefined(condition, value) {
|
|
95
29
|
return condition ? value : void 0;
|
|
96
30
|
}
|
|
97
|
-
function setFromObject(entry, obj, format) {
|
|
98
|
-
Object.entries(obj).forEach(([name, value], index) => {
|
|
99
|
-
const [_name, _value] = format?.(name, value, index) ?? [name, value];
|
|
100
|
-
entry.set(_name, _value);
|
|
101
|
-
});
|
|
102
|
-
return entry;
|
|
103
|
-
}
|
|
104
31
|
|
|
105
|
-
export { cleanText,
|
|
32
|
+
export { cleanText, createArray, getValueOrUndefined, isArray, random, safe, singleOrNull };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jiakun-zhao/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.3.
|
|
4
|
+
"version": "1.3.2",
|
|
5
5
|
"description": "Utils.",
|
|
6
6
|
"author": "Jiakun Zhao <hi@zhaojiakun.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -27,15 +27,15 @@
|
|
|
27
27
|
"dist"
|
|
28
28
|
],
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@antfu/utils": "^9.
|
|
30
|
+
"@antfu/utils": "^9.2.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@antfu/eslint-config": "^4.
|
|
34
|
-
"@types/node": "^22.
|
|
35
|
-
"bumpp": "^10.0
|
|
36
|
-
"eslint": "^9.
|
|
37
|
-
"typescript": "^5.
|
|
38
|
-
"unbuild": "^3.
|
|
33
|
+
"@antfu/eslint-config": "^4.12.0",
|
|
34
|
+
"@types/node": "^22.15.3",
|
|
35
|
+
"bumpp": "^10.1.0",
|
|
36
|
+
"eslint": "^9.25.1",
|
|
37
|
+
"typescript": "^5.8.3",
|
|
38
|
+
"unbuild": "^3.5.0"
|
|
39
39
|
},
|
|
40
40
|
"scripts": {
|
|
41
41
|
"build": "unbuild",
|