type-fest 4.35.0 → 4.36.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "type-fest",
3
- "version": "4.35.0",
3
+ "version": "4.36.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -1,54 +1,23 @@
1
- import type {UpperCaseCharacters, WordSeparators} from './internal';
2
-
3
- // Transforms a string that is fully uppercase into a fully lowercase version. Needed to add support for SCREAMING_SNAKE_CASE, see https://github.com/sindresorhus/type-fest/issues/385
4
- type UpperCaseToLowerCase<T extends string> = T extends Uppercase<T> ? Lowercase<T> : T;
5
-
6
- // This implementation does not support SCREAMING_SNAKE_CASE, it is used internally by `SplitIncludingDelimiters`.
7
- type SplitIncludingDelimiters_<Source extends string, Delimiter extends string> =
8
- Source extends '' ? [] :
9
- Source extends `${infer FirstPart}${Delimiter}${infer SecondPart}` ?
10
- (
11
- Source extends `${FirstPart}${infer UsedDelimiter}${SecondPart}`
12
- ? UsedDelimiter extends Delimiter
13
- ? Source extends `${infer FirstPart}${UsedDelimiter}${infer SecondPart}`
14
- ? [...SplitIncludingDelimiters<FirstPart, Delimiter>, UsedDelimiter, ...SplitIncludingDelimiters<SecondPart, Delimiter>]
15
- : never
16
- : never
17
- : never
18
- ) :
19
- [Source];
20
-
21
- /**
22
- 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.
23
-
24
- @category Template literal
25
- */
26
- export type SplitIncludingDelimiters<Source extends string, Delimiter extends string> = SplitIncludingDelimiters_<UpperCaseToLowerCase<Source>, Delimiter>;
1
+ import type {IsStringLiteral} from './is-literal';
2
+ import type {Words, WordsOptions} from './words';
27
3
 
28
4
  /**
29
- Format a specific part of the splitted string literal that `StringArrayToDelimiterCase<>` fuses together, ensuring desired casing.
30
-
31
- @see StringArrayToDelimiterCase
5
+ Convert an array of words to delimiter case starting with a delimiter with input capitalization.
32
6
  */
33
- type StringPartToDelimiterCase<StringPart extends string, Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
34
- StringPart extends UsedWordSeparators ? Delimiter :
35
- Start extends true ? Lowercase<StringPart> :
36
- StringPart extends UsedUpperCaseCharacters ? `${Delimiter}${Lowercase<StringPart>}` :
37
- StringPart;
38
-
39
- /**
40
- Takes the result of a splitted string literal and recursively concatenates it together into the desired casing.
41
-
42
- It receives `UsedWordSeparators` and `UsedUpperCaseCharacters` as input to ensure it's fully encapsulated.
43
-
44
- @see SplitIncludingDelimiters
45
- */
46
- type StringArrayToDelimiterCase<Parts extends readonly any[], Start extends boolean, UsedWordSeparators extends string, UsedUpperCaseCharacters extends string, Delimiter extends string> =
47
- Parts extends [`${infer FirstPart}`, ...infer RemainingParts]
48
- ? `${StringPartToDelimiterCase<FirstPart, Start, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}${StringArrayToDelimiterCase<RemainingParts, false, UsedWordSeparators, UsedUpperCaseCharacters, Delimiter>}`
49
- : Parts extends [string]
50
- ? string
51
- : '';
7
+ type DelimiterCaseFromArray<
8
+ Words extends string[],
9
+ Delimiter extends string,
10
+ OutputString extends string = '',
11
+ > = Words extends [
12
+ infer FirstWord extends string,
13
+ ...infer RemainingWords extends string[],
14
+ ]
15
+ ? DelimiterCaseFromArray<RemainingWords, Delimiter, `${OutputString}${Delimiter}${FirstWord}`>
16
+ : OutputString;
17
+
18
+ type RemoveFirstLetter<S extends string> = S extends `${infer _}${infer Rest}`
19
+ ? Rest
20
+ : '';
52
21
 
