@tempots/std 0.9.6 → 0.10.3
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/README.md +1 -3
- package/array.cjs +1 -0
- package/array.d.ts +49 -0
- package/array.js +223 -0
- package/bigint.cjs +1 -0
- package/bigint.d.ts +18 -0
- package/bigint.js +96 -0
- package/boolean.cjs +1 -0
- package/boolean.d.ts +23 -0
- package/boolean.js +44 -0
- package/domain.cjs +1 -0
- package/domain.d.ts +22 -0
- package/domain.js +1 -0
- package/equal.cjs +1 -0
- package/equal.d.ts +3 -0
- package/equal.js +68 -0
- package/function.cjs +1 -0
- package/function.d.ts +20 -0
- package/function.js +29 -0
- package/index.cjs +1 -0
- package/index.d.ts +0 -0
- package/index.js +1 -0
- package/maybe.cjs +1 -0
- package/maybe.d.ts +9 -0
- package/maybe.js +9 -0
- package/number.cjs +1 -0
- package/number.d.ts +101 -0
- package/number.js +103 -0
- package/object.cjs +1 -0
- package/object.d.ts +8 -0
- package/object.js +30 -0
- package/package.json +55 -25
- package/regexp.cjs +1 -0
- package/regexp.d.ts +10 -0
- package/regexp.js +14 -0
- package/string.cjs +3 -0
- package/string.d.ts +312 -0
- package/string.js +396 -0
- package/src/arrays.ts +0 -296
- package/src/async-result.ts +0 -103
- package/src/bigint.ts +0 -111
- package/src/booleans.ts +0 -73
- 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/maybe.ts +0 -32
- 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/reg-exps.ts +0 -48
- 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/types/utility.ts +0 -3
- 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/edit.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
|
-
export abstract class Edit<V, I, E> {
|
|
3
|
-
abstract readonly type: 'Input' | 'Error' | 'Value'
|
|
4
|
-
}
|
|
5
|
-
|
|
6
|
-
export class Input<I> extends Edit<never, I, never> {
|
|
7
|
-
override readonly type = 'Input' as const
|
|
8
|
-
constructor (readonly input: I) {
|
|
9
|
-
super()
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export class Error<I, E> extends Edit<never, I, E> {
|
|
14
|
-
override readonly type = 'Error' as const
|
|
15
|
-
constructor (readonly input: I, readonly error: E) {
|
|
16
|
-
super()
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class Value<V> extends Edit<V, never, never> {
|
|
21
|
-
override readonly type = 'Value' as const
|
|
22
|
-
constructor (readonly value: V) {
|
|
23
|
-
super()
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export type Validate<V, I, E> = (
|
|
28
|
-
input: Input<I> | Error<I, E>
|
|
29
|
-
) => Error<I, E> | Value<V>
|
package/src/equals.ts
DELETED
|
@@ -1,116 +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
|
-
export function strictEqual<A> (a: A, b: A): boolean {
|
|
15
|
-
// eslint-disable-next-line no-self-compare
|
|
16
|
-
return a === b || (a !== a && b !== b)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function deepEqual<A> (a: A, b: A): boolean {
|
|
20
|
-
if (strictEqual(a, b)) return true
|
|
21
|
-
if (a == null || b == null) return false
|
|
22
|
-
const aIsArr = Array.isArray(a)
|
|
23
|
-
const bIsArr = Array.isArray(b)
|
|
24
|
-
|
|
25
|
-
if (aIsArr !== bIsArr) return false
|
|
26
|
-
if (aIsArr) {
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
-
const aArr: never[] = a as any
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
-
const bArr: never[] = b as any
|
|
31
|
-
const aLength = aArr.length
|
|
32
|
-
if (aLength !== bArr.length) return false
|
|
33
|
-
for (let i = 0; i < aLength; i++) {
|
|
34
|
-
if (!deepEqual(aArr[i], bArr[i])) return false
|
|
35
|
-
}
|
|
36
|
-
return true
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
const aIsDate = a instanceof Date
|
|
40
|
-
const bIsDate = b instanceof Date
|
|
41
|
-
if (aIsDate !== bIsDate) return false
|
|
42
|
-
if (aIsDate) {
|
|
43
|
-
const aDate: Date = a as never
|
|
44
|
-
const bDate: Date = b as never
|
|
45
|
-
return +aDate === +bDate
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const aIsSet = a instanceof Set
|
|
49
|
-
const bIsSet = b instanceof Set
|
|
50
|
-
if (aIsSet !== bIsSet) return false
|
|
51
|
-
if (aIsSet) {
|
|
52
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
53
|
-
const aSet: Set<any> = a as never
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
-
const bSet: Set<any> = b as never
|
|
56
|
-
if (aSet.size !== bSet.size) return false
|
|
57
|
-
const it = aSet.keys()
|
|
58
|
-
// eslint-disable-next-line no-constant-condition
|
|
59
|
-
while (true) {
|
|
60
|
-
const curr = it.next()
|
|
61
|
-
if (curr.done ?? false) break
|
|
62
|
-
if (!bSet.has(curr.value)) return false
|
|
63
|
-
}
|
|
64
|
-
return true
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const aIsMap = a instanceof Map
|
|
68
|
-
const bIsMap = b instanceof Map
|
|
69
|
-
if (aIsMap !== bIsMap) return false
|
|
70
|
-
if (aIsMap) {
|
|
71
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
-
const aMap: Map<any, any> = a as never
|
|
73
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
74
|
-
const bMap: Map<any, any> = b as never
|
|
75
|
-
const aMapLength = aMap.size
|
|
76
|
-
if (aMapLength !== bMap.size) return false
|
|
77
|
-
const it = aMap.keys()
|
|
78
|
-
// eslint-disable-next-line no-constant-condition
|
|
79
|
-
while (true) {
|
|
80
|
-
const curr = it.next()
|
|
81
|
-
if (curr.done ?? false) break
|
|
82
|
-
if (!deepEqual(aMap.get(curr.value), bMap.get(curr.value))) return false
|
|
83
|
-
}
|
|
84
|
-
return true
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
const aIsObj = typeof a === 'object'
|
|
88
|
-
const bIsObj = typeof b === 'object'
|
|
89
|
-
if (aIsObj !== bIsObj) return false
|
|
90
|
-
if (aIsObj) {
|
|
91
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
92
|
-
const aObj: any = a
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
-
const bObj: any = b
|
|
95
|
-
const aFields = Object.keys(aObj)
|
|
96
|
-
const bFields = Object.keys(bObj)
|
|
97
|
-
const aLength = aFields.length
|
|
98
|
-
|
|
99
|
-
if (aLength !== bFields.length) return false
|
|
100
|
-
|
|
101
|
-
for (let i = 0; i < aLength; i++) {
|
|
102
|
-
const field = aFields[i]
|
|
103
|
-
if (!Object.prototype.hasOwnProperty.call(bObj, field)) return false
|
|
104
|
-
if (!deepEqual(aObj[field], bObj[field])) return false
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return true
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return false
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
export function looseEqual<T> (a: T, b: T): boolean {
|
|
114
|
-
// eslint-disable-next-line eqeqeq
|
|
115
|
-
return a == b
|
|
116
|
-
}
|
package/src/functions.ts
DELETED
|
@@ -1,108 +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
|
-
import { type Fun1, type FirstArgument } from './types/functions'
|
|
15
|
-
|
|
16
|
-
export function compose<A> (): (a: A) => A
|
|
17
|
-
export function compose<A, B> (f1: (a: A) => B): (a: A) => B
|
|
18
|
-
export function compose<A, B, C> (f1: (a: A) => B, f2: (b: B) => C): (a: A) => C
|
|
19
|
-
export function compose<A, B, C, D> (
|
|
20
|
-
f1: (a: A) => B,
|
|
21
|
-
f2: (b: B) => C,
|
|
22
|
-
f3: (c: C) => D
|
|
23
|
-
): (a: A) => D
|
|
24
|
-
export function compose<A, B, C, D, E> (
|
|
25
|
-
f1: (a: A) => B,
|
|
26
|
-
f2: (b: B) => C,
|
|
27
|
-
f3: (c: C) => D,
|
|
28
|
-
f4: (d: D) => E
|
|
29
|
-
): (a: A) => E
|
|
30
|
-
export function compose<A, B, C, D, E, F> (
|
|
31
|
-
f1: (a: A) => B,
|
|
32
|
-
f2: (b: B) => C,
|
|
33
|
-
f3: (c: C) => D,
|
|
34
|
-
f4: (d: D) => E,
|
|
35
|
-
f5: (e: E) => F
|
|
36
|
-
): (a: A) => F
|
|
37
|
-
export function compose<A, B, C, D, E, F, G> (
|
|
38
|
-
f1: (a: A) => B,
|
|
39
|
-
f2: (b: B) => C,
|
|
40
|
-
f3: (c: C) => D,
|
|
41
|
-
f4: (d: D) => E,
|
|
42
|
-
f5: (e: E) => F,
|
|
43
|
-
f6: (f: F) => G
|
|
44
|
-
): (a: A) => G
|
|
45
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
46
|
-
export function compose<Args extends Array<Fun1<any, any>>> (...args: Args) {
|
|
47
|
-
return (a: FirstArgument<Args[0]>) => args.reduce((acc, f) => f(acc), a)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function identity<T> (v: T): T {
|
|
51
|
-
return v
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
55
|
-
export function curryLeft<A, Rest extends any[], Ret> (
|
|
56
|
-
f: (a: A, ...rest: Rest) => Ret
|
|
57
|
-
) {
|
|
58
|
-
return (a: A) =>
|
|
59
|
-
(...rest: Rest): Ret =>
|
|
60
|
-
f(a, ...rest)
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export function curryRight<A, B, C, D> (
|
|
64
|
-
f: (a: A, b: B, c: C) => D
|
|
65
|
-
): (a: A, b: B) => (c: C) => D
|
|
66
|
-
export function curryRight<A, B, C, D, E> (
|
|
67
|
-
f: (a: A, b: B, c: C, d: D) => E
|
|
68
|
-
): (a: A, b: B, c: C) => (d: D) => E
|
|
69
|
-
export function curryRight<A, B, C, D, E, F> (
|
|
70
|
-
f: (a: A, b: B, c: C, d: D, e: E) => F
|
|
71
|
-
): (a: A, b: B, c: C, d: D) => (e: E) => F
|
|
72
|
-
export function curryRight<A, B, C, D, E, F, G> (
|
|
73
|
-
f: (a: A, b: B, c: C, d: D, e: E, f: F) => G
|
|
74
|
-
): (a: A, b: B, c: C, d: D, e: E) => (f: F) => G
|
|
75
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
76
|
-
export function curryRight<Ret> (f: (...rest: any[]) => Ret) {
|
|
77
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
78
|
-
return (...args: any[]) =>
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
-
(a: any): Ret =>
|
|
81
|
-
f(...args.concat([a]))
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function flip<A, B, C> (f: (a: A, b: B) => C): (b: B, a: A) => C
|
|
85
|
-
export function flip<A, B, C, D> (
|
|
86
|
-
f: (a: A, b: B, c: C) => D
|
|
87
|
-
): (c: C, b: B, a: A) => D
|
|
88
|
-
export function flip<A, B, C, D, E> (
|
|
89
|
-
f: (a: A, b: B, c: C, d: D) => E
|
|
90
|
-
): (d: D, c: C, b: B, a: A) => E
|
|
91
|
-
export function flip<A, B, C, D, E> (
|
|
92
|
-
f: (a: A, b: B, c: C, d: D) => E
|
|
93
|
-
): (d: D, c: C, b: B, a: A) => E
|
|
94
|
-
export function flip<A, B, C, D, E, F> (
|
|
95
|
-
f: (a: A, b: B, c: C, d: D, e: E) => F
|
|
96
|
-
): (e: E, d: D, c: C, b: B, a: A) => F
|
|
97
|
-
export function flip<A, B, C, D, E, F, G> (
|
|
98
|
-
f: (a: A, b: B, c: C, d: D, e: E, f: F) => G
|
|
99
|
-
): (f: F, e: E, d: D, c: C, b: B, a: A) => G
|
|
100
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
-
export function flip<Ret> (f: (...rest: any[]) => Ret) {
|
|
102
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
103
|
-
return (...args: any[]) => f(...args.reverse())
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export function id<T> (v: T): T {
|
|
107
|
-
return v
|
|
108
|
-
}
|
package/src/json.ts
DELETED
|
@@ -1,52 +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
|
-
import { Result } from './result'
|
|
15
|
-
|
|
16
|
-
export type JSONPrimitive = string | boolean | number | null
|
|
17
|
-
// eslint-disable-next-line @typescript-eslint/consistent-indexed-object-style
|
|
18
|
-
export interface JSONObject {
|
|
19
|
-
[k: string]: JSONValue
|
|
20
|
-
}
|
|
21
|
-
export type JSONArray = JSONValue[]
|
|
22
|
-
export type JSONValue = JSONPrimitive | JSONObject | JSONArray
|
|
23
|
-
|
|
24
|
-
export function deserialize (s: string): Result<JSONValue, string> {
|
|
25
|
-
try {
|
|
26
|
-
return Result.success(JSON.parse(s))
|
|
27
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
28
|
-
} catch (e: any) {
|
|
29
|
-
return Result.failure(e.message)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export function makeSerializer (options: {
|
|
34
|
-
minified?: boolean
|
|
35
|
-
whitelist?: string[]
|
|
36
|
-
}) {
|
|
37
|
-
return function serialize (v: JSONValue): string {
|
|
38
|
-
return JSON.stringify(
|
|
39
|
-
v,
|
|
40
|
-
options.whitelist,
|
|
41
|
-
options.minified === false ? undefined : 2
|
|
42
|
-
)
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function serialize (v: JSONValue): string {
|
|
47
|
-
return JSON.stringify(v, null, 2)
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function serializeMinified (v: JSONValue): string {
|
|
51
|
-
return JSON.stringify(v)
|
|
52
|
-
}
|
package/src/match.ts
DELETED
|
@@ -1,88 +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
|
-
import { type Differentiate, type DifferentiateAt } from './types/differentiate'
|
|
15
|
-
import { type AnyKey } from './types/utility'
|
|
16
|
-
import { type ObjectWithField, type ObjectWithPath, type TypeAtPath } from './types/objects'
|
|
17
|
-
|
|
18
|
-
export function matchLiteral<A extends AnyKey, B> (
|
|
19
|
-
input: A,
|
|
20
|
-
matcher: { [k in A]: B }
|
|
21
|
-
): B {
|
|
22
|
-
return matcher[input]
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
-
export function match<F extends AnyKey, T extends ObjectWithField<F, any>, B> (
|
|
27
|
-
input: T,
|
|
28
|
-
field: F,
|
|
29
|
-
matcher: { [k in T[F]]: (arg: Differentiate<F, T, k>) => B }
|
|
30
|
-
): B {
|
|
31
|
-
const k = input[field]
|
|
32
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
-
return matcher[k](input as any)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function deepMatch<
|
|
37
|
-
Path extends AnyKey[],
|
|
38
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
|
-
T extends ObjectWithPath<Path, any>,
|
|
40
|
-
B
|
|
41
|
-
> (
|
|
42
|
-
input: T,
|
|
43
|
-
path: Path,
|
|
44
|
-
matcher: {
|
|
45
|
-
[k in TypeAtPath<Path, T>]: (arg: DifferentiateAt<Path, T, k>) => B
|
|
46
|
-
}
|
|
47
|
-
): B {
|
|
48
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
-
const k = path.reduce((res: any, key) => res[key], input) as TypeAtPath<
|
|
50
|
-
Path,
|
|
51
|
-
T
|
|
52
|
-
>
|
|
53
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
|
-
return matcher[k](input as any)
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function createMatch<F extends AnyKey> (field: F) {
|
|
58
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
59
|
-
return <T extends ObjectWithField<F, any>, B>(
|
|
60
|
-
input: T,
|
|
61
|
-
matcher: { [k in T[F]]: (arg: Differentiate<F, T, k>) => B }
|
|
62
|
-
): B => {
|
|
63
|
-
const k = input[field]
|
|
64
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
65
|
-
return matcher[k](input as any)
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export const createDeepMatch =
|
|
70
|
-
<Path extends AnyKey[]>(...path: Path) =>
|
|
71
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
-
<T extends ObjectWithPath<Path, any>, B>(
|
|
73
|
-
input: T,
|
|
74
|
-
matcher: {
|
|
75
|
-
[k in TypeAtPath<Path, T>]: (arg: DifferentiateAt<Path, T, k>) => B
|
|
76
|
-
}
|
|
77
|
-
): B => {
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
79
|
-
const k = path.reduce((res: any, key) => res[key], input) as TypeAtPath<
|
|
80
|
-
Path,
|
|
81
|
-
T
|
|
82
|
-
>
|
|
83
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
-
return matcher[k](input as any)
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export const matchKind = createMatch('kind')
|
|
88
|
-
export const matchType = createMatch('type')
|
package/src/maybe.ts
DELETED
|
@@ -1,32 +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
|
-
export type Maybe<T> = Just<T> | Nothing
|
|
15
|
-
|
|
16
|
-
export type Nothing = undefined | null
|
|
17
|
-
export type Just<T> = T
|
|
18
|
-
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
20
|
-
export const Maybe = {
|
|
21
|
-
nothing: undefined as Maybe<never>,
|
|
22
|
-
just: <T>(value: T): Maybe<T> => {
|
|
23
|
-
return value
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
isNothing: <T>(maybe: Maybe<T>): maybe is Nothing => {
|
|
27
|
-
return maybe == null
|
|
28
|
-
},
|
|
29
|
-
isJust: <T>(maybe: Maybe<T>): maybe is Just<T> => {
|
|
30
|
-
return maybe != null
|
|
31
|
-
}
|
|
32
|
-
}
|
package/src/memoize.ts
DELETED
package/src/newtype.ts
DELETED
|
@@ -1,59 +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
|
-
* Usage:
|
|
16
|
-
*
|
|
17
|
-
* ```ts
|
|
18
|
-
* export interface Int extends Newtype<
|
|
19
|
-
* number,
|
|
20
|
-
* { readonly Int: unique symbol }
|
|
21
|
-
* > {}
|
|
22
|
-
*
|
|
23
|
-
* export const Int = new class extends NewtypeClass<Int> {
|
|
24
|
-
* isValid(v: number) { return Number.isInteger(v) }
|
|
25
|
-
* }()
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
import { type Maybe } from './maybe'
|
|
30
|
-
|
|
31
|
-
export type TypeOfN<T extends Newtype<unknown, unknown>> = T['_T']
|
|
32
|
-
|
|
33
|
-
export interface Newtype<V, S> {
|
|
34
|
-
readonly _T: V
|
|
35
|
-
readonly _S: S
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export abstract class NewtypeClass<T extends Newtype<unknown, unknown>> {
|
|
39
|
-
abstract isValid (v: TypeOfN<T>): boolean
|
|
40
|
-
unsafeOf (v: TypeOfN<T>): T {
|
|
41
|
-
return v as unknown as T
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
of (v: TypeOfN<T>): Maybe<T> {
|
|
45
|
-
return this.isValid(v) ? (v as unknown as T) : undefined
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
get (v: T): TypeOfN<T> {
|
|
49
|
-
return v as unknown as TypeOfN<T>
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
modify (f: (v: TypeOfN<T>) => TypeOfN<T>): (value: T) => Maybe<T> {
|
|
53
|
-
return (value: T) => this.of(f(this.get(value)))
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
unsafeModify (f: (v: TypeOfN<T>) => TypeOfN<T>): (value: T) => T {
|
|
57
|
-
return (value: T) => this.unsafeOf(f(this.get(value)))
|
|
58
|
-
}
|
|
59
|
-
}
|