@tempots/std 0.9.7 → 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.
Files changed (106) hide show
  1. package/README.md +1 -3
  2. package/array.cjs +1 -0
  3. package/{arrays.d.ts → array.d.ts} +7 -7
  4. package/array.js +223 -0
  5. package/bigint.cjs +1 -0
  6. package/bigint.js +96 -110
  7. package/boolean.cjs +1 -0
  8. package/boolean.js +44 -0
  9. package/domain.cjs +1 -0
  10. package/domain.d.ts +22 -0
  11. package/domain.js +1 -0
  12. package/equal.cjs +1 -0
  13. package/equal.js +68 -0
  14. package/function.cjs +1 -0
  15. package/function.js +29 -0
  16. package/index.cjs +1 -0
  17. package/index.d.ts +0 -0
  18. package/index.js +1 -0
  19. package/maybe.cjs +1 -0
  20. package/maybe.d.ts +3 -3
  21. package/maybe.js +8 -24
  22. package/number.cjs +1 -0
  23. package/{numbers.d.ts → number.d.ts} +2 -5
  24. package/number.js +103 -0
  25. package/object.cjs +1 -0
  26. package/object.d.ts +8 -0
  27. package/object.js +30 -0
  28. package/package.json +55 -25
  29. package/regexp.cjs +1 -0
  30. package/regexp.js +14 -0
  31. package/string.cjs +3 -0
  32. package/{strings.d.ts → string.d.ts} +0 -2
  33. package/string.js +396 -0
  34. package/arrays.js +0 -249
  35. package/async-result.d.ts +0 -37
  36. package/async-result.js +0 -75
  37. package/booleans.js +0 -68
  38. package/colors/cmyk.d.ts +0 -21
  39. package/colors/cmyk.js +0 -54
  40. package/colors/convert.d.ts +0 -283
  41. package/colors/convert.js +0 -742
  42. package/colors/hsl.d.ts +0 -24
  43. package/colors/hsl.js +0 -56
  44. package/colors/hsla.d.ts +0 -18
  45. package/colors/hsla.js +0 -35
  46. package/colors/hsluv.d.ts +0 -24
  47. package/colors/hsluv.js +0 -56
  48. package/colors/hsv.d.ts +0 -24
  49. package/colors/hsv.js +0 -54
  50. package/colors/lab.d.ts +0 -24
  51. package/colors/lab.js +0 -54
  52. package/colors/lch.d.ts +0 -19
  53. package/colors/lch.js +0 -44
  54. package/colors/luv.d.ts +0 -19
  55. package/colors/luv.js +0 -45
  56. package/colors/rgb.d.ts +0 -13
  57. package/colors/rgb.js +0 -47
  58. package/colors/rgba.d.ts +0 -12
  59. package/colors/rgba.js +0 -44
  60. package/colors/srgb.d.ts +0 -24
  61. package/colors/srgb.js +0 -51
  62. package/colors/xyz.d.ts +0 -19
  63. package/colors/xyz.js +0 -41
  64. package/edit.d.ts +0 -20
  65. package/edit.js +0 -29
  66. package/equals.js +0 -122
  67. package/functions.js +0 -38
  68. package/json.d.ts +0 -14
  69. package/json.js +0 -33
  70. package/match.d.ts +0 -16
  71. package/match.js +0 -45
  72. package/memoize.d.ts +0 -1
  73. package/memoize.js +0 -9
  74. package/newtype.d.ts +0 -28
  75. package/newtype.js +0 -29
  76. package/numbers.js +0 -183
  77. package/objects.d.ts +0 -9
  78. package/objects.js +0 -33
  79. package/ord.d.ts +0 -19
  80. package/ord.js +0 -73
  81. package/reg-exps.js +0 -43
  82. package/result.d.ts +0 -31
  83. package/result.js +0 -95
  84. package/strings.js +0 -685
  85. package/types/assert.d.ts +0 -12
  86. package/types/assert.js +0 -13
  87. package/types/differentiate.d.ts +0 -13
  88. package/types/differentiate.js +0 -14
  89. package/types/functions.d.ts +0 -22
  90. package/types/functions.js +0 -13
  91. package/types/generic.d.ts +0 -9
  92. package/types/generic.js +0 -13
  93. package/types/objects.d.ts +0 -50
  94. package/types/objects.js +0 -13
  95. package/types/tuples.d.ts +0 -44
  96. package/types/tuples.js +0 -24
  97. package/types/utility.d.ts +0 -2
  98. package/types/utility.js +0 -1
  99. package/uuid.d.ts +0 -13
  100. package/uuid.js +0 -56
  101. package/validation.d.ts +0 -23
  102. package/validation.js +0 -44
  103. /package/{booleans.d.ts → boolean.d.ts} +0 -0
  104. /package/{equals.d.ts → equal.d.ts} +0 -0
  105. /package/{functions.d.ts → function.d.ts} +0 -0
  106. /package/{reg-exps.d.ts → regexp.d.ts} +0 -0
