@clerc/utils 0.43.0 → 1.0.0-beta.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 +29 -22
- package/dist/index.js +32 -60
- package/package.json +14 -13
package/dist/index.d.ts
CHANGED
|
@@ -1,25 +1,32 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
type
|
|
5
|
-
type
|
|
1
|
+
//#region src/types/type-fest.d.ts
|
|
2
|
+
type LiteralUnion<LiteralType, BaseType> = LiteralType | (BaseType & Record<never, never>);
|
|
3
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
4
|
+
type Primitive = null | undefined | string | number | boolean | symbol | bigint;
|
|
5
|
+
type BuiltIns = Primitive | void | Date | RegExp;
|
|
6
|
+
type NonRecursiveType = BuiltIns | Function | (new (...arguments_: any[]) => unknown) | Promise<unknown>;
|
|
7
|
+
type UnknownArray = readonly unknown[];
|
|
8
|
+
type MapsSetsOrArrays = ReadonlyMap<unknown, unknown> | WeakMap<WeakKey, unknown> | ReadonlySet<unknown> | WeakSet<WeakKey> | UnknownArray;
|
|
9
|
+
type ConditionalDeepPrettify<T, E = never, I$1 = unknown> = T extends E ? T : T extends I$1 ? { [TypeKey in keyof T]: ConditionalDeepPrettify<T[TypeKey], E, I$1> } : T;
|
|
10
|
+
type DeepPrettify<T, E = never> = ConditionalDeepPrettify<T, E | NonRecursiveType | MapsSetsOrArrays, object>;
|
|
11
|
+
//#endregion
|
|
12
|
+
//#region src/types/index.d.ts
|
|
6
13
|
type ToArray<T> = T extends any[] ? T : [T];
|
|
7
14
|
type MaybeArray<T> = T | T[];
|
|
15
|
+
type PartialRequired<T, K$1 extends keyof T> = T & { [P in K$1]-?: T[P] };
|
|
16
|
+
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/index.d.ts
|
|
8
19
|
declare const toArray: <T>(a: MaybeArray<T>) => T[];
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
declare
|
|
14
|
-
|
|
15
|
-
declare const kebabCase: <T extends string>(s: T) =>
|
|
16
|
-
declare const
|
|
17
|
-
declare const
|
|
18
|
-
declare
|
|
19
|
-
declare
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
declare const resolveRootCommands: (commands: Commands) => Command<string, _clerc_core.CommandOptions<string[], MaybeArray<string | typeof Root>, _clerc_core.Flags>>[];
|
|
24
|
-
|
|
25
|
-
export { CamelCase, Dict, Equals, KebabCase, MaybeArray, ToArray, arrayEquals, arrayStartsWith, camelCase, gracefulFlagName, gracefulVersion, kebabCase, resolveCommandStrict, resolveRootCommands, resolveSubcommandsByParent, semanticArray, toArray };
|
|
20
|
+
/**
|
|
21
|
+
* Converts a dash-separated string to camelCase.
|
|
22
|
+
* Not using regexp for better performance, because this function is used in parser.
|
|
23
|
+
*/
|
|
24
|
+
declare function camelCase(str: string): string;
|
|
25
|
+
declare function joinWithAnd(values: string[]): string;
|
|
26
|
+
declare const kebabCase: <T extends string>(s: T) => string;
|
|
27
|
+
declare const formatFlagName: (n: string) => string;
|
|
28
|
+
declare const formatVersion: (v: string) => string;
|
|
29
|
+
declare const isTruthy: <T>(item: T) => item is Exclude<T, false | null | undefined>;
|
|
30
|
+
declare const objectIsEmpty: (obj: Record<string, any>) => boolean;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { DeepPrettify, LiteralUnion, MaybeArray, PartialRequired, Prettify, ToArray, UnionToIntersection, camelCase, formatFlagName, formatVersion, isTruthy, joinWithAnd, kebabCase, objectIsEmpty, toArray };
|
package/dist/index.js
CHANGED
|
@@ -1,64 +1,36 @@
|
|
|
1
|
-
|
|
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
|
-
|
|
1
|
+
//#region src/index.ts
|
|
12
2
|
const toArray = (a) => Array.isArray(a) ? a : [a];
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Converts a dash-separated string to camelCase.
|
|
5
|
+
* Not using regexp for better performance, because this function is used in parser.
|
|
6
|
+
*/
|
|
7
|
+
function camelCase(str) {
|
|
8
|
+
const dashIdx = str.indexOf("-");
|
|
9
|
+
if (dashIdx === -1) return str;
|
|
10
|
+
let result = str.slice(0, dashIdx);
|
|
11
|
+
for (let i = dashIdx; i < str.length; i++) if (str[i] === "-" && i + 1 < str.length) {
|
|
12
|
+
const nextChar = str.charCodeAt(i + 1);
|
|
13
|
+
if (nextChar >= 97 && nextChar <= 122) {
|
|
14
|
+
result += String.fromCharCode(nextChar - 32);
|
|
15
|
+
i++;
|
|
16
|
+
} else {
|
|
17
|
+
result += str[i + 1];
|
|
18
|
+
i++;
|
|
19
|
+
}
|
|
20
|
+
} else if (str[i] !== "-") result += str[i];
|
|
21
|
+
return result;
|
|
28
22
|
}
|
|
29
|
-
function
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
return t("utils.and", arr.slice(0, -1).join(", "), arr[arr.length - 1]);
|
|
23
|
+
function joinWithAnd(values) {
|
|
24
|
+
if (values.length === 0) return "";
|
|
25
|
+
if (values.length === 1) return values[0];
|
|
26
|
+
const last = values.pop();
|
|
27
|
+
return `${values.join(", ")} and ${last}`;
|
|
35
28
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
const commandsMap = resolveFlattenCommands(commands, t);
|
|
42
|
-
let current;
|
|
43
|
-
let currentName;
|
|
44
|
-
for (const [k, v] of commandsMap.entries()) {
|
|
45
|
-
if (k === Root || currentName === Root) {
|
|
46
|
-
continue;
|
|
47
|
-
}
|
|
48
|
-
if (arrayEquals(nameArr, k)) {
|
|
49
|
-
current = v;
|
|
50
|
-
currentName = k;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
return [current, currentName];
|
|
54
|
-
}
|
|
55
|
-
function resolveSubcommandsByParent(commands, parent, depth = Infinity) {
|
|
56
|
-
const parentArr = parent === "" ? [] : Array.isArray(parent) ? parent : parent.split(" ");
|
|
57
|
-
return Object.values(commands).filter((c) => {
|
|
58
|
-
const commandNameArr = c.name.split(" ");
|
|
59
|
-
return arrayStartsWith(commandNameArr, parentArr) && commandNameArr.length - parentArr.length <= depth;
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
const resolveRootCommands = (commands) => resolveSubcommandsByParent(commands, "", 1);
|
|
29
|
+
const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
|
|
30
|
+
const formatFlagName = (n) => n.length <= 1 ? `-${n}` : `--${kebabCase(n)}`;
|
|
31
|
+
const formatVersion = (v) => v.length === 0 ? "" : v.startsWith("v") ? v : `v${v}`;
|
|
32
|
+
const isTruthy = Boolean;
|
|
33
|
+
const objectIsEmpty = (obj) => Object.keys(obj).length === 0;
|
|
63
34
|
|
|
64
|
-
|
|
35
|
+
//#endregion
|
|
36
|
+
export { camelCase, formatFlagName, formatVersion, isTruthy, joinWithAnd, kebabCase, objectIsEmpty, toArray };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc utils",
|
|
@@ -12,7 +12,6 @@
|
|
|
12
12
|
"clerc-utils",
|
|
13
13
|
"cli",
|
|
14
14
|
"terminal",
|
|
15
|
-
"utils",
|
|
16
15
|
"utils"
|
|
17
16
|
],
|
|
18
17
|
"homepage": "https://github.com/clercjs/clerc/tree/main/packages/utils#readme",
|
|
@@ -27,28 +26,30 @@
|
|
|
27
26
|
"license": "MIT",
|
|
28
27
|
"sideEffects": false,
|
|
29
28
|
"exports": {
|
|
30
|
-
".":
|
|
31
|
-
"types": "./dist/index.d.ts",
|
|
32
|
-
"import": "./dist/index.js"
|
|
33
|
-
}
|
|
29
|
+
".": "./dist/index.js"
|
|
34
30
|
},
|
|
35
|
-
"main": "dist/index.js",
|
|
36
|
-
"module": "dist/index.js",
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"module": "./dist/index.js",
|
|
37
33
|
"types": "dist/index.d.ts",
|
|
34
|
+
"typesVersions": {
|
|
35
|
+
"*": {
|
|
36
|
+
"*": [
|
|
37
|
+
"./dist/*",
|
|
38
|
+
"./dist/index.d.ts"
|
|
39
|
+
]
|
|
40
|
+
}
|
|
41
|
+
},
|
|
38
42
|
"files": [
|
|
39
43
|
"dist"
|
|
40
44
|
],
|
|
41
45
|
"publishConfig": {
|
|
42
46
|
"access": "public"
|
|
43
47
|
},
|
|
44
|
-
"devDependencies": {
|
|
45
|
-
"type-fest": "^4.3.1"
|
|
46
|
-
},
|
|
47
48
|
"peerDependencies": {
|
|
48
49
|
"@clerc/core": "*"
|
|
49
50
|
},
|
|
50
51
|
"scripts": {
|
|
51
|
-
"build": "
|
|
52
|
-
"watch": "
|
|
52
|
+
"build": "tsdown",
|
|
53
|
+
"watch": "tsdown --watch"
|
|
53
54
|
}
|
|
54
55
|
}
|