@tempots/std 0.9.5 → 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.
Files changed (128) hide show
  1. package/arrays.d.ts +49 -0
  2. package/arrays.js +249 -0
  3. package/async-result.d.ts +37 -0
  4. package/async-result.js +75 -0
  5. package/bigint.d.ts +18 -0
  6. package/bigint.js +110 -0
  7. package/booleans.d.ts +23 -0
  8. package/{src/booleans.ts → booleans.js} +33 -38
  9. package/colors/cmyk.d.ts +21 -0
  10. package/colors/cmyk.js +54 -0
  11. package/colors/convert.d.ts +283 -0
  12. package/colors/convert.js +742 -0
  13. package/colors/hsl.d.ts +24 -0
  14. package/colors/hsl.js +56 -0
  15. package/colors/hsla.d.ts +18 -0
  16. package/colors/hsla.js +35 -0
  17. package/colors/hsluv.d.ts +24 -0
  18. package/colors/hsluv.js +56 -0
  19. package/colors/hsv.d.ts +24 -0
  20. package/colors/hsv.js +54 -0
  21. package/colors/lab.d.ts +24 -0
  22. package/colors/lab.js +54 -0
  23. package/colors/lch.d.ts +19 -0
  24. package/colors/lch.js +44 -0
  25. package/colors/luv.d.ts +19 -0
  26. package/colors/luv.js +45 -0
  27. package/colors/rgb.d.ts +13 -0
  28. package/colors/rgb.js +47 -0
  29. package/colors/rgba.d.ts +12 -0
  30. package/colors/rgba.js +44 -0
  31. package/colors/srgb.d.ts +24 -0
  32. package/colors/srgb.js +51 -0
  33. package/colors/xyz.d.ts +19 -0
  34. package/colors/xyz.js +41 -0
  35. package/edit.d.ts +20 -0
  36. package/edit.js +29 -0
  37. package/equals.d.ts +3 -0
  38. package/equals.js +122 -0
  39. package/functions.d.ts +20 -0
  40. package/functions.js +38 -0
  41. package/json.d.ts +14 -0
  42. package/json.js +33 -0
  43. package/match.d.ts +16 -0
  44. package/match.js +45 -0
  45. package/maybe.d.ts +9 -0
  46. package/{src/maybe.ts → maybe.js} +11 -18
  47. package/memoize.d.ts +1 -0
  48. package/memoize.js +9 -0
  49. package/newtype.d.ts +28 -0
  50. package/newtype.js +29 -0
  51. package/numbers.d.ts +104 -0
  52. package/numbers.js +183 -0
  53. package/objects.d.ts +9 -0
  54. package/objects.js +33 -0
  55. package/ord.d.ts +19 -0
  56. package/ord.js +73 -0
  57. package/package.json +3 -7
  58. package/reg-exps.d.ts +10 -0
  59. package/{src/reg-exps.ts → reg-exps.js} +19 -24
  60. package/result.d.ts +31 -0
  61. package/result.js +95 -0
  62. package/strings.d.ts +314 -0
  63. package/strings.js +685 -0
  64. package/types/assert.d.ts +12 -0
  65. package/types/assert.js +13 -0
  66. package/types/differentiate.d.ts +13 -0
  67. package/types/differentiate.js +14 -0
  68. package/types/functions.d.ts +22 -0
  69. package/types/functions.js +13 -0
  70. package/types/generic.d.ts +9 -0
  71. package/types/generic.js +13 -0
  72. package/types/objects.d.ts +50 -0
  73. package/types/objects.js +13 -0
  74. package/types/tuples.d.ts +44 -0
  75. package/types/tuples.js +24 -0
  76. package/{src/types/utility.ts → types/utility.d.ts} +2 -3
  77. package/types/utility.js +1 -0
  78. package/uuid.d.ts +13 -0
  79. package/uuid.js +56 -0
  80. package/validation.d.ts +23 -0
  81. package/validation.js +44 -0
  82. package/src/arrays.ts +0 -296
  83. package/src/async-result.ts +0 -103
  84. package/src/bigint.ts +0 -111
  85. package/src/colors/cmyk.ts +0 -84
  86. package/src/colors/convert.ts +0 -1093
  87. package/src/colors/hsl.ts +0 -73
  88. package/src/colors/hsla.ts +0 -45
  89. package/src/colors/hsluv.ts +0 -73
  90. package/src/colors/hsv.ts +0 -75
  91. package/src/colors/lab.ts +0 -69
  92. package/src/colors/lch.ts +0 -53
  93. package/src/colors/luv.ts +0 -56
  94. package/src/colors/rgb.ts +0 -55
  95. package/src/colors/rgba.ts +0 -53
  96. package/src/colors/srgb.ts +0 -72
  97. package/src/colors/xyz.ts +0 -52
  98. package/src/edit.ts +0 -29
  99. package/src/equals.ts +0 -116
  100. package/src/functions.ts +0 -108
  101. package/src/json.ts +0 -52
  102. package/src/match.ts +0 -88
  103. package/src/memoize.ts +0 -9
  104. package/src/newtype.ts +0 -59
  105. package/src/numbers.ts +0 -222
  106. package/src/objects.ts +0 -47
  107. package/src/ord.ts +0 -79
  108. package/src/result.ts +0 -140
  109. package/src/strings.ts +0 -768
  110. package/src/types/assert.ts +0 -96
  111. package/src/types/differentiate.ts +0 -89
  112. package/src/types/functions.ts +0 -114
  113. package/src/types/generic.ts +0 -42
  114. package/src/types/objects.ts +0 -212
  115. package/src/types/tuples.ts +0 -244
  116. package/src/uuid.ts +0 -61
  117. package/src/validation.ts +0 -69
  118. package/test/arrays.spec.ts +0 -410
  119. package/test/colors.spec.ts +0 -406
  120. package/test/commmon.ts +0 -9
  121. package/test/equals.spec.ts +0 -165
  122. package/test/functions.spec.ts +0 -9
  123. package/test/index.d.ts +0 -20
  124. package/test/objects.spec.ts +0 -22
  125. package/test/reg-exps.spec.ts +0 -33
  126. package/test/strings.spec.ts +0 -333
  127. package/test/uuid.spec.ts +0 -35
  128. package/tsconfig.json +0 -19
