@clerc/utils 0.3.4 → 0.5.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.cjs CHANGED
@@ -6,9 +6,31 @@ const mustArray = (a) => Array.isArray(a) ? a : [a];
6
6
  const camelCase = (s) => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
7
7
  const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
8
8
  const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
9
- const gracefulVersion = (v) => v.length === 0 ? v.startsWith("v") ? v : `v${v}` : "";
9
+ const gracefulVersion = (v) => v.length === 0 ? "" : v.startsWith("v") ? v : `v${v}`;
10
+ const arrayStartsWith = (arr, start) => {
11
+ if (start.length > arr.length) {
12
+ return false;
13
+ }
14
+ return arr.slice(0, start.length).every((item, i) => item === start[i]);
15
+ };
16
+ const arrayEquals = (arr1, arr2) => {
17
+ if (arr2.length !== arr1.length) {
18
+ return false;
19
+ }
20
+ return arr1.every((item, i) => item === arr2[i]);
21
+ };
22
+ const generateCommandRecordFromCommandArray = (commands) => {
23
+ const record = {};
24
+ for (const command of commands) {
25
+ record[command.name] = command;
26
+ }
27
+ return record;
28
+ };
10
29
 
30
+ exports.arrayEquals = arrayEquals;
31
+ exports.arrayStartsWith = arrayStartsWith;
11
32
  exports.camelCase = camelCase;
33
+ exports.generateCommandRecordFromCommandArray = generateCommandRecordFromCommandArray;
12
34
  exports.gracefulFlagName = gracefulFlagName;
13
35
  exports.gracefulVersion = gracefulVersion;
14
36
  exports.kebabCase = kebabCase;
package/dist/index.d.ts CHANGED
@@ -1,26 +1,32 @@
1
+ import * as clerc from 'clerc';
2
+ import { Command, CommandRecord } from 'clerc';
3
+
1
4
  /**
2
5
  * Copied from type-fest
3
6
  */
4
- declare type Primitive = null | undefined | string | number | boolean | symbol | bigint;
7
+ type Primitive = null | undefined | string | number | boolean | symbol | bigint;
5
8
  /**
6
9
  * Copied from type-fest
7
10
  */
8
- declare type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>);
9
- declare type Dict<T> = Record<string, T>;
10
- declare type MustArray<T> = T extends any[] ? T : [T];
11
- declare type MaybeArray<T> = T | T[];
12
- declare type GetLength<T extends any[]> = T extends {
11
+ type LiteralUnion<LiteralType, BaseType extends Primitive> = LiteralType | (BaseType & Record<never, never>);
12
+ type Dict<T> = Record<string, T>;
13
+ type MustArray<T> = T extends any[] ? T : [T];
14
+ type MaybeArray<T> = T | T[];
15
+ type GetLength<T extends any[]> = T extends {
13
16
  length: infer L extends number;
14
17
  } ? L : never;
15
- declare type GetTail<T extends any[]> = T extends [infer _Head, ...infer Tail] ? Tail : never;
16
- declare type EnhanceSingle<T, E extends Dict<any>> = T & E;
17
- declare type Enhance<T, E extends Dict<any> | Dict<any>[]> = GetLength<MustArray<E>> extends 0 ? T : Enhance<EnhanceSingle<T, MustArray<E>[0]>, GetTail<MustArray<E>>>;
18
+ type GetTail<T extends any[]> = T extends [infer _Head, ...infer Tail] ? Tail : never;
19
+ type EnhanceSingle<T, E extends Dict<any>> = T & E;
20
+ type Enhance<T, E extends Dict<any> | Dict<any>[]> = GetLength<MustArray<E>> extends 0 ? T : Enhance<EnhanceSingle<T, MustArray<E>[0]>, GetTail<MustArray<E>>>;
18
21
  declare const mustArray: <T>(a: MaybeArray<T>) => T[];
19
- declare type CamelCase<T extends string> = T extends `${infer A}-${infer B}${infer C}` ? `${A}${Capitalize<B>}${CamelCase<C>}` : T;
22
+ type CamelCase<T extends string> = T extends `${infer A}-${infer B}${infer C}` ? `${A}${Capitalize<B>}${CamelCase<C>}` : T;
20
23
  declare const camelCase: <T extends string>(s: T) => CamelCase<T>;
21
- declare type KebabCase<T extends string, A extends string = ""> = T extends `${infer F}${infer R}` ? KebabCase<R, `${A}${F extends Lowercase<F> ? "" : "-"}${Lowercase<F>}`> : A;
24
+ type KebabCase<T extends string, A extends string = ""> = T extends `${infer F}${infer R}` ? KebabCase<R, `${A}${F extends Lowercase<F> ? "" : "-"}${Lowercase<F>}`> : A;
22
25
  declare const kebabCase: <T extends string>(s: T) => KebabCase<T, "">;
23
26
  declare const gracefulFlagName: (n: string) => string;
24
- declare const gracefulVersion: (v: string) => string;
27
+ declare const gracefulVersion: (v: string) => string;
28
+ declare const arrayStartsWith: <T>(arr: T[], start: T[]) => boolean;
29
+ declare const arrayEquals: <T>(arr1: T[], arr2: T[]) => boolean;
30
+ declare const generateCommandRecordFromCommandArray: <C extends Command<string, string, clerc.CommandOptions<MaybeArray<string>, Dict<clerc.FlagOptions>>>[]>(commands: C) => CommandRecord;
25
31
 
26
- export { CamelCase, Dict, Enhance, GetLength, GetTail, KebabCase, LiteralUnion, MaybeArray, MustArray, Primitive, camelCase, gracefulFlagName, gracefulVersion, kebabCase, mustArray };
32
+ export { CamelCase, Dict, Enhance, GetLength, GetTail, KebabCase, LiteralUnion, MaybeArray, MustArray, Primitive, arrayEquals, arrayStartsWith, camelCase, generateCommandRecordFromCommandArray, gracefulFlagName, gracefulVersion, kebabCase, mustArray };
package/dist/index.mjs CHANGED
@@ -2,6 +2,25 @@ const mustArray = (a) => Array.isArray(a) ? a : [a];
2
2
  const camelCase = (s) => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
3
3
  const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
4
4
  const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
5
- const gracefulVersion = (v) => v.length === 0 ? v.startsWith("v") ? v : `v${v}` : "";
5
+ const gracefulVersion = (v) => v.length === 0 ? "" : v.startsWith("v") ? v : `v${v}`;
6
+ const arrayStartsWith = (arr, start) => {
7
+ if (start.length > arr.length) {
8
+ return false;
9
+ }
10
+ return arr.slice(0, start.length).every((item, i) => item === start[i]);
11
+ };
12
+ const arrayEquals = (arr1, arr2) => {
13
+ if (arr2.length !== arr1.length) {
14
+ return false;
15
+ }
16
+ return arr1.every((item, i) => item === arr2[i]);
17
+ };
18
+ const generateCommandRecordFromCommandArray = (commands) => {
19
+ const record = {};
20
+ for (const command of commands) {
21
+ record[command.name] = command;
22
+ }
23
+ return record;
24
+ };
6
25
 
7
- export { camelCase, gracefulFlagName, gracefulVersion, kebabCase, mustArray };
26
+ export { arrayEquals, arrayStartsWith, camelCase, generateCommandRecordFromCommandArray, gracefulFlagName, gracefulVersion, kebabCase, mustArray };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clerc/utils",
3
- "version": "0.3.4",
3
+ "version": "0.5.0",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc utils",
6
6
  "keywords": [