diff options
Diffstat (limited to 'node_modules/type-fest/ts41')
-rw-r--r-- | node_modules/type-fest/ts41/camel-case.d.ts | 64 | ||||
-rw-r--r-- | node_modules/type-fest/ts41/delimiter-case.d.ts | 85 | ||||
-rw-r--r-- | node_modules/type-fest/ts41/get.d.ts | 131 | ||||
-rw-r--r-- | node_modules/type-fest/ts41/index.d.ts | 10 | ||||
-rw-r--r-- | node_modules/type-fest/ts41/kebab-case.d.ts | 36 | ||||
-rw-r--r-- | node_modules/type-fest/ts41/pascal-case.d.ts | 36 | ||||
-rw-r--r-- | node_modules/type-fest/ts41/snake-case.d.ts | 35 | ||||
-rw-r--r-- | node_modules/type-fest/ts41/utilities.d.ts | 8 |
8 files changed, 405 insertions, 0 deletions
diff --git a/node_modules/type-fest/ts41/camel-case.d.ts b/node_modules/type-fest/ts41/camel-case.d.ts new file mode 100644 index 0000000..4f9a67b --- /dev/null +++ b/node_modules/type-fest/ts41/camel-case.d.ts @@ -0,0 +1,64 @@ +import {WordSeparators} from '../source/utilities'; +import {Split} from './utilities'; + +/** +Step by step takes the first item in an array literal, formats it and adds it to a string literal, and then recursively appends the remainder. + +Only to be used by `CamelCaseStringArray<>`. + +@see CamelCaseStringArray +*/ +type InnerCamelCaseStringArray<Parts extends any[], PreviousPart> = + Parts extends [`${infer FirstPart}`, ...infer RemainingParts] + ? FirstPart extends undefined + ? '' + : FirstPart extends '' + ? InnerCamelCaseStringArray<RemainingParts, PreviousPart> + : `${PreviousPart extends '' ? FirstPart : Capitalize<FirstPart>}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}` + : ''; + +/** +Starts fusing the output of `Split<>`, an array literal of strings, into a camel-cased string literal. + +It's separate from `InnerCamelCaseStringArray<>` to keep a clean API outwards to the rest of the code. + +@see Split +*/ +type CamelCaseStringArray<Parts extends string[]> = + Parts extends [`${infer FirstPart}`, ...infer RemainingParts] + ? Uncapitalize<`${FirstPart}${InnerCamelCaseStringArray<RemainingParts, FirstPart>}`> + : never; + +/** +Convert a string literal to camel-case. + +This can be useful when, for example, converting some kebab-cased command-line flags or a snake-cased database result. + +@example +``` +import {CamelCase} from 'type-fest'; + +// Simple + +const someVariable: CamelCase<'foo-bar'> = 'fooBar'; + +// Advanced + +type CamelCasedProps<T> = { + [K in keyof T as CamelCase<K>]: T[K] +}; + +interface RawOptions { + 'dry-run': boolean; + 'full_family_name': string; + foo: number; +} + +const dbResult: CamelCasedProps<ModelProps> = { + dryRun: true, + fullFamilyName: 'bar.js', + foo: 123 +}; +``` +*/ +export type CamelCase<K> = K extends string ? CamelCaseStringArray<Split<K, WordSeparators>> : K; diff --git a/node_modules/type-fest/ts41/delimiter-case.d.ts b/node_modules/type-fest/ts41/delimiter-case.d.ts new file mode 100644 index 0000000..52f4eb9 --- /dev/null +++ b/node_modules/type-fest/ts41/delimiter-case.d.ts @@ -0,0 +1,85 @@ +import {UpperCaseCharacters, WordSeparators} from '../source/utilities'; + +/** +Unlike a simpler split, this one includes the delimiter splitted on in the resulting array literal. This is to enable splitting on, for example, upper-case characters. +*/ +export type SplitIncludingDelimiters<Source extends string, Delimiter extends string> = + Source extends '' ? [] : + Source extends `${infer FirstPart}${Delimiter}${infer SecondPart}` ? + ( + Source extends `${FirstPart}${infer UsedDelimiter}${SecondPart}` + ? UsedDelimiter extends Delimiter + ? Source extends `${infer FirstPart}${UsedDelimiter}${infer SecondPart}` + ? [...SplitIncludingDelimiters<FirstPart, Delimiter>, UsedDelimiter, ...SplitIncludingDelimiters<SecondPart, Delimiter>] + : never + : never + : never + ) : + [Source]; + +/** +Format a specific part of the splitted string literal that `StringArrayToDelimiterCase<>` fuses together, ensuring desired casing. + +@see StringArrayToDelimiterCase +*/ +type StringPartToDelimiterCase<StringPart extends string, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> = + StringPart extends UsedWordSeparators ? Delimiter : + StringPart extends UsedUpperCaseCharacters ? `${Delimiter}${Lowercase<StringPart>}` : + StringPart; + +/** +Takes the result of a splitted string literal and recursively concatenates it together into the desired casing. + +It receives `UsedWordSeparators` and `UsedUpperCaseCharacters` as input to ensure it's fully encapsulated. + +@see SplitIncludingDelimiters +*/ +type StringArrayToDelimiterCase<Parts extends any[], UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> = + Parts extends [`${infer FirstPart}`, ...infer RemainingParts] + ? `${StringPartToDelimiterCase<FirstPart, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}` + : ''; + +/** +Convert a string literal to a custom string delimiter casing. + +This can be useful when, for example, converting a camel-cased object property to an oddly cased one. + +@see KebabCase +@see SnakeCase + +@example +``` +import {DelimiterCase} from 'type-fest'; + +// Simple + +const someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar'; + +// Advanced + +type OddlyCasedProps<T> = { + [K in keyof T as DelimiterCase<K, '#'>]: T[K] +}; + +interface SomeOptions { + dryRun: boolean; + includeFile: string; + foo: number; +} + +const rawCliOptions: OddlyCasedProps<SomeOptions> = { + 'dry#run': true, + 'include#file': 'bar.js', + foo: 123 +}; +``` +*/ + +export type DelimiterCase<Value, Delimiter extends string> = Value extends string + ? StringArrayToDelimiterCase< + SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>, + WordSeparators, + UpperCaseCharacters, + Delimiter + > + : Value; diff --git a/node_modules/type-fest/ts41/get.d.ts b/node_modules/type-fest/ts41/get.d.ts new file mode 100644 index 0000000..9103e9a --- /dev/null +++ b/node_modules/type-fest/ts41/get.d.ts @@ -0,0 +1,131 @@ +import {Split} from './utilities'; +import {StringDigit} from '../source/utilities'; + +/** +Like the `Get` type but receives an array of strings as a path parameter. +*/ +type GetWithPath<BaseType, Keys extends readonly string[]> = + Keys extends [] + ? BaseType + : Keys extends [infer Head, ...infer Tail] + ? GetWithPath<PropertyOf<BaseType, Extract<Head, string>>, Extract<Tail, string[]>> + : never; + +/** +Splits a dot-prop style path into a tuple comprised of the properties in the path. Handles square-bracket notation. + +@example +``` +ToPath<'foo.bar.baz'> +//=> ['foo', 'bar', 'baz'] + +ToPath<'foo[0].bar.baz'> +//=> ['foo', '0', 'bar', 'baz'] +``` +*/ +type ToPath<S extends string> = Split<FixPathSquareBrackets<S>, '.'>; + +/** +Replaces square-bracketed dot notation with dots, for example, `foo[0].bar` -> `foo.0.bar`. +*/ +type FixPathSquareBrackets<Path extends string> = + Path extends `${infer Head}[${infer Middle}]${infer Tail}` + ? `${Head}.${Middle}${FixPathSquareBrackets<Tail>}` + : Path; + +/** +Returns true if `LongString` is made up out of `Substring` repeated 0 or more times. + +@example +``` +ConsistsOnlyOf<'aaa', 'a'> //=> true +ConsistsOnlyOf<'ababab', 'ab'> //=> true +ConsistsOnlyOf<'aBa', 'a'> //=> false +ConsistsOnlyOf<'', 'a'> //=> true +``` +*/ +type ConsistsOnlyOf<LongString extends string, Substring extends string> = + LongString extends '' + ? true + : LongString extends `${Substring}${infer Tail}` + ? ConsistsOnlyOf<Tail, Substring> + : false; + +/** +Convert a type which may have number keys to one with string keys, making it possible to index using strings retrieved from template types. + +@example +``` +type WithNumbers = {foo: string; 0: boolean}; +type WithStrings = WithStringKeys<WithNumbers>; + +type WithNumbersKeys = keyof WithNumbers; +//=> 'foo' | 0 +type WithStringsKeys = keyof WithStrings; +//=> 'foo' | '0' +``` +*/ +type WithStringKeys<BaseType extends Record<string | number, any>> = { + [Key in `${Extract<keyof BaseType, string | number>}`]: BaseType[Key] +}; + +/** +Get a property of an object or array. Works when indexing arrays using number-literal-strings, for example, `PropertyOf<number[], '0'> = number`, and when indexing objects with number keys. + +Note: +- Returns `unknown` if `Key` is not a property of `BaseType`, since TypeScript uses structural typing, and it cannot be guaranteed that extra properties unknown to the type system will exist at runtime. +- Returns `undefined` from nullish values, to match the behaviour of most deep-key libraries like `lodash`, `dot-prop`, etc. +*/ +type PropertyOf<BaseType, Key extends string> = + BaseType extends null | undefined + ? undefined + : Key extends keyof BaseType + ? BaseType[Key] + : BaseType extends { + [n: number]: infer Item; + length: number; // Note: This is needed to avoid being too lax with records types using number keys like `{0: string; 1: boolean}`. + } + ? ( + ConsistsOnlyOf<Key, StringDigit> extends true + ? Item + : unknown + ) + : Key extends keyof WithStringKeys<BaseType> + ? WithStringKeys<BaseType>[Key] + : unknown; + +// This works by first splitting the path based on `.` and `[...]` characters into a tuple of string keys. Then it recursively uses the head key to get the next property of the current object, until there are no keys left. Number keys extract the item type from arrays, or are converted to strings to extract types from tuples and dictionaries with number keys. +/** +Get a deeply-nested property from an object using a key path, like Lodash's `.get()` function. + +Use-case: Retrieve a property from deep inside an API response or some other complex object. + +@example +``` +import {Get} from 'type-fest'; +import * as lodash from 'lodash'; + +const get = <BaseType, Path extends string>(object: BaseType, path: Path): Get<BaseType, Path> => + lodash.get(object, path); + +interface ApiResponse { + hits: { + hits: Array<{ + _id: string + _source: { + name: Array<{ + given: string[] + family: string + }> + birthDate: string + } + }> + } +} + +const getName = (apiResponse: ApiResponse) => + get(apiResponse, 'hits.hits[0]._source.name'); + //=> Array<{given: string[]; family: string}> +``` +*/ +export type Get<BaseType, Path extends string> = GetWithPath<BaseType, ToPath<Path>>; diff --git a/node_modules/type-fest/ts41/index.d.ts b/node_modules/type-fest/ts41/index.d.ts new file mode 100644 index 0000000..e49504c --- /dev/null +++ b/node_modules/type-fest/ts41/index.d.ts @@ -0,0 +1,10 @@ +// These are all the basic types that's compatible with all supported TypeScript versions. +export * from '../base'; + +// These are special types that require at least TypeScript 4.1. +export {CamelCase} from './camel-case'; +export {KebabCase} from './kebab-case'; +export {PascalCase} from './pascal-case'; +export {SnakeCase} from './snake-case'; +export {DelimiterCase} from './delimiter-case'; +export {Get} from './get'; diff --git a/node_modules/type-fest/ts41/kebab-case.d.ts b/node_modules/type-fest/ts41/kebab-case.d.ts new file mode 100644 index 0000000..ba6a99d --- /dev/null +++ b/node_modules/type-fest/ts41/kebab-case.d.ts @@ -0,0 +1,36 @@ +import {DelimiterCase} from './delimiter-case'; + +/** +Convert a string literal to kebab-case. + +This can be useful when, for example, converting a camel-cased object property to a kebab-cased CSS class name or a command-line flag. + +@example +``` +import {KebabCase} from 'type-fest'; + +// Simple + +const someVariable: KebabCase<'fooBar'> = 'foo-bar'; + +// Advanced + +type KebabCasedProps<T> = { + [K in keyof T as KebabCase<K>]: T[K] +}; + +interface CliOptions { + dryRun: boolean; + includeFile: string; + foo: number; +} + +const rawCliOptions: KebabCasedProps<CliOptions> = { + 'dry-run': true, + 'include-file': 'bar.js', + foo: 123 +}; +``` +*/ + +export type KebabCase<Value> = DelimiterCase<Value, '-'>; diff --git a/node_modules/type-fest/ts41/pascal-case.d.ts b/node_modules/type-fest/ts41/pascal-case.d.ts new file mode 100644 index 0000000..bfb2a36 --- /dev/null +++ b/node_modules/type-fest/ts41/pascal-case.d.ts @@ -0,0 +1,36 @@ +import {CamelCase} from './camel-case'; + +/** +Converts a string literal to pascal-case. + +@example +``` +import {PascalCase} from 'type-fest'; + +// Simple + +const someVariable: PascalCase<'foo-bar'> = 'FooBar'; + +// Advanced + +type PascalCaseProps<T> = { + [K in keyof T as PascalCase<K>]: T[K] +}; + +interface RawOptions { + 'dry-run': boolean; + 'full_family_name': string; + foo: number; +} + +const dbResult: CamelCasedProps<ModelProps> = { + DryRun: true, + FullFamilyName: 'bar.js', + Foo: 123 +}; +``` +*/ + +export type PascalCase<Value> = CamelCase<Value> extends string + ? Capitalize<CamelCase<Value>> + : CamelCase<Value>; diff --git a/node_modules/type-fest/ts41/snake-case.d.ts b/node_modules/type-fest/ts41/snake-case.d.ts new file mode 100644 index 0000000..272b3d3 --- /dev/null +++ b/node_modules/type-fest/ts41/snake-case.d.ts @@ -0,0 +1,35 @@ +import {DelimiterCase} from './delimiter-case'; + +/** +Convert a string literal to snake-case. + +This can be useful when, for example, converting a camel-cased object property to a snake-cased SQL column name. + +@example +``` +import {SnakeCase} from 'type-fest'; + +// Simple + +const someVariable: SnakeCase<'fooBar'> = 'foo_bar'; + +// Advanced + +type SnakeCasedProps<T> = { + [K in keyof T as SnakeCase<K>]: T[K] +}; + +interface ModelProps { + isHappy: boolean; + fullFamilyName: string; + foo: number; +} + +const dbResult: SnakeCasedProps<ModelProps> = { + 'is_happy': true, + 'full_family_name': 'Carla Smith', + foo: 123 +}; +``` +*/ +export type SnakeCase<Value> = DelimiterCase<Value, '_'>; diff --git a/node_modules/type-fest/ts41/utilities.d.ts b/node_modules/type-fest/ts41/utilities.d.ts new file mode 100644 index 0000000..f82e362 --- /dev/null +++ b/node_modules/type-fest/ts41/utilities.d.ts @@ -0,0 +1,8 @@ +/** +Recursively split a string literal into two parts on the first occurence of the given string, returning an array literal of all the separate parts. +*/ +export type Split<S extends string, D extends string> = + string extends S ? string[] : + S extends '' ? [] : + S extends `${infer T}${D}${infer U}` ? [T, ...Split<U, D>] : + [S]; |