type-fest 4.36.0 → 4.37.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.36.0",
3
+ "version": "4.37.0",
4
4
  "description": "A collection of essential TypeScript types",
5
5
  "license": "(MIT OR CC0-1.0)",
6
6
  "repository": "sindresorhus/type-fest",
@@ -91,7 +91,7 @@ type ArraySliceHelper<
91
91
  ? Sum<ArrayLength, Start> extends infer AddResult extends number
92
92
  ? number extends AddResult // (ArrayLength + Start) < 0
93
93
  ? 0
94
- : AddResult
94
+ : GreaterThan<AddResult, 0> extends true ? AddResult : 0
95
95
  : never
96
96
  : Start,
97
97
  PositiveE extends number = IsNegative<End> extends true ? Sum<ArrayLength, End> : End,
@@ -21,7 +21,9 @@ The implementation of `SplitArrayByIndex` for variable length arrays.
21
21
  type SplitVariableArrayByIndex<T extends UnknownArray,
22
22
  SplitIndex extends number,
23
23
  T1 = Subtract<SplitIndex, StaticPartOfArray<T>['length']>,
24
- T2 = T1 extends number ? BuildTuple<T1, VariablePartOfArray<T>[number]> : [],
24
+ T2 = T1 extends number
25
+ ? BuildTuple<GreaterThanOrEqual<T1, 0> extends true ? T1 : number, VariablePartOfArray<T>[number]>
26
+ : [],
25
27
  > =
26
28
  SplitIndex extends 0
27
29
  ? [[], T]
@@ -75,14 +75,9 @@ type CamelCasedPropertiesArrayDeep<Value extends UnknownArray> =
75
75
  : // Leading spread array
76
76
  Value extends readonly [...infer U, infer V]
77
77
  ? [...CamelCasedPropertiesDeep<U>, CamelCasedPropertiesDeep<V>]
