@ybgnb/file-naming 0.0.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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +1 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hzhilong
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日期格式映射(键:dayjs格式,值:示例)
|
|
3
|
+
*/
|
|
4
|
+
declare const dateFormatMap: {
|
|
5
|
+
readonly 'YYYY-MM-DD': "2026-07-02";
|
|
6
|
+
readonly YYYY_MM_DD: "2026_07_02";
|
|
7
|
+
readonly YYYYMMDD: "20260702";
|
|
8
|
+
readonly 'YY-MM-DD': "26-07-02";
|
|
9
|
+
readonly YY_MM_DD: "26_07_02";
|
|
10
|
+
readonly YYMMDD: "260702";
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* 日期格式
|
|
14
|
+
*/
|
|
15
|
+
type DateFormat = keyof typeof dateFormatMap;
|
|
16
|
+
/**
|
|
17
|
+
* 时间格式映射(键:dayjs格式,值:示例)
|
|
18
|
+
*/
|
|
19
|
+
declare const timeFormatMap: {
|
|
20
|
+
readonly HHmmss: "143045";
|
|
21
|
+
readonly 'HH-mm-ss': "14-30-45";
|
|
22
|
+
readonly HH_mm_ss: "14_30_45";
|
|
23
|
+
readonly HHmm: "1430";
|
|
24
|
+
readonly 'HH-mm': "14-30";
|
|
25
|
+
readonly HH_mm: "14_30";
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* 时间格式
|
|
29
|
+
*/
|
|
30
|
+
type TimeFormat = keyof typeof timeFormatMap;
|
|
31
|
+
/**
|
|
32
|
+
* 序号格式映射
|
|
33
|
+
*/
|
|
34
|
+
declare const serialNumberFormatMap: {
|
|
35
|
+
readonly natural: "自然数";
|
|
36
|
+
readonly zeroPad: "前导零填充";
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* 序号格式
|
|
40
|
+
*/
|
|
41
|
+
type SerialNumberFormat = keyof typeof serialNumberFormatMap;
|
|
42
|
+
/**
|
|
43
|
+
* 扩展的格式
|
|
44
|
+
*/
|
|
45
|
+
interface ExtendedFormats {
|
|
46
|
+
dateFormat: DateFormat;
|
|
47
|
+
timeFormat: TimeFormat;
|
|
48
|
+
serialNumberFormat: SerialNumberFormat;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 文件命名上下文
|
|
53
|
+
*/
|
|
54
|
+
interface FileNamingContext<TData = unknown> {
|
|
55
|
+
/**
|
|
56
|
+
* 序号,单个文件解析时为1
|
|
57
|
+
*/
|
|
58
|
+
serialNumber: number;
|
|
59
|
+
/**
|
|
60
|
+
* 总数,单个文件解析时为1
|
|
61
|
+
*/
|
|
62
|
+
total: number;
|
|
63
|
+
/**
|
|
64
|
+
* 解析日期
|
|
65
|
+
*/
|
|
66
|
+
resolveDate: Date;
|
|
67
|
+
/**
|
|
68
|
+
* 扩展的格式
|
|
69
|
+
*/
|
|
70
|
+
extendedFormats: ExtendedFormats;
|
|
71
|
+
/**
|
|
72
|
+
* 解析的数据
|
|
73
|
+
*/
|
|
74
|
+
data: TData;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 文件命名字段定义
|
|
79
|
+
*/
|
|
80
|
+
interface FileNamingFieldDefinition<TData = unknown> {
|
|
81
|
+
label: string;
|
|
82
|
+
resolve: (context: FileNamingContext<TData>) => string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* 文件命名字段映射
|
|
86
|
+
*/
|
|
87
|
+
declare const baseFileNamingFieldMap: {
|
|
88
|
+
readonly "-": FileNamingFieldDefinition<unknown>;
|
|
89
|
+
readonly _: FileNamingFieldDefinition<unknown>;
|
|
90
|
+
readonly ".": FileNamingFieldDefinition<unknown>;
|
|
91
|
+
readonly ",": FileNamingFieldDefinition<unknown>;
|
|
92
|
+
readonly ";": FileNamingFieldDefinition<unknown>;
|
|
93
|
+
readonly "\uFF08": FileNamingFieldDefinition<unknown>;
|
|
94
|
+
readonly "\uFF09": FileNamingFieldDefinition<unknown>;
|
|
95
|
+
readonly "(": FileNamingFieldDefinition<unknown>;
|
|
96
|
+
readonly ")": FileNamingFieldDefinition<unknown>;
|
|
97
|
+
readonly "[": FileNamingFieldDefinition<unknown>;
|
|
98
|
+
readonly "]": FileNamingFieldDefinition<unknown>;
|
|
99
|
+
readonly "{": FileNamingFieldDefinition<unknown>;
|
|
100
|
+
readonly "}": FileNamingFieldDefinition<unknown>;
|
|
101
|
+
readonly "+": FileNamingFieldDefinition<unknown>;
|
|
102
|
+
readonly "=": FileNamingFieldDefinition<unknown>;
|
|
103
|
+
readonly "~": FileNamingFieldDefinition<unknown>;
|
|
104
|
+
readonly "!": FileNamingFieldDefinition<unknown>;
|
|
105
|
+
readonly "@": FileNamingFieldDefinition<unknown>;
|
|
106
|
+
readonly "#": FileNamingFieldDefinition<unknown>;
|
|
107
|
+
readonly $: FileNamingFieldDefinition<unknown>;
|
|
108
|
+
readonly "%": FileNamingFieldDefinition<unknown>;
|
|
109
|
+
readonly "^": FileNamingFieldDefinition<unknown>;
|
|
110
|
+
readonly "&": FileNamingFieldDefinition<unknown>;
|
|
111
|
+
readonly "\u201C": FileNamingFieldDefinition<unknown>;
|
|
112
|
+
readonly "\u201D": FileNamingFieldDefinition<unknown>;
|
|
113
|
+
readonly "\uFF1A": FileNamingFieldDefinition<unknown>;
|
|
114
|
+
readonly serialNumber: {
|
|
115
|
+
readonly label: "序号";
|
|
116
|
+
readonly resolve: ({ total, serialNumber, extendedFormats }: FileNamingContext<unknown>) => string;
|
|
117
|
+
};
|
|
118
|
+
readonly total: {
|
|
119
|
+
readonly label: "总数";
|
|
120
|
+
readonly resolve: ({ total }: FileNamingContext<unknown>) => string;
|
|
121
|
+
};
|
|
122
|
+
readonly space: {
|
|
123
|
+
readonly label: "空格";
|
|
124
|
+
readonly resolve: () => string;
|
|
125
|
+
};
|
|
126
|
+
readonly fileSeparator: {
|
|
127
|
+
readonly label: "/";
|
|
128
|
+
readonly resolve: () => string;
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* 基础文件命名字段
|
|
133
|
+
*/
|
|
134
|
+
type BaseFileNamingField = keyof typeof baseFileNamingFieldMap;
|
|
135
|
+
/**
|
|
136
|
+
* 所有的基础文件命名字段
|
|
137
|
+
*/
|
|
138
|
+
declare const allBaseFileNamingFields: BaseFileNamingField[];
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 文件命名策略
|
|
142
|
+
*/
|
|
143
|
+
interface FileNamingStrategy<TData = unknown> {
|
|
144
|
+
fields: (FileNamingFieldDefinition<TData> | BaseFileNamingField)[];
|
|
145
|
+
extendedFormats: ExtendedFormats;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 解析文件路径的结果
|
|
150
|
+
*/
|
|
151
|
+
interface ResolveFilePathResult {
|
|
152
|
+
/**
|
|
153
|
+
* 相对路径
|
|
154
|
+
*/
|
|
155
|
+
relativePath: string;
|
|
156
|
+
/**
|
|
157
|
+
* 路径片段(按路径分隔符拆分后的各级目录/文件名,不对应命名字段)
|
|
158
|
+
*/
|
|
159
|
+
segments: string[];
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* 路径片段转换器
|
|
164
|
+
*/
|
|
165
|
+
type PathSegmentTransformer = (segment: string) => string;
|
|
166
|
+
|
|
167
|
+
declare class FileNamer<TData = unknown> {
|
|
168
|
+
private strategy;
|
|
169
|
+
private pathSegmentTransformer;
|
|
170
|
+
constructor(options: Omit<FileNamingStrategy<TData>, 'extendedFormats'> & {
|
|
171
|
+
extendedFormats?: Partial<ExtendedFormats>;
|
|
172
|
+
pathSegmentTransformer?: PathSegmentTransformer;
|
|
173
|
+
});
|
|
174
|
+
private buildPathSegments;
|
|
175
|
+
private resolveOne;
|
|
176
|
+
resolve(data: TData): ResolveFilePathResult;
|
|
177
|
+
resolve(data: TData[]): ResolveFilePathResult[];
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
declare const createCharNamingField: <KEY extends string>(char: KEY, label?: string) => { [k in KEY]: FileNamingFieldDefinition; };
|
|
181
|
+
declare const createCharNamingFields: <const CHARS extends readonly string[]>(chars: CHARS) => { [K in CHARS[number]]: FileNamingFieldDefinition; };
|
|
182
|
+
|
|
183
|
+
declare function defaultSanitizePathSegment(segment: string): string;
|
|
184
|
+
|
|
185
|
+
export { type BaseFileNamingField, type DateFormat, type ExtendedFormats, FileNamer, type FileNamingContext, type FileNamingFieldDefinition, type FileNamingStrategy, type PathSegmentTransformer, type ResolveFilePathResult, type SerialNumberFormat, type TimeFormat, allBaseFileNamingFields, baseFileNamingFieldMap, createCharNamingField, createCharNamingFields, dateFormatMap, defaultSanitizePathSegment, serialNumberFormatMap, timeFormatMap };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var F=(t,e)=>({[t]:{label:e??t,resolve:()=>t}}),m=t=>Object.assign({},...t.map(e=>F(e)));var i={serialNumber:{label:"\u5E8F\u53F7",resolve:({total:t,serialNumber:e,extendedFormats:r})=>r.serialNumberFormat==="natural"?String(e):String(e).padStart(String(t).length,"0")},total:{label:"\u603B\u6570",resolve:({total:t})=>String(t)},space:{label:"\u7A7A\u683C",resolve:()=>" "},fileSeparator:{label:"/",resolve:()=>"/"},...m(["-","_",".",",",";","\uFF08","\uFF09","(",")","[","]","{","}","+","=","~","!","@","#","$","%","^","&","\u201C","\u201D","\uFF1A"])},x=Object.keys(i);var f=/[<>:"/\\|?*\u0000-\u001F]/g,g=new Set(["CON","PRN","AUX","NUL","COM1","COM2","COM3","COM4","COM5","COM6","COM7","COM8","COM9","LPT1","LPT2","LPT3","LPT4","LPT5","LPT6","LPT7","LPT8","LPT9"]);function l(t){let e=t.trim();return e=e.replace(f,"_"),e=e.replace(/[. ]+$/,""),e.length===0?"":(g.has(e.toUpperCase())&&(e=`_${e}`),e)}var p=class{constructor(e){let{dateFormat:r,timeFormat:a,serialNumberFormat:o}=e.extendedFormats??{};this.strategy={fields:e.fields,extendedFormats:{dateFormat:r??"YYYY-MM-DD",timeFormat:a??"HH-mm-ss",serialNumberFormat:o??"zeroPad"}},this.pathSegmentTransformer=e.pathSegmentTransformer??l}buildPathSegments(e){let r=[],a="";for(let o of e){if(o==="/"){a&&(r.push(a),a="");continue}a+=o}return a&&r.push(a),r}resolveOne(e){let r={total:1,serialNumber:1,extendedFormats:this.strategy.extendedFormats,resolveDate:new Date,data:e},a=this.strategy.fields.map(s=>typeof s=="string"?i[s]?i[s].resolve(r):"":s.resolve(r)),n=this.buildPathSegments(a).filter(Boolean).map(this.pathSegmentTransformer);return{segments:n,relativePath:n.join("/")}}resolve(e){return Array.isArray(e)?e.map(r=>this.resolveOne(r)):this.resolveOne(e)}};var M={"YYYY-MM-DD":"2026-07-02",YYYY_MM_DD:"2026_07_02",YYYYMMDD:"20260702","YY-MM-DD":"26-07-02",YY_MM_DD:"26_07_02",YYMMDD:"260702"},S={HHmmss:"143045","HH-mm-ss":"14-30-45",HH_mm_ss:"14_30_45",HHmm:"1430","HH-mm":"14-30",HH_mm:"14_30"},T={natural:"\u81EA\u7136\u6570",zeroPad:"\u524D\u5BFC\u96F6\u586B\u5145"};export{p as FileNamer,x as allBaseFileNamingFields,i as baseFileNamingFieldMap,F as createCharNamingField,m as createCharNamingFields,M as dateFormatMap,l as defaultSanitizePathSegment,T as serialNumberFormatMap,S as timeFormatMap};
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ybgnb/file-naming",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"author": "hzhilong",
|
|
5
|
+
"private": false,
|
|
6
|
+
"description": "文件命名模板解析器",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.cjs",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"require": "./dist/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"scripts": {
|
|
22
|
+
"check": "tsc --build --noEmit && eslint .",
|
|
23
|
+
"lint": "eslint . --fix",
|
|
24
|
+
"format": "prettier --write .",
|
|
25
|
+
"barrelsby": "barrelsby -c barrelsby.json & import-suffixer \"src/**/*.ts\"",
|
|
26
|
+
"build": "tsup",
|
|
27
|
+
"npm login": "npm login",
|
|
28
|
+
"npm publish": "npm publish --access public"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "https://github.com/hzhilong/ybgnb-js",
|
|
33
|
+
"directory": "file-naming"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"ybgnb"
|
|
37
|
+
],
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"bugs": {
|
|
40
|
+
"url": "https://github.com/hzhilong/ybgnb-js/issues"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/hzhilong/ybgnb-js/tree/main/file-naming#readme",
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"@ybgnb/utils": "^0.3.1"
|
|
45
|
+
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@types/node": "^25.6.0",
|
|
48
|
+
"barrelsby": "^2.8.1",
|
|
49
|
+
"eslint": "^9.39.4",
|
|
50
|
+
"eslint-config-prettier": "^10.1.8",
|
|
51
|
+
"eslint-plugin-n": "^18.0.1",
|
|
52
|
+
"import-suffixer": "^0.0.1",
|
|
53
|
+
"jiti": "^2.7.0",
|
|
54
|
+
"prettier": "^3.8.1",
|
|
55
|
+
"tsup": "^8.5.1",
|
|
56
|
+
"typescript": "^6.0.2",
|
|
57
|
+
"typescript-eslint": "^8.58.0"
|
|
58
|
+
}
|
|
59
|
+
}
|