@tempots/std 0.9.6 → 0.9.7
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/arrays.d.ts +49 -0
- package/arrays.js +249 -0
- package/async-result.d.ts +37 -0
- package/async-result.js +75 -0
- package/bigint.d.ts +18 -0
- package/bigint.js +110 -0
- package/booleans.d.ts +23 -0
- package/{src/booleans.ts → booleans.js} +33 -38
- package/colors/cmyk.d.ts +21 -0
- package/colors/cmyk.js +54 -0
- package/colors/convert.d.ts +283 -0
- package/colors/convert.js +742 -0
- package/colors/hsl.d.ts +24 -0
- package/colors/hsl.js +56 -0
- package/colors/hsla.d.ts +18 -0
- package/colors/hsla.js +35 -0
- package/colors/hsluv.d.ts +24 -0
- package/colors/hsluv.js +56 -0
- package/colors/hsv.d.ts +24 -0
- package/colors/hsv.js +54 -0
- package/colors/lab.d.ts +24 -0
- package/colors/lab.js +54 -0
- package/colors/lch.d.ts +19 -0
- package/colors/lch.js +44 -0
- package/colors/luv.d.ts +19 -0
- package/colors/luv.js +45 -0
- package/colors/rgb.d.ts +13 -0
- package/colors/rgb.js +47 -0
- package/colors/rgba.d.ts +12 -0
- package/colors/rgba.js +44 -0
- package/colors/srgb.d.ts +24 -0
- package/colors/srgb.js +51 -0
- package/colors/xyz.d.ts +19 -0
- package/colors/xyz.js +41 -0
- package/edit.d.ts +20 -0
- package/edit.js +29 -0
- package/equals.d.ts +3 -0
- package/equals.js +122 -0
- package/functions.d.ts +20 -0
- package/functions.js +38 -0
- package/json.d.ts +14 -0
- package/json.js +33 -0
- package/match.d.ts +16 -0
- package/match.js +45 -0
- package/maybe.d.ts +9 -0
- package/{src/maybe.ts → maybe.js} +11 -18
- package/memoize.d.ts +1 -0
- package/memoize.js +9 -0
- package/newtype.d.ts +28 -0
- package/newtype.js +29 -0
- package/numbers.d.ts +104 -0
- package/numbers.js +183 -0
- package/objects.d.ts +9 -0
- package/objects.js +33 -0
- package/ord.d.ts +19 -0
- package/ord.js +73 -0
- package/package.json +2 -2
- package/reg-exps.d.ts +10 -0
- package/{src/reg-exps.ts → reg-exps.js} +19 -24
- package/result.d.ts +31 -0
- package/result.js +95 -0
- package/strings.d.ts +314 -0
- package/strings.js +685 -0
- package/types/assert.d.ts +12 -0
- package/types/assert.js +13 -0
- package/types/differentiate.d.ts +13 -0
- package/types/differentiate.js +14 -0
- package/types/functions.d.ts +22 -0
- package/types/functions.js +13 -0
- package/types/generic.d.ts +9 -0
- package/types/generic.js +13 -0
- package/types/objects.d.ts +50 -0
- package/types/objects.js +13 -0
- package/types/tuples.d.ts +44 -0
- package/types/tuples.js +24 -0
- package/{src/types/utility.ts → types/utility.d.ts} +2 -3
- package/types/utility.js +1 -0
- package/uuid.d.ts +13 -0
- package/uuid.js +56 -0
- package/validation.d.ts +23 -0
- package/validation.js +44 -0
- package/src/arrays.ts +0 -296
- package/src/async-result.ts +0 -103
- package/src/bigint.ts +0 -111
- package/src/colors/cmyk.ts +0 -84
- package/src/colors/convert.ts +0 -1093
- package/src/colors/hsl.ts +0 -73
- package/src/colors/hsla.ts +0 -45
- package/src/colors/hsluv.ts +0 -73
- package/src/colors/hsv.ts +0 -75
- package/src/colors/lab.ts +0 -69
- package/src/colors/lch.ts +0 -53
- package/src/colors/luv.ts +0 -56
- package/src/colors/rgb.ts +0 -55
- package/src/colors/rgba.ts +0 -53
- package/src/colors/srgb.ts +0 -72
- package/src/colors/xyz.ts +0 -52
- package/src/edit.ts +0 -29
- package/src/equals.ts +0 -116
- package/src/functions.ts +0 -108
- package/src/json.ts +0 -52
- package/src/match.ts +0 -88
- package/src/memoize.ts +0 -9
- package/src/newtype.ts +0 -59
- package/src/numbers.ts +0 -222
- package/src/objects.ts +0 -47
- package/src/ord.ts +0 -79
- package/src/result.ts +0 -140
- package/src/strings.ts +0 -768
- package/src/types/assert.ts +0 -96
- package/src/types/differentiate.ts +0 -89
- package/src/types/functions.ts +0 -114
- package/src/types/generic.ts +0 -42
- package/src/types/objects.ts +0 -212
- package/src/types/tuples.ts +0 -244
- package/src/uuid.ts +0 -61
- package/src/validation.ts +0 -69
- package/test/arrays.spec.ts +0 -410
- package/test/colors.spec.ts +0 -406
- package/test/commmon.ts +0 -9
- package/test/equals.spec.ts +0 -165
- package/test/functions.spec.ts +0 -9
- package/test/index.d.ts +0 -20
- package/test/objects.spec.ts +0 -22
- package/test/reg-exps.spec.ts +0 -33
- package/test/strings.spec.ts +0 -333
- package/test/uuid.spec.ts +0 -35
- package/tsconfig.json +0 -19
package/src/types/assert.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
https://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software
|
|
8
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
-
See the License for the specific language governing permissions and
|
|
11
|
-
limitations under the License.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { type Pointer } from './generic'
|
|
19
|
-
|
|
20
|
-
export type Assert<A extends true> = A extends never
|
|
21
|
-
? 'FAIL'
|
|
22
|
-
: A extends true
|
|
23
|
-
? 'PASS'
|
|
24
|
-
: 'FAIL'
|
|
25
|
-
export type AssertNot<A extends false> = A extends never
|
|
26
|
-
? 'FAIL'
|
|
27
|
-
: A extends false
|
|
28
|
-
? 'PASS'
|
|
29
|
-
: 'FAIL'
|
|
30
|
-
|
|
31
|
-
export type Extends<A, B> = A extends B ? true : false
|
|
32
|
-
|
|
33
|
-
// do not use with `any`
|
|
34
|
-
export type Same<A, B> = Pointer<A> extends Pointer<B>
|
|
35
|
-
? Pointer<B> extends Pointer<A>
|
|
36
|
-
? true
|
|
37
|
-
: false
|
|
38
|
-
: false
|
|
39
|
-
|
|
40
|
-
export type NotSame<A, B> = Pointer<A> extends Pointer<B>
|
|
41
|
-
? Pointer<B> extends Pointer<A>
|
|
42
|
-
? false
|
|
43
|
-
: true
|
|
44
|
-
: true
|
|
45
|
-
|
|
46
|
-
// does not work comparing intersection types with literals. Use `Same` for that.
|
|
47
|
-
export type Equals<A, B> = (<T>() => T extends A ? 1 : 2) extends <
|
|
48
|
-
T
|
|
49
|
-
>() => T extends B ? 1 : 2
|
|
50
|
-
? true
|
|
51
|
-
: false
|
|
52
|
-
|
|
53
|
-
export type NotEquals<A, B> = Equals<A, B> extends true ? false : true
|
|
54
|
-
|
|
55
|
-
export type IsNever<T> = Pointer<T> extends Pointer<never> ? true : false
|
|
56
|
-
|
|
57
|
-
// TYPE TESTS
|
|
58
|
-
type _primitives_are_not_equals = AssertNot<Equals<number, string>>
|
|
59
|
-
type _primitives_are_not_same = AssertNot<Same<number, string>>
|
|
60
|
-
type _literals_are_equals = Assert<Equals<1, 1>>
|
|
61
|
-
type _literals_are_same = Assert<Same<1, 1>>
|
|
62
|
-
type _literals_are_notsame = Assert<NotSame<1, 2>>
|
|
63
|
-
type _any_is_not_equal_to_literal = Assert<NotEquals<any, 1>>
|
|
64
|
-
type _any_is_equal_to_any = Assert<Equals<any, any>>
|
|
65
|
-
type _union_is_not_equal_to_member = Assert<NotEquals<1 | 2, 1>>
|
|
66
|
-
type _any_is_not_equal_to_never = Assert<NotEquals<any, never>>
|
|
67
|
-
type _any_is_not_same_as_never = Assert<NotEquals<any, never>>
|
|
68
|
-
type _same_tuple_elements_match = Assert<Equals<[number], [number]>>
|
|
69
|
-
type _different_tuple_elements_do_not_match = Assert<NotEquals<[any], [string]>>
|
|
70
|
-
type _intersection_objects_are_same_as_literal = Assert<
|
|
71
|
-
Same<{ x: 1 } & { y: 2 }, { x: 1, y: 2 }>
|
|
72
|
-
>
|
|
73
|
-
type _intersection_objects_not_equal_to_literal = Assert<
|
|
74
|
-
NotEquals<{ x: 1 } & { y: 2 }, { x: 1, y: 2 }>
|
|
75
|
-
>
|
|
76
|
-
type _string_is_not_never = AssertNot<IsNever<string>>
|
|
77
|
-
type _never_is_never = Assert<IsNever<never>>
|
|
78
|
-
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
80
|
-
type _TESTS_ =
|
|
81
|
-
| _primitives_are_not_equals
|
|
82
|
-
| _primitives_are_not_same
|
|
83
|
-
| _literals_are_equals
|
|
84
|
-
| _literals_are_same
|
|
85
|
-
| _literals_are_notsame
|
|
86
|
-
| _any_is_not_equal_to_literal
|
|
87
|
-
| _any_is_equal_to_any
|
|
88
|
-
| _union_is_not_equal_to_member
|
|
89
|
-
| _any_is_not_equal_to_never
|
|
90
|
-
| _any_is_not_same_as_never
|
|
91
|
-
| _same_tuple_elements_match
|
|
92
|
-
| _different_tuple_elements_do_not_match
|
|
93
|
-
| _intersection_objects_are_same_as_literal
|
|
94
|
-
| _intersection_objects_not_equal_to_literal
|
|
95
|
-
| _string_is_not_never
|
|
96
|
-
| _never_is_never
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
/*
|
|
3
|
-
Copyright 2019 Google LLC
|
|
4
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
-
you may not use this file except in compliance with the License.
|
|
6
|
-
You may obtain a copy of the License at
|
|
7
|
-
https://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
Unless required by applicable law or agreed to in writing, software
|
|
9
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
-
See the License for the specific language governing permissions and
|
|
12
|
-
limitations under the License.
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
*
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { type AnyKey } from './utility'
|
|
20
|
-
import { type ObjectWithField } from './objects'
|
|
21
|
-
import { type Tail } from './tuples'
|
|
22
|
-
|
|
23
|
-
// TYPE TESTS
|
|
24
|
-
import { type Assert, type AssertNot, type Equals, type Same } from './assert'
|
|
25
|
-
|
|
26
|
-
export type Differentiate<
|
|
27
|
-
Field extends AnyKey,
|
|
28
|
-
State extends ObjectWithField<Field, any>,
|
|
29
|
-
ExpectedType extends State[Field]
|
|
30
|
-
> = State extends ObjectWithField<Field, ExpectedType> ? State : never
|
|
31
|
-
|
|
32
|
-
export type DifferentiateByKind<
|
|
33
|
-
State extends { kind: any },
|
|
34
|
-
K extends State['kind']
|
|
35
|
-
> = Differentiate<'kind', State, K>
|
|
36
|
-
|
|
37
|
-
export type DifferentiateAt<
|
|
38
|
-
Path extends AnyKey[],
|
|
39
|
-
State,
|
|
40
|
-
ExpectedType
|
|
41
|
-
> = Path extends []
|
|
42
|
-
? State
|
|
43
|
-
: Path extends [infer T]
|
|
44
|
-
? T extends keyof State
|
|
45
|
-
? ExpectedType extends State[T]
|
|
46
|
-
? Differentiate<T, State, ExpectedType>
|
|
47
|
-
: never
|
|
48
|
-
: never
|
|
49
|
-
: Path extends [infer K, ...any[]]
|
|
50
|
-
? K extends keyof State
|
|
51
|
-
? Tail<Path> extends infer Rest
|
|
52
|
-
? Rest extends AnyKey[]
|
|
53
|
-
? State & { [k in K]: DifferentiateAt<Rest, State[k], ExpectedType> }
|
|
54
|
-
: never
|
|
55
|
-
: never
|
|
56
|
-
: never
|
|
57
|
-
: never
|
|
58
|
-
|
|
59
|
-
interface A { kind: 'A', a: string }
|
|
60
|
-
interface B { kind: 'B', b: string }
|
|
61
|
-
type AB = A | B
|
|
62
|
-
|
|
63
|
-
// DifferentiateByKind
|
|
64
|
-
type T0 = Differentiate<'kind', AB, 'A'>
|
|
65
|
-
|
|
66
|
-
type _T0_should_only_include_A = Assert<Equals<T0, A>>
|
|
67
|
-
type _T0_should_not_include_B = AssertNot<Equals<T0, B>>
|
|
68
|
-
|
|
69
|
-
// DifferentiateByKind
|
|
70
|
-
type T1 = DifferentiateByKind<AB, 'A'>
|
|
71
|
-
|
|
72
|
-
type _T1_should_only_include_A = Assert<Equals<T1, A>>
|
|
73
|
-
type _T1_should_not_include_B = AssertNot<Equals<T1, B>>
|
|
74
|
-
|
|
75
|
-
// DifferentiateAt
|
|
76
|
-
interface C { ab: AB, c: string }
|
|
77
|
-
type T2 = DifferentiateAt<['ab', 'kind'], C, 'A'>
|
|
78
|
-
|
|
79
|
-
type _T2_should_only_include_A = Assert<
|
|
80
|
-
Same<T2, { ab: { kind: 'A', a: string }, c: string }>
|
|
81
|
-
>
|
|
82
|
-
|
|
83
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
84
|
-
type _TESTS_ =
|
|
85
|
-
| _T0_should_only_include_A
|
|
86
|
-
| _T0_should_not_include_B
|
|
87
|
-
| _T1_should_only_include_A
|
|
88
|
-
| _T1_should_not_include_B
|
|
89
|
-
| _T2_should_only_include_A
|
package/src/types/functions.ts
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
https://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software
|
|
8
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
-
See the License for the specific language governing permissions and
|
|
11
|
-
limitations under the License.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
// TYPE TESTS
|
|
19
|
-
import { type Assert, type Equals, type Extends, type AssertNot, type IsNever } from './assert'
|
|
20
|
-
import { type Tail } from './tuples'
|
|
21
|
-
|
|
22
|
-
export type Equality<T> = (a: T, b: T) => boolean
|
|
23
|
-
|
|
24
|
-
export type Effect<T> = (value: T) => void
|
|
25
|
-
|
|
26
|
-
export type Proc = () => void
|
|
27
|
-
|
|
28
|
-
export type Lazy<T> = () => T
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
-
export type Fun = (...args: any[]) => any
|
|
31
|
-
export type Fun1<A, T> = (a1: A) => T
|
|
32
|
-
export type Fun2<A, B, T> = (a1: A, a2: B) => T
|
|
33
|
-
export type Fun3<A, B, C, T> = (a1: A, a2: B, a3: C) => T
|
|
34
|
-
export type Fun4<A, B, C, D, T> = (a1: A, a2: B, a3: C, a4: D) => T
|
|
35
|
-
export type Fun5<A, B, C, D, E, T> = (a1: A, a2: B, a3: C, a4: D, a5: E) => T
|
|
36
|
-
export type Fun6<A, B, C, D, E, F, T> = (
|
|
37
|
-
a1: A,
|
|
38
|
-
a2: B,
|
|
39
|
-
a3: C,
|
|
40
|
-
a4: D,
|
|
41
|
-
a5: E,
|
|
42
|
-
a6: F
|
|
43
|
-
) => T
|
|
44
|
-
|
|
45
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
46
|
-
export type AnyFunction = Fun | Function
|
|
47
|
-
|
|
48
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
-
export type FirstArgument<F extends AnyFunction> = F extends (a: infer A) => any
|
|
50
|
-
? unknown extends A
|
|
51
|
-
? never
|
|
52
|
-
: A
|
|
53
|
-
: never
|
|
54
|
-
|
|
55
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
-
export type OnlyIfDoesNotReturnNever<F extends Fun> = F extends Fun1<any, never>
|
|
57
|
-
? never
|
|
58
|
-
: F
|
|
59
|
-
|
|
60
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
61
|
-
type CheckFunctionsChain<In, Args extends Array<Fun1<any, any>>> = {
|
|
62
|
-
empty: (i: In) => In
|
|
63
|
-
one: FirstArgument<Args[0]> extends In ? Args[0] : never
|
|
64
|
-
n: FirstArgument<Args[0]> extends In
|
|
65
|
-
? (
|
|
66
|
-
arg: FirstArgument<Args[0]>
|
|
67
|
-
) => ReturnType<CheckFunctionsChain<ReturnType<Args[0]>, Tail<Args>>>
|
|
68
|
-
: never
|
|
69
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
|
-
}[Args extends [] ? 'empty' : Args extends [any] ? 'one' : 'n']
|
|
71
|
-
|
|
72
|
-
export type FunctionsChain<In, Args extends Fun[]> = OnlyIfDoesNotReturnNever<
|
|
73
|
-
CheckFunctionsChain<In, Args>
|
|
74
|
-
>
|
|
75
|
-
type _procedure_is_AnyFunction = Assert<Extends<() => void, AnyFunction>>
|
|
76
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
77
|
-
type _Function_is_AnyFunction = Assert<Extends<Function, AnyFunction>>
|
|
78
|
-
type _string_is_not_AnyFunction = AssertNot<Extends<string, AnyFunction>>
|
|
79
|
-
|
|
80
|
-
type _first_argument_matches_type = Assert<
|
|
81
|
-
Equals<FirstArgument<(a: string) => number>, string>
|
|
82
|
-
>
|
|
83
|
-
type _first_argument_does_not_exist = Assert<
|
|
84
|
-
IsNever<FirstArgument<() => number>>
|
|
85
|
-
>
|
|
86
|
-
|
|
87
|
-
type _chain_matches_first_arg_and_last_return_type = Assert<
|
|
88
|
-
Equals<
|
|
89
|
-
FunctionsChain<
|
|
90
|
-
string,
|
|
91
|
-
[(s: string) => boolean, (b: boolean) => number, (n: number) => Date]
|
|
92
|
-
>,
|
|
93
|
-
Fun1<string, Date>
|
|
94
|
-
>
|
|
95
|
-
>
|
|
96
|
-
|
|
97
|
-
type _chain_is_never_when_does_not_check_out = Assert<
|
|
98
|
-
IsNever<
|
|
99
|
-
FunctionsChain<
|
|
100
|
-
string,
|
|
101
|
-
[(s: string) => boolean, (b: number) => number, (n: number) => Date]
|
|
102
|
-
>
|
|
103
|
-
>
|
|
104
|
-
>
|
|
105
|
-
|
|
106
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
107
|
-
type _TESTS_ =
|
|
108
|
-
| _procedure_is_AnyFunction
|
|
109
|
-
| _Function_is_AnyFunction
|
|
110
|
-
| _string_is_not_AnyFunction
|
|
111
|
-
| _first_argument_matches_type
|
|
112
|
-
| _first_argument_does_not_exist
|
|
113
|
-
| _chain_matches_first_arg_and_last_return_type
|
|
114
|
-
| _chain_is_never_when_does_not_check_out
|
package/src/types/generic.ts
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
https://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software
|
|
8
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
-
See the License for the specific language governing permissions and
|
|
11
|
-
limitations under the License.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { type Equals, type NotEquals, type Assert } from './assert'
|
|
19
|
-
|
|
20
|
-
export type Pointer<T> = () => T
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
-
export type DeRef<T extends () => any> = T extends () => infer Ret ? Ret : never
|
|
23
|
-
|
|
24
|
-
export type WhenEquals<X, Y, A = X, B = never> = Equals<X, Y> extends true
|
|
25
|
-
? A
|
|
26
|
-
: B
|
|
27
|
-
|
|
28
|
-
export type WhenNotEquals<X, Y, A = X, B = never> = NotEquals<X, Y> extends true
|
|
29
|
-
? A
|
|
30
|
-
: B
|
|
31
|
-
|
|
32
|
-
export type Cast<A, B> = A extends B ? A : B
|
|
33
|
-
|
|
34
|
-
type _when_true_returns_A = Assert<
|
|
35
|
-
Equals<WhenEquals<string, string, number, boolean>, number>
|
|
36
|
-
>
|
|
37
|
-
type _when_false_returns_B = Assert<
|
|
38
|
-
Equals<WhenEquals<string, number, number, boolean>, boolean>
|
|
39
|
-
>
|
|
40
|
-
|
|
41
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
42
|
-
type _TESTS_ = _when_true_returns_A | _when_false_returns_B
|
package/src/types/objects.ts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
Copyright 2019 Google LLC
|
|
3
|
-
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
you may not use this file except in compliance with the License.
|
|
5
|
-
You may obtain a copy of the License at
|
|
6
|
-
https://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
-
Unless required by applicable law or agreed to in writing, software
|
|
8
|
-
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
-
See the License for the specific language governing permissions and
|
|
11
|
-
limitations under the License.
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
import { type AnyKey } from './utility'
|
|
19
|
-
import { type Tail } from './tuples'
|
|
20
|
-
import { type AnyFunction } from './functions'
|
|
21
|
-
|
|
22
|
-
// TYPE TESTS
|
|
23
|
-
import { type Assert, type Equals, type AssertNot } from './assert'
|
|
24
|
-
import { type WhenEquals } from './generic'
|
|
25
|
-
|
|
26
|
-
export type ObjectWithField<F extends AnyKey, V> = { [_ in F]: V }
|
|
27
|
-
|
|
28
|
-
export type ObjectWithPath<Path extends AnyKey[], V> = Path extends []
|
|
29
|
-
? // eslint-disable-next-line @typescript-eslint/ban-types
|
|
30
|
-
{}
|
|
31
|
-
: Path extends [infer T]
|
|
32
|
-
? T extends AnyKey
|
|
33
|
-
? { [_ in T]: V }
|
|
34
|
-
: never
|
|
35
|
-
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
-
Path extends [infer K, ...any[]]
|
|
37
|
-
? K extends AnyKey
|
|
38
|
-
? Tail<Path> extends infer Rest
|
|
39
|
-
? Rest extends AnyKey[]
|
|
40
|
-
? { [_ in K]: ObjectWithPath<Rest, V> }
|
|
41
|
-
: never
|
|
42
|
-
: never
|
|
43
|
-
: never
|
|
44
|
-
: never
|
|
45
|
-
|
|
46
|
-
export type TypeAtPath<Path extends AnyKey[], O> = {
|
|
47
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
48
|
-
next: Path extends [infer K, ...any[]]
|
|
49
|
-
? K extends AnyKey
|
|
50
|
-
? Tail<Path> extends infer Rest
|
|
51
|
-
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
-
O extends Record<AnyKey, any>
|
|
53
|
-
? Rest extends AnyKey[]
|
|
54
|
-
? TypeAtPath<Rest, O[K]>
|
|
55
|
-
: never
|
|
56
|
-
: never
|
|
57
|
-
: never
|
|
58
|
-
: never
|
|
59
|
-
: never
|
|
60
|
-
empty: O
|
|
61
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
62
|
-
done: O extends Record<AnyKey, any>
|
|
63
|
-
? Path extends [infer K]
|
|
64
|
-
? K extends AnyKey
|
|
65
|
-
? O[K]
|
|
66
|
-
: never
|
|
67
|
-
: never
|
|
68
|
-
: never
|
|
69
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
70
|
-
}[Path extends [] ? 'empty' : Path extends [any] ? 'done' : 'next']
|
|
71
|
-
|
|
72
|
-
export type WritableKeys<T> = {
|
|
73
|
-
[K in keyof T]-?: WhenEquals<
|
|
74
|
-
{ [Q in K]: T[K] },
|
|
75
|
-
{ -readonly [Q in K]: T[K] },
|
|
76
|
-
K
|
|
77
|
-
>
|
|
78
|
-
}[keyof T]
|
|
79
|
-
|
|
80
|
-
export type ReadonlyKeys<T> = {
|
|
81
|
-
[P in keyof T]-?: WhenEquals<
|
|
82
|
-
{ [Q in P]: T[P] },
|
|
83
|
-
{ -readonly [Q in P]: T[P] },
|
|
84
|
-
never,
|
|
85
|
-
P
|
|
86
|
-
>
|
|
87
|
-
}[keyof T]
|
|
88
|
-
|
|
89
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
90
|
-
export type Id<T> = {} & { [P in keyof T]: T[P] }
|
|
91
|
-
|
|
92
|
-
export type KeysWithFieldType<T, Condition> = {
|
|
93
|
-
[K in keyof T]: WhenEquals<T[K], Condition, K>
|
|
94
|
-
}[keyof T]
|
|
95
|
-
|
|
96
|
-
export type KeysWithoutFieldType<T, Condition> = {
|
|
97
|
-
[K in keyof T]: WhenEquals<T[K], Condition, never, K>
|
|
98
|
-
}[keyof T]
|
|
99
|
-
|
|
100
|
-
export type RemoveNullableFromFields<T> = {
|
|
101
|
-
[K in keyof T]: NonNullable<T[K]>
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export type WritableFields<T> = Pick<T, WritableKeys<T>>
|
|
105
|
-
export type ReadonlyFields<T> = Pick<T, ReadonlyKeys<T>>
|
|
106
|
-
export type ExcludeFunctionFields<T> = Pick<
|
|
107
|
-
T,
|
|
108
|
-
KeysWithoutFieldType<T, AnyFunction>
|
|
109
|
-
>
|
|
110
|
-
|
|
111
|
-
export type Merge<A, B> = Id<A & B>
|
|
112
|
-
|
|
113
|
-
type _merged_ = Merge<{ a: string, c?: number }, { b?: boolean }>
|
|
114
|
-
type _merge_two_object_types_ = Assert<
|
|
115
|
-
Equals<_merged_, { a: string, b?: boolean, c?: number }>
|
|
116
|
-
>
|
|
117
|
-
|
|
118
|
-
type _object_with_field_has_field = Assert<
|
|
119
|
-
Equals<{ a: number }, ObjectWithField<'a', number>>
|
|
120
|
-
>
|
|
121
|
-
type _object_with_field_has_no_field = AssertNot<
|
|
122
|
-
Equals<{ a: number }, ObjectWithField<'c', number>>
|
|
123
|
-
>
|
|
124
|
-
|
|
125
|
-
type _object_with_empty_path_is_empty_object = Assert<
|
|
126
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
127
|
-
Equals<{}, ObjectWithPath<[], number>>
|
|
128
|
-
>
|
|
129
|
-
type _object_with_tuple_of_one_is_object = Assert<
|
|
130
|
-
Equals<{ a: number }, ObjectWithPath<['a'], number>>
|
|
131
|
-
>
|
|
132
|
-
type _object_with_tuple_of_two_is_object = Assert<
|
|
133
|
-
Equals<{ a: { b: number } }, ObjectWithPath<['a', 'b'], number>>
|
|
134
|
-
>
|
|
135
|
-
type _object_with_tuple_of_three_is_object = Assert<
|
|
136
|
-
Equals<{ a: { b: { c: number } } }, ObjectWithPath<['a', 'b', 'c'], number>>
|
|
137
|
-
>
|
|
138
|
-
|
|
139
|
-
interface _nested { a: { b: boolean[], c: number }, d: string }
|
|
140
|
-
type _type_at_empty_path_is_type_of_argument = Assert<
|
|
141
|
-
Equals<_nested, TypeAtPath<[], _nested>>
|
|
142
|
-
>
|
|
143
|
-
type _type_at_level_1 = Assert<
|
|
144
|
-
Equals<{ b: boolean[], c: number }, TypeAtPath<['a'], _nested>>
|
|
145
|
-
>
|
|
146
|
-
type _type_at_level_2 = Assert<
|
|
147
|
-
Equals<boolean[], TypeAtPath<['a', 'b'], _nested>>
|
|
148
|
-
>
|
|
149
|
-
type _type_at_level_3 = Assert<
|
|
150
|
-
Equals<boolean, TypeAtPath<['a', 'b', number], _nested>>
|
|
151
|
-
>
|
|
152
|
-
|
|
153
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
154
|
-
type _writable_keys_of_empty_is_never = Assert<Equals<WritableKeys<{}>, never>>
|
|
155
|
-
type _writable_keys_of_writable_is_keys = Assert<
|
|
156
|
-
Equals<WritableKeys<{ a: string, b: string }>, 'a' | 'b'>
|
|
157
|
-
>
|
|
158
|
-
type _writable_keys_of_mixed_is_subset = Assert<
|
|
159
|
-
Equals<WritableKeys<{ a: string, b: string, readonly c: number }>, 'a' | 'b'>
|
|
160
|
-
>
|
|
161
|
-
|
|
162
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
163
|
-
type _readonly_keys_of_empty_is_never = Assert<Equals<ReadonlyKeys<{}>, never>>
|
|
164
|
-
type _readonly_keys_of_readonly_is_keys = Assert<
|
|
165
|
-
Equals<ReadonlyKeys<{ readonly a: string, readonly b: string }>, 'a' | 'b'>
|
|
166
|
-
>
|
|
167
|
-
type _readonly_keys_of_mixed_is_subset = Assert<
|
|
168
|
-
Equals<
|
|
169
|
-
ReadonlyKeys<{ readonly a: string, readonly b: string, c: number }>,
|
|
170
|
-
'a' | 'b'
|
|
171
|
-
>
|
|
172
|
-
>
|
|
173
|
-
|
|
174
|
-
type _KeysWithFieldType_matches = Assert<
|
|
175
|
-
Equals<KeysWithFieldType<{ a: string, b: number }, string>, 'a'>
|
|
176
|
-
>
|
|
177
|
-
type _KeysWithoutFieldType_matches = Assert<
|
|
178
|
-
Equals<KeysWithoutFieldType<{ a: string, b: number }, number>, 'a'>
|
|
179
|
-
>
|
|
180
|
-
type _RemoveNullableFromFields_matches = Assert<
|
|
181
|
-
Equals<
|
|
182
|
-
RemoveNullableFromFields<{
|
|
183
|
-
a: string | null | undefined
|
|
184
|
-
b: number | null
|
|
185
|
-
c: boolean
|
|
186
|
-
}>,
|
|
187
|
-
{ a: string, b: number, c: boolean }
|
|
188
|
-
>
|
|
189
|
-
>
|
|
190
|
-
|
|
191
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
192
|
-
type _TESTS_ =
|
|
193
|
-
| _merge_two_object_types_
|
|
194
|
-
| _object_with_field_has_field
|
|
195
|
-
| _object_with_field_has_no_field
|
|
196
|
-
| _object_with_empty_path_is_empty_object
|
|
197
|
-
| _object_with_tuple_of_one_is_object
|
|
198
|
-
| _object_with_tuple_of_two_is_object
|
|
199
|
-
| _object_with_tuple_of_three_is_object
|
|
200
|
-
| _type_at_empty_path_is_type_of_argument
|
|
201
|
-
| _type_at_level_1
|
|
202
|
-
| _type_at_level_2
|
|
203
|
-
| _type_at_level_3
|
|
204
|
-
| _writable_keys_of_empty_is_never
|
|
205
|
-
| _writable_keys_of_writable_is_keys
|
|
206
|
-
| _writable_keys_of_mixed_is_subset
|
|
207
|
-
| _readonly_keys_of_empty_is_never
|
|
208
|
-
| _readonly_keys_of_readonly_is_keys
|
|
209
|
-
| _readonly_keys_of_mixed_is_subset
|
|
210
|
-
| _KeysWithFieldType_matches
|
|
211
|
-
| _KeysWithoutFieldType_matches
|
|
212
|
-
| _RemoveNullableFromFields_matches
|