@jiakun-zhao/utils 1.2.0 → 1.3.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.cjs CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  const utils = require('@antfu/utils');
4
4
 
5
+ const isArray = Array.isArray;
5
6
  function singleOrNull(arr) {
6
7
  return arr.length === 1 ? arr[0] : null;
7
8
  }
@@ -11,8 +12,10 @@ function transform(value, fn) {
11
12
  }
12
13
  function safe(fn) {
13
14
  try {
14
- return fn();
15
+ const res = fn();
16
+ return res instanceof Promise ? res.catch(() => null) : res;
15
17
  } catch {
18
+ return null;
16
19
  }
17
20
  }
18
21
 
@@ -68,29 +71,55 @@ const colors = {
68
71
  /** 背景白色 */
69
72
  bgWhite: "\x1B[47m"
70
73
  };
71
- const map = {
74
+ const messages = {
72
75
  info: { color: colors.blue, symbol: "\u{1D4F2}" },
73
76
  warn: { color: colors.yellow, symbol: "\u{1D4F2}" },
74
77
  success: { color: colors.green, symbol: "\u2714\uFE0E" },
75
78
  error: { color: colors.red, symbol: "\u2718" }
76
79
  };
77
- function print(msg, type) {
78
- if (!type)
79
- console.log(msg);
80
- else
81
- console.log(`${map[type].color}${map[type].symbol} ${colors.grey}${msg}${colors.reset}`);
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}`);
82
89
  }
83
90
 
84
- const timestamp = () => +Date.now();
85
- const isArray = Array.isArray;
91
+ function cleanText(str, searchValue) {
92
+ return str.replace(searchValue, "");
93
+ }
86
94
 
95
+ function getValueOrUndefined(condition, value) {
96
+ return condition ? value : void 0;
97
+ }
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
+
106
+ exports.cleanText = cleanText;
107
+ exports.colors = colors;
108
+ exports.getValueOrUndefined = getValueOrUndefined;
87
109
  exports.isArray = isArray;
88
110
  exports.print = print;
89
111
  exports.random = random;
90
112
  exports.safe = safe;
113
+ exports.setFromObject = setFromObject;
91
114
  exports.singleOrNull = singleOrNull;
92
- exports.timestamp = timestamp;
93
115
  exports.transform = transform;
116
+ Object.prototype.hasOwnProperty.call(utils, '__proto__') &&
117
+ !Object.prototype.hasOwnProperty.call(exports, '__proto__') &&
118
+ Object.defineProperty(exports, '__proto__', {
119
+ enumerable: true,
120
+ value: utils['__proto__']
121
+ });
122
+
94
123
  Object.keys(utils).forEach(function (k) {
95
124
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
96
125
  });
package/dist/index.d.cts CHANGED
@@ -1,40 +1,99 @@
1
1
  export * from '@antfu/utils';
2
2
 
3
+ declare const isArray: (arg: any) => arg is any[];
3
4
  declare function singleOrNull<T>(arr: T[]): T | null;
4
5
 
5
6
  declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
6
- declare function safe<R>(fn: () => R): R | undefined;
7
+ declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
7
8
 
8
9
  /**
9
10
  * @description min <= result < max
10
11
  */
11
12
  declare function random(max: number, min?: number): number;
12
13
 
13
- declare const map: {
14
- info: {
15
- color: string;
16
- symbol: string;
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: "𝓲";
17
66
  };
18
- warn: {
19
- color: string;
20
- symbol: string;
67
+ readonly warn: {
68
+ readonly color: "\u001B[33m";
69
+ readonly symbol: "𝓲";
21
70
  };
22
- success: {
23
- color: string;
24
- symbol: string;
71
+ readonly success: {
72
+ readonly color: "\u001B[32m";
73
+ readonly symbol: "✔︎";
25
74
  };
26
- error: {
27
- color: string;
28
- symbol: string;
75
+ readonly error: {
76
+ readonly color: "\u001B[31m";
77
+ readonly symbol: "✘";
29
78
  };
30
79
  };
31
80
  /**
32
81
  * process.stdout.write('msg')
33
82
  * process.stdout.clearLine(0)
34
83
  */
35
- declare function print(msg: any, type?: keyof typeof map): void;
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;
36
91
 
37
- declare const timestamp: () => number;
38
- declare const isArray: (arg: any) => arg is any[];
92
+ declare function cleanText(str: string, searchValue: RegExp | string): string;
93
+
94
+ declare function getValueOrUndefined<T>(condition: unknown, value: T): T | undefined;
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;
39
98
 
40
- export { isArray, print, random, safe, singleOrNull, timestamp, transform };
99
+ export { cleanText, colors, getValueOrUndefined, isArray, print, random, safe, setFromObject, singleOrNull, transform };
package/dist/index.d.mts CHANGED
@@ -1,40 +1,99 @@
1
1
  export * from '@antfu/utils';
2
2
 
3
+ declare const isArray: (arg: any) => arg is any[];
3
4
  declare function singleOrNull<T>(arr: T[]): T | null;
4
5
 
5
6
  declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
6
- declare function safe<R>(fn: () => R): R | undefined;
7
+ declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
7
8
 
8
9
  /**
9
10
  * @description min <= result < max
10
11
  */
11
12
  declare function random(max: number, min?: number): number;
12
13
 
13
- declare const map: {
14
- info: {
15
- color: string;
16
- symbol: string;
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: "𝓲";
17
66
  };
18
- warn: {
19
- color: string;
20
- symbol: string;
67
+ readonly warn: {
68
+ readonly color: "\u001B[33m";
69
+ readonly symbol: "𝓲";
21
70
  };
22
- success: {
23
- color: string;
24
- symbol: string;
71
+ readonly success: {
72
+ readonly color: "\u001B[32m";
73
+ readonly symbol: "✔︎";
25
74
  };
26
- error: {
27
- color: string;
28
- symbol: string;
75
+ readonly error: {
76
+ readonly color: "\u001B[31m";
77
+ readonly symbol: "✘";
29
78
  };
30
79
  };
31
80
  /**
32
81
  * process.stdout.write('msg')
33
82
  * process.stdout.clearLine(0)
34
83
  */
35
- declare function print(msg: any, type?: keyof typeof map): void;
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;
36
91
 
37
- declare const timestamp: () => number;
38
- declare const isArray: (arg: any) => arg is any[];
92
+ declare function cleanText(str: string, searchValue: RegExp | string): string;
93
+
94
+ declare function getValueOrUndefined<T>(condition: unknown, value: T): T | undefined;
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;
39
98
 
40
- export { isArray, print, random, safe, singleOrNull, timestamp, transform };
99
+ export { cleanText, colors, getValueOrUndefined, isArray, print, random, safe, setFromObject, singleOrNull, transform };
package/dist/index.d.ts CHANGED
@@ -1,40 +1,99 @@
1
1
  export * from '@antfu/utils';
2
2
 
3
+ declare const isArray: (arg: any) => arg is any[];
3
4
  declare function singleOrNull<T>(arr: T[]): T | null;
4
5
 
5
6
  declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
6
- declare function safe<R>(fn: () => R): R | undefined;
7
+ declare function safe<R>(fn: () => R): typeof fn extends () => Promise<infer T> ? Promise<T | null> : (R | null);
7
8
 
8
9
  /**
9
10
  * @description min <= result < max
10
11
  */
11
12
  declare function random(max: number, min?: number): number;
12
13
 
13
- declare const map: {
14
- info: {
15
- color: string;
16
- symbol: string;
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: "𝓲";
17
66
  };
18
- warn: {
19
- color: string;
20
- symbol: string;
67
+ readonly warn: {
68
+ readonly color: "\u001B[33m";
69
+ readonly symbol: "𝓲";
21
70
  };
22
- success: {
23
- color: string;
24
- symbol: string;
71
+ readonly success: {
72
+ readonly color: "\u001B[32m";
73
+ readonly symbol: "✔︎";
25
74
  };
26
- error: {
27
- color: string;
28
- symbol: string;
75
+ readonly error: {
76
+ readonly color: "\u001B[31m";
77
+ readonly symbol: "✘";
29
78
  };
30
79
  };
31
80
  /**
32
81
  * process.stdout.write('msg')
33
82
  * process.stdout.clearLine(0)
34
83
  */
35
- declare function print(msg: any, type?: keyof typeof map): void;
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;
36
91
 
37
- declare const timestamp: () => number;
38
- declare const isArray: (arg: any) => arg is any[];
92
+ declare function cleanText(str: string, searchValue: RegExp | string): string;
93
+
94
+ declare function getValueOrUndefined<T>(condition: unknown, value: T): T | undefined;
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;
39
98
 
40
- export { isArray, print, random, safe, singleOrNull, timestamp, transform };
99
+ export { cleanText, colors, getValueOrUndefined, isArray, print, random, safe, setFromObject, singleOrNull, transform };
package/dist/index.mjs CHANGED
@@ -1,5 +1,7 @@
1
+ import { isString } from '@antfu/utils';
1
2
  export * from '@antfu/utils';
2
3
 
4
+ const isArray = Array.isArray;
3
5
  function singleOrNull(arr) {
4
6
  return arr.length === 1 ? arr[0] : null;
5
7
  }
@@ -9,8 +11,10 @@ function transform(value, fn) {
9
11
  }
10
12
  function safe(fn) {
11
13
  try {
12
- return fn();
14
+ const res = fn();
15
+ return res instanceof Promise ? res.catch(() => null) : res;
13
16
  } catch {
17
+ return null;
14
18
  }
15
19
  }
16
20
 
@@ -66,20 +70,36 @@ const colors = {
66
70
  /** 背景白色 */
67
71
  bgWhite: "\x1B[47m"
68
72
  };
69
- const map = {
73
+ const messages = {
70
74
  info: { color: colors.blue, symbol: "\u{1D4F2}" },
71
75
  warn: { color: colors.yellow, symbol: "\u{1D4F2}" },
72
76
  success: { color: colors.green, symbol: "\u2714\uFE0E" },
73
77
  error: { color: colors.red, symbol: "\u2718" }
74
78
  };
75
- function print(msg, type) {
76
- if (!type)
77
- console.log(msg);
78
- else
79
- console.log(`${map[type].color}${map[type].symbol} ${colors.grey}${msg}${colors.reset}`);
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}`);
80
88
  }
