@clerc/utils 0.4.0 → 0.6.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 ADDED
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ const mustArray = (a) => Array.isArray(a) ? a : [a];
6
+ const camelCase = (s) => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
7
+ const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
8
+ const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
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
+ };
29
+
30
+ exports.arrayEquals = arrayEquals;
31
+ exports.arrayStartsWith = arrayStartsWith;
32
+ exports.camelCase = camelCase;
33
+ exports.generateCommandRecordFromCommandArray = generateCommandRecordFromCommandArray;
34
+ exports.gracefulFlagName = gracefulFlagName;
35
+ exports.gracefulVersion = gracefulVersion;
36
+ exports.kebabCase = kebabCase;
37
+ exports.mustArray = mustArray;
@@ -0,0 +1,32 @@
1
+ import * as clerc from 'clerc';
2
+ import { Command, CommandRecord } from 'clerc';
3
+
4
+ /**
5
+ * Copied from type-fest
6
+ */
7
+ type Primitive = null | undefined | string | number | boolean | symbol | bigint;
8
+ /**
9
+ * Copied from type-fest
10
+ */
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 {
16
+ length: infer L extends number;
17
+ } ? L : never;
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>>>;
21
+ declare const mustArray: <T>(a: MaybeArray<T>) => T[];
22
+ type CamelCase<T extends string> = T extends `${infer A}-${infer B}${infer C}` ? `${A}${Capitalize<B>}${CamelCase<C>}` : T;
23
+ declare const camelCase: <T extends string>(s: T) => CamelCase<T>;
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;
25
+ declare const kebabCase: <T extends string>(s: T) => KebabCase<T, "">;
26
+ declare const gracefulFlagName: (n: 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;
31
+
32
+ export { CamelCase, Dict, Enhance, GetLength, GetTail, KebabCase, LiteralUnion, MaybeArray, MustArray, Primitive, arrayEquals, arrayStartsWith, camelCase, generateCommandRecordFromCommandArray, gracefulFlagName, gracefulVersion, kebabCase, mustArray };
package/dist/index.mjs ADDED
@@ -0,0 +1,26 @@
1
+ const mustArray = (a) => Array.isArray(a) ? a : [a];
2
+ const camelCase = (s) => s.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
3
+ const kebabCase = (s) => s.replace(/([A-Z])/g, (_, c) => `-${c.toLowerCase()}`);
4
+ const gracefulFlagName = (n) => n.length <= 1 ? `-${n}` : `--${n}`;
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
+ };
25
+
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.4.0",
3
+ "version": "0.6.0",
4
4
  "author": "Ray <nn_201312@163.com> (https://github.com/so1ve)",
5
5
  "description": "Clerc utils",
6
6
  "keywords": [