@molt/types
Advanced tools
Comparing version 0.0.1 to 0.1.0
{ | ||
"name": "@molt/types", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Advanced Types for parsing CLI flags and more.", | ||
@@ -24,7 +24,8 @@ "files": [ | ||
"devDependencies": { | ||
"conditional-type-checks": "^1.0.6" | ||
"conditional-type-checks": "1.0.6", | ||
"tsd": "0.23.0" | ||
}, | ||
"scripts": { | ||
"test": "vitest", | ||
"clean": "tsc --build --clean", | ||
"types:check": "tsc --noEmit", | ||
"build": "pnpm clean && pnpm build:esm", | ||
@@ -31,0 +32,0 @@ "build:esm": "pnpm tsc --project tsconfig.esm.json" |
@@ -15,4 +15,17 @@ # @molt/types | ||
The `FlagName` namespace provides a `Parse` type and some other utility types. `Parse` turns an expression of CLI flags into structured object type data _at the type level_. It also ships with a rich set of human-friendly error messages when the input is invalid. | ||
The `FlagName` namespace provides a `Parse` type and some other utility types. `Parse` turns an expression of CLI flags into structured object type data _at the type level_. | ||
#### Features | ||
- Capture long flag | ||
- Capture short flag | ||
- Capture alias short flags | ||
- Capture alias long flags | ||
- Flexible syntax | ||
- Optional leading dashes `--`/`-` | ||
- Clear human-friendly error messages when parsing fails. | ||
- Catch name duplicate | ||
- Enforce reserved names | ||
- Statically Normalize kebob case to camel case | ||
#### Example | ||
@@ -19,0 +32,0 @@ |
import { Str } from '../prelude' | ||
import { FlagNames, FlagNamesEmpty } from './data' | ||
import { Any } from 'ts-toolbelt' | ||
import { String } from 'ts-toolbelt' | ||
// prettier-ignore | ||
export namespace Checks { | ||
export type LongFlagTooShort<Name extends string> = Str.Length<Name> extends 1 ? true : false | ||
export type ShortFlagTooLong<Name extends string> = Str.Length<Name> extends 1 ? false : true | ||
export type LongFlagTooShort<Name extends string> = String.Length<Name> extends 1 ? true : false | ||
export type ShortFlagTooLong<Name extends string> = String.Length<Name> extends 1 ? false : true | ||
export type AliasDuplicate<Names extends FlagNames, Name extends string> = Name extends Names['long'] | Names['short'] ? true : false | ||
@@ -28,7 +28,7 @@ export type NameAlreadyTaken<Limits extends SomeLimits, Name extends string> = Name extends Limits['usedNames'] ? true : false | ||
// prettier-ignore | ||
type AddAliasLong<Names extends FlagNames, Name extends string> = Omit<Names, 'aliases'> & { aliases: { long: [...Names['aliases']['long'], Name], short: Names['aliases']['short'] }} | ||
type AddAliasLong<Names extends FlagNames, Name extends string> = Omit<Names, 'aliases'> & { aliases: { long: [...Names['aliases']['long'], Str.KebabToCamelCase<Name>], short: Names['aliases']['short'] }} | ||
// prettier-ignore | ||
type AddAliasShort<Names extends FlagNames, Name extends string> = Omit<Names, 'aliases'> & { aliases: { long: Names['aliases']['long'], short: [...Names['aliases']['short'], Name] }} | ||
// prettier-ignore | ||
type AddLong<Names extends FlagNames, Name extends string> = Omit<Names,'long'> & { long: Name } | ||
type AddLong<Names extends FlagNames, Name extends string> = Omit<Names,'long'> & { long: Str.KebabToCamelCase<Name> } | ||
// prettier-ignore | ||
@@ -51,3 +51,3 @@ type AddShort<Names extends FlagNames, Name extends string> = Omit<Names,'short'> & { short: Name } | ||
names extends FlagNames = FlagNamesEmpty | ||
> = Any.Compute<ParseFlagNameDo<E, limits, names>> | ||
> = ParseFlagNameDo<E, limits, names> | ||
@@ -101,3 +101,3 @@ //prettier-ignore | ||
Checks.NameReserved<limits, name> extends true ? Errors.NameReserved<name> : | ||
Str.Length<name> extends 1 ? | ||
String.Length<name> extends 1 ? | ||
names['short'] extends undefined ? | ||
@@ -113,3 +113,3 @@ ParseFlagNameDo<tail, limits, AddShort<names, name>> : | ||
Checks.NameReserved<limits, name> extends true ? Errors.NameReserved<name> : | ||
Str.Length<name> extends 1 ? | ||
String.Length<name> extends 1 ? | ||
names['short'] extends undefined ? | ||
@@ -116,0 +116,0 @@ AddShort<names, name> : |
@@ -39,6 +39,10 @@ export namespace Num { | ||
export type Length<S extends string > = Length_<0, S> | ||
export type KebabToCamelCase<S extends string> = S extends `${infer P1}-${infer P2}${infer P3}` | ||
? `${Lowercase<P1>}${Uppercase<P2>}${KebabToCamelCase<P3>}` | ||
: Lowercase<S> | ||
type Length_<Count extends number, S extends string > = | ||
S extends `${infer _}${infer tail}` ? Length_<Num.Increment<Count>, tail> : Count | ||
export type SnakeToCamelCase<S extends string> = S extends `${infer P1}_${infer P2}${infer P3}` | ||
? `${Lowercase<P1>}${Uppercase<P2>}${SnakeToCamelCase<P3>}` | ||
: Lowercase<S> | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
162325
326
53
2