81
89
 
82
- const timestamp = () => +Date.now();
83
- const isArray = Array.isArray;
90
+ function cleanText(str, searchValue) {
91
+ return str.replace(searchValue, "");
92
+ }
93
+
94
+ function getValueOrUndefined(condition, value) {
95
+ return condition ? value : void 0;
96
+ }
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
+ }
84
104
 
85
- export { isArray, print, random, safe, singleOrNull, timestamp, transform };
105
+ export { cleanText, colors, getValueOrUndefined, isArray, print, random, safe, setFromObject, singleOrNull, transform };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jiakun-zhao/utils",
3
3
  "type": "module",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "description": "Utils.",
6
6
  "author": "Jiakun Zhao <hi@zhaojiakun.com>",
7
7
  "license": "MIT",
@@ -27,21 +27,19 @@
27
27
  "dist"
28
28
  ],
29
29
  "dependencies": {
30
- "@antfu/utils": "^0.7.8"
30
+ "@antfu/utils": "^9.1.0"
31
31
  },
32
32
  "devDependencies": {
33
- "@antfu/eslint-config": "^2.19.0",
34
- "@types/node": "^20.12.12",
35
- "bumpp": "^9.4.1",
36
- "eslint": "^9.3.0",
37
- "typescript": "^5.4.5",
38
- "unbuild": "^2.0.0",
39
- "vitest": "^1.6.0"
33
+ "@antfu/eslint-config": "^4.3.0",
34
+ "@types/node": "^22.13.4",
35
+ "bumpp": "^10.0.3",
36
+ "eslint": "^9.20.1",
37
+ "typescript": "^5.7.3",
38
+ "unbuild": "^3.3.1"
40
39
  },
41
40
  "scripts": {
42
41
  "build": "unbuild",
43
42
  "stub": "unbuild --stub",
44
- "test": "vitest -u",
45
43
  "release": "bumpp && pnpm publish && npx cnpm sync @jiakun-zhao/utils"
46
44
  }
47
45
  }