package/colors/srgb.js ADDED
@@ -0,0 +1,51 @@
1
+ import { clamp, nearEquals } from '../numbers';
2
+ const TOLLERANCE = 0.001;
3
+ export class SRGB {
4
+ static fromString(s) {
5
+ const m = s.match(/^rgb\((\d+(?:\.\d+)?)%, ?(\d+(?:\.\d+)?)%, ?(\d+(?:\.\d+)?)%\)$/);
6
+ if (m != null) {
7
+ const [, red, blue, green] = m;
8
+ return new SRGB(parseFloat(red), parseFloat(blue), parseFloat(green));
9
+ }
10
+ else {
11
+ throw new Error(`Invalid RGB string: ${s}`);
12
+ }
13
+ }
14
+ static ofChannels([red, blue, green]) {
15
+ return new SRGB(clamp(red * 100, 0, 100), clamp(blue * 100, 0, 100), clamp(green * 100, 0, 100));
16
+ }
17
+ red;
18
+ blue;
19
+ green;
20
+ /**
21
+ *
22
+ * @param red Percentage (0-100)
23
+ * @param blue Percentage (0-100)
24
+ * @param green Percentage (0-100)
25
+ */
26
+ constructor(red, blue, green) {
27
+ this.red = clamp(red, 0, 100);
28
+ this.blue = clamp(blue, 0, 100);
29
+ this.green = clamp(green, 0, 100);
30
+ }
31
+ withRed(red) {
32
+ return new SRGB(red, this.blue, this.green);
33
+ }
34
+ withBlue(blue) {
35
+ return new SRGB(this.red, blue, this.green);
36
+ }
37
+ withGreen(green) {
38
+ return new SRGB(this.red, this.blue, green);
39
+ }
40
+ toString() {
41
+ return `rgb(${this.red}%, ${this.blue}%, ${this.green}%)`;
42
+ }
43
+ toChannels() {
44
+ return [this.red / 100, this.blue / 100, this.green / 100];
45
+ }
46
+ equals(other, tollerance = TOLLERANCE) {
47
+ return (nearEquals(this.red, other.red, tollerance) &&
48
+ nearEquals(this.blue, other.blue, tollerance) &&
49
+ nearEquals(this.green, other.green, tollerance));
50
+ }
51
+ }
@@ -0,0 +1,19 @@
1
+ export declare class XYZ {
2
+ static ofChannels([x, y, z]: [x: number, y: number, z: number]): XYZ;
3
+ readonly x: number;
4
+ readonly y: number;
5
+ readonly z: number;
6
+ /**
7
+ *
8
+ * @param x
9
+ * @param y 100 is the brightest white
10
+ * @param z
11
+ */
12
+ constructor(x: number, y: number, z: number);
13
+ withX(x: number): XYZ;
14
+ withY(y: number): XYZ;
15
+ withZ(z: number): XYZ;
16
+ toString(): string;
17
+ toChannels(): [number, number, number];
18
+ equals(other: XYZ, tollerance?: number): boolean;
19
+ }
package/colors/xyz.js ADDED
@@ -0,0 +1,41 @@
1
+ import { nearEquals } from '../numbers';
2
+ const TOLLERANCE = 0.0001;
3
+ export class XYZ {
4
+ static ofChannels([x, y, z]) {
5
+ return new XYZ(x, y, z);
6
+ }
7
+ x;
8
+ y;
9
+ z;
10
+ /**
11
+ *
12
+ * @param x
13
+ * @param y 100 is the brightest white
14
+ * @param z
15
+ */
16
+ constructor(x, y, z) {
17
+ this.x = x;
18
+ this.y = y;
19
+ this.z = z;
20
+ }
21
+ withX(x) {
22
+ return new XYZ(x, this.y, this.z);
23
+ }
24
+ withY(y) {
25
+ return new XYZ(this.x, y, this.z);
26
+ }
27
+ withZ(z) {
28
+ return new XYZ(this.x, this.y, z);
29
+ }
30
+ toString() {
31
+ return `XYZ(${this.x}, ${this.y}, ${this.z})`;
32
+ }
33
+ toChannels() {
34
+ return [this.x, this.y, this.z];
35
+ }
36
+ equals(other, tollerance = TOLLERANCE) {
37
+ return (nearEquals(this.x, other.x, tollerance) &&
38
+ nearEquals(this.y, other.y, tollerance) &&
39
+ nearEquals(this.z, other.z, tollerance));
40
+ }
41
+ }
package/edit.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export declare abstract class Edit<V, I, E> {
2
+ abstract readonly type: 'Input' | 'Error' | 'Value';
3
+ }
4
+ export declare class Input<I> extends Edit<never, I, never> {
5
+ readonly input: I;
6
+ readonly type: "Input";
7
+ constructor(input: I);
8
+ }
9
+ export declare class Error<I, E> extends Edit<never, I, E> {
10
+ readonly input: I;
11
+ readonly error: E;
12
+ readonly type: "Error";
13
+ constructor(input: I, error: E);
14
+ }
15
+ export declare class Value<V> extends Edit<V, never, never> {
16
+ readonly value: V;
17
+ readonly type: "Value";
18
+ constructor(value: V);
19
+ }
20
+ export type Validate<V, I, E> = (input: Input<I> | Error<I, E>) => Error<I, E> | Value<V>;
package/edit.js ADDED
@@ -0,0 +1,29 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
+ export class Edit {
3
+ }
4
+ export class Input extends Edit {
5
+ input;
6
+ type = 'Input';
7
+ constructor(input) {
8
+ super();
9
+ this.input = input;
10
+ }
11
+ }
12
+ export class Error extends Edit {
13
+ input;
14
+ error;
15
+ type = 'Error';
16
+ constructor(input, error) {
17
+ super();
18
+ this.input = input;
19
+ this.error = error;
20
+ }
21
+ }
22
+ export class Value extends Edit {
23
+ value;
24
+ type = 'Value';
25
+ constructor(value) {
26
+ super();
27
+ this.value = value;
28
+ }
29
+ }
package/equals.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export declare function strictEqual<A>(a: A, b: A): boolean;
2
+ export declare function deepEqual<A>(a: A, b: A): boolean;
3
+ export declare function looseEqual<T>(a: T, b: T): boolean;
package/equals.js ADDED
@@ -0,0 +1,122 @@
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
+ export function strictEqual(a, b) {
14
+ // eslint-disable-next-line no-self-compare
15
+ return a === b || (a !== a && b !== b);
16
+ }
17
+ export function deepEqual(a, b) {
18
+ if (strictEqual(a, b))
19
+ return true;
20
+ if (a == null || b == null)
21
+ return false;
22
+ const aIsArr = Array.isArray(a);
23
+ const bIsArr = Array.isArray(b);
24
+ if (aIsArr !== bIsArr)
25
+ return false;
26
+ if (aIsArr) {
27
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
+ const aArr = a;
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
+ const bArr = b;
31
+ const aLength = aArr.length;
32
+ if (aLength !== bArr.length)
33
+ return false;
34
+ for (let i = 0; i < aLength; i++) {
35
+ if (!deepEqual(aArr[i], bArr[i]))
36
+ return false;
37
+ }
38
+ return true;
39
+ }
40
+ const aIsDate = a instanceof Date;
41
+ const bIsDate = b instanceof Date;
42
+ if (aIsDate !== bIsDate)
43
+ return false;
44
+ if (aIsDate) {
45
+ const aDate = a;
46
+ const bDate = b;
47
+ return +aDate === +bDate;
48
+ }
49
+ const aIsSet = a instanceof Set;
50
+ const bIsSet = b instanceof Set;
51
+ if (aIsSet !== bIsSet)
52
+ return false;
53
+ if (aIsSet) {
54
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
+ const aSet = a;
56
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
+ const bSet = b;
58
+ if (aSet.size !== bSet.size)
59
+ return false;
60
+ const it = aSet.keys();
61
+ // eslint-disable-next-line no-constant-condition
62
+ while (true) {
63
+ const curr = it.next();
64
+ if (curr.done ?? false)
65
+ break;
66
+ if (!bSet.has(curr.value))
67
+ return false;
68
+ }
69
+ return true;
70
+ }
71
+ const aIsMap = a instanceof Map;
72
+ const bIsMap = b instanceof Map;
73
+ if (aIsMap !== bIsMap)
74
+ return false;
75
+ if (aIsMap) {
76
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
77
+ const aMap = a;
78
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
+ const bMap = b;
80
+ const aMapLength = aMap.size;
81
+ if (aMapLength !== bMap.size)
82
+ return false;
83
+ const it = aMap.keys();
84
+ // eslint-disable-next-line no-constant-condition
85
+ while (true) {
86
+ const curr = it.next();
87
+ if (curr.done ?? false)
88
+ break;
89
+ if (!deepEqual(aMap.get(curr.value), bMap.get(curr.value)))
90
+ return false;
91
+ }
92
+ return true;
93
+ }
94
+ const aIsObj = typeof a === 'object';
95
+ const bIsObj = typeof b === 'object';
96
+ if (aIsObj !== bIsObj)
97
+ return false;
98
+ if (aIsObj) {
99
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
100
+ const aObj = a;
101
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
102
+ const bObj = b;
103
+ const aFields = Object.keys(aObj);
104
+ const bFields = Object.keys(bObj);
105
+ const aLength = aFields.length;
106
+ if (aLength !== bFields.length)
107
+ return false;
108
+ for (let i = 0; i < aLength; i++) {
109
+ const field = aFields[i];
110
+ if (!Object.prototype.hasOwnProperty.call(bObj, field))
111
+ return false;
112
+ if (!deepEqual(aObj[field], bObj[field]))
113
+ return false;
114
+ }
115
+ return true;
116
+ }
117
+ return false;
118
+ }
119
+ export function looseEqual(a, b) {
120
+ // eslint-disable-next-line eqeqeq
121
+ return a == b;
122
+ }
package/functions.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ export declare function compose<A>(): (a: A) => A;
2
+ export declare function compose<A, B>(f1: (a: A) => B): (a: A) => B;
3
+ export declare function compose<A, B, C>(f1: (a: A) => B, f2: (b: B) => C): (a: A) => C;
4
+ export declare function compose<A, B, C, D>(f1: (a: A) => B, f2: (b: B) => C, f3: (c: C) => D): (a: A) => D;
5
+ export declare function compose<A, B, C, D, E>(f1: (a: A) => B, f2: (b: B) => C, f3: (c: C) => D, f4: (d: D) => E): (a: A) => E;
6
+ export declare function compose<A, B, C, D, E, F>(f1: (a: A) => B, f2: (b: B) => C, f3: (c: C) => D, f4: (d: D) => E, f5: (e: E) => F): (a: A) => F;
7
+ export declare function compose<A, B, C, D, E, F, G>(f1: (a: A) => B, f2: (b: B) => C, f3: (c: C) => D, f4: (d: D) => E, f5: (e: E) => F, f6: (f: F) => G): (a: A) => G;
8
+ export declare function identity<T>(v: T): T;
9
+ export declare function curryLeft<A, Rest extends any[], Ret>(f: (a: A, ...rest: Rest) => Ret): (a: A) => (...rest: Rest) => Ret;
10
+ export declare function curryRight<A, B, C, D>(f: (a: A, b: B, c: C) => D): (a: A, b: B) => (c: C) => D;
11
+ export declare function curryRight<A, B, C, D, E>(f: (a: A, b: B, c: C, d: D) => E): (a: A, b: B, c: C) => (d: D) => E;
12
+ export declare function curryRight<A, B, C, D, E, F>(f: (a: A, b: B, c: C, d: D, e: E) => F): (a: A, b: B, c: C, d: D) => (e: E) => F;
13
+ export declare function curryRight<A, B, C, D, E, F, G>(f: (a: A, b: B, c: C, d: D, e: E, f: F) => G): (a: A, b: B, c: C, d: D, e: E) => (f: F) => G;
14
+ export declare function flip<A, B, C>(f: (a: A, b: B) => C): (b: B, a: A) => C;
15
+ export declare function flip<A, B, C, D>(f: (a: A, b: B, c: C) => D): (c: C, b: B, a: A) => D;
16
+ export declare function flip<A, B, C, D, E>(f: (a: A, b: B, c: C, d: D) => E): (d: D, c: C, b: B, a: A) => E;
17
+ export declare function flip<A, B, C, D, E>(f: (a: A, b: B, c: C, d: D) => E): (d: D, c: C, b: B, a: A) => E;
18
+ export declare function flip<A, B, C, D, E, F>(f: (a: A, b: B, c: C, d: D, e: E) => F): (e: E, d: D, c: C, b: B, a: A) => F;
19
+ export declare function flip<A, B, C, D, E, F, G>(f: (a: A, b: B, c: C, d: D, e: E, f: F) => G): (f: F, e: E, d: D, c: C, b: B, a: A) => G;
20
+ export declare function id<T>(v: T): T;
package/functions.js ADDED
@@ -0,0 +1,38 @@
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
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ export function compose(...args) {
15
+ return (a) => args.reduce((acc, f) => f(acc), a);
16
+ }
17
+ export function identity(v) {
18
+ return v;
19
+ }
20
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ export function curryLeft(f) {
22
+ return (a) => (...rest) => f(a, ...rest);
23
+ }
24
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
25
+ export function curryRight(f) {
26
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
+ return (...args) =>
28
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
+ (a) => f(...args.concat([a]));
30
+ }
31
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
32
+ export function flip(f) {
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ return (...args) => f(...args.reverse());
35
+ }
36
+ export function id(v) {
37
+ return v;
38
+ }
package/json.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ import { Result } from './result';
2
+ export type JSONPrimitive = string | boolean | number | null;
3
+ export interface JSONObject {
4
+ [k: string]: JSONValue;
5
+ }
6
+ export type JSONArray = JSONValue[];
7
+ export type JSONValue = JSONPrimitive | JSONObject | JSONArray;
8
+ export declare function deserialize(s: string): Result<JSONValue, string>;
9
+ export declare function makeSerializer(options: {
10
+ minified?: boolean;
11
+ whitelist?: string[];
12
+ }): (v: JSONValue) => string;
13
+ export declare function serialize(v: JSONValue): string;
14
+ export declare function serializeMinified(v: JSONValue): string;
package/json.js ADDED
@@ -0,0 +1,33 @@
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
+ import { Result } from './result';
14
+ export function deserialize(s) {
15
+ try {
16
+ return Result.success(JSON.parse(s));
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ }
19
+ catch (e) {
20
+ return Result.failure(e.message);
21
+ }
22
+ }
23
+ export function makeSerializer(options) {
24
+ return function serialize(v) {
25
+ return JSON.stringify(v, options.whitelist, options.minified === false ? undefined : 2);
26
+ };
27
+ }
28
+ export function serialize(v) {
29
+ return JSON.stringify(v, null, 2);
30
+ }
31
+ export function serializeMinified(v) {
32
+ return JSON.stringify(v);
33
+ }
package/match.d.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { type Differentiate, type DifferentiateAt } from './types/differentiate';
2
+ import { type AnyKey } from './types/utility';
3
+ import { type ObjectWithField, type ObjectWithPath, type TypeAtPath } from './types/objects';
4
+ export declare function matchLiteral<A extends AnyKey, B>(input: A, matcher: {
5
+ [k in A]: B;
6
+ }): B;
7
+ export declare function match<F extends AnyKey, T extends ObjectWithField<F, any>, B>(input: T, field: F, matcher: {
8
+ [k in T[F]]: (arg: Differentiate<F, T, k>) => B;
9
+ }): B;
10
+ export declare function deepMatch<Path extends AnyKey[], T extends ObjectWithPath<Path, any>, B>(input: T, path: Path, matcher: {
11
+ [k in TypeAtPath<Path, T>]: (arg: DifferentiateAt<Path, T, k>) => B;
12
+ }): B;
13
+ export declare function createMatch<F extends AnyKey>(field: F): <T extends ObjectWithField<F, any>, B>(input: T, matcher: { [k in T[F]]: (arg: Differentiate<F, T, k>) => B; }) => B;
14
+ export declare const createDeepMatch: <Path extends AnyKey[]>(...path: Path) => <T extends ObjectWithPath<Path, any>, B>(input: T, matcher: { [k in TypeAtPath<Path, T>]: (arg: DifferentiateAt<Path, T, k>) => B; }) => B;
15
+ export declare const matchKind: <T extends ObjectWithField<"kind", any>, B>(input: T, matcher: { [k in T["kind"]]: (arg: Differentiate<"kind", T, k>) => B; }) => B;
16
+ export declare const matchType: <T extends ObjectWithField<"type", any>, B>(input: T, matcher: { [k in T["type"]]: (arg: Differentiate<"type", T, k>) => B; }) => B;
package/match.js ADDED
@@ -0,0 +1,45 @@
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
+ export function matchLiteral(input, matcher) {
14
+ return matcher[input];
15
+ }
16
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
17
+ export function match(input, field, matcher) {
18
+ const k = input[field];
19
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
+ return matcher[k](input);
21
+ }
22
+ export function deepMatch(input, path, matcher) {
23
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
+ const k = path.reduce((res, key) => res[key], input);
25
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
26
+ return matcher[k](input);
27
+ }
28
+ export function createMatch(field) {
29
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
+ return (input, matcher) => {
31
+ const k = input[field];
32
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
+ return matcher[k](input);
34
+ };
35
+ }
36
+ export const createDeepMatch = (...path) =>
37
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
38
+ (input, matcher) => {
39
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
+ const k = path.reduce((res, key) => res[key], input);
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ return matcher[k](input);
43
+ };
44
+ export const matchKind = createMatch('kind');
45
+ export const matchType = createMatch('type');
package/maybe.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type Maybe<T> = Just<T> | Nothing;
2
+ export type Nothing = undefined | null;
3
+ export type Just<T> = T;
4
+ export declare const Maybe: {
5
+ nothing: Maybe<never>;
6
+ just: <T>(value: T) => Maybe<T>;
7
+ isNothing: <T_1>(maybe: Maybe<T_1>) => maybe is Nothing;
8
+ isJust: <T_2>(maybe: Maybe<T_2>) => maybe is T_2;
9
+ };
@@ -10,23 +10,16 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
10
  See the License for the specific language governing permissions and