53
22
  /**
54
23
  Convert a string literal to a custom string delimiter casing.
@@ -65,6 +34,7 @@ import type {DelimiterCase} from 'type-fest';
65
34
  // Simple
66
35
 
67
36
  const someVariable: DelimiterCase<'fooBar', '#'> = 'foo#bar';
37
+ const someVariableNoSplitOnNumbers: DelimiterCase<'p2pNetwork', '#', {splitOnNumbers: false}> = 'p2p#network';
68
38
 
69
39
  // Advanced
70
40
 
@@ -87,13 +57,17 @@ const rawCliOptions: OddlyCasedProperties<SomeOptions> = {
87
57
 
88
58
  @category Change case
89
59
  @category Template literal
90
- */
91
- export type DelimiterCase<Value, Delimiter extends string> = string extends Value ? Value : Value extends string
92
- ? StringArrayToDelimiterCase<
93
- SplitIncludingDelimiters<Value, WordSeparators | UpperCaseCharacters>,
94
- true,
95
- WordSeparators,
96
- UpperCaseCharacters,
97
- Delimiter
98
- >
60
+ */
61
+ export type DelimiterCase<
62
+ Value,
63
+ Delimiter extends string,
64
+ Options extends WordsOptions = {},
65
+ > = Value extends string
66
+ ? IsStringLiteral<Value> extends false
67
+ ? Value
68
+ : Lowercase<
69
+ RemoveFirstLetter<
70
+ DelimiterCaseFromArray<Words<Value, Options>, Delimiter>
71
+ >
72
+ >
99
73
  : Value;
@@ -1,4 +1,5 @@
1
1
  import type {DelimiterCase} from './delimiter-case';
2
+ import type {WordsOptions} from './words';
2
3
 
3
4
  /**
4
5
  Convert a string literal to kebab-case.
@@ -12,6 +13,7 @@ import type {KebabCase} from 'type-fest';
12
13
  // Simple
13
14
 
14
15
  const someVariable: KebabCase<'fooBar'> = 'foo-bar';
16
+ const someVariableNoSplitOnNumbers: KebabCase<'p2pNetwork', {splitOnNumbers: false}> = 'p2p-network';
15
17
 
16
18
  // Advanced
17
19
 
@@ -35,4 +37,7 @@ const rawCliOptions: KebabCasedProperties<CliOptions> = {
35
37
  @category Change case
36
38
  @category Template literal
37
39
  */
38
- export type KebabCase<Value> = DelimiterCase<Value, '-'>;
40
+ export type KebabCase<
41
+ Value,
42
+ Options extends WordsOptions = {},
43
+ > = DelimiterCase<Value, '-', Options>;
@@ -60,8 +60,16 @@ export type Replace<
60
60
  Search extends string,
61
61
  Replacement extends string,
62
62
  Options extends ReplaceOptions = {},
63
+ > = _Replace<Input, Search, Replacement, Options>;
64
+
65
+ type _Replace<
66
+ Input extends string,
67
+ Search extends string,
68
+ Replacement extends string,
69
+ Options extends ReplaceOptions,
70
+ Accumulator extends string = '',
63
71
  > = Input extends `${infer Head}${Search}${infer Tail}`
64
72
  ? Options['all'] extends true
65
- ? `${Head}${Replacement}${Replace<Tail, Search, Replacement, Options>}`
73
+ ? _Replace<Tail, Search, Replacement, Options, `${Accumulator}${Head}${Replacement}`>
66
74
  : `${Head}${Replacement}${Tail}`
67
- : Input;
75
+ : `${Accumulator}${Input}`;
@@ -1,15 +1,5 @@
1
- import type {SplitIncludingDelimiters} from './delimiter-case';
2
1
  import type {SnakeCase} from './snake-case';
3
- import type {Includes} from './includes';
4
-
5
- /**
6
- Returns a boolean for whether the string is screaming snake case.
7
- */
8
- type IsScreamingSnakeCase<Value extends string> = Value extends Uppercase<Value>
9
- ? Includes<SplitIncludingDelimiters<Lowercase<Value>, '_'>, '_'> extends true
10
- ? true
11
- : false
12
- : false;
2
+ import type {WordsOptions} from './words';
13
3
 
14
4
  /**
15
5
  Convert a string literal to screaming-snake-case.
@@ -21,13 +11,14 @@ This can be useful when, for example, converting a camel-cased object property t
21
11
  import type {ScreamingSnakeCase} from 'type-fest';
22
12
 
23
13
  const someVariable: ScreamingSnakeCase<'fooBar'> = 'FOO_BAR';
14
+ const someVariableNoSplitOnNumbers: ScreamingSnakeCase<'p2pNetwork', {splitOnNumbers: false}> = 'P2P_NETWORK';
15
+
24
16
  ```
25
17
 
26
18
  @category Change case
27
19
  @category Template literal
28
- */
29
- export type ScreamingSnakeCase<Value> = Value extends string
30
- ? IsScreamingSnakeCase<Value> extends true
31
- ? Value
32
- : Uppercase<SnakeCase<Value>>
33
- : Value;
20
+ */
21
+ export type ScreamingSnakeCase<
22
+ Value,
23
+ Options extends WordsOptions = {},
24
+ > = Value extends string ? Uppercase<SnakeCase<Value, Options>> : Value;
@@ -1,4 +1,5 @@
1
1
  import type {DelimiterCase} from './delimiter-case';
