@clerc/utils 0.42.0 → 0.42.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/dist/index.d.ts +5 -5
- package/dist/index.js +15 -19
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _clerc_core from '@clerc/core';
|
|
2
|
-
import { I18N, Commands, CommandType,
|
|
2
|
+
import { I18N, Commands, CommandType, TranslateFn, Command, RootType, Root } from '@clerc/core';
|
|
3
3
|
|
|
4
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>;
|
|
@@ -15,10 +15,10 @@ type KebabCase<T extends string, A extends string = ""> = T extends `${infer Pre
|
|
|
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 function arrayEquals<T>(
|
|
19
|
-
declare function arrayStartsWith<T>(
|
|
20
|
-
declare function semanticArray(
|
|
21
|
-
declare function resolveCommandStrict(commands: Commands, name: CommandType | string[], t:
|
|
18
|
+
declare function arrayEquals<T>(arr1: T[], arr2: T[]): boolean;
|
|
19
|
+
declare function arrayStartsWith<T>(arr: T[], start: T[]): boolean;
|
|
20
|
+
declare function semanticArray(arr: string[], { add, t }: I18N): string;
|
|
21
|
+
declare function resolveCommandStrict(commands: Commands, name: CommandType | string[], t: TranslateFn): [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
|
@@ -14,34 +14,30 @@ const camelCase = (word) => word.replace(/[\W_]([a-z\d])?/gi, (_, c) => c ? c.to
|
|
|
14
14
|
const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
|
|
15
15
|
const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${kebabCase(n)}`;
|
|
16
16
|
const gracefulVersion = (v) => v.length === 0 ? "" : v.startsWith("v") ? v : `v${v}`;
|
|
17
|
-
function arrayEquals(
|
|
18
|
-
if (
|
|
17
|
+
function arrayEquals(arr1, arr2) {
|
|
18
|
+
if (arr2.length !== arr1.length) {
|
|
19
19
|
return false;
|
|
20
20
|
}
|
|
21
|
-
return
|
|
21
|
+
return arr1.every((item, i) => item === arr2[i]);
|
|
22
22
|
}
|
|
23
|
-
function arrayStartsWith(
|
|
24
|
-
if (start.length >
|
|
23
|
+
function arrayStartsWith(arr, start) {
|
|
24
|
+
if (start.length > arr.length) {
|
|
25
25
|
return false;
|
|
26
26
|
}
|
|
27
|
-
return arrayEquals(
|
|
27
|
+
return arrayEquals(arr.slice(0, start.length), start);
|
|
28
28
|
}
|
|
29
|
-
function semanticArray(
|
|
29
|
+
function semanticArray(arr, { add, t }) {
|
|
30
30
|
add(locales);
|
|
31
|
-
if (
|
|
32
|
-
return
|
|
31
|
+
if (arr.length <= 1) {
|
|
32
|
+
return arr[0];
|
|
33
33
|
}
|
|
34
|
-
return t(
|
|
35
|
-
"utils.and",
|
|
36
|
-
array.slice(0, -1).join(", "),
|
|
37
|
-
array[array.length - 1]
|
|
38
|
-
);
|
|
34
|
+
return t("utils.and", arr.slice(0, -1).join(", "), arr[arr.length - 1]);
|
|
39
35
|
}
|
|
40
36
|
function resolveCommandStrict(commands, name, t) {
|
|
41
37
|
if (name === Root) {
|
|
42
38
|
return [commands[Root], Root];
|
|
43
39
|
}
|
|
44
|
-
const
|
|
40
|
+
const nameArr = toArray(name);
|
|
45
41
|
const commandsMap = resolveFlattenCommands(commands, t);
|
|
46
42
|
let current;
|
|
47
43
|
let currentName;
|
|
@@ -49,7 +45,7 @@ function resolveCommandStrict(commands, name, t) {
|
|
|
49
45
|
if (k === Root || currentName === Root) {
|
|
50
46
|
continue;
|
|
51
47
|
}
|
|
52
|
-
if (arrayEquals(
|
|
48
|
+
if (arrayEquals(nameArr, k)) {
|
|
53
49
|
current = v;
|
|
54
50
|
currentName = k;
|
|
55
51
|
}
|
|
@@ -57,10 +53,10 @@ function resolveCommandStrict(commands, name, t) {
|
|
|
57
53
|
return [current, currentName];
|
|
58
54
|
}
|
|
59
55
|
function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
60
|
-
const
|
|
56
|
+
const parentArr = parent === "" ? [] : Array.isArray(parent) ? parent : parent.split(" ");
|
|
61
57
|
return Object.values(commands).filter((c) => {
|
|
62
|
-
const
|
|
63
|
-
return arrayStartsWith(
|
|
58
|
+
const commandNameArr = c.name.split(" ");
|
|
59
|
+
return arrayStartsWith(commandNameArr, parentArr) && commandNameArr.length - parentArr.length <= depth;
|
|
64
60
|
});
|
|
65
61
|
}
|
|
66
62
|
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|