@jiakun-zhao/utils 1.1.1 → 1.2.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/README.md +0 -1
- package/dist/index.cjs +69 -37
- package/dist/index.d.cts +29 -27
- package/dist/index.d.mts +29 -27
- package/dist/index.d.ts +29 -27
- package/dist/index.mjs +65 -23
- package/package.json +9 -20
- package/dist/fs.cjs +0 -45
- package/dist/fs.d.cts +0 -10
- package/dist/fs.d.mts +0 -10
- package/dist/fs.d.ts +0 -10
- package/dist/fs.mjs +0 -33
- package/fs.d.ts +0 -2
- package/index.d.ts +0 -2
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,34 +1,14 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const isBoolean = (val) => typeof val === "boolean";
|
|
5
|
-
const isNumber = (val) => typeof val === "number";
|
|
6
|
-
const isString = (val) => typeof val === "string";
|
|
7
|
-
const toString = (value) => Object.prototype.toString.call(value);
|
|
8
|
-
const isObject = (val) => toString(val) === "[object Object]";
|
|
9
|
-
const isUndefined = (val) => toString(val) === "[object Undefined]";
|
|
10
|
-
const isNull = (val) => toString(val) === "[object Null]";
|
|
11
|
-
const isRegExp = (val) => toString(val) === "[object RegExp]";
|
|
12
|
-
const isDate = (val) => toString(val) === "[object Date]";
|
|
13
|
-
const isArray = Array.isArray;
|
|
14
|
-
const notNullish = (v) => v != null;
|
|
15
|
-
const notNull = (v) => v !== null;
|
|
16
|
-
const notUndefined = (v) => v !== void 0;
|
|
3
|
+
const utils = require('@antfu/utils');
|
|
17
4
|
|
|
18
5
|
function singleOrNull(arr) {
|
|
19
6
|
return arr.length === 1 ? arr[0] : null;
|
|
20
7
|
}
|
|
21
|
-
function toArray(array) {
|
|
22
|
-
array = array ?? [];
|
|
23
|
-
return isArray(array) ? array : [array];
|
|
24
|
-
}
|
|
25
8
|
|
|
26
|
-
function
|
|
9
|
+
function transform(value, fn) {
|
|
27
10
|
return fn(value);
|
|
28
11
|
}
|
|
29
|
-
function takeIf(value, predicate, defaultValue) {
|
|
30
|
-
return predicate(value) ? value : notUndefined(defaultValue) ? defaultValue : null;
|
|
31
|
-
}
|
|
32
12
|
function safe(fn) {
|
|
33
13
|
try {
|
|
34
14
|
return fn();
|
|
@@ -40,25 +20,77 @@ function random(max, min = 0) {
|
|
|
40
20
|
return Math.floor(Math.random() * max) + min;
|
|
41
21
|
}
|
|
42
22
|
|
|
23
|
+
const colors = {
|
|
24
|
+
/** 重置颜色 */
|
|
25
|
+
reset: "\x1B[0m",
|
|
26
|
+
/** 亮色(加粗) */
|
|
27
|
+
bright: "\x1B[1m",
|
|
28
|
+
/** 斜体 */
|
|
29
|
+
italic: "\x1B[3m",
|
|
30
|
+
/** 下划线 */
|
|
31
|
+
underline: "\x1B[4m",
|
|
32
|
+
/** 反向 */
|
|
33
|
+
reverse: "\x1B[7m",
|
|
34
|
+
/** 隐藏 */
|
|
35
|
+
hidden: "\x1B[8m",
|
|
36
|
+
/** 灰色 */
|
|
37
|
+
grey: "\x1B[2m",
|
|
38
|
+
/** 黑色 */
|
|
39
|
+
black: "\x1B[30m",
|
|
40
|
+
/** 红色 */
|
|
41
|
+
red: "\x1B[31m",
|
|
42
|
+
/** 绿色 */
|
|
43
|
+
green: "\x1B[32m",
|
|
44
|
+
/** 黄色 */
|
|
45
|
+
yellow: "\x1B[33m",
|
|
46
|
+
/** 蓝色 */
|
|
47
|
+
blue: "\x1B[34m",
|
|
48
|
+
/** 品红 */
|
|
49
|
+
magenta: "\x1B[35m",
|
|
50
|
+
/** 青色 */
|
|
51
|
+
cyan: "\x1B[36m",
|
|
52
|
+
/** 白色 */
|
|
53
|
+
white: "\x1B[37m",
|
|
54
|
+
/** 背景黑色 */
|
|
55
|
+
bgBlack: "\x1B[40m",
|
|
56
|
+
/** 背景红色 */
|
|
57
|
+
bgRed: "\x1B[41m",
|
|
58
|
+
/** 背景绿色 */
|
|
59
|
+
bgGreen: "\x1B[42m",
|
|
60
|
+
/** 背景黄色 */
|
|
61
|
+
bgYellow: "\x1B[43m",
|
|
62
|
+
/** 背景蓝色 */
|
|
63
|
+
bgBlue: "\x1B[44m",
|
|
64
|
+
/** 背景品红 */
|
|
65
|
+
bgMagenta: "\x1B[45m",
|
|
66
|
+
/** 背景青色 */
|
|
67
|
+
bgCyan: "\x1B[46m",
|
|
68
|
+
/** 背景白色 */
|
|
69
|
+
bgWhite: "\x1B[47m"
|
|
70
|
+
};
|
|
71
|
+
const map = {
|
|
72
|
+
info: { color: colors.blue, symbol: "\u{1D4F2}" },
|
|
73
|
+
warn: { color: colors.yellow, symbol: "\u{1D4F2}" },
|
|
74
|
+
success: { color: colors.green, symbol: "\u2714\uFE0E" },
|
|
75
|
+
error: { color: colors.red, symbol: "\u2718" }
|
|
76
|
+
};
|
|
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}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
43
84
|
const timestamp = () => +Date.now();
|
|
85
|
+
const isArray = Array.isArray;
|
|
44
86
|
|
|
45
87
|
exports.isArray = isArray;
|
|
46
|
-
exports.
|
|
47
|
-
exports.isDate = isDate;
|
|
48
|
-
exports.isFunction = isFunction;
|
|
49
|
-
exports.isNull = isNull;
|
|
50
|
-
exports.isNumber = isNumber;
|
|
51
|
-
exports.isObject = isObject;
|
|
52
|
-
exports.isRegExp = isRegExp;
|
|
53
|
-
exports.isString = isString;
|
|
54
|
-
exports.isUndefined = isUndefined;
|
|
55
|
-
exports.notNull = notNull;
|
|
56
|
-
exports.notNullish = notNullish;
|
|
57
|
-
exports.notUndefined = notUndefined;
|
|
88
|
+
exports.print = print;
|
|
58
89
|
exports.random = random;
|
|
59
90
|
exports.safe = safe;
|
|
60
91
|
exports.singleOrNull = singleOrNull;
|
|
61
|
-
exports.take = take;
|
|
62
|
-
exports.takeIf = takeIf;
|
|
63
92
|
exports.timestamp = timestamp;
|
|
64
|
-
exports.
|
|
93
|
+
exports.transform = transform;
|
|
94
|
+
Object.keys(utils).forEach(function (k) {
|
|
95
|
+
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) exports[k] = utils[k];
|
|
96
|
+
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
|
|
1
3
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
2
|
-
declare function toArray<T>(array?: T | Array<T>): Array<T>;
|
|
3
4
|
|
|
4
|
-
declare function
|
|
5
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
6
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
|
|
5
|
+
declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
7
6
|
declare function safe<R>(fn: () => R): R | undefined;
|
|
8
7
|
|
|
9
|
-
type Nullable<T> = T | null | undefined;
|
|
10
|
-
type NotNullable<T> = Exclude<T, null | undefined>;
|
|
11
|
-
type NotNull<T> = Exclude<T, null>;
|
|
12
|
-
type NotUndefined<T> = Exclude<T, undefined>;
|
|
13
|
-
type Arrayable<T> = T | Array<T>;
|
|
14
|
-
type ElementOf<T> = T extends Array<infer Element> ? Element : never;
|
|
15
|
-
type Fn<R = void> = (...args: any[]) => R;
|
|
16
|
-
|
|
17
|
-
declare const isFunction: (val: any) => val is Fn;
|
|
18
|
-
declare const isBoolean: (val: any) => val is boolean;
|
|
19
|
-
declare const isNumber: (val: any) => val is number;
|
|
20
|
-
declare const isString: (val: unknown) => val is string;
|
|
21
|
-
declare const isObject: (val: any) => val is object;
|
|
22
|
-
declare const isUndefined: (val: any) => val is undefined;
|
|
23
|
-
declare const isNull: (val: any) => val is null;
|
|
24
|
-
declare const isRegExp: (val: any) => val is RegExp;
|
|
25
|
-
declare const isDate: (val: any) => val is Date;
|
|
26
|
-
declare const isArray: (arg: any) => arg is any[];
|
|
27
|
-
declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
|
|
28
|
-
declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
|
|
29
|
-
declare const notUndefined: <T>(v: T | undefined) => v is Exclude<T, undefined>;
|
|
30
|
-
|
|
31
8
|
/**
|
|
32
9
|
* @description min <= result < max
|
|
33
10
|
*/
|
|
34
11
|
declare function random(max: number, min?: number): number;
|
|
35
12
|
|
|
13
|
+
declare const map: {
|
|
14
|
+
info: {
|
|
15
|
+
color: string;
|
|
16
|
+
symbol: string;
|
|
17
|
+
};
|
|
18
|
+
warn: {
|
|
19
|
+
color: string;
|
|
20
|
+
symbol: string;
|
|
21
|
+
};
|
|
22
|
+
success: {
|
|
23
|
+
color: string;
|
|
24
|
+
symbol: string;
|
|
25
|
+
};
|
|
26
|
+
error: {
|
|
27
|
+
color: string;
|
|
28
|
+
symbol: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* process.stdout.write('msg')
|
|
33
|
+
* process.stdout.clearLine(0)
|
|
34
|
+
*/
|
|
35
|
+
declare function print(msg: any, type?: keyof typeof map): void;
|
|
36
|
+
|
|
36
37
|
declare const timestamp: () => number;
|
|
38
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
37
39
|
|
|
38
|
-
export {
|
|
40
|
+
export { isArray, print, random, safe, singleOrNull, timestamp, transform };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
|
|
1
3
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
2
|
-
declare function toArray<T>(array?: T | Array<T>): Array<T>;
|
|
3
4
|
|
|
4
|
-
declare function
|
|
5
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
6
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
|
|
5
|
+
declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
7
6
|
declare function safe<R>(fn: () => R): R | undefined;
|
|
8
7
|
|
|
9
|
-
type Nullable<T> = T | null | undefined;
|
|
10
|
-
type NotNullable<T> = Exclude<T, null | undefined>;
|
|
11
|
-
type NotNull<T> = Exclude<T, null>;
|
|
12
|
-
type NotUndefined<T> = Exclude<T, undefined>;
|
|
13
|
-
type Arrayable<T> = T | Array<T>;
|
|
14
|
-
type ElementOf<T> = T extends Array<infer Element> ? Element : never;
|
|
15
|
-
type Fn<R = void> = (...args: any[]) => R;
|
|
16
|
-
|
|
17
|
-
declare const isFunction: (val: any) => val is Fn;
|
|
18
|
-
declare const isBoolean: (val: any) => val is boolean;
|
|
19
|
-
declare const isNumber: (val: any) => val is number;
|
|
20
|
-
declare const isString: (val: unknown) => val is string;
|
|
21
|
-
declare const isObject: (val: any) => val is object;
|
|
22
|
-
declare const isUndefined: (val: any) => val is undefined;
|
|
23
|
-
declare const isNull: (val: any) => val is null;
|
|
24
|
-
declare const isRegExp: (val: any) => val is RegExp;
|
|
25
|
-
declare const isDate: (val: any) => val is Date;
|
|
26
|
-
declare const isArray: (arg: any) => arg is any[];
|
|
27
|
-
declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
|
|
28
|
-
declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
|
|
29
|
-
declare const notUndefined: <T>(v: T | undefined) => v is Exclude<T, undefined>;
|
|
30
|
-
|
|
31
8
|
/**
|
|
32
9
|
* @description min <= result < max
|
|
33
10
|
*/
|
|
34
11
|
declare function random(max: number, min?: number): number;
|
|
35
12
|
|
|
13
|
+
declare const map: {
|
|
14
|
+
info: {
|
|
15
|
+
color: string;
|
|
16
|
+
symbol: string;
|
|
17
|
+
};
|
|
18
|
+
warn: {
|
|
19
|
+
color: string;
|
|
20
|
+
symbol: string;
|
|
21
|
+
};
|
|
22
|
+
success: {
|
|
23
|
+
color: string;
|
|
24
|
+
symbol: string;
|
|
25
|
+
};
|
|
26
|
+
error: {
|
|
27
|
+
color: string;
|
|
28
|
+
symbol: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* process.stdout.write('msg')
|
|
33
|
+
* process.stdout.clearLine(0)
|
|
34
|
+
*/
|
|
35
|
+
declare function print(msg: any, type?: keyof typeof map): void;
|
|
36
|
+
|
|
36
37
|
declare const timestamp: () => number;
|
|
38
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
37
39
|
|
|
38
|
-
export {
|
|
40
|
+
export { isArray, print, random, safe, singleOrNull, timestamp, transform };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,38 +1,40 @@
|
|
|
1
|
+
export * from '@antfu/utils';
|
|
2
|
+
|
|
1
3
|
declare function singleOrNull<T>(arr: T[]): T | null;
|
|
2
|
-
declare function toArray<T>(array?: T | Array<T>): Array<T>;
|
|
3
4
|
|
|
4
|
-
declare function
|
|
5
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean): T | null;
|
|
6
|
-
declare function takeIf<T>(value: T, predicate: (it: T) => boolean, defaultValue: T): T;
|
|
5
|
+
declare function transform<T, R = void>(value: T, fn: (it: T) => R): R;
|
|
7
6
|
declare function safe<R>(fn: () => R): R | undefined;
|
|
8
7
|
|
|
9
|
-
type Nullable<T> = T | null | undefined;
|
|
10
|
-
type NotNullable<T> = Exclude<T, null | undefined>;
|
|
11
|
-
type NotNull<T> = Exclude<T, null>;
|
|
12
|
-
type NotUndefined<T> = Exclude<T, undefined>;
|
|
13
|
-
type Arrayable<T> = T | Array<T>;
|
|
14
|
-
type ElementOf<T> = T extends Array<infer Element> ? Element : never;
|
|
15
|
-
type Fn<R = void> = (...args: any[]) => R;
|
|
16
|
-
|
|
17
|
-
declare const isFunction: (val: any) => val is Fn;
|
|
18
|
-
declare const isBoolean: (val: any) => val is boolean;
|
|
19
|
-
declare const isNumber: (val: any) => val is number;
|
|
20
|
-
declare const isString: (val: unknown) => val is string;
|
|
21
|
-
declare const isObject: (val: any) => val is object;
|
|
22
|
-
declare const isUndefined: (val: any) => val is undefined;
|
|
23
|
-
declare const isNull: (val: any) => val is null;
|
|
24
|
-
declare const isRegExp: (val: any) => val is RegExp;
|
|
25
|
-
declare const isDate: (val: any) => val is Date;
|
|
26
|
-
declare const isArray: (arg: any) => arg is any[];
|
|
27
|
-
declare const notNullish: <T>(v: T | null | undefined) => v is NonNullable<T>;
|
|
28
|
-
declare const notNull: <T>(v: T | null) => v is Exclude<T, null>;
|
|
29
|
-
declare const notUndefined: <T>(v: T | undefined) => v is Exclude<T, undefined>;
|
|
30
|
-
|
|
31
8
|
/**
|
|
32
9
|
* @description min <= result < max
|
|
33
10
|
*/
|
|
34
11
|
declare function random(max: number, min?: number): number;
|
|
35
12
|
|
|
13
|
+
declare const map: {
|
|
14
|
+
info: {
|
|
15
|
+
color: string;
|
|
16
|
+
symbol: string;
|
|
17
|
+
};
|
|
18
|
+
warn: {
|
|
19
|
+
color: string;
|
|
20
|
+
symbol: string;
|
|
21
|
+
};
|
|
22
|
+
success: {
|
|
23
|
+
color: string;
|
|
24
|
+
symbol: string;
|
|
25
|
+
};
|
|
26
|
+
error: {
|
|
27
|
+
color: string;
|
|
28
|
+
symbol: string;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* process.stdout.write('msg')
|
|
33
|
+
* process.stdout.clearLine(0)
|
|
34
|
+
*/
|
|
35
|
+
declare function print(msg: any, type?: keyof typeof map): void;
|
|
36
|
+
|
|
36
37
|
declare const timestamp: () => number;
|
|
38
|
+
declare const isArray: (arg: any) => arg is any[];
|
|
37
39
|
|
|
38
|
-
export {
|
|
40
|
+
export { isArray, print, random, safe, singleOrNull, timestamp, transform };
|
package/dist/index.mjs
CHANGED
|
@@ -1,32 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
const isBoolean = (val) => typeof val === "boolean";
|
|
3
|
-
const isNumber = (val) => typeof val === "number";
|
|
4
|
-
const isString = (val) => typeof val === "string";
|
|
5
|
-
const toString = (value) => Object.prototype.toString.call(value);
|
|
6
|
-
const isObject = (val) => toString(val) === "[object Object]";
|
|
7
|
-
const isUndefined = (val) => toString(val) === "[object Undefined]";
|
|
8
|
-
const isNull = (val) => toString(val) === "[object Null]";
|
|
9
|
-
const isRegExp = (val) => toString(val) === "[object RegExp]";
|
|
10
|
-
const isDate = (val) => toString(val) === "[object Date]";
|
|
11
|
-
const isArray = Array.isArray;
|
|
12
|
-
const notNullish = (v) => v != null;
|
|
13
|
-
const notNull = (v) => v !== null;
|
|
14
|
-
const notUndefined = (v) => v !== void 0;
|
|
1
|
+
export * from '@antfu/utils';
|
|
15
2
|
|
|
16
3
|
function singleOrNull(arr) {
|
|
17
4
|
return arr.length === 1 ? arr[0] : null;
|
|
18
5
|
}
|
|
19
|
-
function toArray(array) {
|
|
20
|
-
array = array ?? [];
|
|
21
|
-
return isArray(array) ? array : [array];
|
|
22
|
-
}
|
|
23
6
|
|
|
24
|
-
function
|
|
7
|
+
function transform(value, fn) {
|
|
25
8
|
return fn(value);
|
|
26
9
|
}
|
|
27
|
-
function takeIf(value, predicate, defaultValue) {
|
|
28
|
-
return predicate(value) ? value : notUndefined(defaultValue) ? defaultValue : null;
|
|
29
|
-
}
|
|
30
10
|
function safe(fn) {
|
|
31
11
|
try {
|
|
32
12
|
return fn();
|
|
@@ -38,6 +18,68 @@ function random(max, min = 0) {
|
|
|
38
18
|
return Math.floor(Math.random() * max) + min;
|
|
39
19
|
}
|
|
40
20
|
|
|
21
|
+
const colors = {
|
|
22
|
+
/** 重置颜色 */
|
|
23
|
+
reset: "\x1B[0m",
|
|
24
|
+
/** 亮色(加粗) */
|
|
25
|
+
bright: "\x1B[1m",
|
|
26
|
+
/** 斜体 */
|
|
27
|
+
italic: "\x1B[3m",
|
|
28
|
+
/** 下划线 */
|
|
29
|
+
underline: "\x1B[4m",
|
|
30
|
+
/** 反向 */
|
|
31
|
+
reverse: "\x1B[7m",
|
|
32
|
+
/** 隐藏 */
|
|
33
|
+
hidden: "\x1B[8m",
|
|
34
|
+
/** 灰色 */
|
|
35
|
+
grey: "\x1B[2m",
|
|
36
|
+
/** 黑色 */
|
|
37
|
+
black: "\x1B[30m",
|
|
38
|
+
/** 红色 */
|
|
39
|
+
red: "\x1B[31m",
|
|
40
|
+
/** 绿色 */
|
|
41
|
+
green: "\x1B[32m",
|
|
42
|
+
/** 黄色 */
|
|
43
|
+
yellow: "\x1B[33m",
|
|
44
|
+
/** 蓝色 */
|
|
45
|
+
blue: "\x1B[34m",
|
|
46
|
+
/** 品红 */
|
|
47
|
+
magenta: "\x1B[35m",
|
|
48
|
+
/** 青色 */
|
|
49
|
+
cyan: "\x1B[36m",
|
|
50
|
+
/** 白色 */
|
|
51
|
+
white: "\x1B[37m",
|
|
52
|
+
/** 背景黑色 */
|
|
53
|
+
bgBlack: "\x1B[40m",
|
|
54
|
+
/** 背景红色 */
|
|
55
|
+
bgRed: "\x1B[41m",
|
|
56
|
+
/** 背景绿色 */
|
|
57
|
+
bgGreen: "\x1B[42m",
|
|
58
|
+
/** 背景黄色 */
|
|
59
|
+
bgYellow: "\x1B[43m",
|
|
60
|
+
/** 背景蓝色 */
|
|
61
|
+
bgBlue: "\x1B[44m",
|
|
62
|
+
/** 背景品红 */
|
|
63
|
+
bgMagenta: "\x1B[45m",
|
|
64
|
+
/** 背景青色 */
|
|
65
|
+
bgCyan: "\x1B[46m",
|
|
66
|
+
/** 背景白色 */
|
|
67
|
+
bgWhite: "\x1B[47m"
|
|
68
|
+
};
|
|
69
|
+
const map = {
|
|
70
|
+
info: { color: colors.blue, symbol: "\u{1D4F2}" },
|
|
71
|
+
warn: { color: colors.yellow, symbol: "\u{1D4F2}" },
|
|
72
|
+
success: { color: colors.green, symbol: "\u2714\uFE0E" },
|
|
73
|
+
error: { color: colors.red, symbol: "\u2718" }
|
|
74
|
+
};
|
|
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}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
41
82
|
const timestamp = () => +Date.now();
|
|
83
|
+
const isArray = Array.isArray;
|
|
42
84
|
|
|
43
|
-
export { isArray,
|
|
85
|
+
export { isArray, print, random, safe, singleOrNull, timestamp, transform };
|
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.2.0",
|
|
5
5
|
"description": "Utils.",
|
|
6
6
|
"author": "Jiakun Zhao <hi@zhaojiakun.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -18,36 +18,25 @@
|
|
|
18
18
|
"types": "./dist/index.d.ts",
|
|
19
19
|
"import": "./dist/index.mjs",
|
|
20
20
|
"require": "./dist/index.cjs"
|
|
21
|
-
},
|
|
22
|
-
"./fs": {
|
|
23
|
-
"types": "./dist/fs.d.ts",
|
|
24
|
-
"import": "./dist/fs.mjs",
|
|
25
|
-
"require": "./dist/fs.cjs"
|
|
26
21
|
}
|
|
27
22
|
},
|
|
28
23
|
"main": "./dist/index.cjs",
|
|
29
24
|
"module": "./dist/index.mjs",
|
|
30
25
|
"types": "./dist/index.d.ts",
|
|
31
26
|
"files": [
|
|
32
|
-
"dist"
|
|
33
|
-
"index.d.ts",
|
|
34
|
-
"fs.d.ts"
|
|
27
|
+
"dist"
|
|
35
28
|
],
|
|
36
29
|
"dependencies": {
|
|
37
|
-
"
|
|
30
|
+
"@antfu/utils": "^0.7.8"
|
|
38
31
|
},
|
|
39
32
|
"devDependencies": {
|
|
40
|
-
"@
|
|
41
|
-
"@types/node": "^20.
|
|
42
|
-
"bumpp": "^9.
|
|
43
|
-
"eslint": "^
|
|
44
|
-
"
|
|
45
|
-
"typescript": "^5.3.2",
|
|
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",
|
|
46
38
|
"unbuild": "^2.0.0",
|
|
47
|
-
"vitest": "^
|
|
48
|
-
},
|
|
49
|
-
"eslintConfig": {
|
|
50
|
-
"extends": "@jiakun-zhao"
|
|
39
|
+
"vitest": "^1.6.0"
|
|
51
40
|
},
|
|
52
41
|
"scripts": {
|
|
53
42
|
"build": "unbuild",
|
package/dist/fs.cjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const node_crypto = require('node:crypto');
|
|
4
|
-
const node_fs = require('node:fs');
|
|
5
|
-
const promises = require('node:fs/promises');
|
|
6
|
-
const node_path = require('node:path');
|
|
7
|
-
const fastGlob = require('fast-glob');
|
|
8
|
-
|
|
9
|
-
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
10
|
-
|
|
11
|
-
const fastGlob__default = /*#__PURE__*/_interopDefaultCompat(fastGlob);
|
|
12
|
-
|
|
13
|
-
async function exists(...path) {
|
|
14
|
-
return await promises.access(node_path.join(...path), promises.constants.F_OK).then(() => true, () => false);
|
|
15
|
-
}
|
|
16
|
-
async function readText(...path) {
|
|
17
|
-
return await promises.readFile(node_path.join(...path), "utf-8");
|
|
18
|
-
}
|
|
19
|
-
async function readJson(...path) {
|
|
20
|
-
return JSON.parse(await readText(...path));
|
|
21
|
-
}
|
|
22
|
-
async function writeText(content, ...path) {
|
|
23
|
-
await promises.writeFile(node_path.join(...path), content, "utf-8");
|
|
24
|
-
}
|
|
25
|
-
async function writeJson(content, ...path) {
|
|
26
|
-
await writeText(`${JSON.stringify(content, null, 2)}
|
|
27
|
-
`, ...path);
|
|
28
|
-
}
|
|
29
|
-
async function toMD5(...path) {
|
|
30
|
-
const hash = node_crypto.createHash("md5");
|
|
31
|
-
const stream = node_fs.createReadStream(node_path.join(...path));
|
|
32
|
-
return new Promise((resolve, reject) => {
|
|
33
|
-
stream.on("data", (chunk) => hash.update(chunk));
|
|
34
|
-
stream.on("end", () => resolve(hash.digest("base64url")));
|
|
35
|
-
stream.on("error", reject);
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
exports.glob = fastGlob__default;
|
|
40
|
-
exports.exists = exists;
|
|
41
|
-
exports.readJson = readJson;
|
|
42
|
-
exports.readText = readText;
|
|
43
|
-
exports.toMD5 = toMD5;
|
|
44
|
-
exports.writeJson = writeJson;
|
|
45
|
-
exports.writeText = writeText;
|
package/dist/fs.d.cts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { default as glob } from 'fast-glob';
|
|
2
|
-
|
|
3
|
-
declare function exists(...path: string[]): Promise<boolean>;
|
|
4
|
-
declare function readText(...path: string[]): Promise<string>;
|
|
5
|
-
declare function readJson<T>(...path: string[]): Promise<T>;
|
|
6
|
-
declare function writeText(content: string, ...path: string[]): Promise<void>;
|
|
7
|
-
declare function writeJson(content: object, ...path: string[]): Promise<void>;
|
|
8
|
-
declare function toMD5(...path: string[]): Promise<string>;
|
|
9
|
-
|
|
10
|
-
export { exists, readJson, readText, toMD5, writeJson, writeText };
|
package/dist/fs.d.mts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { default as glob } from 'fast-glob';
|
|
2
|
-
|
|
3
|
-
declare function exists(...path: string[]): Promise<boolean>;
|
|
4
|
-
declare function readText(...path: string[]): Promise<string>;
|
|
5
|
-
declare function readJson<T>(...path: string[]): Promise<T>;
|
|
6
|
-
declare function writeText(content: string, ...path: string[]): Promise<void>;
|
|
7
|
-
declare function writeJson(content: object, ...path: string[]): Promise<void>;
|
|
8
|
-
declare function toMD5(...path: string[]): Promise<string>;
|
|
9
|
-
|
|
10
|
-
export { exists, readJson, readText, toMD5, writeJson, writeText };
|
package/dist/fs.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export { default as glob } from 'fast-glob';
|
|
2
|
-
|
|
3
|
-
declare function exists(...path: string[]): Promise<boolean>;
|
|
4
|
-
declare function readText(...path: string[]): Promise<string>;
|
|
5
|
-
declare function readJson<T>(...path: string[]): Promise<T>;
|
|
6
|
-
declare function writeText(content: string, ...path: string[]): Promise<void>;
|
|
7
|
-
declare function writeJson(content: object, ...path: string[]): Promise<void>;
|
|
8
|
-
declare function toMD5(...path: string[]): Promise<string>;
|
|
9
|
-
|
|
10
|
-
export { exists, readJson, readText, toMD5, writeJson, writeText };
|
package/dist/fs.mjs
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { createHash } from 'node:crypto';
|
|
2
|
-
import { createReadStream } from 'node:fs';
|
|
3
|
-
import { access, constants, readFile, writeFile } from 'node:fs/promises';
|
|
4
|
-
import { join } from 'node:path';
|
|
5
|
-
export { default as glob } from 'fast-glob';
|
|
6
|
-
|
|
7
|
-
async function exists(...path) {
|
|
8
|
-
return await access(join(...path), constants.F_OK).then(() => true, () => false);
|
|
9
|
-
}
|
|
10
|
-
async function readText(...path) {
|
|
11
|
-
return await readFile(join(...path), "utf-8");
|
|
12
|
-
}
|
|
13
|
-
async function readJson(...path) {
|
|
14
|
-
return JSON.parse(await readText(...path));
|
|
15
|
-
}
|
|
16
|
-
async function writeText(content, ...path) {
|
|
17
|
-
await writeFile(join(...path), content, "utf-8");
|
|
18
|
-
}
|
|
19
|
-
async function writeJson(content, ...path) {
|
|
20
|
-
await writeText(`${JSON.stringify(content, null, 2)}
|
|
21
|
-
`, ...path);
|
|
22
|
-
}
|
|
23
|
-
async function toMD5(...path) {
|
|
24
|
-
const hash = createHash("md5");
|
|
25
|
-
const stream = createReadStream(join(...path));
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
stream.on("data", (chunk) => hash.update(chunk));
|
|
28
|
-
stream.on("end", () => resolve(hash.digest("base64url")));
|
|
29
|
-
stream.on("error", reject);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export { exists, readJson, readText, toMD5, writeJson, writeText };
|
package/fs.d.ts
DELETED
package/index.d.ts
DELETED