@cloudcome/utils-core 1.2.2 → 1.4.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/dist/enum.cjs +2 -2
- package/dist/enum.cjs.map +1 -1
- package/dist/enum.mjs +2 -2
- package/dist/enum.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/path.cjs +5 -1
- package/dist/path.cjs.map +1 -1
- package/dist/path.d.ts +11 -0
- package/dist/path.mjs +5 -1
- package/dist/path.mjs.map +1 -1
- package/package.json +1 -1
package/dist/enum.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
function
|
|
3
|
+
function _defineEnum(definition) {
|
|
4
4
|
const keys = Object.keys(definition);
|
|
5
5
|
return {
|
|
6
6
|
...[...Object.entries(definition)].reduce((acc, [key, dfn]) => {
|
|
@@ -50,7 +50,7 @@ function defineEnum(definition) {
|
|
|
50
50
|
function declareEnum() {
|
|
51
51
|
return {
|
|
52
52
|
define(definition) {
|
|
53
|
-
return
|
|
53
|
+
return _defineEnum(definition);
|
|
54
54
|
}
|
|
55
55
|
};
|
|
56
56
|
}
|
package/dist/enum.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.cjs","sources":["../src/enum.ts"],"sourcesContent":["import type { AnyObject, MergeIntersection, UnionToIntersection, UnionToTuple } from './types';\n\nexport type EnumKey = string;\nexport type EnumValue = number | string;\nexport type EnumMetaAppend = {\n key?: string;\n value?: string | number;\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n [key: string]: any;\n};\nexport type EnumMeta<A extends EnumMetaAppend> = A & {\n value: EnumValue;\n};\nexport type EnumDescription<A extends EnumMetaAppend> = Record<EnumKey, EnumMeta<A>>;\n\nconst enumError = Symbol('enumKeyError');\n\ntype _StartWithDollarSign<T extends string> = T extends `$${infer R}` ? R : never;\n\ntype _CheckDefinition<O extends AnyObject> = {\n // [K in keyof O & string]: K extends Capitalize<K> ? O[K] : `错误:枚举键名 ${K} 必须大写字母开头`;\n [K in keyof O & string]: K extends Capitalize<K>\n ? K extends `$${infer R}`\n ? O[K] & { [enumError]: `错误:枚举键名 ${K} 不能以 $ 符号开头` }\n : O[K]\n : O[K] & { [enumError]: `错误:枚举键名 ${K} 必须以大写字母开头` };\n};\n\ntype _ToOriginDefProp<T extends AnyObject> = {\n [K in keyof T as `$${K & string}`]: MergeIntersection<T[K] & { readonly key: K }>;\n};\n\ntype _KVRecord<T> = T extends Record<string, AnyObject>\n ? {\n readonly [K in keyof T]: T[K]['value'];\n }\n : never;\n\ntype _VKRecord<T> = T extends Record<string, AnyObject>\n ? MergeIntersection<\n UnionToIntersection<\n {\n [K in keyof T]: {\n [P in keyof T[K] as P extends 'value' ? T[K][P] & (string | number) : never]: K;\n };\n }[keyof T]\n >\n >\n : never;\n\nexport type EnumExpose<A extends EnumMetaAppend, E extends EnumDescription<A>> = _ToOriginDefProp<E> &\n _KVRecord<E> & {\n readonly definition: E;\n readonly descriptions: MergeIntersection<A & { key: keyof E }>[];\n readonly keys: UnionToTuple<keyof E>;\n readonly length: UnionToTuple<keyof E>['length'];\n readonly values: UnionToTuple<E[keyof E]['value']>;\n readonly kvRecord: _KVRecord<E>;\n readonly vkRecord: _VKRecord<E>;\n toKeyRecord: <P extends keyof A>(prop: P) => Record<keyof E, A[P]>;\n toValRecord: <P extends keyof A>(prop: P) => Record<E[keyof E]['value'], A[P]>;\n };\n\n/**\n * 定义一个枚举类型,如果需要类型提示,需要使用 declareEnum 函数定义枚举。\n * @template A - 枚举元数据附加类型,扩展自 EnumMetaAppend\n * @template E - 枚举描述类型,键为枚举键名,值为枚举元数据\n * @param {_CheckDefinition<E>} definition - 枚举定义对象\n * @returns {EnumExpose<A, E>} 返回枚举的完整暴露对象\n * @example\n * ```typescript\n * const Status = defineEnum({\n * Pending: { value: 0, label: '待处理' },\n * Approved: { value: 1, label: '已批准' }\n * });\n * ```\n *\n * @property {E} definition - 原始枚举定义对象\n * @property {Array<MergeIntersection<A & { key: keyof E }>>} descriptions - 枚举项的完整描述数组\n * @property {UnionToTuple<keyof E>} keys - 枚举键名的元组\n * @property {\"UnionToTuple<keyof E>[\\\"length\\\"]\"} length - 枚举项的数量\n * @property {UnionToTuple<E[keyof E][\"value\"]>} values - 枚举值的元组\n * @property {Record<keyof E, E[keyof E][\"value\"]>} kvRecord - 键到值的映射记录\n * @property {Record<E[keyof E][\"value\"], E[keyof E]>} vkRecord - 值到键的映射记录\n * @property {function} toKeyRecord - 根据属性名创建键到属性值的映射记录\n * @property {function} toValueRecord - 根据属性名创建值到属性值的映射记录\n */\nfunction
|
|
1
|
+
{"version":3,"file":"enum.cjs","sources":["../src/enum.ts"],"sourcesContent":["import type { AnyObject, MergeIntersection, UnionToIntersection, UnionToTuple } from './types';\n\nexport type EnumKey = string;\nexport type EnumValue = number | string;\nexport type EnumMetaAppend = {\n key?: string;\n value?: string | number;\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n [key: string]: any;\n};\nexport type EnumMeta<A extends EnumMetaAppend> = A & {\n value: EnumValue;\n};\nexport type EnumDescription<A extends EnumMetaAppend> = Record<EnumKey, EnumMeta<A>>;\n\nconst enumError = Symbol('enumKeyError');\n\ntype _StartWithDollarSign<T extends string> = T extends `$${infer R}` ? R : never;\n\ntype _CheckDefinition<O extends AnyObject> = {\n // [K in keyof O & string]: K extends Capitalize<K> ? O[K] : `错误:枚举键名 ${K} 必须大写字母开头`;\n [K in keyof O & string]: K extends Capitalize<K>\n ? K extends `$${infer R}`\n ? O[K] & { [enumError]: `错误:枚举键名 ${K} 不能以 $ 符号开头` }\n : O[K]\n : O[K] & { [enumError]: `错误:枚举键名 ${K} 必须以大写字母开头` };\n};\n\ntype _ToOriginDefProp<T extends AnyObject> = {\n [K in keyof T as `$${K & string}`]: MergeIntersection<T[K] & { readonly key: K }>;\n};\n\ntype _KVRecord<T> = T extends Record<string, AnyObject>\n ? {\n readonly [K in keyof T]: T[K]['value'];\n }\n : never;\n\ntype _VKRecord<T> = T extends Record<string, AnyObject>\n ? MergeIntersection<\n UnionToIntersection<\n {\n [K in keyof T]: {\n [P in keyof T[K] as P extends 'value' ? T[K][P] & (string | number) : never]: K;\n };\n }[keyof T]\n >\n >\n : never;\n\nexport type EnumExpose<A extends EnumMetaAppend, E extends EnumDescription<A>> = _ToOriginDefProp<E> &\n _KVRecord<E> & {\n readonly definition: E;\n readonly descriptions: MergeIntersection<A & { key: keyof E }>[];\n readonly keys: UnionToTuple<keyof E>;\n readonly length: UnionToTuple<keyof E>['length'];\n readonly values: UnionToTuple<E[keyof E]['value']>;\n readonly kvRecord: _KVRecord<E>;\n readonly vkRecord: _VKRecord<E>;\n toKeyRecord: <P extends keyof A>(prop: P) => Record<keyof E, A[P]>;\n toValRecord: <P extends keyof A>(prop: P) => Record<E[keyof E]['value'], A[P]>;\n };\n\n/**\n * 定义一个枚举类型,如果需要类型提示,需要使用 declareEnum 函数定义枚举。\n * @template A - 枚举元数据附加类型,扩展自 EnumMetaAppend\n * @template E - 枚举描述类型,键为枚举键名,值为枚举元数据\n * @param {_CheckDefinition<E>} definition - 枚举定义对象\n * @returns {EnumExpose<A, E>} 返回枚举的完整暴露对象\n * @example\n * ```typescript\n * const Status = defineEnum({\n * Pending: { value: 0, label: '待处理' },\n * Approved: { value: 1, label: '已批准' }\n * });\n * ```\n *\n * @property {E} definition - 原始枚举定义对象\n * @property {Array<MergeIntersection<A & { key: keyof E }>>} descriptions - 枚举项的完整描述数组\n * @property {UnionToTuple<keyof E>} keys - 枚举键名的元组\n * @property {\"UnionToTuple<keyof E>[\\\"length\\\"]\"} length - 枚举项的数量\n * @property {UnionToTuple<E[keyof E][\"value\"]>} values - 枚举值的元组\n * @property {Record<keyof E, E[keyof E][\"value\"]>} kvRecord - 键到值的映射记录\n * @property {Record<E[keyof E][\"value\"], E[keyof E]>} vkRecord - 值到键的映射记录\n * @property {function} toKeyRecord - 根据属性名创建键到属性值的映射记录\n * @property {function} toValueRecord - 根据属性名创建值到属性值的映射记录\n */\nfunction _defineEnum<A extends EnumMetaAppend, const E extends EnumDescription<A>>(\n definition: _CheckDefinition<E>,\n): EnumExpose<A, E> {\n const keys = Object.keys(definition);\n\n return {\n ...[...Object.entries(definition)].reduce((acc, [key, dfn]) => {\n if (key[0].toUpperCase() !== key[0]) {\n throw new Error(`错误:枚举键名 ${key} 必须以大写字母开头`);\n }\n\n if (key.startsWith('$')) {\n throw new Error(`错误:枚举键名 ${key} 不能以 $ 符号开头`);\n }\n\n // @ts-ignore\n acc[key] = dfn.value;\n // @ts-ignore\n acc[`$${key}`] = { key, ...dfn };\n return acc;\n }, {}),\n definition,\n descriptions: keys.map((key) => ({\n key,\n ...definition[key],\n })),\n keys,\n length: keys.length,\n values: keys.map((key) => definition[key as keyof E].value),\n kvRecord: keys.reduce((acc, key) => {\n // @ts-ignore\n acc[key] = definition[key].value;\n return acc;\n }, {}),\n vkRecord: keys.reduce((acc, key) => {\n // @ts-ignore\n acc[definition[key].value] = key;\n return acc;\n }, {}),\n toKeyRecord<P extends keyof A>(prop: P) {\n return keys.reduce((acc, key) => {\n // @ts-ignore\n acc[key] = definition[key][prop];\n return acc;\n }, {});\n },\n toValRecord<P extends keyof A>(prop: P) {\n return keys.reduce(\n (acc, key) => {\n // @ts-ignore\n acc[definition[key].value] = definition[key][prop];\n return acc;\n },\n {} as Record<E[keyof E]['value'], A[P]>,\n );\n },\n } as unknown as EnumExpose<A, E>;\n}\n\n/**\n * 声明一个枚举工厂函数\n * @template A - 枚举元数据附加类型,扩展自 EnumMetaAppend\n * @returns {Object} 返回包含 define 方法的对象\n *\n * @property {function} define - 定义枚举的函数\n * @template E - 枚举描述类型\n * @param {_CheckDefinition<E>} definition - 枚举定义对象\n * @returns {EnumExpose<A, E>} 返回枚举的完整暴露对象\n * @example\n * ```typescript\n * const createStatusEnum = declareEnum<{ label: string }>();\n * const Status = createStatusEnum.define({\n * Pending: { value: 0, label: '待处理' },\n * Approved: { value: 1, label: '已批准' }\n * });\n * ```\n */\nexport function declareEnum<A extends EnumMetaAppend>() {\n return {\n define<const E extends EnumDescription<A>>(definition: _CheckDefinition<E>): EnumExpose<A, E> {\n return _defineEnum(definition);\n },\n };\n}\n"],"names":[],"mappings":";;AAuFA,SAAS,YACP,YACkB;AACZ,QAAA,OAAO,OAAO,KAAK,UAAU;AAE5B,SAAA;AAAA,IACL,GAAG,CAAC,GAAG,OAAO,QAAQ,UAAU,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM;AAC7D,UAAI,IAAI,CAAC,EAAE,kBAAkB,IAAI,CAAC,GAAG;AACnC,cAAM,IAAI,MAAM,WAAW,GAAG,YAAY;AAAA,MAAA;AAGxC,UAAA,IAAI,WAAW,GAAG,GAAG;AACvB,cAAM,IAAI,MAAM,WAAW,GAAG,aAAa;AAAA,MAAA;AAIzC,UAAA,GAAG,IAAI,IAAI;AAEf,UAAI,IAAI,GAAG,EAAE,IAAI,EAAE,KAAK,GAAG,IAAI;AACxB,aAAA;AAAA,IACT,GAAG,EAAE;AAAA,IACL;AAAA,IACA,cAAc,KAAK,IAAI,CAAC,SAAS;AAAA,MAC/B;AAAA,MACA,GAAG,WAAW,GAAG;AAAA,IAAA,EACjB;AAAA,IACF;AAAA,IACA,QAAQ,KAAK;AAAA,IACb,QAAQ,KAAK,IAAI,CAAC,QAAQ,WAAW,GAAc,EAAE,KAAK;AAAA,IAC1D,UAAU,KAAK,OAAO,CAAC,KAAK,QAAQ;AAElC,UAAI,GAAG,IAAI,WAAW,GAAG,EAAE;AACpB,aAAA;AAAA,IACT,GAAG,EAAE;AAAA,IACL,UAAU,KAAK,OAAO,CAAC,KAAK,QAAQ;AAElC,UAAI,WAAW,GAAG,EAAE,KAAK,IAAI;AACtB,aAAA;AAAA,IACT,GAAG,EAAE;AAAA,IACL,YAA+B,MAAS;AACtC,aAAO,KAAK,OAAO,CAAC,KAAK,QAAQ;AAE/B,YAAI,GAAG,IAAI,WAAW,GAAG,EAAE,IAAI;AACxB,eAAA;AAAA,MACT,GAAG,EAAE;AAAA,IACP;AAAA,IACA,YAA+B,MAAS;AACtC,aAAO,KAAK;AAAA,QACV,CAAC,KAAK,QAAQ;AAER,cAAA,WAAW,GAAG,EAAE,KAAK,IAAI,WAAW,GAAG,EAAE,IAAI;AAC1C,iBAAA;AAAA,QACT;AAAA,QACA,CAAA;AAAA,MACF;AAAA,IAAA;AAAA,EAEJ;AACF;AAoBO,SAAS,cAAwC;AAC/C,SAAA;AAAA,IACL,OAA2C,YAAmD;AAC5F,aAAO,YAAY,UAAU;AAAA,IAAA;AAAA,EAEjC;AACF;;"}
|
package/dist/enum.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
function
|
|
1
|
+
function _defineEnum(definition) {
|
|
2
2
|
const keys = Object.keys(definition);
|
|
3
3
|
return {
|
|
4
4
|
...[...Object.entries(definition)].reduce((acc, [key, dfn]) => {
|
|
@@ -48,7 +48,7 @@ function defineEnum(definition) {
|
|
|
48
48
|
function declareEnum() {
|
|
49
49
|
return {
|
|
50
50
|
define(definition) {
|
|
51
|
-
return
|
|
51
|
+
return _defineEnum(definition);
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
54
|
}
|
package/dist/enum.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enum.mjs","sources":["../src/enum.ts"],"sourcesContent":["import type { AnyObject, MergeIntersection, UnionToIntersection, UnionToTuple } from './types';\n\nexport type EnumKey = string;\nexport type EnumValue = number | string;\nexport type EnumMetaAppend = {\n key?: string;\n value?: string | number;\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n [key: string]: any;\n};\nexport type EnumMeta<A extends EnumMetaAppend> = A & {\n value: EnumValue;\n};\nexport type EnumDescription<A extends EnumMetaAppend> = Record<EnumKey, EnumMeta<A>>;\n\nconst enumError = Symbol('enumKeyError');\n\ntype _StartWithDollarSign<T extends string> = T extends `$${infer R}` ? R : never;\n\ntype _CheckDefinition<O extends AnyObject> = {\n // [K in keyof O & string]: K extends Capitalize<K> ? O[K] : `错误:枚举键名 ${K} 必须大写字母开头`;\n [K in keyof O & string]: K extends Capitalize<K>\n ? K extends `$${infer R}`\n ? O[K] & { [enumError]: `错误:枚举键名 ${K} 不能以 $ 符号开头` }\n : O[K]\n : O[K] & { [enumError]: `错误:枚举键名 ${K} 必须以大写字母开头` };\n};\n\ntype _ToOriginDefProp<T extends AnyObject> = {\n [K in keyof T as `$${K & string}`]: MergeIntersection<T[K] & { readonly key: K }>;\n};\n\ntype _KVRecord<T> = T extends Record<string, AnyObject>\n ? {\n readonly [K in keyof T]: T[K]['value'];\n }\n : never;\n\ntype _VKRecord<T> = T extends Record<string, AnyObject>\n ? MergeIntersection<\n UnionToIntersection<\n {\n [K in keyof T]: {\n [P in keyof T[K] as P extends 'value' ? T[K][P] & (string | number) : never]: K;\n };\n }[keyof T]\n >\n >\n : never;\n\nexport type EnumExpose<A extends EnumMetaAppend, E extends EnumDescription<A>> = _ToOriginDefProp<E> &\n _KVRecord<E> & {\n readonly definition: E;\n readonly descriptions: MergeIntersection<A & { key: keyof E }>[];\n readonly keys: UnionToTuple<keyof E>;\n readonly length: UnionToTuple<keyof E>['length'];\n readonly values: UnionToTuple<E[keyof E]['value']>;\n readonly kvRecord: _KVRecord<E>;\n readonly vkRecord: _VKRecord<E>;\n toKeyRecord: <P extends keyof A>(prop: P) => Record<keyof E, A[P]>;\n toValRecord: <P extends keyof A>(prop: P) => Record<E[keyof E]['value'], A[P]>;\n };\n\n/**\n * 定义一个枚举类型,如果需要类型提示,需要使用 declareEnum 函数定义枚举。\n * @template A - 枚举元数据附加类型,扩展自 EnumMetaAppend\n * @template E - 枚举描述类型,键为枚举键名,值为枚举元数据\n * @param {_CheckDefinition<E>} definition - 枚举定义对象\n * @returns {EnumExpose<A, E>} 返回枚举的完整暴露对象\n * @example\n * ```typescript\n * const Status = defineEnum({\n * Pending: { value: 0, label: '待处理' },\n * Approved: { value: 1, label: '已批准' }\n * });\n * ```\n *\n * @property {E} definition - 原始枚举定义对象\n * @property {Array<MergeIntersection<A & { key: keyof E }>>} descriptions - 枚举项的完整描述数组\n * @property {UnionToTuple<keyof E>} keys - 枚举键名的元组\n * @property {\"UnionToTuple<keyof E>[\\\"length\\\"]\"} length - 枚举项的数量\n * @property {UnionToTuple<E[keyof E][\"value\"]>} values - 枚举值的元组\n * @property {Record<keyof E, E[keyof E][\"value\"]>} kvRecord - 键到值的映射记录\n * @property {Record<E[keyof E][\"value\"], E[keyof E]>} vkRecord - 值到键的映射记录\n * @property {function} toKeyRecord - 根据属性名创建键到属性值的映射记录\n * @property {function} toValueRecord - 根据属性名创建值到属性值的映射记录\n */\nfunction
|
|
1
|
+
{"version":3,"file":"enum.mjs","sources":["../src/enum.ts"],"sourcesContent":["import type { AnyObject, MergeIntersection, UnionToIntersection, UnionToTuple } from './types';\n\nexport type EnumKey = string;\nexport type EnumValue = number | string;\nexport type EnumMetaAppend = {\n key?: string;\n value?: string | number;\n // biome-ignore lint/suspicious/noExplicitAny: <explanation>\n [key: string]: any;\n};\nexport type EnumMeta<A extends EnumMetaAppend> = A & {\n value: EnumValue;\n};\nexport type EnumDescription<A extends EnumMetaAppend> = Record<EnumKey, EnumMeta<A>>;\n\nconst enumError = Symbol('enumKeyError');\n\ntype _StartWithDollarSign<T extends string> = T extends `$${infer R}` ? R : never;\n\ntype _CheckDefinition<O extends AnyObject> = {\n // [K in keyof O & string]: K extends Capitalize<K> ? O[K] : `错误:枚举键名 ${K} 必须大写字母开头`;\n [K in keyof O & string]: K extends Capitalize<K>\n ? K extends `$${infer R}`\n ? O[K] & { [enumError]: `错误:枚举键名 ${K} 不能以 $ 符号开头` }\n : O[K]\n : O[K] & { [enumError]: `错误:枚举键名 ${K} 必须以大写字母开头` };\n};\n\ntype _ToOriginDefProp<T extends AnyObject> = {\n [K in keyof T as `$${K & string}`]: MergeIntersection<T[K] & { readonly key: K }>;\n};\n\ntype _KVRecord<T> = T extends Record<string, AnyObject>\n ? {\n readonly [K in keyof T]: T[K]['value'];\n }\n : never;\n\ntype _VKRecord<T> = T extends Record<string, AnyObject>\n ? MergeIntersection<\n UnionToIntersection<\n {\n [K in keyof T]: {\n [P in keyof T[K] as P extends 'value' ? T[K][P] & (string | number) : never]: K;\n };\n }[keyof T]\n >\n >\n : never;\n\nexport type EnumExpose<A extends EnumMetaAppend, E extends EnumDescription<A>> = _ToOriginDefProp<E> &\n _KVRecord<E> & {\n readonly definition: E;\n readonly descriptions: MergeIntersection<A & { key: keyof E }>[];\n readonly keys: UnionToTuple<keyof E>;\n readonly length: UnionToTuple<keyof E>['length'];\n readonly values: UnionToTuple<E[keyof E]['value']>;\n readonly kvRecord: _KVRecord<E>;\n readonly vkRecord: _VKRecord<E>;\n toKeyRecord: <P extends keyof A>(prop: P) => Record<keyof E, A[P]>;\n toValRecord: <P extends keyof A>(prop: P) => Record<E[keyof E]['value'], A[P]>;\n };\n\n/**\n * 定义一个枚举类型,如果需要类型提示,需要使用 declareEnum 函数定义枚举。\n * @template A - 枚举元数据附加类型,扩展自 EnumMetaAppend\n * @template E - 枚举描述类型,键为枚举键名,值为枚举元数据\n * @param {_CheckDefinition<E>} definition - 枚举定义对象\n * @returns {EnumExpose<A, E>} 返回枚举的完整暴露对象\n * @example\n * ```typescript\n * const Status = defineEnum({\n * Pending: { value: 0, label: '待处理' },\n * Approved: { value: 1, label: '已批准' }\n * });\n * ```\n *\n * @property {E} definition - 原始枚举定义对象\n * @property {Array<MergeIntersection<A & { key: keyof E }>>} descriptions - 枚举项的完整描述数组\n * @property {UnionToTuple<keyof E>} keys - 枚举键名的元组\n * @property {\"UnionToTuple<keyof E>[\\\"length\\\"]\"} length - 枚举项的数量\n * @property {UnionToTuple<E[keyof E][\"value\"]>} values - 枚举值的元组\n * @property {Record<keyof E, E[keyof E][\"value\"]>} kvRecord - 键到值的映射记录\n * @property {Record<E[keyof E][\"value\"], E[keyof E]>} vkRecord - 值到键的映射记录\n * @property {function} toKeyRecord - 根据属性名创建键到属性值的映射记录\n * @property {function} toValueRecord - 根据属性名创建值到属性值的映射记录\n */\nfunction _defineEnum<A extends EnumMetaAppend, const E extends EnumDescription<A>>(\n definition: _CheckDefinition<E>,\n): EnumExpose<A, E> {\n const keys = Object.keys(definition);\n\n return {\n ...[...Object.entries(definition)].reduce((acc, [key, dfn]) => {\n if (key[0].toUpperCase() !== key[0]) {\n throw new Error(`错误:枚举键名 ${key} 必须以大写字母开头`);\n }\n\n if (key.startsWith('$')) {\n throw new Error(`错误:枚举键名 ${key} 不能以 $ 符号开头`);\n }\n\n // @ts-ignore\n acc[key] = dfn.value;\n // @ts-ignore\n acc[`$${key}`] = { key, ...dfn };\n return acc;\n }, {}),\n definition,\n descriptions: keys.map((key) => ({\n key,\n ...definition[key],\n })),\n keys,\n length: keys.length,\n values: keys.map((key) => definition[key as keyof E].value),\n kvRecord: keys.reduce((acc, key) => {\n // @ts-ignore\n acc[key] = definition[key].value;\n return acc;\n }, {}),\n vkRecord: keys.reduce((acc, key) => {\n // @ts-ignore\n acc[definition[key].value] = key;\n return acc;\n }, {}),\n toKeyRecord<P extends keyof A>(prop: P) {\n return keys.reduce((acc, key) => {\n // @ts-ignore\n acc[key] = definition[key][prop];\n return acc;\n }, {});\n },\n toValRecord<P extends keyof A>(prop: P) {\n return keys.reduce(\n (acc, key) => {\n // @ts-ignore\n acc[definition[key].value] = definition[key][prop];\n return acc;\n },\n {} as Record<E[keyof E]['value'], A[P]>,\n );\n },\n } as unknown as EnumExpose<A, E>;\n}\n\n/**\n * 声明一个枚举工厂函数\n * @template A - 枚举元数据附加类型,扩展自 EnumMetaAppend\n * @returns {Object} 返回包含 define 方法的对象\n *\n * @property {function} define - 定义枚举的函数\n * @template E - 枚举描述类型\n * @param {_CheckDefinition<E>} definition - 枚举定义对象\n * @returns {EnumExpose<A, E>} 返回枚举的完整暴露对象\n * @example\n * ```typescript\n * const createStatusEnum = declareEnum<{ label: string }>();\n * const Status = createStatusEnum.define({\n * Pending: { value: 0, label: '待处理' },\n * Approved: { value: 1, label: '已批准' }\n * });\n * ```\n */\nexport function declareEnum<A extends EnumMetaAppend>() {\n return {\n define<const E extends EnumDescription<A>>(definition: _CheckDefinition<E>): EnumExpose<A, E> {\n return _defineEnum(definition);\n },\n };\n}\n"],"names":[],"mappings":"AAuFA,SAAS,YACP,YACkB;AACZ,QAAA,OAAO,OAAO,KAAK,UAAU;AAE5B,SAAA;AAAA,IACL,GAAG,CAAC,GAAG,OAAO,QAAQ,UAAU,CAAC,EAAE,OAAO,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM;AAC7D,UAAI,IAAI,CAAC,EAAE,kBAAkB,IAAI,CAAC,GAAG;AACnC,cAAM,IAAI,MAAM,WAAW,GAAG,YAAY;AAAA,MAAA;AAGxC,UAAA,IAAI,WAAW,GAAG,GAAG;AACvB,cAAM,IAAI,MAAM,WAAW,GAAG,aAAa;AAAA,MAAA;AAIzC,UAAA,GAAG,IAAI,IAAI;AAEf,UAAI,IAAI,GAAG,EAAE,IAAI,EAAE,KAAK,GAAG,IAAI;AACxB,aAAA;AAAA,IACT,GAAG,EAAE;AAAA,IACL;AAAA,IACA,cAAc,KAAK,IAAI,CAAC,SAAS;AAAA,MAC/B;AAAA,MACA,GAAG,WAAW,GAAG;AAAA,IAAA,EACjB;AAAA,IACF;AAAA,IACA,QAAQ,KAAK;AAAA,IACb,QAAQ,KAAK,IAAI,CAAC,QAAQ,WAAW,GAAc,EAAE,KAAK;AAAA,IAC1D,UAAU,KAAK,OAAO,CAAC,KAAK,QAAQ;AAElC,UAAI,GAAG,IAAI,WAAW,GAAG,EAAE;AACpB,aAAA;AAAA,IACT,GAAG,EAAE;AAAA,IACL,UAAU,KAAK,OAAO,CAAC,KAAK,QAAQ;AAElC,UAAI,WAAW,GAAG,EAAE,KAAK,IAAI;AACtB,aAAA;AAAA,IACT,GAAG,EAAE;AAAA,IACL,YAA+B,MAAS;AACtC,aAAO,KAAK,OAAO,CAAC,KAAK,QAAQ;AAE/B,YAAI,GAAG,IAAI,WAAW,GAAG,EAAE,IAAI;AACxB,eAAA;AAAA,MACT,GAAG,EAAE;AAAA,IACP;AAAA,IACA,YAA+B,MAAS;AACtC,aAAO,KAAK;AAAA,QACV,CAAC,KAAK,QAAQ;AAER,cAAA,WAAW,GAAG,EAAE,KAAK,IAAI,WAAW,GAAG,EAAE,IAAI;AAC1C,iBAAA;AAAA,QACT;AAAA,QACA,CAAA;AAAA,MACF;AAAA,IAAA;AAAA,EAEJ;AACF;AAoBO,SAAS,cAAwC;AAC/C,SAAA;AAAA,IACL,OAA2C,YAAmD;AAC5F,aAAO,YAAY,UAAU;AAAA,IAAA;AAAA,EAEjC;AACF;"}
|
package/dist/index.cjs
CHANGED
package/dist/index.mjs
CHANGED
package/dist/path.cjs
CHANGED
|
@@ -40,7 +40,7 @@ function pathNormalize(path) {
|
|
|
40
40
|
}
|
|
41
41
|
push(slice);
|
|
42
42
|
}
|
|
43
|
-
return points.join("/");
|
|
43
|
+
return points.join("/") || (isAbs ? "/" : ".");
|
|
44
44
|
}
|
|
45
45
|
function pathJoin(from, ...to) {
|
|
46
46
|
return pathNormalize([from, ...to].join("/"));
|
|
@@ -68,8 +68,12 @@ function pathRelativize(path) {
|
|
|
68
68
|
if (path.startsWith("../")) return path;
|
|
69
69
|
return `./${path}`;
|
|
70
70
|
}
|
|
71
|
+
function pathDirname(path) {
|
|
72
|
+
return pathJoin(path, "..");
|
|
73
|
+
}
|
|
71
74
|
exports.isAbsolutePath = isAbsolutePath;
|
|
72
75
|
exports.isRelativePath = isRelativePath;
|
|
76
|
+
exports.pathDirname = pathDirname;
|
|
73
77
|
exports.pathJoin = pathJoin;
|
|
74
78
|
exports.pathNormalize = pathNormalize;
|
|
75
79
|
exports.pathRelativize = pathRelativize;
|
package/dist/path.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.cjs","sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n"],"names":["arrayEach"],"mappings":";;;AAOA,SAAS,gBAAgB,OAAwB;AAC/C,SAAO,UAAU;AACnB;AAOA,SAAS,eAAe,OAAwB;AAC9C,SAAO,UAAU;AACnB;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,KAAK,WAAW,GAAG;AAC5B;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,CAAC,eAAe,IAAI;AAC7B;AAYO,SAAS,cAAc,MAAsB;AAC5C,QAAA,SAAS,KACZ,QAAQ,OAAO,GAAG,EAClB,QAAQ,WAAW,GAAG,EACtB,QAAQ,WAAW,IAAI,EACvB,QAAQ,WAAW,GAAG,EACtB,MAAM,GAAG,EACT,IAAI,CAAC,UAAU,MAAM,KAAA,CAAM;AAC9B,QAAM,SAAmB,CAAC;AACpB,QAAA,QAAQ,OAAO,CAAC,MAAM;AAEtB,QAAA,OAAO,CAAC,UAAkB;AAC9B,WAAO,KAAK,KAAK;AAAA,EACnB;AAEA,QAAM,OAAO,MAAM;AAEb,QAAA,OAAO,WAAW,KAAK,MAAO;AAGlC,QAAI,OAAO,WAAW,KAAK,OAAO,GAAG,EAAE,MAAM,MAAM;AACjD,aAAO,KAAK,IAAI;AAAA,IAAA,OACX;AACL,aAAO,IAAI;AAAA,IAAA;AAAA,EAEf;AAEA,aAAW,SAAS,QAAQ;AACpB,UAAA,YAAY,gBAAgB,KAAK;AACjC,UAAA,WAAW,eAAe,KAAK;AASrC,QAAI,WAAW;AACb;AAAA,IAAA;AAGF,QAAI,UAAU;AACP,WAAA;AACL;AAAA,IAAA;AAGF,SAAK,KAAK;AAAA,EAAA;
|
|
1
|
+
{"version":3,"file":"path.cjs","sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/') || (isAbs ? '/' : '.');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n\n/**\n * 获取路径的目录部分\n * @param {string} path - 输入的路径字符串\n * @returns {string} - 返回路径的目录部分\n * @example\n * ```typescript\n * const dir = pathDirname('/path/to/file.txt');\n * console.log(dir); // 输出: '/path/to'\n * ```\n */\nexport function pathDirname(path: string): string {\n return pathJoin(path, '..');\n}\n"],"names":["arrayEach"],"mappings":";;;AAOA,SAAS,gBAAgB,OAAwB;AAC/C,SAAO,UAAU;AACnB;AAOA,SAAS,eAAe,OAAwB;AAC9C,SAAO,UAAU;AACnB;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,KAAK,WAAW,GAAG;AAC5B;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,CAAC,eAAe,IAAI;AAC7B;AAYO,SAAS,cAAc,MAAsB;AAC5C,QAAA,SAAS,KACZ,QAAQ,OAAO,GAAG,EAClB,QAAQ,WAAW,GAAG,EACtB,QAAQ,WAAW,IAAI,EACvB,QAAQ,WAAW,GAAG,EACtB,MAAM,GAAG,EACT,IAAI,CAAC,UAAU,MAAM,KAAA,CAAM;AAC9B,QAAM,SAAmB,CAAC;AACpB,QAAA,QAAQ,OAAO,CAAC,MAAM;AAEtB,QAAA,OAAO,CAAC,UAAkB;AAC9B,WAAO,KAAK,KAAK;AAAA,EACnB;AAEA,QAAM,OAAO,MAAM;AAEb,QAAA,OAAO,WAAW,KAAK,MAAO;AAGlC,QAAI,OAAO,WAAW,KAAK,OAAO,GAAG,EAAE,MAAM,MAAM;AACjD,aAAO,KAAK,IAAI;AAAA,IAAA,OACX;AACL,aAAO,IAAI;AAAA,IAAA;AAAA,EAEf;AAEA,aAAW,SAAS,QAAQ;AACpB,UAAA,YAAY,gBAAgB,KAAK;AACjC,UAAA,WAAW,eAAe,KAAK;AASrC,QAAI,WAAW;AACb;AAAA,IAAA;AAGF,QAAI,UAAU;AACP,WAAA;AACL;AAAA,IAAA;AAGF,SAAK,KAAK;AAAA,EAAA;AAGZ,SAAO,OAAO,KAAK,GAAG,MAAM,QAAQ,MAAM;AAC5C;AAagB,SAAA,SAAS,SAAiB,IAAsB;AACvD,SAAA,cAAc,CAAC,MAAM,GAAG,EAAE,EAAE,KAAK,GAAG,CAAC;AAC9C;AAagB,SAAA,YAAY,SAAiB,IAAsB;AACjE,QAAM,QAAQ,CAAC,MAAM,GAAG,EAAE,EAAE,IAAI,aAAa;AAE7C,MAAI,gBAAgB;AACpB,MAAI,iBAAiB;AAErBA,QAAA;AAAA,IACE;AAAA,IACA,CAAC,MAAM,UAAU;AACX,UAAA,eAAe,IAAI,GAAG;AACR,wBAAA;AACC,yBAAA;AACV,eAAA;AAAA,MAAA;AAAA,IAEX;AAAA,IACA;AAAA,EACF;AAEA,SAAO,SAAS,eAAe,GAAG,MAAM,MAAM,iBAAiB,CAAC,CAAC;AACnE;AA0BO,SAAS,eAAe,MAAsB;AAC/C,MAAA,eAAe,IAAI,EAAU,QAAA;AACjC,MAAI,KAAK,WAAW,IAAI,EAAU,QAAA;AAClC,MAAI,KAAK,WAAW,KAAK,EAAU,QAAA;AACnC,SAAO,KAAK,IAAI;AAClB;AAYO,SAAS,YAAY,MAAsB;AACzC,SAAA,SAAS,MAAM,IAAI;AAC5B;;;;;;;;"}
|
package/dist/path.d.ts
CHANGED
|
@@ -80,3 +80,14 @@ export declare function pathResolve(from: string, ...to: string[]): string;
|
|
|
80
80
|
* ```
|
|
81
81
|
*/
|
|
82
82
|
export declare function pathRelativize(path: string): string;
|
|
83
|
+
/**
|
|
84
|
+
* 获取路径的目录部分
|
|
85
|
+
* @param {string} path - 输入的路径字符串
|
|
86
|
+
* @returns {string} - 返回路径的目录部分
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* const dir = pathDirname('/path/to/file.txt');
|
|
90
|
+
* console.log(dir); // 输出: '/path/to'
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
export declare function pathDirname(path: string): string;
|
package/dist/path.mjs
CHANGED
|
@@ -38,7 +38,7 @@ function pathNormalize(path) {
|
|
|
38
38
|
}
|
|
39
39
|
push(slice);
|
|
40
40
|
}
|
|
41
|
-
return points.join("/");
|
|
41
|
+
return points.join("/") || (isAbs ? "/" : ".");
|
|
42
42
|
}
|
|
43
43
|
function pathJoin(from, ...to) {
|
|
44
44
|
return pathNormalize([from, ...to].join("/"));
|
|
@@ -66,9 +66,13 @@ function pathRelativize(path) {
|
|
|
66
66
|
if (path.startsWith("../")) return path;
|
|
67
67
|
return `./${path}`;
|
|
68
68
|
}
|
|
69
|
+
function pathDirname(path) {
|
|
70
|
+
return pathJoin(path, "..");
|
|
71
|
+
}
|
|
69
72
|
export {
|
|
70
73
|
isAbsolutePath,
|
|
71
74
|
isRelativePath,
|
|
75
|
+
pathDirname,
|
|
72
76
|
pathJoin,
|
|
73
77
|
pathNormalize,
|
|
74
78
|
pathRelativize,
|
package/dist/path.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"path.mjs","sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n"],"names":[],"mappings":";AAOA,SAAS,gBAAgB,OAAwB;AAC/C,SAAO,UAAU;AACnB;AAOA,SAAS,eAAe,OAAwB;AAC9C,SAAO,UAAU;AACnB;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,KAAK,WAAW,GAAG;AAC5B;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,CAAC,eAAe,IAAI;AAC7B;AAYO,SAAS,cAAc,MAAsB;AAC5C,QAAA,SAAS,KACZ,QAAQ,OAAO,GAAG,EAClB,QAAQ,WAAW,GAAG,EACtB,QAAQ,WAAW,IAAI,EACvB,QAAQ,WAAW,GAAG,EACtB,MAAM,GAAG,EACT,IAAI,CAAC,UAAU,MAAM,KAAA,CAAM;AAC9B,QAAM,SAAmB,CAAC;AACpB,QAAA,QAAQ,OAAO,CAAC,MAAM;AAEtB,QAAA,OAAO,CAAC,UAAkB;AAC9B,WAAO,KAAK,KAAK;AAAA,EACnB;AAEA,QAAM,OAAO,MAAM;AAEb,QAAA,OAAO,WAAW,KAAK,MAAO;AAGlC,QAAI,OAAO,WAAW,KAAK,OAAO,GAAG,EAAE,MAAM,MAAM;AACjD,aAAO,KAAK,IAAI;AAAA,IAAA,OACX;AACL,aAAO,IAAI;AAAA,IAAA;AAAA,EAEf;AAEA,aAAW,SAAS,QAAQ;AACpB,UAAA,YAAY,gBAAgB,KAAK;AACjC,UAAA,WAAW,eAAe,KAAK;AASrC,QAAI,WAAW;AACb;AAAA,IAAA;AAGF,QAAI,UAAU;AACP,WAAA;AACL;AAAA,IAAA;AAGF,SAAK,KAAK;AAAA,EAAA;
|
|
1
|
+
{"version":3,"file":"path.mjs","sources":["../src/path.ts"],"sourcesContent":["import { arrayEach } from './array';\n\n/**\n * 判断是否是当前目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是当前目录标记'.'则返回true,否则返回false\n */\nfunction _isCurrentSlice(slice: string): boolean {\n return slice === '.';\n}\n\n/**\n * 判断是否是上级目录标记\n * @param {string} slice - 路径片段\n * @returns {boolean} - 如果是上级目录标记'..'则返回true,否则返回false\n */\nfunction _isParentSlice(slice: string): boolean {\n return slice === '..';\n}\n\n/**\n * 判断是否是绝对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是绝对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isAbs = isAbsolutePath('/path/to/file');\n * console.log(isAbs); // 输出: true\n * ```\n */\nexport function isAbsolutePath(path: string): boolean {\n return path.startsWith('/');\n}\n\n/**\n * 判断是否是相对路径\n * @param {string} path - 路径字符串\n * @returns {boolean} - 如果是相对路径则返回true,否则返回false\n * @example\n * ```typescript\n * const isRel = isRelativePath('path/to/file');\n * console.log(isRel); // 输出: true\n * ```\n */\nexport function isRelativePath(path: string): boolean {\n return !isAbsolutePath(path);\n}\n\n/**\n * 标准化路径\n * @param {string} path - 要标准化的路径字符串。\n * @returns {string} - 标准化后的路径字符串。\n * @example\n * ```typescript\n * const normalizedPath = pathNormalize('/path///to///file');\n * console.log(normalizedPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathNormalize(path: string): string {\n const slices = path\n .replace(/\\\\/g, '/')\n .replace(/\\/{2,}/g, '/')\n .replace(/\\.{3,}/g, '..')\n .replace(/\\/\\.\\//g, '/')\n .split('/')\n .map((point) => point.trim());\n const points: string[] = [];\n const isAbs = slices[0] === '';\n\n const push = (point: string) => {\n points.push(point);\n };\n\n const back = () => {\n // 绝对路径不能退到根目录\n if (points.length === 1 && isAbs) return;\n\n //\n if (points.length === 0 || points.at(-1) === '..') {\n points.push('..');\n } else {\n points.pop();\n }\n };\n\n for (const slice of slices) {\n const isCurrent = _isCurrentSlice(slice);\n const isParent = _isParentSlice(slice);\n\n // // 未进入实际路径\n // if (!inPoints) {\n // push(slice);\n // inPoints = !isCurrent && !isParent;\n // continue;\n // }\n\n if (isCurrent) {\n continue;\n }\n\n if (isParent) {\n back();\n continue;\n }\n\n push(slice);\n }\n\n return points.join('/') || (isAbs ? '/' : '.');\n}\n\n/**\n * 路径合并\n * @param {string} from - 起始路径。\n * @param {...string[]} to - 要合并的路径片段。\n * @returns {string} - 合并后的路径字符串。\n * @example\n * ```typescript\n * const fullPath = pathJoin('/path', '/to', 'file');\n * console.log(fullPath); // 输出: '/path/to/file'\n * ```\n */\nexport function pathJoin(from: string, ...to: string[]): string {\n return pathNormalize([from, ...to].join('/'));\n}\n\n/**\n * 解析路径\n * @param {string} from - 起始路径\n * @param {...string[]} to - 要解析的路径片段\n * @returns {string} - 解析后的绝对路径\n * @example\n * ```typescript\n * const resolvedPath = pathResolve('/path', '/to', 'file');\n * console.log(resolvedPath); // 输出: '/to/file'\n * ```\n */\nexport function pathResolve(from: string, ...to: string[]): string {\n const paths = [from, ...to].map(pathNormalize);\n\n let lastStartPath = from;\n let lastStartIndex = 0;\n\n arrayEach(\n paths,\n (path, index) => {\n if (isAbsolutePath(path)) {\n lastStartPath = path;\n lastStartIndex = index;\n return false;\n }\n },\n true,\n );\n\n return pathJoin(lastStartPath, ...paths.slice(lastStartIndex + 1));\n}\n\n/**\n * 将相对路径转换为标准的相对路径格式(添加'./'前缀)\n *\n * @param {string} path - 要处理的路径字符串\n * @returns {string} 处理后的路径字符串\n *\n * @example <caption>处理绝对路径</caption>\n * ```typescript\n * const result = pathRelativize('/path/to/file');\n * console.log(result); // 输出: '/path/to/file'\n * ```\n *\n * @example <caption>处理已带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('./path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n *\n * @example <caption>处理不带'./'前缀的相对路径</caption>\n * ```typescript\n * const result = pathRelativize('path/to/file');\n * console.log(result); // 输出: './path/to/file'\n * ```\n */\nexport function pathRelativize(path: string): string {\n if (isAbsolutePath(path)) return path;\n if (path.startsWith('./')) return path;\n if (path.startsWith('../')) return path;\n return `./${path}`;\n}\n\n/**\n * 获取路径的目录部分\n * @param {string} path - 输入的路径字符串\n * @returns {string} - 返回路径的目录部分\n * @example\n * ```typescript\n * const dir = pathDirname('/path/to/file.txt');\n * console.log(dir); // 输出: '/path/to'\n * ```\n */\nexport function pathDirname(path: string): string {\n return pathJoin(path, '..');\n}\n"],"names":[],"mappings":";AAOA,SAAS,gBAAgB,OAAwB;AAC/C,SAAO,UAAU;AACnB;AAOA,SAAS,eAAe,OAAwB;AAC9C,SAAO,UAAU;AACnB;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,KAAK,WAAW,GAAG;AAC5B;AAYO,SAAS,eAAe,MAAuB;AAC7C,SAAA,CAAC,eAAe,IAAI;AAC7B;AAYO,SAAS,cAAc,MAAsB;AAC5C,QAAA,SAAS,KACZ,QAAQ,OAAO,GAAG,EAClB,QAAQ,WAAW,GAAG,EACtB,QAAQ,WAAW,IAAI,EACvB,QAAQ,WAAW,GAAG,EACtB,MAAM,GAAG,EACT,IAAI,CAAC,UAAU,MAAM,KAAA,CAAM;AAC9B,QAAM,SAAmB,CAAC;AACpB,QAAA,QAAQ,OAAO,CAAC,MAAM;AAEtB,QAAA,OAAO,CAAC,UAAkB;AAC9B,WAAO,KAAK,KAAK;AAAA,EACnB;AAEA,QAAM,OAAO,MAAM;AAEb,QAAA,OAAO,WAAW,KAAK,MAAO;AAGlC,QAAI,OAAO,WAAW,KAAK,OAAO,GAAG,EAAE,MAAM,MAAM;AACjD,aAAO,KAAK,IAAI;AAAA,IAAA,OACX;AACL,aAAO,IAAI;AAAA,IAAA;AAAA,EAEf;AAEA,aAAW,SAAS,QAAQ;AACpB,UAAA,YAAY,gBAAgB,KAAK;AACjC,UAAA,WAAW,eAAe,KAAK;AASrC,QAAI,WAAW;AACb;AAAA,IAAA;AAGF,QAAI,UAAU;AACP,WAAA;AACL;AAAA,IAAA;AAGF,SAAK,KAAK;AAAA,EAAA;AAGZ,SAAO,OAAO,KAAK,GAAG,MAAM,QAAQ,MAAM;AAC5C;AAagB,SAAA,SAAS,SAAiB,IAAsB;AACvD,SAAA,cAAc,CAAC,MAAM,GAAG,EAAE,EAAE,KAAK,GAAG,CAAC;AAC9C;AAagB,SAAA,YAAY,SAAiB,IAAsB;AACjE,QAAM,QAAQ,CAAC,MAAM,GAAG,EAAE,EAAE,IAAI,aAAa;AAE7C,MAAI,gBAAgB;AACpB,MAAI,iBAAiB;AAErB;AAAA,IACE;AAAA,IACA,CAAC,MAAM,UAAU;AACX,UAAA,eAAe,IAAI,GAAG;AACR,wBAAA;AACC,yBAAA;AACV,eAAA;AAAA,MAAA;AAAA,IAEX;AAAA,IACA;AAAA,EACF;AAEA,SAAO,SAAS,eAAe,GAAG,MAAM,MAAM,iBAAiB,CAAC,CAAC;AACnE;AA0BO,SAAS,eAAe,MAAsB;AAC/C,MAAA,eAAe,IAAI,EAAU,QAAA;AACjC,MAAI,KAAK,WAAW,IAAI,EAAU,QAAA;AAClC,MAAI,KAAK,WAAW,KAAK,EAAU,QAAA;AACnC,SAAO,KAAK,IAAI;AAClB;AAYO,SAAS,YAAY,MAAsB;AACzC,SAAA,SAAS,MAAM,IAAI;AAC5B;"}
|