befly-shared 1.3.3 → 1.3.4
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/package.json +8 -3
- package/types/arrayToTree.ts +11 -0
- package/types/deepTransformKeys.ts +16 -0
- package/types/genShortId.ts +5 -0
- package/types/hashPassword.ts +5 -0
- package/types/normalizePathnameListInput.ts +5 -0
- package/types/scanViewsDir.ts +25 -0
- package/types/withDefaultColumns.ts +5 -0
- package/utils/hashPassword.ts +27 -0
- package/utils/withDefaultColumns.ts +48 -0
package/package.json
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly-shared",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Befly 纯函数与纯常量共享包(不包含 Node/浏览器特定实现)",
|
|
6
6
|
"license": "Apache-2.0",
|
|
7
7
|
"author": "chensuiyi <bimostyle@qq.com>",
|
|
8
8
|
"files": [
|
|
9
9
|
"package.json",
|
|
10
|
+
"types/",
|
|
10
11
|
"utils/"
|
|
11
12
|
],
|
|
12
13
|
"type": "module",
|
|
13
14
|
"exports": {
|
|
14
|
-
"./utils/*": "./utils/*.ts"
|
|
15
|
+
"./utils/*": "./utils/*.ts",
|
|
16
|
+
"./types/*": "./types/*.ts"
|
|
15
17
|
},
|
|
16
18
|
"scripts": {
|
|
17
19
|
"test": "bun test"
|
|
18
20
|
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"es-toolkit": "^1.43.0"
|
|
23
|
+
},
|
|
19
24
|
"engines": {
|
|
20
25
|
"bun": ">=1.3.0"
|
|
21
26
|
},
|
|
22
|
-
"gitHead": "
|
|
27
|
+
"gitHead": "564115664323c9d73351945129aa7d57d440eb58"
|
|
23
28
|
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* arrayToTree 类型定义(类型模块,仅供 type 引用)。
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type ArrayToTreeResult<T extends Record<string, any>> = {
|
|
6
|
+
flat: Array<T>;
|
|
7
|
+
tree: Array<T>;
|
|
8
|
+
map: Map<string, T>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ArrayToTree = <T extends Record<string, any>>(items: T[], id?: string, pid?: string, children?: string, sort?: string) => ArrayToTreeResult<T>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* deepTransformKeys 类型定义(类型模块,仅供 type 引用)。
|
|
3
|
+
*
|
|
4
|
+
* 注意:types 模块不包含运行时实现;运行时请从 befly-shared/utils/* 引入。
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
export type KeyTransformer = (key: string) => string;
|
|
8
|
+
|
|
9
|
+
export type PresetTransform = "camel" | "snake" | "kebab" | "pascal" | "upper" | "lower";
|
|
10
|
+
|
|
11
|
+
export interface TransformOptions {
|
|
12
|
+
maxDepth?: number;
|
|
13
|
+
excludeKeys?: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type DeepTransformKeys = <T = any>(data: any, transformer: KeyTransformer | PresetTransform, options?: TransformOptions) => T;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* scanViewsDir 相关纯函数的类型定义(类型模块,仅供 type 引用)。
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export type ViewDirMeta = {
|
|
6
|
+
title: string;
|
|
7
|
+
order?: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type MenuNodeLike<T> = {
|
|
11
|
+
name?: string;
|
|
12
|
+
path?: string;
|
|
13
|
+
sort?: number;
|
|
14
|
+
children?: T[];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type CleanDirName = (name: string) => string;
|
|
18
|
+
|
|
19
|
+
export type NormalizeMenuPath = (path: string) => string;
|
|
20
|
+
|
|
21
|
+
export type NormalizeMenuTree = <T extends MenuNodeLike<T>>(menus: T[]) => T[];
|
|
22
|
+
|
|
23
|
+
export type ExtractScriptSetupBlock = (vueContent: string) => string | null;
|
|
24
|
+
|
|
25
|
+
export type ExtractDefinePageMetaFromScriptSetup = (scriptSetup: string) => ViewDirMeta | null;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 密码哈希工具
|
|
3
|
+
* 使用 SHA-256 + 盐值对密码进行单向哈希
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 使用 SHA-256 对密码进行哈希
|
|
8
|
+
* @param password - 原始密码
|
|
9
|
+
* @param salt - 盐值,默认为 befly
|
|
10
|
+
* @returns 哈希后的密码(十六进制字符串)
|
|
11
|
+
*/
|
|
12
|
+
export async function hashPassword(password: string, salt: string = "befly"): Promise<string> {
|
|
13
|
+
const data = password + salt;
|
|
14
|
+
|
|
15
|
+
// 将字符串转换为 Uint8Array
|
|
16
|
+
const encoder = new TextEncoder();
|
|
17
|
+
const dataBuffer = encoder.encode(data);
|
|
18
|
+
|
|
19
|
+
// 使用 Web Crypto API 进行 SHA-256 哈希
|
|
20
|
+
const hashBuffer = await crypto.subtle.digest("SHA-256", dataBuffer);
|
|
21
|
+
|
|
22
|
+
// 将 ArrayBuffer 转换为十六进制字符串
|
|
23
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
24
|
+
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
25
|
+
|
|
26
|
+
return hashHex;
|
|
27
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 为表格列添加默认配置(纯函数)。
|
|
3
|
+
*
|
|
4
|
+
* 设计目标:
|
|
5
|
+
* - 不依赖浏览器/Node 特定 API
|
|
6
|
+
* - 不修改入参 columns 的对象引用(会返回新数组,并对每个列对象做浅拷贝合并)
|
|
7
|
+
*
|
|
8
|
+
* 默认行为:
|
|
9
|
+
* - base 默认:{ width: 200, ellipsis: true }
|
|
10
|
+
* - 特殊列:operation/state/id
|
|
11
|
+
* - colKey 以 At/At2 结尾时:默认 { align: "center" }
|
|
12
|
+
* - customConfig 可覆盖/扩展默认 specialColumnConfig(浅合并/替换:若传入 operation 配置,将整体替换默认 operation 配置,默认 fixed="right" 不会被保留)
|
|
13
|
+
*/
|
|
14
|
+
export function withDefaultColumns(columns: any[], customConfig?: Record<string, any>): any[] {
|
|
15
|
+
const safeColumns = Array.isArray(columns) ? columns : [];
|
|
16
|
+
|
|
17
|
+
const specialColumnConfig: Record<string, any> = Object.assign(
|
|
18
|
+
{
|
|
19
|
+
operation: { width: 100, align: "center", fixed: "right" },
|
|
20
|
+
state: { width: 100, align: "center" },
|
|
21
|
+
id: { width: 200, align: "center" }
|
|
22
|
+
},
|
|
23
|
+
customConfig || {}
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
return safeColumns.map((col) => {
|
|
27
|
+
const colKey = col && (col as any).colKey;
|
|
28
|
+
|
|
29
|
+
let specialConfig = colKey ? specialColumnConfig[colKey] : undefined;
|
|
30
|
+
|
|
31
|
+
if (!specialConfig && typeof colKey === "string" && (colKey.endsWith("At") || colKey.endsWith("At2"))) {
|
|
32
|
+
specialConfig = { align: "center" };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const base = {
|
|
36
|
+
width: 200,
|
|
37
|
+
ellipsis: true
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const merged = Object.assign({}, base);
|
|
41
|
+
if (specialConfig) {
|
|
42
|
+
Object.assign(merged, specialConfig);
|
|
43
|
+
}
|
|
44
|
+
Object.assign(merged, col);
|
|
45
|
+
|
|
46
|
+
return merged;
|
|
47
|
+
});
|
|
48
|
+
}
|