11
11
  limitations under the License.
12
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
13
  // eslint-disable-next-line @typescript-eslint/no-redeclare
20
14
  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
- }
15
+ nothing: undefined,
16
+ just: (value) => {
17
+ return value;
18
+ },
19
+ isNothing: (maybe) => {
20
+ return maybe == null;
21
+ },
22
+ isJust: (maybe) => {
23
+ return maybe != null;
24
+ }
25
+ };
package/memoize.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function memoize<T>(f: () => T): () => T;
package/memoize.js ADDED
@@ -0,0 +1,9 @@
1
+ export function memoize(f) {
2
+ let value;
3
+ return () => {
4
+ if (value === undefined) {
5
+ value = f();
6
+ }
7
+ return value;
8
+ };
9
+ }
package/newtype.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Usage:
3
+ *
4
+ * ```ts
5
+ * export interface Int extends Newtype<
6
+ * number,
7
+ * { readonly Int: unique symbol }
8
+ * > {}
9
+ *
10
+ * export const Int = new class extends NewtypeClass<Int> {
11
+ * isValid(v: number) { return Number.isInteger(v) }
12
+ * }()
13
+ * ```
14
+ */
15
+ import { type Maybe } from './maybe';
16
+ export type TypeOfN<T extends Newtype<unknown, unknown>> = T['_T'];
17
+ export interface Newtype<V, S> {
18
+ readonly _T: V;
19
+ readonly _S: S;
20
+ }
21
+ export declare abstract class NewtypeClass<T extends Newtype<unknown, unknown>> {
22
+ abstract isValid(v: TypeOfN<T>): boolean;
23
+ unsafeOf(v: TypeOfN<T>): T;
24
+ of(v: TypeOfN<T>): Maybe<T>;
25
+ get(v: T): TypeOfN<T>;
26
+ modify(f: (v: TypeOfN<T>) => TypeOfN<T>): (value: T) => Maybe<T>;
27
+ unsafeModify(f: (v: TypeOfN<T>) => TypeOfN<T>): (value: T) => T;
28
+ }
package/newtype.js ADDED
@@ -0,0 +1,29 @@
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
+ export class NewtypeClass {
14
+ unsafeOf(v) {
15
+ return v;
16
+ }
17
+ of(v) {
18
+ return this.isValid(v) ? v : undefined;
19
+ }
20
+ get(v) {
21
+ return v;
22
+ }
23
+ modify(f) {
24
+ return (value) => this.of(f(this.get(value)));
25
+ }
26
+ unsafeModify(f) {
27
+ return (value) => this.unsafeOf(f(this.get(value)));
28
+ }
29
+ }