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