2
+ import type {WordsOptions} from './words';
2
3
 
3
4
  /**
4
5
  Convert a string literal to snake-case.
@@ -12,6 +13,7 @@ import type {SnakeCase} from 'type-fest';
12
13
  // Simple
13
14
 
14
15
  const someVariable: SnakeCase<'fooBar'> = 'foo_bar';
16
+ const someVariableNoSplitOnNumbers: SnakeCase<'p2pNetwork', {splitOnNumbers: false}> = 'p2p_network';
15
17
 
16
18
  // Advanced
17
19
 
@@ -35,4 +37,7 @@ const dbResult: SnakeCasedProperties<ModelProps> = {
35
37
  @category Change case
36
38
  @category Template literal
37
39
  */
38
- export type SnakeCase<Value> = DelimiterCase<Value, '_'>;
40
+ export type SnakeCase<
41
+ Value,
42
+ Options extends WordsOptions = {},
43
+ > = DelimiterCase<Value, '_', Options>;
@@ -18,6 +18,7 @@ declare namespace TsConfigJson {
18
18
  | 'ES2022'
19
19
  | 'ESNext'
20
20
  | 'Node16'
21
+ | 'Node18'
21
22
  | 'NodeNext'
22
23
  | 'Preserve'
23
24
  | 'None'
@@ -32,6 +33,7 @@ declare namespace TsConfigJson {
32
33
  | 'es2022'
33
34
  | 'esnext'
34
35
  | 'node16'
36
+ | 'node18'
35
37
  | 'nodenext'
36
38
  | 'preserve'
37
39
  | 'none';
@@ -1109,6 +1111,20 @@ declare namespace TsConfigJson {
1109
1111
  Suppress deprecation warnings
1110
1112
  */
1111
1113
  ignoreDeprecations?: CompilerOptions.IgnoreDeprecations;
1114
+
1115
+ /**
1116
+ Do not allow runtime constructs that are not part of ECMAScript.
1117
+
1118
+ @default false
1119
+ */
1120
+ erasableSyntaxOnly?: boolean;
1121
+
1122
+ /**
1123
+ Enable lib replacement.
1124
+
1125
+ @default true
1126
+ */
1127
+ libReplacement?: boolean;
1112
1128
  };
1113
1129
 
1114
1130
  namespace WatchOptions {
@@ -1160,12 +1176,12 @@ declare namespace TsConfigJson {
1160
1176
  synchronousWatchDirectory?: boolean;
1161
1177
 
1162
1178
  /**
1163
- Specifies a list of directories to exclude from watch
1179
+ Specifies a list of directories to exclude from watch.
1164
1180
  */
1165
1181
  excludeDirectories?: string[];
1166
1182
 
1167
1183
  /**
1168
- Specifies a list of files to exclude from watch
1184
+ Specifies a list of files to exclude from watch.
1169
1185
  */
1170
1186
  excludeFiles?: string[];
1171
1187
  };
package/source/words.d.ts CHANGED
@@ -1,11 +1,46 @@
1
- import type {IsLowerCase, IsNumeric, IsUpperCase, WordSeparators} from './internal';
1
+ import type {
2
+ IsLowerCase,
3
+ IsNumeric,
4
+ IsUpperCase,
5
+ WordSeparators,
6
+ } from './internal';
2
7
 
3
8
  type SkipEmptyWord<Word extends string> = Word extends '' ? [] : [Word];
4
9
 
5
- type RemoveLastCharacter<Sentence extends string, Character extends string> = Sentence extends `${infer LeftSide}${Character}`
10
+ type RemoveLastCharacter<
11
+ Sentence extends string,
12
+ Character extends string,
13
+ > = Sentence extends `${infer LeftSide}${Character}`
6
14
  ? SkipEmptyWord<LeftSide>
7
15
  : never;
8
16
 
17
+ /**
18
+ Words options.
19
+
20
+ @see {@link Words}
21
+ */
22
+ export type WordsOptions = {
23
+ /**
24
+ Split on numeric sequence.
25
+
26
+ @default true
27
+
28
+ @example
29
+ ```
30
+ type Example1 = Words<'p2pNetwork', {splitOnNumbers: true}>;
31
+ //=> ["p", "2", "p", "Network"]
32
+
33
+ type Example2 = Words<'p2pNetwork', {splitOnNumbers: false}>;
34
+ //=> ["p2p", "Network"]
35
+ ```
36
+ */
37
+ splitOnNumbers?: boolean;
38
+ };
39
+
40
+ type DefaultOptions = {
41
+ splitOnNumbers: true;
42
+ };
43
+
9
44
  /**
10
45
  Split a string (almost) like Lodash's `_.words()` function.
11
46
 
@@ -31,37 +66,53 @@ type Words3 = Words<'--hello the_world'>;
31
66
 
32
67
  type Words4 = Words<'lifeIs42'>;
33
68
  //=> ['life', 'Is', '42']
69
+
70
+ type Words5 = Words<'p2pNetwork', {splitOnNumbers: false}>;
71
+ //=> ['p2p', 'Network']
34
72
  ```
35
73
 
36
74
  @category Change case
37
75
  @category Template literal
38
76
  */
39
- export type Words<
77
+ export type Words<Sentence extends string, Options extends WordsOptions = {}> = WordsImplementation<Sentence, {
78
+ splitOnNumbers: Options['splitOnNumbers'] extends boolean ? Options['splitOnNumbers'] : DefaultOptions['splitOnNumbers'];
79
+ }>;
80
+
81
+ type WordsImplementation<
40
82
  Sentence extends string,
83
+ Options extends Required<WordsOptions>,
41
84
  LastCharacter extends string = '',
42
85
  CurrentWord extends string = '',
43
86
  > = Sentence extends `${infer FirstCharacter}${infer RemainingCharacters}`
44
87
  ? FirstCharacter extends WordSeparators
45
88
  // Skip word separator
46
- ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters>]
89
+ ? [...SkipEmptyWord<CurrentWord>, ...WordsImplementation<RemainingCharacters, Options>]
47
90
  : LastCharacter extends ''
48
91
  // Fist char of word
49
- ? Words<RemainingCharacters, FirstCharacter, FirstCharacter>
50
- // Case change: non-numeric to numeric, push word
92
+ ? WordsImplementation<RemainingCharacters, Options, FirstCharacter, FirstCharacter>
93
+ // Case change: non-numeric to numeric
51
94
  : [false, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
52
- ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
53
- // Case change: numeric to non-numeric, push word
95
+ ? Options['splitOnNumbers'] extends true
96
+ // Split on number: push word
97
+ ? [...SkipEmptyWord<CurrentWord>, ...WordsImplementation<RemainingCharacters, Options, FirstCharacter, FirstCharacter>]
98
+ // No split on number: concat word
99
+ : WordsImplementation<RemainingCharacters, Options, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
100
+ // Case change: numeric to non-numeric
54
101
  : [true, false] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
55
- ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
102
+ ? Options['splitOnNumbers'] extends true
103
+ // Split on number: push word
104
+ ? [...SkipEmptyWord<CurrentWord>, ...WordsImplementation<RemainingCharacters, Options, FirstCharacter, FirstCharacter>]
105
+ // No split on number: concat word
106
+ : WordsImplementation<RemainingCharacters, Options, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
56
107
  // No case change: concat word
57
108
  : [true, true] extends [IsNumeric<LastCharacter>, IsNumeric<FirstCharacter>]
58
- ? Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
109
+ ? WordsImplementation<RemainingCharacters, Options, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
59
110
  // Case change: lower to upper, push word
60
111
  : [true, true] extends [IsLowerCase<LastCharacter>, IsUpperCase<FirstCharacter>]
61
- ? [...SkipEmptyWord<CurrentWord>, ...Words<RemainingCharacters, FirstCharacter, FirstCharacter>]
112
+ ? [...SkipEmptyWord<CurrentWord>, ...WordsImplementation<RemainingCharacters, Options, FirstCharacter, FirstCharacter>]
62
113
  // Case change: upper to lower, brings back the last character, push word
63
114
  : [true, true] extends [IsUpperCase<LastCharacter>, IsLowerCase<FirstCharacter>]
64
- ? [...RemoveLastCharacter<CurrentWord, LastCharacter>, ...Words<RemainingCharacters, FirstCharacter, `${LastCharacter}${FirstCharacter}`>]
115
+ ? [...RemoveLastCharacter<CurrentWord, LastCharacter>, ...WordsImplementation<RemainingCharacters, Options, FirstCharacter, `${LastCharacter}${FirstCharacter}`>]
65
116
  // No case change: concat word
66
- : Words<RemainingCharacters, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
117
+ : WordsImplementation<RemainingCharacters, Options, FirstCharacter, `${CurrentWord}${FirstCharacter}`>
67
118
  : [...SkipEmptyWord<CurrentWord>];