@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.
Files changed (3) hide show
  1. package/dist/index.d.ts +29 -22
  2. package/dist/index.js +32 -60
  3. package/package.json +14 -13
package/dist/index.d.ts CHANGED
@@ -1,25 +1,32 @@
1
- import * as _clerc_core from '@clerc/core';
2
- import { I18N, Commands, CommandType, TranslateFn, Command, RootType, Root } from '@clerc/core';
3
-
4
- type Equals<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
5
- type Dict<T> = Record<string, T>;
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
- 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
- type Numeric = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
11
- type AlphaNumeric = AlphabetLowercase | Uppercase<AlphabetLowercase> | Numeric;
12
- type CamelCase<Word extends string> = Word extends `${infer FirstCharacter}${infer Rest}` ? FirstCharacter extends AlphaNumeric ? `${FirstCharacter}${CamelCase<Rest>}` : Capitalize<CamelCase<Rest>> : Word;
13
- declare const camelCase: (word: string) => string;
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
- declare const kebabCase: <T extends string>(s: T) => KebabCase<T, "">;
16
- declare const gracefulFlagName: (n: string) => string;
17
- declare const gracefulVersion: (v: string) => string;
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
- 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
- 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
-
1
+ //#region src/index.ts
12
2
  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(arr1, arr2) {
18
- if (arr2.length !== arr1.length) {
19
- return false;
20
- }
21
- return arr1.every((item, i) => item === arr2[i]);
22
- }
23
- function arrayStartsWith(arr, start) {
24
- if (start.length > arr.length) {
25
- return false;
26
- }
27
- return arrayEquals(arr.slice(0, start.length), start);
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 semanticArray(arr, { add, t }) {
30
- add(locales);
31
- if (arr.length <= 1) {
32
- return arr[0];
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
- function resolveCommandStrict(commands, name, t) {
37
- if (name === Root) {
38
- return [commands[Root], Root];
39
- }
40
- const nameArr = toArray(name);
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
- export { arrayEquals, arrayStartsWith, camelCase, gracefulFlagName, gracefulVersion, kebabCase, resolveCommandStrict, resolveRootCommands, resolveSubcommandsByParent, semanticArray, toArray };
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.43.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": "pkgroll",
52
- "watch": "pkgroll --watch"
52
+ "build": "tsdown",
53
+ "watch": "tsdown --watch"
53
54
  }
54
55
  }