@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.
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 +2 -2
  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
@@ -0,0 +1,13 @@
1
+ /**
2
+ *
3
+ */
4
+ import { type AnyKey } from './utility';
5
+ import { type ObjectWithField } from './objects';
6
+ import { type Tail } from './tuples';
7
+ export type Differentiate<Field extends AnyKey, State extends ObjectWithField<Field, any>, ExpectedType extends State[Field]> = State extends ObjectWithField<Field, ExpectedType> ? State : never;
8
+ export type DifferentiateByKind<State extends {
9
+ kind: any;
10
+ }, K extends State['kind']> = Differentiate<'kind', State, K>;
11
+ export type DifferentiateAt<Path extends AnyKey[], State, ExpectedType> = Path extends [] ? State : Path extends [infer T] ? T extends keyof State ? ExpectedType extends State[T] ? Differentiate<T, State, ExpectedType> : never : never : Path extends [infer K, ...any[]] ? K extends keyof State ? Tail<Path> extends infer Rest ? Rest extends AnyKey[] ? State & {
12
+ [k in K]: DifferentiateAt<Rest, State[k], ExpectedType>;
13
+ } : never : never : never : never;
@@ -0,0 +1,14 @@
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
+ export {};
@@ -0,0 +1,22 @@
1
+ import { type Tail } from './tuples';
2
+ export type Equality<T> = (a: T, b: T) => boolean;
3
+ export type Effect<T> = (value: T) => void;
4
+ export type Proc = () => void;
5
+ export type Lazy<T> = () => T;
6
+ export type Fun = (...args: any[]) => any;
7
+ export type Fun1<A, T> = (a1: A) => T;
8
+ export type Fun2<A, B, T> = (a1: A, a2: B) => T;
9
+ export type Fun3<A, B, C, T> = (a1: A, a2: B, a3: C) => T;
10
+ export type Fun4<A, B, C, D, T> = (a1: A, a2: B, a3: C, a4: D) => T;
11
+ export type Fun5<A, B, C, D, E, T> = (a1: A, a2: B, a3: C, a4: D, a5: E) => T;
12
+ export type Fun6<A, B, C, D, E, F, T> = (a1: A, a2: B, a3: C, a4: D, a5: E, a6: F) => T;
13
+ export type AnyFunction = Fun | Function;
14
+ export type FirstArgument<F extends AnyFunction> = F extends (a: infer A) => any ? unknown extends A ? never : A : never;
15
+ export type OnlyIfDoesNotReturnNever<F extends Fun> = F extends Fun1<any, never> ? never : F;
16
+ type CheckFunctionsChain<In, Args extends Array<Fun1<any, any>>> = {
17
+ empty: (i: In) => In;
18
+ one: FirstArgument<Args[0]> extends In ? Args[0] : never;
19
+ n: FirstArgument<Args[0]> extends In ? (arg: FirstArgument<Args[0]>) => ReturnType<CheckFunctionsChain<ReturnType<Args[0]>, Tail<Args>>> : never;
20
+ }[Args extends [] ? 'empty' : Args extends [any] ? 'one' : 'n'];
21
+ export type FunctionsChain<In, Args extends Fun[]> = OnlyIfDoesNotReturnNever<CheckFunctionsChain<In, Args>>;
22
+ export {};
@@ -0,0 +1,13 @@
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 {};
@@ -0,0 +1,9 @@
1
+ /**
2
+ *
3
+ */
4
+ import { type Equals, type NotEquals } from './assert';
5
+ export type Pointer<T> = () => T;
6
+ export type DeRef<T extends () => any> = T extends () => infer Ret ? Ret : never;
7
+ export type WhenEquals<X, Y, A = X, B = never> = Equals<X, Y> extends true ? A : B;
8
+ export type WhenNotEquals<X, Y, A = X, B = never> = NotEquals<X, Y> extends true ? A : B;
9
+ export type Cast<A, B> = A extends B ? A : B;
@@ -0,0 +1,13 @@
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 {};
@@ -0,0 +1,50 @@
1
+ /**
2
+ *
3
+ */
4
+ import { type AnyKey } from './utility';
5
+ import { type Tail } from './tuples';
6
+ import { type AnyFunction } from './functions';
7
+ import { type WhenEquals } from './generic';
8
+ export type ObjectWithField<F extends AnyKey, V> = {
9
+ [_ in F]: V;
10
+ };
11
+ export type ObjectWithPath<Path extends AnyKey[], V> = Path extends [] ? {} : Path extends [infer T] ? T extends AnyKey ? {
12
+ [_ in T]: V;
13
+ } : never : Path extends [infer K, ...any[]] ? K extends AnyKey ? Tail<Path> extends infer Rest ? Rest extends AnyKey[] ? {
14
+ [_ in K]: ObjectWithPath<Rest, V>;
15
+ } : never : never : never : never;
16
+ export type TypeAtPath<Path extends AnyKey[], O> = {
17
+ next: Path extends [infer K, ...any[]] ? K extends AnyKey ? Tail<Path> extends infer Rest ? O extends Record<AnyKey, any> ? Rest extends AnyKey[] ? TypeAtPath<Rest, O[K]> : never : never : never : never : never;
18
+ empty: O;
19
+ done: O extends Record<AnyKey, any> ? Path extends [infer K] ? K extends AnyKey ? O[K] : never : never : never;
20
+ }[Path extends [] ? 'empty' : Path extends [any] ? 'done' : 'next'];
21
+ export type WritableKeys<T> = {
22
+ [K in keyof T]-?: WhenEquals<{
23
+ [Q in K]: T[K];
24
+ }, {
25
+ -readonly [Q in K]: T[K];
26
+ }, K>;
27
+ }[keyof T];
28
+ export type ReadonlyKeys<T> = {
29
+ [P in keyof T]-?: WhenEquals<{
30
+ [Q in P]: T[P];
31
+ }, {
32
+ -readonly [Q in P]: T[P];
33
+ }, never, P>;
34
+ }[keyof T];
35
+ export type Id<T> = {} & {
36
+ [P in keyof T]: T[P];
37
+ };
38
+ export type KeysWithFieldType<T, Condition> = {
39
+ [K in keyof T]: WhenEquals<T[K], Condition, K>;
40
+ }[keyof T];
41
+ export type KeysWithoutFieldType<T, Condition> = {
42
+ [K in keyof T]: WhenEquals<T[K], Condition, never, K>;
43
+ }[keyof T];
44
+ export type RemoveNullableFromFields<T> = {
45
+ [K in keyof T]: NonNullable<T[K]>;
46
+ };
47
+ export type WritableFields<T> = Pick<T, WritableKeys<T>>;
48
+ export type ReadonlyFields<T> = Pick<T, ReadonlyKeys<T>>;
49
+ export type ExcludeFunctionFields<T> = Pick<T, KeysWithoutFieldType<T, AnyFunction>>;
50
+ export type Merge<A, B> = Id<A & B>;
@@ -0,0 +1,13 @@
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 {};
@@ -0,0 +1,44 @@
1
+ /**
2
+ *
3
+ */
4
+ import { type Pointer, type DeRef } from './generic';
5
+ export type Tail<T extends any[]> = ((...args: T) => void) extends (_: any, ...rest: infer Rest) => void ? Rest : never;
6
+ type DropNImpl<N extends number, T extends any[], I extends any[]> = {
7
+ return: T;
8
+ next: DropNImpl<N, Tail<T>, Prepend<any, I>>;
9
+ }[Length<I> extends N ? 'return' : 'next'];
10
+ export type DropN<N extends number, T extends any[]> = DropNImpl<N, T, []>;
11
+ type ReverseImpl<T extends any[], R extends any[], I extends any[]> = {
12
+ return: DeRef<Pointer<R>>;
13
+ next: ReverseImpl<T, Prepend<T[Length<I>], R>, Prepend<any, I>>;
14
+ }[Length<I> extends Length<T> ? 'return' : 'next'];
15
+ export type Reverse<T extends any[]> = ReverseImpl<T, [], []>;
16
+ export type Head<T extends any[]> = T extends [infer H, ...any[]] ? H : never;
17
+ export type Last<T extends any[]> = {
18
+ n: Last<Tail<T>>;
19
+ one: T extends [infer H] ? H : never;
20
+ empty: never;
21
+ }[T extends [] ? 'empty' : T extends [any] ? 'one' : 'n'];
22
+ export type TupleToUnion<T extends any[]> = T[number];
23
+ export type Prepend<Insert, Tail extends any[]> = [Insert, ...Tail];
24
+ export type Append<Tuple extends any[], Element> = [...Tuple, Element];
25
+ export type Length<T extends any[]> = T['length'];
26
+ export type LoseLastImpl<A extends any[], B extends any[]> = {
27
+ empty: A;
28
+ next: LoseLastImpl<Prepend<B[0], A>, Tail<B>>;
29
+ }[B extends [] ? 'empty' : B extends [any] ? 'empty' : 'next'];
30
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
31
+ type LastOf<T> = UnionToIntersection<T extends any ? () => T : never> extends () => infer R ? R : never;
32
+ export type Push<T extends any[], V> = [...T, V];
33
+ export type UnionToTuple<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple1<Exclude<T, L>>, L>;
34
+ type UnionToTuple1<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple2<Exclude<T, L>>, L>;
35
+ type UnionToTuple2<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple3<Exclude<T, L>>, L>;
36
+ type UnionToTuple3<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple4<Exclude<T, L>>, L>;
37
+ type UnionToTuple4<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple5<Exclude<T, L>>, L>;
38
+ type UnionToTuple5<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple6<Exclude<T, L>>, L>;
39
+ type UnionToTuple6<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple7<Exclude<T, L>>, L>;
40
+ type UnionToTuple7<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple8<Exclude<T, L>>, L>;
41
+ type UnionToTuple8<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTuple9<Exclude<T, L>>, L>;
42
+ type UnionToTuple9<T, L = LastOf<T>, N = [T] extends [never] ? true : false> = true extends N ? [] : Push<UnionToTupleX<Exclude<T, L>>, L>;
43
+ type UnionToTupleX<T> = never;
44
+ export {};
@@ -0,0 +1,24 @@
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
+ export {};
15
+ // | _Concat_0_0
16
+ // | _Concat_0_1
17
+ // | _Concat_1_0
18
+ // | _Concat_2_3
19
+ // | _Pop_1
20
+ // | _Pop_2
21
+ // | _LoseLast_1
22
+ // | _LoseLast_2
23
+ // | _LoseLast_3
24
+ // | _LoseLast_4
@@ -1,3 +1,2 @@
1
- export type AnyKey = string | number | symbol
2
-
3
- export type Primitive = string | boolean | number | null | undefined
1
+ export type AnyKey = string | number | symbol;
2
+ export type Primitive = string | boolean | number | null | undefined;
@@ -0,0 +1 @@
1
+ export {};
package/uuid.d.ts ADDED
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Helper functions to generate and validate [UUID](http://en.wikipedia.org/wiki/Universally_unique_identifier)
3
+ * strings (version 4).
4
+ */
5
+ import { type Newtype, NewtypeClass } from './newtype';
6
+ /**
7
+ * `Uuid.create()` returns a string value representing a random UUID string.
8
+ */
9
+ export declare function create(): UUID;
10
+ export type UUID = Newtype<string, {
11
+ readonly UUID: unique symbol;
12
+ }>;
13
+ export declare const UUID: NewtypeClass<UUID>;
package/uuid.js ADDED
@@ -0,0 +1,56 @@
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
+ * Helper functions to generate and validate [UUID](http://en.wikipedia.org/wiki/Universally_unique_identifier)
15
+ * strings (version 4).
16
+ */
17
+ import { NewtypeClass } from './newtype';
18
+ const random = (max) => Math.floor(Math.random() * max);
19
+ const srandom = () => '0123456789abcdef'.charAt(random(0x10));
20
+ /**
21
+ * `Uuid.create()` returns a string value representing a random UUID string.
22
+ */
23
+ export function create() {
24
+ const s = [];
25
+ let i = 0;
26
+ for (i = 0; i < 8; i++)
27
+ s[i] = srandom();
28
+ s[8] = '-';
29
+ for (i = 9; i < 13; i++)
30
+ s[i] = srandom();
31
+ s[13] = '-';
32
+ s[14] = '4';
33
+ for (i = 15; i < 18; i++)
34
+ s[i] = srandom();
35
+ s[18] = '-';
36
+ s[19] = '89AB'.charAt(random(0x3));
37
+ for (i = 20; i < 23; i++)
38
+ s[i] = srandom();
39
+ s[23] = '-';
40
+ for (i = 24; i < 36; i++)
41
+ s[i] = srandom();
42
+ return UUID.unsafeOf(s.join(''));
43
+ }
44
+ const pattern = /^[0123456789abcdef]{8}-[0123456789abcdef]{4}-4[0123456789abcdef]{3}-[89ab][0123456789abcdef]{3}-[0123456789abcdef]{12}$/i;
45
+ // eslint-disable-next-line @typescript-eslint/no-redeclare
46
+ export const UUID = new (class extends NewtypeClass {
47
+ /**
48
+ * Returns `true` if the passed `uuid` conforms to the UUID v.4 format.
49
+ */
50
+ isValid(uuid) {
51
+ return pattern.test(uuid);
52
+ }
53
+ toString(uuid) {
54
+ return UUID.get(uuid);
55
+ }
56
+ })();
@@ -0,0 +1,23 @@
1
+ import { Result } from './result';
2
+ export interface Valid {
3
+ type: 'valid';
4
+ }
5
+ export interface Invalid<E> {
6
+ type: 'invalid';
7
+ error: E;
8
+ }
9
+ export type Validation<E> = Valid | Invalid<E>;
10
+ export type PromiseValidation<E> = PromiseLike<Validation<E>>;
11
+ export declare const Validation: {
12
+ valid: {
13
+ type: "valid";
14
+ };
15
+ invalid<E>(error: E): Validation<E>;
16
+ isValid<E_1>(r: Validation<E_1>): r is Valid;
17
+ isInvalid<E_2>(r: Validation<E_2>): r is Invalid<E_2>;
18
+ cmatch: <V, E_3>(valid: () => V, invalid: (error: E_3) => V) => (r: Validation<E_3>) => V;
19
+ match: <V_1, E_4>(r: Validation<E_4>, valid: () => V_1, invalid: (error: E_4) => V_1) => V_1;
20
+ toResult: <T, E_5>(value: T) => (validation: Validation<E_5>) => Result<T, E_5>;
21
+ whenValid: <E_6>(apply: () => void) => (r: Validation<E_6>) => Validation<E_6>;
22
+ whenInvalid: <E_7>(apply: (e: E_7) => void) => (r: Validation<E_7>) => Validation<E_7>;
23
+ };
package/validation.js ADDED
@@ -0,0 +1,44 @@
1
+ import { Result } from './result';
2
+ // eslint-disable-next-line @typescript-eslint/no-redeclare
3
+ export const Validation = {
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ valid: { type: 'valid' },
6
+ invalid(error) {
7
+ return { type: 'invalid', error };
8
+ },
9
+ isValid(r) {
10
+ return r.type === 'valid';
11
+ },
12
+ isInvalid(r) {
13
+ return r.type === 'invalid';
14
+ },
15
+ cmatch: (valid, invalid) => (r) => {
16
+ if (Validation.isValid(r)) {
17
+ return valid();
18
+ }
19
+ else {
20
+ return invalid(r.error);
21
+ }
22
+ },
23
+ match: (r, valid, invalid) => {
24
+ if (Validation.isValid(r)) {
25
+ return valid();
26
+ }
27
+ else {
28
+ return invalid(r.error);
29
+ }
30
+ },
31
+ toResult: (value) => Validation.cmatch(() => Result.success(value), (err) => Result.failure(err)),
32
+ whenValid: (apply) => (r) => {
33
+ if (Validation.isValid(r)) {
34
+ apply();
35
+ }
36
+ return r;
37
+ },
38
+ whenInvalid: (apply) => (r) => {
39
+ if (Validation.isInvalid(r)) {
40
+ apply(r.error);
41
+ }
42
+ return r;
43
+ }
44
+ };
package/src/arrays.ts DELETED
@@ -1,296 +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
- * Utility functions to manipulate `Array` values.
16
- */
17
-
18
- import { Maybe } from './maybe'
19
- import { type Ordering, type Compare } from './ord'
20
- import { type Primitive } from './types/utility'
21
- import { keys } from './objects'
22
-
23
- export function map<A, B> (arr: A[], f: (a: A, index: number) => B): B[] {
24
- return Array.from({ length: arr.length }, (_, i) => f(arr[i], i))
25
- }
26
-
27
- export function mapNotNull<A, B> (
28
- arr: A[],
29
- f: (a: A, index: number) => B | null | undefined
30
- ): B[] {
31
- const buff = [] as B[]
32
- for (let i = 0; i < arr.length; i++) {
33
- const v = f(arr[i], i)
34
- if (v != null) {
35
- buff.push(v)
36
- }
37
- }
38
- return buff
39
- }
40
-
41
- export function flatMap<A, B> (arr: A[], f: (a: A) => B[]): B[] {
42
- const buff = []
43
- for (const el of arr) {
44
- buff.push(...f(el))
45
- }
46
- return buff
47
- }
48
-
49
- export function head<A> (arr: A[]): Maybe<A> {
50
- return arr.length > 0 ? arr[0] : Maybe.nothing
51
- }
52
-
53
- export function tail<A> (arr: A[]): A[] {
54
- return arr.slice(1)
55
- }
56
-
57
- export function equals<T> (a: T[], b: T[], equality: (a: T, b: T) => boolean): boolean {
58
- if (a.length !== b.length) return false
59
- else {
60
- for (let i = 0; i < a.length; i++) {
61
- if (!equality(a[i], b[i])) return false
62
- }
63
- return true
64
- }
65
- }
66
-
67
- export function makeEquals<T> (equality: (a: T, b: T) => boolean) {
68
- return function (a: T[], b: T[]) {
69
- return equals(a, b, equality)
70
- }
71
- }
72
-
73
- export function isEmpty<T> (arr: T[]): arr is [] {
74
- return arr.length === 0
75
- }
76
-
77
- export function hasValues<T> (arr: T[]): arr is [T, ...T[]] {
78
- return arr.length > 0
79
- }
80
-
81
- export function filter<T> (arr: T[], predicate: (v: T) => boolean): T[] {
82
- const buff = [] as T[]
83
- for (const a of arr) if (predicate(a)) buff.push(a)
84
- return buff
85
- }
86
-
87
- export function filterNulls<T> (arr: Array<T | null | undefined>): T[] {
88
- return filter(arr, v => v != null) as T[]
89
- }
90
-
91
- export function flatten<T> (arr: T[][]): T[] {
92
- return ([] as T[]).concat(...arr)
93
- }
94
-
95
- export function foldLeft<T, B> (arr: T[], f: (acc: B, curr: T) => B, b: B): B {
96
- for (const a of arr) {
97
- b = f(b, a)
98
- }
99
- return b
100
- }
101
-
102
- export function all<T> (arr: T[], predicate: (v: T) => boolean): boolean {
103
- for (const a of arr) {
104
- if (!predicate(a)) {
105
- return false
106
- }
107
- }
108
- return true
109
- }
110
-
111
- export function any<T> (arr: T[], predicate: (v: T) => boolean): boolean {
112
- for (const a of arr) {
113
- if (predicate(a)) {
114
- return true
115
- }
116
- }
117
- return false
118
- }
119
-
120
- export function each<T> (arr: T[], f: (v: T) => void): void {
121
- for (const a of arr) f(a)
122
- }
123
-
124
- export function concat<A> (...arrs: A[][]): A[] {
125
- return ([] as A[]).concat(...arrs)
126
- }
127
-
128
- export function makeCompare<A> (comparef: Compare<A>, shorterFirst = true) {
129
- return function (a: A[], b: A[]) {
130
- if (a.length < b.length) {
131
- return -1 * (shorterFirst ? 1 : -1)
132
- } else if (a.length > b.length) {
133
- return 1 * (shorterFirst ? 1 : -1)
134
- }
135
- for (let i = 0; i < a.length; i++) {
136
- const ord = comparef(a[i], b[i])
137
- if (ord !== 0) return ord
138
- }
139
- return 0
140
- }
141
- }
142
-
143
- export function sort<A> (compare: (a: A, b: A) => Ordering, arr: A[]): A[] {
144
- return arr.slice().sort(compare)
145
- }
146
-
147
- export function range<A> (length: number, f: (index: number) => A): A[] {
148
- return Array.from({ length }, (_, i) => f(i))
149
- }
150
-
151
- export function numbersRange (length: number, startAt = 0): number[] {
152
- return range(length, i => startAt + i)
153
- }
154
-
155
- export function fill<A> (length: number, value: A): A[] {
156
- return range(length, () => value)
157
- }
158
-
159
- export function distinctPrimitive<T extends Primitive> (values: T[]): T[] {
160
- return Array.from(new Set(values))
161
- }
162
-
163
- export function distinctByPredicate<T> (
164
- values: T[],
165
- predicate: (a: T) => string
166
- ): T[] {
167
- const map: Record<string, T> = {}
168
- values.forEach(v => {
169
- map[predicate(v)] = v
170
- })
171
- return keys(map).map(k => map[k])
172
- }
173
-
174
- export function remove<A> (
175
- arr: A[],
176
- item: A,
177
- predicate?: (a: A) => boolean
178
- ): boolean {
179
- let index
180
- if (predicate !== undefined) {
181
- index = arr.findIndex(predicate)
182
- } else {
183
- index = arr.indexOf(item)
184
- }
185
- if (index < 0) {
186
- return false
187
- } else {
188
- arr.splice(index, 1)
189
- return true
190
- }
191
- }
192
-
193
- export function ofIterableIterator<A> (it: IterableIterator<A>): A[] {
194
- const buff = [] as A[]
195
- for (let r = it.next(); !(r.done ?? false); r = it.next()) {
196
- buff.push(r.value)
197
- }
198
- return buff
199
- }
200
-
201
- export interface DiffOperations<T> {
202
- removals: Array<{ at: number, qt: number }>
203
- swaps: Array<{ from: number, to: number }>
204
- inserts: Array<{ at: number, values: T[] }>
205
- }
206
-
207
- export function diffOperations<T, K> (
208
- from: T[],
209
- to: T[],
210
- getKey: (v: T) => K
211
- ): DiffOperations<T> {
212
- const ops: DiffOperations<T> = {
213
- removals: [],
214
- swaps: [],
215
- inserts: []
216
- }
217
- const { removals, inserts, swaps } = ops
218
- const mapB = new Map<K, number>()
219
- to.forEach((v, i) => mapB.set(getKey(v), i))
220
-
221
- const indexesOfAThatDoNotExistInB = from
222
- .map((v, i) => [v, i] as const)
223
- .filter(([v]) => !mapB.has(getKey(v)))
224
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
225
- .map(([_, i]) => i)
226
- for (let i = indexesOfAThatDoNotExistInB.length - 1; i >= 0; i--) {
227
- const p = indexesOfAThatDoNotExistInB[i]
228
- const last = removals.length > 0 ? removals[removals.length - 1] : undefined
229
- if ((last != null) && last.at === p + 1) {
230
- last.at--
231
- last.qt++
232
- } else {
233
- removals.push({ at: p, qt: 1 })
234
- }
235
- }
236
-
237
- const mapA = new Map<K, number>()
238
- from.forEach((v, i) => mapA.set(getKey(v), i))
239
-
240
- const indexesOfBThatDoNotExistInA = to
241
- .map((v, i) => [v, i] as const)
242
- .filter(([v]) => !mapA.has(getKey(v)))
243
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
244
- .map(([_, i]) => i)
245
- for (const p of indexesOfBThatDoNotExistInA) {
246
- const last = inserts.length > 0 ? inserts[inserts.length - 1] : undefined
247
- if ((last != null) && last.at + last.values.length === p) {
248
- last.values.push(to[p])
249
- } else {
250
- inserts.push({ at: p, values: [to[p]] })
251
- }
252
- }
253
-
254
- const ra = from.filter((_, i) => !indexesOfAThatDoNotExistInB.includes(i))
255
- const mapRA = new Map<K, number>()
256
- for (let i = 0; i < ra.length; i++) {
257
- mapRA.set(getKey(ra[i]), i)
258
- }
259
-
260
- const rb = to.filter((_, i) => !indexesOfBThatDoNotExistInA.includes(i))
261
- for (let i = 0; i < rb.length; i++) {
262
- const bk = getKey(rb[i])
263
- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
264
- const ai = mapRA.get(bk)!
265
- if (ai == null || i === ai) continue
266
- const ak = getKey(ra[i])
267
- mapRA.delete(ak)
268
- swaps.push({ from: i, to: ai })
269
- }
270
- return ops
271
- }
272
-
273
- export function applyOperations<T> (
274
- operations: DiffOperations<T>,
275
- start: T[]
276
- ): T[] {
277
- const buff = [...start]
278
- for (const { at, qt } of operations.removals) {
279
- buff.splice(at, qt)
280
- }
281
- for (const { from, to } of operations.swaps) {
282
- const t = buff[to]
283
- buff[to] = buff[from]
284
- buff[from] = t
285
- }
286
- for (const op of operations.inserts) {
287
- buff.splice(op.at, 0, ...op.values)
288
- }
289
- return buff
290
- }
291
-
292
- export function joinWithConjunction<A> (arr: A[], conjunction = ' and ', separator = ', '): string {
293
- if (arr.length === 0) return ''
294
- if (arr.length === 1) return String(arr[0])
295
- return `${arr.slice(0, -1).join(separator)}${conjunction}${String(arr[arr.length - 1])}`
296
- }