package/numbers.js DELETED
@@ -1,183 +0,0 @@
1
- import { lpad } from './strings';
2
- export const TOLERANCE = 10e-5;
3
- /**
4
- Constant value employed to see if two `number` values are very close.
5
- **/
6
- export const EPSILON = 1e-9;
7
- /**
8
- Returns the angular distance between 2 angles.
9
- **/
10
- export function angleDifference(a, b, turn = 360.0) {
11
- let r = (b - a) % turn;
12
- if (r < 0)
13
- r += turn;
14
- if (r > turn / 2)
15
- r -= turn;
16
- return r;
17
- }
18
- /**
19
- Rounds a number up to the specified number of decimals.
20
- **/
21
- export function ceilTo(v, decimals) {
22
- const p = Math.pow(10, decimals);
23
- return Math.ceil(v * p) / p;
24
- }
25
- /**
26
- `clamp` restricts a value within the specified range.
27
- ```ts
28
- log(clamp(1.3, 0, 1)) // prints 1
29
- log(clamp(0.8, 0, 1)) // prints 0.8
30
- log(clamp(-0.5, 0, 1)) // prints 0.0
31
- ```
32
- **/
33
- export function clamp(value, min, max) {
34
- return Math.min(Math.max(value, min), max);
35
- }
36
- export function clampInt(value, min, max) {
37
- return Math.trunc(clamp(value, min, max));
38
- }
39
- /**
40
- Like clamp but you only pass one argument (`max`) that is used as the upper limit
41
- and the opposite (additive inverse or `-max`) as the lower limit.
42
- **/
43
- export function clampSym(v, max) {
44
- return clamp(v, -max, max);
45
- }
46
- /**
47
- It returns the comparison value (an integer number) between two `float` values.
48
- **/
49
- export function compare(a, b) {
50
- return a < b ? -1 : a > b ? 1 : 0;
51
- }
52
- /**
53
- Rounds a number down to the specified number of decimals.
54
- **/
55
- export function floorTo(v, decimals) {
56
- const p = Math.pow(10, decimals);
57
- return Math.floor(v * p) / p;
58
- }
59
- /**
60
- `normalize` clamps the passwed value between 0 and 1.
61
- **/
62
- export function normalize(value) {
63
- return clamp(value, 0, 1);
64
- }
65
- export function toHex(num, length = 0) {
66
- return lpad(num.toString(16), '0', length);
67
- }
68
- /**
69
- `interpolate` returns a value between `a` and `b` for any value of `t` (normally between 0 and 1).
70
- **/
71
- export function interpolate(a, b, t) {
72
- return (b - a) * t + a;
73
- }
74
- /**
75
- Interpolates values in a polar coordinate system looking for the narrowest delta angle.
76
- It can be either clock-wise or counter-clock-wise.
77
- **/
78
- export function interpolateAngle(a, b, t, turn = 360.0) {
79
- return wrapCircular(interpolate(a, a + angleDifference(a, b, turn), t), turn);
80
- }
81
- /**
82
- Interpolates values in a polar coordinate system looking for the wideset delta angle.
83
- It can be either clock-wise or counter-clock-wise.
84
- **/
85
- export function interpolateAngleWidest(a, b, t, turn = 360) {
86
- return wrapCircular(interpolateAngle(a, b, t, turn) - turn / 2, turn);
87
- }
88
- /**
89
- Interpolates values in a polar coordinate system always in clock-wise direction.
90
- **/
91
- export function interpolateAngleCW(a, b, t, turn = 360) {
92
- a = wrapCircular(a, turn);
93
- b = wrapCircular(b, turn);
94
- if (b < a)
95
- b += turn;
96
- return wrapCircular(interpolate(a, b, t), turn);
97
- }
98
- /**
99
- Interpolates values in a polar coordinate system always in counter-clock-wise direction.
100
- **/
101
- export function interpolateAngleCCW(a, b, t, turn = 360) {
102
- a = wrapCircular(a, turn);
103
- b = wrapCircular(b, turn);
104
- if (b > a)
105
- b -= turn;
106
- return wrapCircular(interpolate(a, b, t), turn);
107
- }
108
- /**
109
- number numbers can sometime introduce tiny errors even for simple operations.
110
- `nearEquals` compares two floats using a tiny tollerance (last optional
111
- argument). By default it is defined as `EPSILON`.
112
- **/
113
- export function nearEquals(a, b, tollerance = EPSILON) {
114
- if (isFinite(a)) {
115
- if (!isFinite(b))
116
- return false;
117
- return Math.abs(a - b) <= tollerance;
118
- }
119
- if (isNaN(a))
120
- return isNaN(b);
121
- if (isNaN(b))
122
- return false;
123
- if (!isFinite(b))
124
- return (a > 0) === (b > 0);
125
- // a is Infinity and b is finite
126
- return false;
127
- }
128
- /**
129
- number numbers can sometime introduce tiny errors even for simple operations.
130
- `nearEqualAngles` compares two angles (default is 360deg) using a tiny
131
- tollerance (last optional argument). By default the tollerance is defined as
132
- `EPSILON`.
133
- **/
134
- export function nearEqualAngles(a, b, turn = 360.0, tollerance = EPSILON) {
135
- return Math.abs(angleDifference(a, b, turn)) <= tollerance;
136
- }
137
- /**
138
- `nearZero` finds if the passed number is zero or very close to it. By default
139
- `EPSILON` is used as the tollerance value.
140
- **/
141
- export function nearZero(n, tollerance = EPSILON) {
142
- return Math.abs(n) <= tollerance;
143
- }
144
- /**
145
- Computes the nth root (`index`) of `base`.
146
- **/
147
- export function root(base, index) {
148
- return Math.pow(base, 1 / index);
149
- }
150
- /**
151
- Rounds a number to the specified number of decimals.
152
- **/
153
- export function roundTo(f, decimals) {
154
- const p = Math.pow(10, decimals);
155
- return Math.fround(f * p) / p;
156
- }
157
- /**
158
- `sign` returns `-1` if `value` is a negative number, `1` otherwise.
159
- */
160
- export function sign(value) {
161
- return value < 0 ? -1 : 1;
162
- }
163
- /**
164
- Passed two boundaries values (`min`, `max`), `wrap` ensures that the passed value `v` will
165
- be included in the boundaries. If the value exceeds `max`, the value is reduced by `min`
166
- repeatedely until it falls within the range. Similar and inverted treatment is performed if
167
- the value is below `min`.
168
- **/
169
- export function wrap(v, min, max) {
170
- const range = max - min + 1;
171
- if (v < min)
172
- v += range * ((min - v) / range + 1);
173
- return min + ((v - min) % range);
174
- }
175
- /**
176
- Similar to `wrap`, it works for numbers between 0 and `max`.
177
- **/
178
- export function wrapCircular(v, max) {
179
- v = v % max;
180
- if (v < 0)
181
- v += max;
182
- return v;
183
- }
package/objects.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import { type TupleToUnion } from './types/tuples';
2
- import { type Merge } from './types/objects';
3
- import { type AnyKey } from './types/utility';
4
- export declare function keys<T extends object>(obj: T): Array<keyof T>;
5
- export declare function sameKeys<T extends object>(a: T, b: T): boolean;
6
- export declare function isObject(obj: unknown): obj is Record<AnyKey, unknown>;
7
- export declare function removeFields<T extends object, F extends Array<keyof T>>(ob: T, ...fields: F): Omit<T, TupleToUnion<F>>;
8
- export declare function merge<A extends Record<AnyKey, unknown>, B extends Record<AnyKey, unknown>>(a: A, b: B): Merge<A, B>;
9
- export declare function isEmpty(obj: object): boolean;
package/objects.js DELETED
@@ -1,33 +0,0 @@
1
- export function keys(obj) {
2
- return Object.keys(obj);
3
- }
4
- export function sameKeys(a, b) {
5
- const ak = keys(a);
6
- const bk = keys(b);
7
- if (ak.length !== bk.length)
8
- return false;
9
- for (const k of ak) {
10
- if (!(k in b))
11
- return false;
12
- }
13
- return true;
14
- }
15
- export function isObject(obj) {
16
- return Object.prototype.toString.call(obj) === '[object Object]';
17
- }
18
- export function removeFields(ob, ...fields) {
19
- const ks = keys(ob);
20
- return ks.reduce((acc, key) => {
21
- if (!fields.includes(key))
22
- acc[key] = ob[key];
23
- return acc;
24
- }, {});
25
- }
26
- export function merge(a, b) {
27
- return Object.assign({}, a, b);
28
- }
29
- export function isEmpty(obj) {
30
- return (obj != null &&
31
- Object.keys(obj).length === 0 &&
32
- Object.getPrototypeOf(obj) === Object.prototype);
33
- }
package/ord.d.ts DELETED
@@ -1,19 +0,0 @@
1
- export declare enum Ordering {
2
- LT = -1,
3
- EQ = 0,
4
- GT = 1
5
- }
6
- export type CompareOrd<T> = (a: T, b: T) => Ordering;
7
- export type Compare<T> = (a: T, b: T) => number;
8
- export declare class Ord<T> {
9
- readonly compare: CompareOrd<T>;
10
- static fromNumberComparison<T>(compare: (a: T, b: T) => number): Ord<T>;
11
- constructor(compare: CompareOrd<T>);
12
- max(a: T, b: T): T;
13
- min(a: T, b: T): T;
14
- equals(a: T, b: T): boolean;
15
- contramap<B>(f: (b: B) => T): Ord<B>;
16
- inverse(): Ord<T>;
17
- numberComparison(a0: T, a1: T): number;
18
- }
19
- export declare function fromNumberComparison<A>(f: (a: A, b: A) => number): (a: A, b: A) => Ordering;
package/ord.js DELETED
@@ -1,73 +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
- export var Ordering;
14
- (function (Ordering) {
15
- Ordering[Ordering["LT"] = -1] = "LT";
16
- Ordering[Ordering["EQ"] = 0] = "EQ";
17
- Ordering[Ordering["GT"] = 1] = "GT";
18
- })(Ordering || (Ordering = {}));
19
- export class Ord {
20
- compare;
21
- static fromNumberComparison(compare) {
22
- return new Ord(fromNumberComparison(compare));
23
- }
24
- constructor(compare) {
25
- this.compare = compare;
26
- }
27
- max(a, b) {
28
- switch (this.compare(a, b)) {
29
- case Ordering.LT:
30
- case Ordering.EQ:
31
- return a;
32
- case Ordering.GT:
33
- return b;
34
- }
35
- }
36
- min(a, b) {
37
- switch (this.compare(a, b)) {
38
- case Ordering.GT:
39
- case Ordering.EQ:
40
- return a;
41
- case Ordering.LT:
42
- return b;
43
- }
44
- }
45
- equals(a, b) {
46
- switch (this.compare(a, b)) {
47
- case Ordering.EQ:
48
- return true;
49
- default:
50
- return false;
51
- }
52
- }
53
- contramap(f) {
54
- return new Ord((b0, b1) => this.compare(f(b0), f(b1)));
55
- }
56
- inverse() {
57
- return new Ord((a0, a1) => this.compare(a1, a0));
58
- }
59
- numberComparison(a0, a1) {
60
- return this.compare(a0, a1);
61
- }
62
- }
63
- export function fromNumberComparison(f) {
64
- return function compare(a, b) {
65
- const r = f(a, b);
66
- if (r < 0)
67
- return Ordering.LT;
68
- else if (r === 0)
69
- return Ordering.EQ;
70
- else
71
- return Ordering.GT;
72
- };
73
- }
package/reg-exps.js DELETED
@@ -1,43 +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
- * Utility module to manipulate `RegExp` instances.
15
- */
16
- /**
17
- * Map the function `f` on each occurance matched by the pattern.
18
- * @param f
19
- * @param pattern
20
- * @param subject
21
- */
22
- export function map(subject, pattern, f) {
23
- const buff = [];
24
- let pos = 0;
25
- let result;
26
- if (pattern.global) {
27
- pattern.lastIndex = 0;
28
- while ((result = pattern.exec(subject)) !== null) {
29
- buff.push(subject.substring(pos, result.index));
30
- buff.push(f(...result));
31
- pos = result.index + result[0].length;
32
- }
33
- }
34
- else {
35
- while ((result = pattern.exec(subject.substring(pos))) !== null) {
36
- buff.push(subject.substring(pos, pos + result.index));
37
- buff.push(f(...result));
38
- pos += result.index + result[0].length;
39
- }
40
- }
41
- buff.push(subject.substring(pos));
42
- return buff.join('');
43
- }
package/result.d.ts DELETED
@@ -1,31 +0,0 @@
1
- import { type AsyncResult } from './async-result';
2
- export interface Success<V> {
3
- type: 'success';
4
- value: V;
5
- }
6
- export interface Failure<E> {
7
- type: 'failure';
8
- error: E;
9
- }
10
- export type Result<V, E> = Success<V> | Failure<E>;
11
- export type PromiseResult<V, E> = PromiseLike<Result<V, E>>;
12
- export declare const Result: {
13
- success<V>(value: V): Result<V, any>;
14
- failure<E>(error: E): Result<any, E>;
15
- cmap: <V1, V2, E_1>(f: (value: V1) => V2) => (r: Result<V1, E_1>) => Result<V2, E_1>;
16
- map: <V1_1, V2_1, E_2>(r: Result<V1_1, E_2>, f: (value: V1_1) => V2_1) => Result<V2_1, E_2>;
17
- cflatMap: <V1_2, V2_2, E_3>(f: (value: V1_2) => Result<V2_2, E_3>) => (r: Result<V1_2, E_3>) => Result<V2_2, E_3>;
18
- flatMap: <V1_3, V2_3, E_4>(r: Result<V1_3, E_4>, f: (value: V1_3) => Result<V2_3, E_4>) => Result<V2_3, E_4>;
19
- toAsync<V_1, E_5>(r: Result<V_1, E_5>): AsyncResult<V_1, E_5>;
20
- isSuccess<V_2, E_6>(r: Result<V_2, E_6>): r is Success<V_2>;
21
- isFailure<V_3, E_7>(r: Result<V_3, E_7>): r is Failure<E_7>;
22
- getOrElse<V_4, E_8>(r: Result<V_4, E_8>, alt: V_4): V_4;
23
- getOrElseLazy<V_5, E_9>(r: Result<V_5, E_9>, altf: () => V_5): V_5;
24
- getOrNull<V_6, E_10>(r: Result<V_6, E_10>): V_6 | null;
25
- getOrUndefined<V_7, E_11>(r: Result<V_7, E_11>): V_7 | undefined;
26
- cmatch: <V1_4, V2_4, E_12>(success: (value: V1_4) => V2_4, failure: (error: E_12) => V2_4) => (r: Result<V1_4, E_12>) => V2_4;
27
- match: <V1_5, V2_5, E_13>(r: Result<V1_5, E_13>, success: (value: V1_5) => V2_5, failure: (error: E_13) => V2_5) => V2_5;
28
- whenSuccess: <V_8, E_14>(apply: (v: V_8) => void) => (r: Result<V_8, E_14>) => Result<V_8, E_14>;
29
- whenFailure: <V_9, E_15>(apply: (e: E_15) => void) => (r: Result<V_9, E_15>) => Result<V_9, E_15>;
30
- combine: <V_10, E_16>(r1: Result<V_10, E_16>, r2: Result<V_10, E_16>, combineV: (v1: V_10, v2: V_10) => V_10, combineE: (e1: E_16, e2: E_16) => E_16) => Result<V_10, E_16>;
31
- };
package/result.js DELETED
@@ -1,95 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-redeclare
2
- export const Result = {
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- success(value) {
5
- return { type: 'success', value };
6
- },
7
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
8
- failure(error) {
9
- return { type: 'failure', error };
10
- },
11
- cmap: (f) => (r) => {
12
- if (r.type === 'success') {
13
- return Result.success(f(r.value));
14
- }
15
- else {
16
- return r;
17
- }
18
- },
19
- map: (r, f) => {
20
- if (r.type === 'success') {
21
- return Result.success(f(r.value));
22
- }
23
- else {
24
- return r;
25
- }
26
- },
27
- cflatMap: (f) => (r) => {
28
- if (r.type === 'success') {
29
- return f(r.value);
30
- }
31
- else {
32
- return r;
33
- }
34
- },
35
- flatMap: (r, f) => {
36
- if (r.type === 'success') {
37
- return f(r.value);
38
- }
39
- else {
40
- return r;
41
- }
42
- },
43
- toAsync(r) {
44
- return r;
45
- },
46
- isSuccess(r) {
47
- return r.type === 'success';
48
- },
49
- isFailure(r) {
50
- return r.type === 'failure';
51
- },
52
- getOrElse(r, alt) {
53
- return Result.isSuccess(r) ? r.value : alt;
54
- },
55
- getOrElseLazy(r, altf) {
56
- return Result.isSuccess(r) ? r.value : altf();
57
- },
58
- getOrNull(r) {
59
- return Result.isSuccess(r) ? r.value : null;
60
- },
61
- getOrUndefined(r) {
62
- return Result.isSuccess(r) ? r.value : undefined;
63
- },
64
- cmatch: (success, failure) => (r) => {
65
- if (Result.isSuccess(r)) {
66
- return success(r.value);
67
- }
68
- else {
69
- return failure(r.error);
70
- }
71
- },
72
- match: (r, success, failure) => {
73
- if (Result.isSuccess(r)) {
74
- return success(r.value);
75
- }
76
- else {
77
- return failure(r.error);
78
- }
79
- },
80
- whenSuccess: (apply) => (r) => {
81
- if (Result.isSuccess(r)) {
82
- apply(r.value);
83
- }
84
- return r;
85
- },
86
- whenFailure: (apply) => (r) => {
87
- if (Result.isFailure(r)) {
88
- apply(r.error);
89
- }
90
- return r;
91
- },
92
- combine: (r1, r2, combineV, combineE) => Result.match(r1, (v1) => Result.match(r2, (v2) => Result.success(combineV(v1, v2)), (e2) => Result.failure(e2)), (e1) => Result.match(r2,
93
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
94
- (_) => Result.failure(e1), (e2) => Result.failure(combineE(e1, e2))))
95
- };