@clerc/utils 0.40.0 → 0.42.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/index.d.ts +7 -7
- package/dist/index.js +68 -1
- package/package.json +14 -14
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as _clerc_core from '@clerc/core';
|
|
2
|
-
import { I18N, Commands, CommandType,
|
|
2
|
+
import { I18N, Commands, CommandType, TranslateFunction, Command, RootType, Root } from '@clerc/core';
|
|
3
3
|
|
|
4
|
-
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends
|
|
4
|
+
type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
5
5
|
type Dict<T> = Record<string, T>;
|
|
6
6
|
type ToArray<T> = T extends any[] ? T : [T];
|
|
7
7
|
type MaybeArray<T> = T | T[];
|
|
@@ -9,16 +9,16 @@ declare const toArray: <T>(a: MaybeArray<T>) => T[];
|
|
|
9
9
|
type AlphabetLowercase = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z";
|
|
10
10
|
type Numeric = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
|
|
11
11
|
type AlphaNumeric = AlphabetLowercase | Uppercase<AlphabetLowercase> | Numeric;
|
|
12
|
-
type CamelCase<Word extends string> = Word extends `${infer FirstCharacter}${infer Rest}` ?
|
|
12
|
+
type CamelCase<Word extends string> = Word extends `${infer FirstCharacter}${infer Rest}` ? FirstCharacter extends AlphaNumeric ? `${FirstCharacter}${CamelCase<Rest>}` : Capitalize<CamelCase<Rest>> : Word;
|
|
13
13
|
declare const camelCase: (word: string) => string;
|
|
14
14
|
type KebabCase<T extends string, A extends string = ""> = T extends `${infer Prefix}${infer Suffix}` ? KebabCase<Suffix, `${A}${Prefix extends Lowercase<Prefix> ? "" : "-"}${Lowercase<Prefix>}`> : A;
|
|
15
15
|
declare const kebabCase: <T extends string>(s: T) => KebabCase<T, "">;
|
|
16
16
|
declare const gracefulFlagName: (n: string) => string;
|
|
17
17
|
declare const gracefulVersion: (v: string) => string;
|
|
18
|
-
declare
|
|
19
|
-
declare
|
|
20
|
-
declare
|
|
21
|
-
declare function resolveCommandStrict(commands: Commands, name: CommandType | string[], t:
|
|
18
|
+
declare function arrayEquals<T>(array1: T[], array2: T[]): boolean;
|
|
19
|
+
declare function arrayStartsWith<T>(array: T[], start: T[]): boolean;
|
|
20
|
+
declare function semanticArray(array: string[], { add, t }: I18N): string;
|
|
21
|
+
declare function resolveCommandStrict(commands: Commands, name: CommandType | string[], t: TranslateFunction): [Command<string | RootType> | undefined, string[] | RootType | undefined];
|
|
22
22
|
declare function resolveSubcommandsByParent(commands: Commands, parent: string | string[], depth?: number): Command<string, _clerc_core.CommandOptions<string[], MaybeArray<string | typeof Root>, _clerc_core.Flags>>[];
|
|
23
23
|
declare const resolveRootCommands: (commands: Commands) => Command<string, _clerc_core.CommandOptions<string[], MaybeArray<string | typeof Root>, _clerc_core.Flags>>[];
|
|
24
24
|
|
package/dist/index.js
CHANGED
|
@@ -1 +1,68 @@
|
|
|
1
|
-
import{Root
|
|
1
|
+
import { Root, resolveFlattenCommands } from '@clerc/core';
|
|
2
|
+
|
|
3
|
+
const locales = {
|
|
4
|
+
"en": {
|
|
5
|
+
"utils.and": "%s and %s"
|
|
6
|
+
},
|
|
7
|
+
"zh-CN": {
|
|
8
|
+
"utils.and": "%s \u548C %s"
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const toArray = (a) => Array.isArray(a) ? a : [a];
|
|
13
|
+
const camelCase = (word) => word.replace(/[\W_]([a-z\d])?/gi, (_, c) => c ? c.toUpperCase() : "");
|
|
14
|
+
const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
|
|
15
|
+
const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${kebabCase(n)}`;
|
|
16
|
+
const gracefulVersion = (v) => v.length === 0 ? "" : v.startsWith("v") ? v : `v${v}`;
|
|
17
|
+
function arrayEquals(array1, array2) {
|
|
18
|
+
if (array2.length !== array1.length) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return array1.every((item, index) => item === array2[index]);
|
|
22
|
+
}
|
|
23
|
+
function arrayStartsWith(array, start) {
|
|
24
|
+
if (start.length > array.length) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return arrayEquals(array.slice(0, start.length), start);
|
|
28
|
+
}
|
|
29
|
+
function semanticArray(array, { add, t }) {
|
|
30
|
+
add(locales);
|
|
31
|
+
if (array.length <= 1) {
|
|
32
|
+
return array[0];
|
|
33
|
+
}
|
|
34
|
+
return t(
|
|
35
|
+
"utils.and",
|
|
36
|
+
array.slice(0, -1).join(", "),
|
|
37
|
+
array[array.length - 1]
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
function resolveCommandStrict(commands, name, t) {
|
|
41
|
+
if (name === Root) {
|
|
42
|
+
return [commands[Root], Root];
|
|
43
|
+
}
|
|
44
|
+
const nameArray = toArray(name);
|
|
45
|
+
const commandsMap = resolveFlattenCommands(commands, t);
|
|
46
|
+
let current;
|
|
47
|
+
let currentName;
|
|
48
|
+
for (const [k, v] of commandsMap.entries()) {
|
|
49
|
+
if (k === Root || currentName === Root) {
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (arrayEquals(nameArray, k)) {
|
|
53
|
+
current = v;
|
|
54
|
+
currentName = k;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return [current, currentName];
|
|
58
|
+
}
|
|
59
|
+
function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
60
|
+
const parentArray = parent === "" ? [] : Array.isArray(parent) ? parent : parent.split(" ");
|
|
61
|
+
return Object.values(commands).filter((c) => {
|
|
62
|
+
const commandNameArray = c.name.split(" ");
|
|
63
|
+
return arrayStartsWith(commandNameArray, parentArray) && commandNameArray.length - parentArray.length <= depth;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
67
|
+
|
|
68
|
+
export { arrayEquals, arrayStartsWith, camelCase, gracefulFlagName, gracefulVersion, kebabCase, resolveCommandStrict, resolveRootCommands, resolveSubcommandsByParent, semanticArray, toArray };
|
package/package.json
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.42.0",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
|
+
"type": "module",
|
|
5
6
|
"description": "Clerc utils",
|
|
6
7
|
"keywords": [
|
|
7
|
-
"
|
|
8
|
-
"clerc",
|
|
9
|
-
"clerc-utils",
|
|
10
|
-
"utils",
|
|
8
|
+
"args",
|
|
11
9
|
"arguments",
|
|
12
10
|
"argv",
|
|
13
|
-
"
|
|
11
|
+
"clerc",
|
|
12
|
+
"clerc-utils",
|
|
13
|
+
"cli",
|
|
14
14
|
"terminal",
|
|
15
|
+
"utils",
|
|
15
16
|
"utils"
|
|
16
17
|
],
|
|
17
|
-
"
|
|
18
|
-
"homepage": "https://github.com/so1ve/clerc/tree/main/packages/utils#readme",
|
|
18
|
+
"homepage": "https://github.com/clercjs/clerc/tree/main/packages/utils#readme",
|
|
19
19
|
"repository": {
|
|
20
20
|
"type": "git",
|
|
21
|
-
"url": "git+https://github.com/
|
|
21
|
+
"url": "git+https://github.com/clercjs/clerc.git",
|
|
22
22
|
"directory": "/"
|
|
23
23
|
},
|
|
24
24
|
"bugs": {
|
|
25
|
-
"url": "https://github.com/
|
|
25
|
+
"url": "https://github.com/clercjs/clerc/issues"
|
|
26
26
|
},
|
|
27
27
|
"license": "MIT",
|
|
28
28
|
"sideEffects": false,
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
"publishConfig": {
|
|
42
42
|
"access": "public"
|
|
43
43
|
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"type-fest": "^4.0.0"
|
|
46
|
+
},
|
|
44
47
|
"peerDependencies": {
|
|
45
48
|
"@clerc/core": "*"
|
|
46
49
|
},
|
|
47
|
-
"devDependencies": {
|
|
48
|
-
"type-fest": "^3.8.0"
|
|
49
|
-
},
|
|
50
50
|
"scripts": {
|
|
51
|
-
"build": "pkgroll
|
|
51
|
+
"build": "pkgroll",
|
|
52
52
|
"watch": "pkgroll --watch"
|
|
53
53
|
}
|
|
54
54
|
}
|