78
- : Value extends readonly [...infer U, infer V]
79
- ? readonly [
80
- ...CamelCasedPropertiesDeep<U>,
81
- CamelCasedPropertiesDeep<V>,
82
- ]
83
- : // Array
84
- Value extends Array<infer U>
85
- ? Array<CamelCasedPropertiesDeep<U>>
86
- : Value extends ReadonlyArray<infer U>
87
- ? ReadonlyArray<CamelCasedPropertiesDeep<U>>
88
- : never;
78
+ : // Array
79
+ Value extends Array<infer U>
80
+ ? Array<CamelCasedPropertiesDeep<U>>
81
+ : Value extends ReadonlyArray<infer U>
82
+ ? ReadonlyArray<CamelCasedPropertiesDeep<U>>
83
+ : never;
@@ -61,7 +61,7 @@ const rawCliOptions: OddlyCasedProperties<SomeOptions> = {
61
61
  export type DelimiterCase<
62
62
  Value,
63
63
  Delimiter extends string,
64
- Options extends WordsOptions = {},
64
+ Options extends WordsOptions = {splitOnNumbers: false},
65
65
  > = Value extends string
66
66
  ? IsStringLiteral<Value> extends false
67
67
  ? Value
@@ -1,4 +1,5 @@
1
1
  import type {IsNever} from '../is-never';
2
+ import type {NegativeInfinity, PositiveInfinity} from '../numeric';
2
3
  import type {UnknownArray} from '../unknown-array';
3
4
  import type {StringToNumber} from './string';
4
5
 
@@ -89,3 +90,29 @@ type InternalUnionMax<N extends number, T extends UnknownArray = []> =
89
90
  : T['length'] extends N
90
91
  ? InternalUnionMax<Exclude<N, T['length']>, T>
91
92
  : InternalUnionMax<N, [...T, unknown]>;
93
+
94
+ /**
95
+ Returns the number with reversed sign.
96
+
97
+ @example
98
+ ```
99
+ ReverseSign<-1>;
100
+ //=> 1
101
+
102
+ ReverseSign<1>;
103
+ //=> -1
104
+
105
+ ReverseSign<NegativeInfinity>
106
+ //=> PositiveInfinity
107
+
108
+ ReverseSign<PositiveInfinity>
109
+ //=> NegativeInfinity
110
+ ```
111
+ */
112
+ export type ReverseSign<N extends number> =
113
+ // Handle edge cases
114
+ N extends 0 ? 0 : N extends PositiveInfinity ? NegativeInfinity : N extends NegativeInfinity ? PositiveInfinity :
115
+ // Handle negative numbers
116
+ `${N}` extends `-${infer P extends number}` ? P
117
+ // Handle positive numbers
118
+ : `-${N}` extends `${infer R extends number}` ? R : never;
@@ -39,5 +39,5 @@ const rawCliOptions: KebabCasedProperties<CliOptions> = {
39
39
  */
40
40
  export type KebabCase<
41
41
  Value,
42
- Options extends WordsOptions = {},
42
+ Options extends WordsOptions = {splitOnNumbers: false},
43
43
  > = DelimiterCase<Value, '-', Options>;
package/source/paths.d.ts CHANGED
@@ -246,13 +246,7 @@ type InternalPaths<T, Options extends Required<PathsOptions>> =
246
246
  bracketNotation: Options['bracketNotation'];
247
247
  maxRecursionDepth: Subtract<MaxDepth, 1>;
248
248
  leavesOnly: Options['leavesOnly'];
249
- depth: Options['depth'] extends infer Depth extends number // For distributing `Options['depth']`
250
- ? Depth extends 0 // Don't subtract further if `Depth` has reached `0`
251
- ? never
252
- : ToString<Depth> extends `-${number}` // Don't subtract if `Depth` is -ve
253
- ? never
254
- : Subtract<Options['depth'], 1> // If `Subtract` supported -ve numbers, then `depth` could have simply been `Subtract<Options['depth'], 1>`
255
- : never; // Should never happen
249
+ depth: Subtract<Options['depth'], 1>;
256
250
  }> extends infer SubPath
257
251
  ? SubPath extends string | number
258
252
  ? (
@@ -68,8 +68,12 @@ type _Replace<
68
68
  Replacement extends string,
69
69
  Options extends ReplaceOptions,
70
70
  Accumulator extends string = '',
71
- > = Input extends `${infer Head}${Search}${infer Tail}`
72
- ? Options['all'] extends true
73
- ? _Replace<Tail, Search, Replacement, Options, `${Accumulator}${Head}${Replacement}`>
74
- : `${Head}${Replacement}${Tail}`
75
- : `${Accumulator}${Input}`;
71
+ > = Search extends string // For distributing `Search`
72
+ ? Replacement extends string // For distributing `Replacement`
73
+ ? Input extends `${infer Head}${Search}${infer Tail}`
74
+ ? Options['all'] extends true
75
+ ? _Replace<Tail, Search, Replacement, Options, `${Accumulator}${Head}${Replacement}`>
76
+ : `${Head}${Replacement}${Tail}`
77
+ : `${Accumulator}${Input}`
78
+ : never
79
+ : never;
@@ -20,5 +20,5 @@ const someVariableNoSplitOnNumbers: ScreamingSnakeCase<'p2pNetwork', {splitOnNum
20
20
  */
21
21
  export type ScreamingSnakeCase<
22
22
  Value,
23
- Options extends WordsOptions = {},
23
+ Options extends WordsOptions = {splitOnNumbers: false},
24
24
  > = Value extends string ? Uppercase<SnakeCase<Value, Options>> : Value;
@@ -39,5 +39,5 @@ const dbResult: SnakeCasedProperties<ModelProps> = {
39
39
  */
40
40
  export type SnakeCase<
41
41
  Value,
42
- Options extends WordsOptions = {},
42
+ Options extends WordsOptions = {splitOnNumbers: false},
43
43
  > = DelimiterCase<Value, '_', Options>;
package/source/split.d.ts CHANGED
@@ -1,3 +1,43 @@
1
+ import type {And} from './and';
2
+ import type {Not} from './internal';
3
+ import type {IsStringLiteral} from './is-literal';
4
+ import type {Or} from './or';
5
+
6
+ /**
7
+ Split options.
8
+
9
+ @see {@link Split}
10
+ */
11
+ type SplitOptions = {
12
+ /**
13
+ When enabled, instantiations with non-literal string types (e.g., `string`, `Uppercase<string>`, `on${string}`) simply return back `string[]` without performing any splitting, as the exact structure cannot be statically determined.
14
+
15
+ Note: In the future, this option might be enabled by default, so if you currently rely on this being disabled, you should consider explicitly enabling it.
16
+
17
+ @default false
18
+
19
+ @example
20
+ ```ts
21
+ type Example1 = Split<`foo.${string}.bar`, '.', {strictLiteralChecks: false}>;
22
+ //=> ['foo', string, 'bar']
23
+
24
+ type Example2 = Split<`foo.${string}`, '.', {strictLiteralChecks: true}>;
25
+ //=> string[]
26
+
27
+ type Example3 = Split<'foobarbaz', `b${string}`, {strictLiteralChecks: false}>;
28
+ //=> ['foo', 'r', 'z']
29
+
30
+ type Example4 = Split<'foobarbaz', `b${string}`, {strictLiteralChecks: true}>;
31
+ //=> string[]
32
+ ```
33
+ */
34
+ strictLiteralChecks?: boolean;
35
+ };
36
+
37
+ type DefaultSplitOptions = {
38
+ strictLiteralChecks: false;
39
+ };
40
+
1
41
  /**
2
42
  Represents an array of strings split using a given character or character set.
3
43
 
@@ -16,20 +56,34 @@ let array: Item[];
16
56
  array = split(items, ',');
17
57
  ```
18
58
 
59
+ @see {@link SplitOptions}
60
+
19
61
  @category String
20
62
  @category Template literal
21
63
  */
22
64
  export type Split<
23
65
  S extends string,
24
66
  Delimiter extends string,
25
- > = SplitHelper<S, Delimiter>;
67
+ Options extends SplitOptions = {},
68
+ > = SplitHelper<S, Delimiter, {
69
+ strictLiteralChecks: Options['strictLiteralChecks'] extends boolean ? Options['strictLiteralChecks'] : DefaultSplitOptions['strictLiteralChecks'];
70
+ }>;
26
71
 
27
72
  type SplitHelper<
28
73
  S extends string,
29
74
  Delimiter extends string,
75
+ Options extends Required<SplitOptions>,
30
76
  Accumulator extends string[] = [],
31
- > = S extends `${infer Head}${Delimiter}${infer Tail}`
32
- ? SplitHelper<Tail, Delimiter, [...Accumulator, Head]>
33
- : Delimiter extends ''
34
- ? Accumulator
35
- : [...Accumulator, S];
77
+ > = S extends string // For distributing `S`
78
+ ? Delimiter extends string // For distributing `Delimeter`
79
+ // If `strictLiteralChecks` is `false` OR `S` and `Delimiter` both are string literals, then perform the split
80
+ ? Or<Not<Options['strictLiteralChecks']>, And<IsStringLiteral<S>, IsStringLiteral<Delimiter>>> extends true
81
+ ? S extends `${infer Head}${Delimiter}${infer Tail}`
82
+ ? SplitHelper<Tail, Delimiter, Options, [...Accumulator, Head]>
83
+ : Delimiter extends ''
84
+ ? Accumulator
85
+ : [...Accumulator, S]
86
+ // Otherwise, return `string[]`
87
+ : string[]
88
+ : never // Should never happen
89
+ : never; // Should never happen
@@ -1,17 +1,12 @@
1
- import type {NumberAbsolute, BuildTuple} from './internal';
2
- import type {IsEqual} from './is-equal';
1
+ import type {NumberAbsolute, BuildTuple, ReverseSign} from './internal';
3
2
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
4
3
  import type {LessThan} from './less-than';
5
- import type {Sum} from './sum';
6
- import type {And} from './and';
7
- import type {Or} from './or';
8
4
 
9
5
  /**
10
6
  Returns the difference between two numbers.
11
7
 
12
8
  Note:
13
9
  - A or B can only support `-999` ~ `999`.
14
- - If the result is negative, you can only get `number`.
15
10
 
16
11
  @example
17
12
  ```
@@ -24,7 +19,10 @@ Subtract<111, -222>;
24
19
  //=> 333
25
20
 
26
21
  Subtract<-111, 222>;
27
- //=> number
22
+ //=> -333
23
+
24
+ Subtract<18, 96>;
25
+ //=> -78
28
26
 
29
27
  Subtract<PositiveInfinity, 9999>;
30
28
  //=> PositiveInfinity
@@ -35,36 +33,51 @@ Subtract<PositiveInfinity, PositiveInfinity>;
35
33
 
36
34
  @category Numeric
37
35
  */
38
- // TODO: Support big integer and negative number.
39
- export type Subtract<A extends number, B extends number> = number extends A | B
40
- ? number
41
- : [
42
- IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
43
- IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
44
- ] extends infer R extends [boolean, boolean, boolean, boolean]
45
- ? Or<
46
- And<IsEqual<R[0], true>, IsEqual<R[2], false>>,
47
- And<IsEqual<R[3], true>, IsEqual<R[1], false>>
48
- > extends true
49
- ? PositiveInfinity
50
- : Or<
51
- And<IsEqual<R[1], true>, IsEqual<R[3], false>>,
52
- And<IsEqual<R[2], true>, IsEqual<R[0], false>>
53
- > extends true
54
- ? NegativeInfinity
55
- : true extends R[number]
56
- ? number
57
- : [IsNegative<A>, IsNegative<B>] extends infer R
58
- ? [false, false] extends R
59
- ? BuildTuple<A> extends infer R
60
- ? R extends [...BuildTuple<B>, ...infer R]
61
- ? R['length']
62
- : number
63
- : never
64
- : LessThan<A, B> extends true
65
- ? number
66
- : [false, true] extends R
67
- ? Sum<A, NumberAbsolute<B>>
68
- : Subtract<NumberAbsolute<B>, NumberAbsolute<A>>
69
- : never
36
+ // TODO: Support big integer.
37
+ export type Subtract<A extends number, B extends number> =
38
+ // Handle cases when A or B is the actual "number" type
39
+ number extends A | B ? number
40
+ // Handle cases when A and B are both +/- infinity
41
+ : A extends B & (PositiveInfinity | NegativeInfinity) ? number
42
+ // Handle cases when A is - infinity or B is + infinity
43
+ : A extends NegativeInfinity ? NegativeInfinity : B extends PositiveInfinity ? NegativeInfinity
44
+ // Handle cases when A is + infinity or B is - infinity
45
+ : A extends PositiveInfinity ? PositiveInfinity : B extends NegativeInfinity ? PositiveInfinity
46
+ // Handle case when numbers are equal to each other
47
+ : A extends B ? 0
48
+ // Handle cases when A or B is 0
49
+ : A extends 0 ? ReverseSign<B> : B extends 0 ? A
50
+ // Handle remaining regular cases
51
+ : SubtractPostChecks<A, B>;
52
+
53
+ /**
54
+ Subtracts two numbers A and B, such that they are not equal and neither of them are 0, +/- infinity or the `number` type
55
+ */
56
+ type SubtractPostChecks<A extends number, B extends number, AreNegative = [IsNegative<A>, IsNegative<B>]> =
57
+ AreNegative extends [false, false]
58
+ ? SubtractPositives<A, B>
59
+ : AreNegative extends [true, true]
60
+ // When both numbers are negative we subtract the absolute values and then reverse the sign
61
+ ? ReverseSign<SubtractPositives<NumberAbsolute<A>, NumberAbsolute<B>>>
62
+ // When the signs are different we can add the absolute values and then reverse the sign if A < B
63
+ : [...BuildTuple<NumberAbsolute<A>>, ...BuildTuple<NumberAbsolute<B>>] extends infer R extends unknown[]
64
+ ? LessThan<A, B> extends true ? ReverseSign<R['length']> : R['length']
65
+ : never;
66
+
67
+ /**
68
+ Subtracts two positive numbers.
69
+ */
70
+ type SubtractPositives<A extends number, B extends number> =
71
+ LessThan<A, B> extends true
72
+ // When A < B we can reverse the result of B - A
73
+ ? ReverseSign<SubtractIfAGreaterThanB<B, A>>
74
+ : SubtractIfAGreaterThanB<A, B>;
75
+
76
+ /**
77
+ Subtracts two positive numbers A and B such that A > B.
78
+ */
79
+ type SubtractIfAGreaterThanB<A extends number, B extends number> =
80
+ // This is where we always want to end up and do the actual subtraction
81
+ BuildTuple<A> extends [...BuildTuple<B>, ...infer R]
82
+ ? R['length']
70
83
  : never;
package/source/sum.d.ts CHANGED
@@ -1,17 +1,12 @@
1
- import type {NumberAbsolute, BuildTuple, TupleMax, TupleMin} from './internal';
2
- import type {IsEqual} from './is-equal';
1
+ import type {NumberAbsolute, BuildTuple, TupleMax, ReverseSign} from './internal';
3
2
  import type {PositiveInfinity, NegativeInfinity, IsNegative} from './numeric';
4
3
  import type {Subtract} from './subtract';
5
- import type {And} from './and';
6
- import type {Or} from './or';
7
4
 
8
5
  /**
9
6
  Returns the sum of two numbers.
10
7
 
11
8
  Note:
12
9
  - A or B can only support `-999` ~ `999`.
13
- - A and B can only be small integers, less than 1000.
14
- - If the result is negative, you can only get `number`.
15
10
 
16
11
  @example
17
12
  ```
@@ -24,7 +19,7 @@ Sum<-111, 222>;
24
19
  //=> 111
25
20
 
26
21
  Sum<111, -222>;
27
- //=> number
22
+ //=> -111
28
23
 
29
24
  Sum<PositiveInfinity, -9999>;
30
25
  //=> PositiveInfinity
@@ -35,36 +30,49 @@ Sum<PositiveInfinity, NegativeInfinity>;
35
30
 
36
31
  @category Numeric
37
32
  */
38
- // TODO: Support big integer and negative number.
39
- export type Sum<A extends number, B extends number> = number extends A | B
40
- ? number
41
- : [
42
- IsEqual<A, PositiveInfinity>, IsEqual<A, NegativeInfinity>,
43
- IsEqual<B, PositiveInfinity>, IsEqual<B, NegativeInfinity>,
44
- ] extends infer R extends [boolean, boolean, boolean, boolean]
45
- ? Or<
46
- And<IsEqual<R[0], true>, IsEqual<R[3], false>>,
47
- And<IsEqual<R[2], true>, IsEqual<R[1], false>>
48
- > extends true
49
- ? PositiveInfinity
50
- : Or<
51
- And<IsEqual<R[1], true>, IsEqual<R[2], false>>,
52
- And<IsEqual<R[3], true>, IsEqual<R[0], false>>
53
- > extends true
54
- ? NegativeInfinity
55
- : true extends R[number]
56
- ? number
57
- : ([IsNegative<A>, IsNegative<B>] extends infer R
58
- ? [false, false] extends R
59
- ? [...BuildTuple<A>, ...BuildTuple<B>]['length']
60
- : [true, true] extends R
61
- ? number
62
- : TupleMax<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Max_
63
- ? TupleMin<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Min_ extends number
64
- ? Max_ extends A | B
65
- ? Subtract<Max_, Min_>
66
- : number
67
- : never
68
- : never
69
- : never) & number
33
+ // TODO: Support big integer.
34
+ export type Sum<A extends number, B extends number> =
35
+ // Handle cases when A or B is the actual "number" type
36
+ number extends A | B ? number
37
+ // Handle cases when A and B are both +/- infinity
38
+ : A extends B & (PositiveInfinity | NegativeInfinity) ? A // A or B could be used here as they are equal
39
+ // Handle cases when A and B are opposite infinities
40
+ : A | B extends PositiveInfinity | NegativeInfinity ? number
41
+ // Handle cases when A is +/- infinity
42
+ : A extends PositiveInfinity | NegativeInfinity ? A
43
+ // Handle cases when B is +/- infinity
44
+ : B extends PositiveInfinity | NegativeInfinity ? B
45
+ // Handle cases when A or B is 0 or it's the same number with different signs
46
+ : A extends 0 ? B : B extends 0 ? A : A extends ReverseSign<B> ? 0
47
+ // Handle remaining regular cases
48
+ : SumPostChecks<A, B>;
49
+
50
+ /**
51
+ Adds two numbers A and B, such that they are not equal with different signs and neither of them are 0, +/- infinity or the `number` type
52
+ */
53
+ type SumPostChecks<A extends number, B extends number, AreNegative = [IsNegative<A>, IsNegative<B>]> =
54
+ AreNegative extends [false, false]
55
+ // When both numbers are positive we can add them together
56
+ ? SumPositives<A, B>
57
+ : AreNegative extends [true, true]
58
+ // When both numbers are negative we add the absolute values and then reverse the sign
59
+ ? ReverseSign<SumPositives<NumberAbsolute<A>, NumberAbsolute<B>>>
60
+ // When the signs are different we can subtract the absolute values, remove the sign
61
+ // and then reverse the sign if the larger absolute value is negative
62
+ : NumberAbsolute<Subtract<NumberAbsolute<A>, NumberAbsolute<B>>> extends infer Result extends number
63
+ ? TupleMax<[NumberAbsolute<A>, NumberAbsolute<B>]> extends infer Max_ extends number
64
+ ? Max_ extends A | B
65
+ // The larger absolute value is positive, so the result is positive
66
+ ? Result
67
+ // The larger absolute value is negative, so the result is negative
68
+ : ReverseSign<Result>
69
+ : never
70
+ : never;
71
+
72
+ /**
73
+ Adds two positive numbers.
74
+ */
75
+ type SumPositives<A extends number, B extends number> =
76
+ [...BuildTuple<A>, ...BuildTuple<B>]['length'] extends infer Result extends number
77
+ ? Result
70
78
  : never;