@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/tuples.ts
DELETED
|
@@ -1,244 +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 Pointer, type DeRef } from './generic'
|
|
20
|
-
|
|
21
|
-
// export type LoseLast<A extends any[]> = Reverse<LoseLastImpl<[], A>>
|
|
22
|
-
|
|
23
|
-
// TYPE TESTS
|
|
24
|
-
import { type Assert, type Equals, type AssertNot, type NotEquals } from './assert'
|
|
25
|
-
|
|
26
|
-
export type Tail<T extends any[]> = ((...args: T) => void) extends (
|
|
27
|
-
_: any,
|
|
28
|
-
...rest: infer Rest
|
|
29
|
-
) => void
|
|
30
|
-
? Rest
|
|
31
|
-
: never
|
|
32
|
-
|
|
33
|
-
type DropNImpl<N extends number, T extends any[], I extends any[]> = {
|
|
34
|
-
return: T
|
|
35
|
-
next: DropNImpl<N, Tail<T>, Prepend<any, I>>
|
|
36
|
-
}[Length<I> extends N ? 'return' : 'next']
|
|
37
|
-
|
|
38
|
-
export type DropN<N extends number, T extends any[]> = DropNImpl<N, T, []>
|
|
39
|
-
|
|
40
|
-
type ReverseImpl<T extends any[], R extends any[], I extends any[]> = {
|
|
41
|
-
return: DeRef<Pointer<R>>
|
|
42
|
-
next: ReverseImpl<T, Prepend<T[Length<I>], R>, Prepend<any, I>>
|
|
43
|
-
}[Length<I> extends Length<T> ? 'return' : 'next']
|
|
44
|
-
|
|
45
|
-
export type Reverse<T extends any[]> = ReverseImpl<T, [], []>
|
|
46
|
-
|
|
47
|
-
export type Head<T extends any[]> = T extends [infer H, ...any[]] ? H : never
|
|
48
|
-
|
|
49
|
-
export type Last<T extends any[]> = {
|
|
50
|
-
n: Last<Tail<T>>
|
|
51
|
-
one: T extends [infer H] ? H : never
|
|
52
|
-
empty: never
|
|
53
|
-
}[T extends [] ? 'empty' : T extends [any] ? 'one' : 'n']
|
|
54
|
-
|
|
55
|
-
export type TupleToUnion<T extends any[]> = T[number]
|
|
56
|
-
|
|
57
|
-
export type Prepend<Insert, Tail extends any[]> = [Insert, ...Tail]
|
|
58
|
-
|
|
59
|
-
export type Append<Tuple extends any[], Element> = [...Tuple, Element]
|
|
60
|
-
|
|
61
|
-
export type Length<T extends any[]> = T['length']
|
|
62
|
-
|
|
63
|
-
// export type Concat<A extends any[], B extends any[]> =
|
|
64
|
-
// ReverseImpl<Cast<ReverseImpl<A, [], []>, any[]>, B, []>
|
|
65
|
-
|
|
66
|
-
// export type Append<A extends any[], B> = Concat<A, [B]>
|
|
67
|
-
|
|
68
|
-
// export type Pop<A extends any[]> = Head<Reverse<A>>
|
|
69
|
-
|
|
70
|
-
export type LoseLastImpl<A extends any[], B extends any[]> = {
|
|
71
|
-
empty: A
|
|
72
|
-
next: LoseLastImpl<Prepend<B[0], A>, Tail<B>>
|
|
73
|
-
}[B extends [] ? 'empty' : B extends [any] ? 'empty' : 'next']
|
|
74
|
-
|
|
75
|
-
export type UnionToIntersection<U> = (
|
|
76
|
-
U extends any ? (k: U) => void : never
|
|
77
|
-
) extends (k: infer I) => void
|
|
78
|
-
? I
|
|
79
|
-
: never
|
|
80
|
-
|
|
81
|
-
type LastOf<T> = UnionToIntersection<
|
|
82
|
-
T extends any ? () => T : never
|
|
83
|
-
> extends () => infer R
|
|
84
|
-
? R
|
|
85
|
-
: never
|
|
86
|
-
|
|
87
|
-
export type Push<T extends any[], V> = [...T, V]
|
|
88
|
-
|
|
89
|
-
export type UnionToTuple<
|
|
90
|
-
T,
|
|
91
|
-
L = LastOf<T>,
|
|
92
|
-
N = [T] extends [never] ? true : false
|
|
93
|
-
> = true extends N ? [] : Push<UnionToTuple1<Exclude<T, L>>, L>
|
|
94
|
-
type UnionToTuple1<
|
|
95
|
-
T,
|
|
96
|
-
L = LastOf<T>,
|
|
97
|
-
N = [T] extends [never] ? true : false
|
|
98
|
-
> = true extends N ? [] : Push<UnionToTuple2<Exclude<T, L>>, L>
|
|
99
|
-
type UnionToTuple2<
|
|
100
|
-
T,
|
|
101
|
-
L = LastOf<T>,
|
|
102
|
-
N = [T] extends [never] ? true : false
|
|
103
|
-
> = true extends N ? [] : Push<UnionToTuple3<Exclude<T, L>>, L>
|
|
104
|
-
type UnionToTuple3<
|
|
105
|
-
T,
|
|
106
|
-
L = LastOf<T>,
|
|
107
|
-
N = [T] extends [never] ? true : false
|
|
108
|
-
> = true extends N ? [] : Push<UnionToTuple4<Exclude<T, L>>, L>
|
|
109
|
-
type UnionToTuple4<
|
|
110
|
-
T,
|
|
111
|
-
L = LastOf<T>,
|
|
112
|
-
N = [T] extends [never] ? true : false
|
|
113
|
-
> = true extends N ? [] : Push<UnionToTuple5<Exclude<T, L>>, L>
|
|
114
|
-
type UnionToTuple5<
|
|
115
|
-
T,
|
|
116
|
-
L = LastOf<T>,
|
|
117
|
-
N = [T] extends [never] ? true : false
|
|
118
|
-
> = true extends N ? [] : Push<UnionToTuple6<Exclude<T, L>>, L>
|
|
119
|
-
type UnionToTuple6<
|
|
120
|
-
T,
|
|
121
|
-
L = LastOf<T>,
|
|
122
|
-
N = [T] extends [never] ? true : false
|
|
123
|
-
> = true extends N ? [] : Push<UnionToTuple7<Exclude<T, L>>, L>
|
|
124
|
-
type UnionToTuple7<
|
|
125
|
-
T,
|
|
126
|
-
L = LastOf<T>,
|
|
127
|
-
N = [T] extends [never] ? true : false
|
|
128
|
-
> = true extends N ? [] : Push<UnionToTuple8<Exclude<T, L>>, L>
|
|
129
|
-
type UnionToTuple8<
|
|
130
|
-
T,
|
|
131
|
-
L = LastOf<T>,
|
|
132
|
-
N = [T] extends [never] ? true : false
|
|
133
|
-
> = true extends N ? [] : Push<UnionToTuple9<Exclude<T, L>>, L>
|
|
134
|
-
type UnionToTuple9<
|
|
135
|
-
T,
|
|
136
|
-
L = LastOf<T>,
|
|
137
|
-
N = [T] extends [never] ? true : false
|
|
138
|
-
> = true extends N ? [] : Push<UnionToTupleX<Exclude<T, L>>, L>
|
|
139
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
140
|
-
type UnionToTupleX<T> = never
|
|
141
|
-
|
|
142
|
-
type _head = Head<[number, string]>
|
|
143
|
-
type _tail = Tail<[number, string, boolean]>
|
|
144
|
-
type _head_is_number = Assert<Equals<_head, number>>
|
|
145
|
-
type _tail_is_string_boolean = Assert<Equals<_tail, [string, boolean]>>
|
|
146
|
-
type _head_is_not_string = AssertNot<Equals<Head<[number, string]>, boolean>>
|
|
147
|
-
type _last_is_boolean = Assert<Equals<Last<[number, string, boolean]>, boolean>>
|
|
148
|
-
type _last_is_not_string = Assert<NotEquals<Last<[number, string]>, boolean>>
|
|
149
|
-
type _tuple_to_union_of_empty_is_never = Assert<Equals<TupleToUnion<[]>, never>>
|
|
150
|
-
type _tuple_to_union_of_not_empty_is_list = Assert<
|
|
151
|
-
Equals<TupleToUnion<[string, 1]>, string | 1>
|
|
152
|
-
>
|
|
153
|
-
type _union_to_tuple_of_not_empty_set_is_list = Assert<
|
|
154
|
-
Equals<UnionToTuple<string | 1>, [string, 1]>
|
|
155
|
-
>
|
|
156
|
-
type _prepend_works_on_empty_tail = Assert<
|
|
157
|
-
Equals<Prepend<string, []>, [string]>
|
|
158
|
-
>
|
|
159
|
-
type _prepend_works_on_non_empty_tail = Assert<
|
|
160
|
-
Equals<Prepend<string, [number]>, [string, number]>
|
|
161
|
-
>
|
|
162
|
-
type _prepend_fails_on_mismatching_types = Assert<
|
|
163
|
-
NotEquals<Prepend<string, [boolean]>, [string, number]>
|
|
164
|
-
>
|
|
165
|
-
type _append_works_on_empty_list = Assert<Equals<Append<[], string>, [string]>>
|
|
166
|
-
type _append_works_on_non_empty_list = Assert<
|
|
167
|
-
Equals<Append<[number], string>, [number, string]>
|
|
168
|
-
>
|
|
169
|
-
type _append_works_on_non_empty_list2 = Assert<
|
|
170
|
-
Equals<Append<[number, boolean], string>, [number, boolean, string]>
|
|
171
|
-
>
|
|
172
|
-
type _append_fails_on_mismatching_types = Assert<
|
|
173
|
-
NotEquals<Append<[boolean], string>, [string, number]>
|
|
174
|
-
>
|
|
175
|
-
type _length_works_with_empty = Assert<Equals<Length<[]>, 0>>
|
|
176
|
-
type _length_works_with_one = Assert<Equals<Length<[1]>, 1>>
|
|
177
|
-
type _length_works_with_tewo = Assert<Equals<Length<[1, 2]>, 2>>
|
|
178
|
-
type _length_works_with_three = Assert<Equals<Length<[1, 2, 3]>, 3>>
|
|
179
|
-
type _length_works_with_four = Assert<Equals<Length<[1, 2, 3, 4]>, 4>>
|
|
180
|
-
|
|
181
|
-
type _DropN_works_dropping_1_of_2 = Assert<Equals<DropN<1, [1, 2]>, [2]>>
|
|
182
|
-
type _DropN_works_dropping_2_of_4 = Assert<
|
|
183
|
-
Equals<DropN<2, [1, 2, 3, 4]>, [3, 4]>
|
|
184
|
-
>
|
|
185
|
-
type _DropN_works_dropping_4_of_4 = Assert<Equals<DropN<4, [1, 2, 3, 4]>, []>>
|
|
186
|
-
|
|
187
|
-
type _reverse_empty_works = Assert<Equals<Reverse<[]>, []>>
|
|
188
|
-
type _reverse_one_works = Assert<Equals<Reverse<[1]>, [1]>>
|
|
189
|
-
type _reverse_two_works = Assert<Equals<Reverse<[1, 2]>, [2, 1]>>
|
|
190
|
-
type _reverse_three_works = Assert<Equals<Reverse<[1, 2, 3]>, [3, 2, 1]>>
|
|
191
|
-
type _reverse_four_works = Assert<Equals<Reverse<[1, 2, 3, 4]>, [4, 3, 2, 1]>>
|
|
192
|
-
|
|
193
|
-
// type _Concat_0_0 = Assert<Equals<Concat<[], []>, []>>
|
|
194
|
-
// type _Concat_0_1 = Assert<Equals<Concat<[], [1]>, [1]>>
|
|
195
|
-
// type _Concat_1_0 = Assert<Equals<Concat<[1], []>, [1]>>
|
|
196
|
-
// type _Concat_2_3 = Assert<Equals<Concat<[1, 2], [3, 4, 5]>, [1, 2, 3, 4, 5]>>
|
|
197
|
-
|
|
198
|
-
// type _Pop_1 = Assert<Equals<Pop<[1]>, 1>>
|
|
199
|
-
// type _Pop_2 = Assert<Equals<Pop<[1, 2]>, 2>>
|
|
200
|
-
// type _LoseLast_1 = Assert<Equals<LoseLast<[1]>, []>>
|
|
201
|
-
// type _LoseLast_2 = Assert<Equals<LoseLast<[1, 2]>, [1]>>
|
|
202
|
-
// type _LoseLast_3 = Assert<Equals<LoseLast<[1, 2, 3]>, [1, 2]>>
|
|
203
|
-
// type _LoseLast_4 = Assert<Equals<LoseLast<[1, 2, 3, 4]>, [1, 2, 3]>>
|
|
204
|
-
|
|
205
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
206
|
-
type _TESTS_ =
|
|
207
|
-
| _head_is_number
|
|
208
|
-
| _tail_is_string_boolean
|
|
209
|
-
| _head_is_not_string
|
|
210
|
-
| _last_is_boolean
|
|
211
|
-
| _last_is_not_string
|
|
212
|
-
| _tuple_to_union_of_empty_is_never
|
|
213
|
-
| _tuple_to_union_of_not_empty_is_list
|
|
214
|
-
| _union_to_tuple_of_not_empty_set_is_list
|
|
215
|
-
| _prepend_works_on_empty_tail
|
|
216
|
-
| _prepend_works_on_non_empty_tail
|
|
217
|
-
| _prepend_fails_on_mismatching_types
|
|
218
|
-
| _append_works_on_empty_list
|
|
219
|
-
| _append_works_on_non_empty_list
|
|
220
|
-
| _append_works_on_non_empty_list2
|
|
221
|
-
| _append_fails_on_mismatching_types
|
|
222
|
-
| _length_works_with_empty
|
|
223
|
-
| _length_works_with_one
|
|
224
|
-
| _length_works_with_tewo
|
|
225
|
-
| _length_works_with_three
|
|
226
|
-
| _length_works_with_four
|
|
227
|
-
| _DropN_works_dropping_1_of_2
|
|
228
|
-
| _DropN_works_dropping_2_of_4
|
|
229
|
-
| _DropN_works_dropping_4_of_4
|
|
230
|
-
| _reverse_empty_works
|
|
231
|
-
| _reverse_one_works
|
|
232
|
-
| _reverse_two_works
|
|
233
|
-
| _reverse_three_works
|
|
234
|
-
| _reverse_four_works
|
|
235
|
-
// | _Concat_0_0
|
|
236
|
-
// | _Concat_0_1
|
|
237
|
-
// | _Concat_1_0
|
|
238
|
-
// | _Concat_2_3
|
|
239
|
-
// | _Pop_1
|
|
240
|
-
// | _Pop_2
|
|
241
|
-
// | _LoseLast_1
|
|
242
|
-
// | _LoseLast_2
|
|
243
|
-
// | _LoseLast_3
|
|
244
|
-
// | _LoseLast_4
|
package/src/uuid.ts
DELETED
|
@@ -1,61 +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
|
-
* Helper functions to generate and validate [UUID](http://en.wikipedia.org/wiki/Universally_unique_identifier)
|
|
16
|
-
* strings (version 4).
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { type Newtype, NewtypeClass } from './newtype'
|
|
20
|
-
|
|
21
|
-
const random = (max: number): number => Math.floor(Math.random() * max)
|
|
22
|
-
const srandom = (): string => '0123456789abcdef'.charAt(random(0x10))
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* `Uuid.create()` returns a string value representing a random UUID string.
|
|
26
|
-
*/
|
|
27
|
-
export function create (): UUID {
|
|
28
|
-
const s = [] as string[]
|
|
29
|
-
let i = 0
|
|
30
|
-
for (i = 0; i < 8; i++) s[i] = srandom()
|
|
31
|
-
s[8] = '-'
|
|
32
|
-
for (i = 9; i < 13; i++) s[i] = srandom()
|
|
33
|
-
s[13] = '-'
|
|
34
|
-
s[14] = '4'
|
|
35
|
-
for (i = 15; i < 18; i++) s[i] = srandom()
|
|
36
|
-
s[18] = '-'
|
|
37
|
-
s[19] = '89AB'.charAt(random(0x3))
|
|
38
|
-
for (i = 20; i < 23; i++) s[i] = srandom()
|
|
39
|
-
s[23] = '-'
|
|
40
|
-
for (i = 24; i < 36; i++) s[i] = srandom()
|
|
41
|
-
return UUID.unsafeOf(s.join(''))
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const pattern =
|
|
45
|
-
/^[0123456789abcdef]{8}-[0123456789abcdef]{4}-4[0123456789abcdef]{3}-[89ab][0123456789abcdef]{3}-[0123456789abcdef]{12}$/i
|
|
46
|
-
|
|
47
|
-
export type UUID = Newtype<string, { readonly UUID: unique symbol }>
|
|
48
|
-
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
50
|
-
export const UUID: NewtypeClass<UUID> = new (class extends NewtypeClass<UUID> {
|
|
51
|
-
/**
|
|
52
|
-
* Returns `true` if the passed `uuid` conforms to the UUID v.4 format.
|
|
53
|
-
*/
|
|
54
|
-
isValid (uuid: string): boolean {
|
|
55
|
-
return pattern.test(uuid)
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
override toString (uuid: UUID): string {
|
|
59
|
-
return UUID.get(uuid)
|
|
60
|
-
}
|
|
61
|
-
})()
|
package/src/validation.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { Result } from './result'
|
|
2
|
-
|
|
3
|
-
export interface Valid {
|
|
4
|
-
type: 'valid'
|
|
5
|
-
}
|
|
6
|
-
export interface Invalid<E> {
|
|
7
|
-
type: 'invalid'
|
|
8
|
-
error: E
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export type Validation<E> = Valid | Invalid<E>
|
|
12
|
-
|
|
13
|
-
export type PromiseValidation<E> = PromiseLike<Validation<E>>
|
|
14
|
-
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
16
|
-
export const Validation = {
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
-
valid: { type: 'valid' } satisfies Validation<any>,
|
|
19
|
-
invalid<E>(error: E): Validation<E> {
|
|
20
|
-
return { type: 'invalid', error }
|
|
21
|
-
},
|
|
22
|
-
isValid<E>(r: Validation<E>): r is Valid {
|
|
23
|
-
return r.type === 'valid'
|
|
24
|
-
},
|
|
25
|
-
isInvalid<E>(r: Validation<E>): r is Invalid<E> {
|
|
26
|
-
return r.type === 'invalid'
|
|
27
|
-
},
|
|
28
|
-
cmatch:
|
|
29
|
-
<V, E>(valid: () => V, invalid: (error: E) => V) =>
|
|
30
|
-
(r: Validation<E>): V => {
|
|
31
|
-
if (Validation.isValid(r)) {
|
|
32
|
-
return valid()
|
|
33
|
-
} else {
|
|
34
|
-
return invalid(r.error)
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
|
-
match: <V, E>(
|
|
38
|
-
r: Validation<E>,
|
|
39
|
-
valid: () => V,
|
|
40
|
-
invalid: (error: E) => V
|
|
41
|
-
): V => {
|
|
42
|
-
if (Validation.isValid(r)) {
|
|
43
|
-
return valid()
|
|
44
|
-
} else {
|
|
45
|
-
return invalid(r.error)
|
|
46
|
-
}
|
|
47
|
-
},
|
|
48
|
-
toResult: <T, E>(value: T): ((validation: Validation<E>) => Result<T, E>) =>
|
|
49
|
-
Validation.cmatch(
|
|
50
|
-
() => Result.success<T>(value),
|
|
51
|
-
(err: E) => Result.failure<E>(err)
|
|
52
|
-
),
|
|
53
|
-
whenValid:
|
|
54
|
-
<E>(apply: () => void) =>
|
|
55
|
-
(r: Validation<E>): Validation<E> => {
|
|
56
|
-
if (Validation.isValid(r)) {
|
|
57
|
-
apply()
|
|
58
|
-
}
|
|
59
|
-
return r
|
|
60
|
-
},
|
|
61
|
-
whenInvalid:
|
|
62
|
-
<E>(apply: (e: E) => void) =>
|
|
63
|
-
(r: Validation<E>): Validation<E> => {
|
|
64
|
-
if (Validation.isInvalid(r)) {
|
|
65
|
-
apply(r.error)
|
|
66
|
-
}
|
|
67
|
-
return r
|
|
68
|
-
}
|
|
69
|
-
}
|