@anjianshi/utils 2.2.7 → 2.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/env-node/fs.d.ts +19 -0
- package/env-node/fs.js +48 -0
- package/env-node/index.d.ts +3 -15
- package/env-node/index.js +2 -42
- package/env-node/random.d.ts +12 -0
- package/env-node/random.js +25 -0
- package/env-react/emotion.d.ts +18 -0
- package/env-react/emotion.jsx +34 -0
- package/lang/index.d.ts +1 -0
- package/lang/index.js +1 -0
- package/lang/random.d.ts +12 -0
- package/lang/random.js +23 -0
- package/package.json +23 -5
- package/url.d.ts +2 -4
package/env-node/fs.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 确认一个路径是否存在且是文件
|
|
3
|
+
*/
|
|
4
|
+
export declare function isFileExists(filepath: string): Promise<boolean>;
|
|
5
|
+
/**
|
|
6
|
+
* 确认一个路径是否存在且是文件夹
|
|
7
|
+
*/
|
|
8
|
+
export declare function isDirectoryExists(dirpath: string): Promise<boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* 返回当前文件的绝对路径
|
|
11
|
+
* 需要传入当前文件的 ImportMeta 对象(可通过 import.meta 取得)
|
|
12
|
+
*/
|
|
13
|
+
export declare function getFilePath(fileUrl: string | ImportMeta): string;
|
|
14
|
+
/**
|
|
15
|
+
* 返回文件所处目录的绝对路径
|
|
16
|
+
*/
|
|
17
|
+
export declare function getDirectoryPath(fileUrl: string | ImportMeta): string;
|
|
18
|
+
/** 确保目录存在,如果不存在就创建(会递归创建上级目录) */
|
|
19
|
+
export declare function mkdirp(dirpath: string, mode?: number | string): Promise<void>;
|
package/env-node/fs.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import * as fs from 'node:fs';
|
|
2
|
+
import * as path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
/**
|
|
5
|
+
* 确认一个路径是否存在且是文件
|
|
6
|
+
*/
|
|
7
|
+
export async function isFileExists(filepath) {
|
|
8
|
+
try {
|
|
9
|
+
const res = await fs.promises.stat(filepath);
|
|
10
|
+
return res.isFile();
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 确认一个路径是否存在且是文件夹
|
|
18
|
+
*/
|
|
19
|
+
export async function isDirectoryExists(dirpath) {
|
|
20
|
+
try {
|
|
21
|
+
const res = await fs.promises.stat(dirpath);
|
|
22
|
+
return res.isDirectory();
|
|
23
|
+
}
|
|
24
|
+
catch (e) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 返回当前文件的绝对路径
|
|
30
|
+
* 需要传入当前文件的 ImportMeta 对象(可通过 import.meta 取得)
|
|
31
|
+
*/
|
|
32
|
+
export function getFilePath(fileUrl) {
|
|
33
|
+
if (typeof fileUrl !== 'string')
|
|
34
|
+
fileUrl = fileUrl.url;
|
|
35
|
+
return fileURLToPath(new URL(fileUrl));
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 返回文件所处目录的绝对路径
|
|
39
|
+
*/
|
|
40
|
+
export function getDirectoryPath(fileUrl) {
|
|
41
|
+
return path.dirname(getFilePath(fileUrl));
|
|
42
|
+
}
|
|
43
|
+
/** 确保目录存在,如果不存在就创建(会递归创建上级目录) */
|
|
44
|
+
export async function mkdirp(dirpath, mode) {
|
|
45
|
+
if (!(await isDirectoryExists(dirpath))) {
|
|
46
|
+
await fs.promises.mkdir(dirpath, { recursive: true, mode });
|
|
47
|
+
}
|
|
48
|
+
}
|
package/env-node/index.d.ts
CHANGED
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Node 环境下的工具函数
|
|
3
3
|
*/
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
* 确认一个路径是否存在且是文件夹
|
|
7
|
-
*/
|
|
8
|
-
export declare function isDirectoryExists(dirpath: string): Promise<boolean>;
|
|
9
|
-
/**
|
|
10
|
-
* 返回当前文件的绝对路径
|
|
11
|
-
* 需要传入当前文件的 ImportMeta 对象(可通过 import.meta 取得)
|
|
12
|
-
*/
|
|
13
|
-
export declare function getFilePath(fileUrl: string | ImportMeta): string;
|
|
14
|
-
/**
|
|
15
|
-
* 返回文件所处目录的绝对路径
|
|
16
|
-
*/
|
|
17
|
-
export declare function getFileDir(fileUrl: string | ImportMeta): string;
|
|
4
|
+
export * from './fs.js';
|
|
5
|
+
export * from './random.js';
|
package/env-node/index.js
CHANGED
|
@@ -1,45 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Node 环境下的工具函数
|
|
3
3
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
|
-
/**
|
|
8
|
-
* 确认一个路径是否存在且是文件
|
|
9
|
-
*/
|
|
10
|
-
export async function isFileExists(filepath) {
|
|
11
|
-
try {
|
|
12
|
-
const res = await fs.promises.stat(filepath);
|
|
13
|
-
return res.isFile();
|
|
14
|
-
}
|
|
15
|
-
catch (e) {
|
|
16
|
-
return false;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* 确认一个路径是否存在且是文件夹
|
|
21
|
-
*/
|
|
22
|
-
export async function isDirectoryExists(dirpath) {
|
|
23
|
-
try {
|
|
24
|
-
const res = await fs.promises.stat(dirpath);
|
|
25
|
-
return res.isDirectory();
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* 返回当前文件的绝对路径
|
|
33
|
-
* 需要传入当前文件的 ImportMeta 对象(可通过 import.meta 取得)
|
|
34
|
-
*/
|
|
35
|
-
export function getFilePath(fileUrl) {
|
|
36
|
-
if (typeof fileUrl !== 'string')
|
|
37
|
-
fileUrl = fileUrl.url;
|
|
38
|
-
return fileURLToPath(new URL(fileUrl));
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* 返回文件所处目录的绝对路径
|
|
42
|
-
*/
|
|
43
|
-
export function getFileDir(fileUrl) {
|
|
44
|
-
return path.dirname(getFilePath(fileUrl));
|
|
45
|
-
}
|
|
4
|
+
export * from './fs.js';
|
|
5
|
+
export * from './random.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 返回随机数,包含 min 和 max
|
|
3
|
+
*/
|
|
4
|
+
export declare function getRandomInt(min: number, max: number): number;
|
|
5
|
+
/**
|
|
6
|
+
* 返回随机字符串
|
|
7
|
+
*/
|
|
8
|
+
export declare function getRandomString(length?: number, seed?: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* 从给定的选择中随机选中一项
|
|
11
|
+
*/
|
|
12
|
+
export declare function choiceRandom<T>(choices: T[]): NonNullable<T>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 类似 lang/random.ts,但基于 Node.js 的 crypto 模块,提供密码级的随机数。
|
|
3
|
+
*/
|
|
4
|
+
import crypto from 'node:crypto';
|
|
5
|
+
/**
|
|
6
|
+
* 返回随机数,包含 min 和 max
|
|
7
|
+
*/
|
|
8
|
+
export function getRandomInt(min, max) {
|
|
9
|
+
return crypto.randomInt(min, max + 1);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 返回随机字符串
|
|
13
|
+
*/
|
|
14
|
+
export function getRandomString(length = 6, seed = '0123456789abcdefghijklmnopqrstuvwxyz') {
|
|
15
|
+
let result = '';
|
|
16
|
+
while (result.length < length)
|
|
17
|
+
result += seed[getRandomInt(0, seed.length - 1)];
|
|
18
|
+
return result;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 从给定的选择中随机选中一项
|
|
22
|
+
*/
|
|
23
|
+
export function choiceRandom(choices) {
|
|
24
|
+
return choices[getRandomInt(0, choices.length - 1)];
|
|
25
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通过 React Hook 把 emotion css 转换成 className
|
|
3
|
+
* (不再需要 <ClassName>)
|
|
4
|
+
*
|
|
5
|
+
* 使用前提:
|
|
6
|
+
* 1. 只支持浏览器渲染
|
|
7
|
+
* 2. 用 EmotionCacheProvider 包裹 App 根元素
|
|
8
|
+
*
|
|
9
|
+
* 来自:
|
|
10
|
+
* https://github.com/emotion-js/emotion/issues/1853#issuecomment-623349622
|
|
11
|
+
*/
|
|
12
|
+
import { type EmotionCache } from '@emotion/react';
|
|
13
|
+
import { type CSSInterpolation } from '@emotion/serialize';
|
|
14
|
+
export declare const useEmotionCache: () => EmotionCache | undefined;
|
|
15
|
+
export declare const EmotionCacheProvider: import("react").FC<{
|
|
16
|
+
children: React.ReactNode;
|
|
17
|
+
} & import("react").ClassAttributes<any>>;
|
|
18
|
+
export declare function useEmotionClassName(): (...args: CSSInterpolation[]) => string;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通过 React Hook 把 emotion css 转换成 className
|
|
3
|
+
* (不再需要 <ClassName>)
|
|
4
|
+
*
|
|
5
|
+
* 使用前提:
|
|
6
|
+
* 1. 只支持浏览器渲染
|
|
7
|
+
* 2. 用 EmotionCacheProvider 包裹 App 根元素
|
|
8
|
+
*
|
|
9
|
+
* 来自:
|
|
10
|
+
* https://github.com/emotion-js/emotion/issues/1853#issuecomment-623349622
|
|
11
|
+
*/
|
|
12
|
+
import { withEmotionCache } from '@emotion/react';
|
|
13
|
+
import { serializeStyles } from '@emotion/serialize';
|
|
14
|
+
import { insertStyles } from '@emotion/utils';
|
|
15
|
+
import { createContext, useContext, useCallback } from 'react';
|
|
16
|
+
const CacheContext = createContext(undefined);
|
|
17
|
+
export const useEmotionCache = () => useContext(CacheContext);
|
|
18
|
+
export const EmotionCacheProvider = withEmotionCache(({ children }, cache) => {
|
|
19
|
+
return <CacheContext.Provider value={cache}>{children}</CacheContext.Provider>;
|
|
20
|
+
});
|
|
21
|
+
export function useEmotionClassName() {
|
|
22
|
+
const cache = useEmotionCache();
|
|
23
|
+
return useCallback((...args) => {
|
|
24
|
+
if (!cache) {
|
|
25
|
+
if (process.env.NODE_ENV === 'production') {
|
|
26
|
+
return 'emotion-cache-missing';
|
|
27
|
+
}
|
|
28
|
+
throw new Error('No emotion cache found!');
|
|
29
|
+
}
|
|
30
|
+
const serialized = serializeStyles(args, cache.registered);
|
|
31
|
+
insertStyles(cache, serialized, false);
|
|
32
|
+
return cache.key + '-' + serialized.name;
|
|
33
|
+
}, [cache]);
|
|
34
|
+
}
|
package/lang/index.d.ts
CHANGED
package/lang/index.js
CHANGED
package/lang/random.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 返回随机数,包含 min 和 max
|
|
3
|
+
*/
|
|
4
|
+
export declare function getRandomInt(min: number, max: number): number;
|
|
5
|
+
/**
|
|
6
|
+
* 返回随机字符串
|
|
7
|
+
*/
|
|
8
|
+
export declare function getRandomString(length?: number, seed?: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* 从给定的选择中随机选中一项
|
|
11
|
+
*/
|
|
12
|
+
export declare function choiceRandom<T>(choices: T[]): NonNullable<T>;
|
package/lang/random.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 返回随机数,包含 min 和 max
|
|
3
|
+
*/
|
|
4
|
+
export function getRandomInt(min, max) {
|
|
5
|
+
const minCeiled = Math.ceil(min);
|
|
6
|
+
const maxFloored = Math.floor(max);
|
|
7
|
+
return Math.floor(Math.random() * (maxFloored - minCeiled + 1) + minCeiled);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 返回随机字符串
|
|
11
|
+
*/
|
|
12
|
+
export function getRandomString(length = 6, seed = '0123456789abcdefghijklmnopqrstuvwxyz') {
|
|
13
|
+
let result = '';
|
|
14
|
+
while (result.length < length)
|
|
15
|
+
result += seed[getRandomInt(0, seed.length - 1)];
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 从给定的选择中随机选中一项
|
|
20
|
+
*/
|
|
21
|
+
export function choiceRandom(choices) {
|
|
22
|
+
return choices[getRandomInt(0, choices.length - 1)];
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anjianshi/utils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Common JavaScript Utils",
|
|
5
5
|
"homepage": "https://github.com/anjianshi/js-packages/utils",
|
|
6
6
|
"bugs": {
|
|
@@ -21,19 +21,25 @@
|
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@types/lodash": "^4.17.7",
|
|
24
|
-
"@types/node": "^20.
|
|
24
|
+
"@types/node": "^20.16.2",
|
|
25
|
+
"@types/react": "^18.3.5",
|
|
25
26
|
"dotenv": "^16.4.5",
|
|
26
27
|
"typeorm": "^0.3.20",
|
|
27
28
|
"typescript": "^5.5.4",
|
|
28
29
|
"vconsole": "^3.15.1",
|
|
29
30
|
"@anjianshi/presets-eslint-node": "4.0.8",
|
|
30
|
-
"@anjianshi/presets-eslint-typescript": "5.0.5",
|
|
31
31
|
"@anjianshi/presets-typescript": "3.2.2",
|
|
32
|
-
"@anjianshi/presets-
|
|
32
|
+
"@anjianshi/presets-eslint-typescript": "5.0.5",
|
|
33
|
+
"@anjianshi/presets-prettier": "3.0.1",
|
|
34
|
+
"@anjianshi/presets-eslint-react": "4.0.7"
|
|
33
35
|
},
|
|
34
36
|
"peerDependencies": {
|
|
37
|
+
"@emotion/react": "^11.13.3",
|
|
38
|
+
"@emotion/serialize": "^1.3.1",
|
|
39
|
+
"@emotion/utils": "^1.4.0",
|
|
35
40
|
"chalk": "^5.3.0",
|
|
36
|
-
"dayjs": "^1.11.10"
|
|
41
|
+
"dayjs": "^1.11.10",
|
|
42
|
+
"react": "^18.3.1"
|
|
37
43
|
},
|
|
38
44
|
"peerDependenciesMeta": {
|
|
39
45
|
"chalk": {
|
|
@@ -41,6 +47,18 @@
|
|
|
41
47
|
},
|
|
42
48
|
"dayjs": {
|
|
43
49
|
"optional": true
|
|
50
|
+
},
|
|
51
|
+
"@emotion/react": {
|
|
52
|
+
"optional": true
|
|
53
|
+
},
|
|
54
|
+
"@emotion/serialize": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"@emotion/utils": {
|
|
58
|
+
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"react": {
|
|
61
|
+
"optional": true
|
|
44
62
|
}
|
|
45
63
|
},
|
|
46
64
|
"eslintIgnore": [],
|
package/url.d.ts
CHANGED
|
@@ -32,10 +32,8 @@ export { parseQuery };
|
|
|
32
32
|
* - 数组会多次赋值:{ a: [1,2,3] } => 'a=1&a=2&a=3',不支持嵌套数组
|
|
33
33
|
* - encode 为 true 时会对 value 执行 encodeURIComponent(默认为 true)
|
|
34
34
|
*/
|
|
35
|
-
type StringifyVal = string | number | boolean;
|
|
36
|
-
type StringifyQuery =
|
|
37
|
-
[key: string]: StringifyVal | StringifyVal[] | undefined;
|
|
38
|
-
};
|
|
35
|
+
export type StringifyVal = string | number | boolean;
|
|
36
|
+
export type StringifyQuery = Record<string, StringifyVal | StringifyVal[] | undefined>;
|
|
39
37
|
export declare function stringifyQuery(obj: StringifyQuery, encode?: boolean): string;
|
|
40
38
|
/**
|
|
41
39
|
* 拆分 URL 的各个部分
|