@tempots/std 0.9.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.
Files changed (132) hide show
  1. package/dist/arrays.d.ts +49 -0
  2. package/dist/arrays.js +249 -0
  3. package/dist/async-result.d.ts +37 -0
  4. package/dist/async-result.js +75 -0
  5. package/dist/bigint.d.ts +18 -0
  6. package/dist/bigint.js +110 -0
  7. package/dist/booleans.d.ts +23 -0
  8. package/dist/booleans.js +68 -0
  9. package/dist/colors/cmyk.d.ts +21 -0
  10. package/dist/colors/cmyk.js +54 -0
  11. package/dist/colors/convert.d.ts +283 -0
  12. package/dist/colors/convert.js +742 -0
  13. package/dist/colors/hsl.d.ts +24 -0
  14. package/dist/colors/hsl.js +56 -0
  15. package/dist/colors/hsla.d.ts +18 -0
  16. package/dist/colors/hsla.js +35 -0
  17. package/dist/colors/hsluv.d.ts +24 -0
  18. package/dist/colors/hsluv.js +56 -0
  19. package/dist/colors/hsv.d.ts +24 -0
  20. package/dist/colors/hsv.js +54 -0
  21. package/dist/colors/lab.d.ts +24 -0
  22. package/dist/colors/lab.js +54 -0
  23. package/dist/colors/lch.d.ts +19 -0
  24. package/dist/colors/lch.js +44 -0
  25. package/dist/colors/luv.d.ts +19 -0
  26. package/dist/colors/luv.js +45 -0
  27. package/dist/colors/rgb.d.ts +13 -0
  28. package/dist/colors/rgb.js +47 -0
  29. package/dist/colors/rgba.d.ts +12 -0
  30. package/dist/colors/rgba.js +44 -0
  31. package/dist/colors/srgb.d.ts +24 -0
  32. package/dist/colors/srgb.js +51 -0
  33. package/dist/colors/xyz.d.ts +19 -0
  34. package/dist/colors/xyz.js +41 -0
  35. package/dist/edit.d.ts +20 -0
  36. package/dist/edit.js +29 -0
  37. package/dist/equals.d.ts +3 -0
  38. package/dist/equals.js +122 -0
  39. package/dist/functions.d.ts +20 -0
  40. package/dist/functions.js +38 -0
  41. package/dist/json.d.ts +14 -0
  42. package/dist/json.js +33 -0
  43. package/dist/match.d.ts +16 -0
  44. package/dist/match.js +45 -0
  45. package/dist/maybe.d.ts +9 -0
  46. package/dist/maybe.js +25 -0
  47. package/dist/memoize.d.ts +1 -0
  48. package/dist/memoize.js +9 -0
  49. package/dist/newtype.d.ts +28 -0
  50. package/dist/newtype.js +29 -0
  51. package/dist/numbers.d.ts +104 -0
  52. package/dist/numbers.js +183 -0
  53. package/dist/objects.d.ts +9 -0
  54. package/dist/objects.js +33 -0
  55. package/dist/ord.d.ts +19 -0
  56. package/dist/ord.js +73 -0
  57. package/dist/reg-exps.d.ts +10 -0
  58. package/dist/reg-exps.js +43 -0
  59. package/dist/result.d.ts +31 -0
  60. package/dist/result.js +95 -0
  61. package/dist/strings.d.ts +314 -0
  62. package/dist/strings.js +685 -0
  63. package/dist/types/assert.d.ts +12 -0
  64. package/dist/types/assert.js +13 -0
  65. package/dist/types/differentiate.d.ts +13 -0
  66. package/dist/types/differentiate.js +14 -0
  67. package/dist/types/functions.d.ts +22 -0
  68. package/dist/types/functions.js +13 -0
  69. package/dist/types/generic.d.ts +9 -0
  70. package/dist/types/generic.js +13 -0
  71. package/dist/types/objects.d.ts +50 -0
  72. package/dist/types/objects.js +13 -0
  73. package/dist/types/tuples.d.ts +44 -0
  74. package/dist/types/tuples.js +24 -0
  75. package/dist/types/utility.d.ts +2 -0
  76. package/dist/types/utility.js +1 -0
  77. package/dist/uuid.d.ts +13 -0
  78. package/dist/uuid.js +56 -0
  79. package/dist/validation.d.ts +23 -0
  80. package/dist/validation.js +44 -0
  81. package/package.json +36 -0
  82. package/src/arrays.ts +296 -0
  83. package/src/async-result.ts +103 -0
  84. package/src/bigint.ts +111 -0
  85. package/src/booleans.ts +73 -0
  86. package/src/colors/cmyk.ts +84 -0
  87. package/src/colors/convert.ts +1093 -0
  88. package/src/colors/hsl.ts +73 -0
  89. package/src/colors/hsla.ts +45 -0
  90. package/src/colors/hsluv.ts +73 -0
  91. package/src/colors/hsv.ts +75 -0
  92. package/src/colors/lab.ts +69 -0
  93. package/src/colors/lch.ts +53 -0
  94. package/src/colors/luv.ts +56 -0
  95. package/src/colors/rgb.ts +55 -0
  96. package/src/colors/rgba.ts +53 -0
  97. package/src/colors/srgb.ts +72 -0
  98. package/src/colors/xyz.ts +52 -0
  99. package/src/edit.ts +29 -0
  100. package/src/equals.ts +116 -0
  101. package/src/functions.ts +108 -0
  102. package/src/json.ts +52 -0
  103. package/src/match.ts +88 -0
  104. package/src/maybe.ts +32 -0
  105. package/src/memoize.ts +9 -0
  106. package/src/newtype.ts +59 -0
  107. package/src/numbers.ts +222 -0
  108. package/src/objects.ts +47 -0
  109. package/src/ord.ts +79 -0
  110. package/src/reg-exps.ts +48 -0
  111. package/src/result.ts +140 -0
  112. package/src/strings.ts +768 -0
  113. package/src/types/assert.ts +96 -0
  114. package/src/types/differentiate.ts +89 -0
  115. package/src/types/functions.ts +114 -0
  116. package/src/types/generic.ts +42 -0
  117. package/src/types/objects.ts +212 -0
  118. package/src/types/tuples.ts +244 -0
  119. package/src/types/utility.ts +3 -0
  120. package/src/uuid.ts +61 -0
  121. package/src/validation.ts +69 -0
  122. package/test/arrays.spec.ts +410 -0
  123. package/test/colors.spec.ts +406 -0
  124. package/test/commmon.ts +9 -0
  125. package/test/equals.spec.ts +165 -0
  126. package/test/functions.spec.ts +9 -0
  127. package/test/index.d.ts +20 -0
  128. package/test/objects.spec.ts +22 -0
  129. package/test/reg-exps.spec.ts +33 -0
  130. package/test/strings.spec.ts +333 -0
  131. package/test/uuid.spec.ts +35 -0
  132. package/tsconfig.json +19 -0
@@ -0,0 +1,244 @@
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
@@ -0,0 +1,3 @@
1
+ export type AnyKey = string | number | symbol
2
+
3
+ export type Primitive = string | boolean | number | null | undefined
package/src/uuid.ts ADDED
@@ -0,0 +1,61 @@
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
+ })()
@@ -0,0 +1,69 @@
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
+ }