@oscarpalmer/atoms 0.153.0 → 0.154.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/src/function/assert.ts +67 -9
- package/src/math.ts +4 -4
package/package.json
CHANGED
|
@@ -54,6 +54,10 @@
|
|
|
54
54
|
"types": "./types/function/index.d.ts",
|
|
55
55
|
"default": "./dist/function/index.js"
|
|
56
56
|
},
|
|
57
|
+
"./function/assert": {
|
|
58
|
+
"types": "./types/function/assert.d.ts",
|
|
59
|
+
"default": "./dist/function/assert.js"
|
|
60
|
+
},
|
|
57
61
|
"./function/retry": {
|
|
58
62
|
"types": "./types/function/retry.d.ts",
|
|
59
63
|
"default": "./dist/function/retry.js"
|
|
@@ -180,5 +184,5 @@
|
|
|
180
184
|
},
|
|
181
185
|
"type": "module",
|
|
182
186
|
"types": "./types/index.d.ts",
|
|
183
|
-
"version": "0.
|
|
184
|
-
}
|
|
187
|
+
"version": "0.154.0"
|
|
188
|
+
}
|
package/src/function/assert.ts
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
import type {Constructor} from '../models';
|
|
2
2
|
|
|
3
|
+
// #region Types
|
|
4
|
+
|
|
3
5
|
export type Asserter<Value> = (value: unknown) => asserts value is Value;
|
|
4
6
|
|
|
7
|
+
// #endregion
|
|
8
|
+
|
|
9
|
+
// #region Functions
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Asserts that a condition is true, throwing an error if it is not
|
|
13
|
+
* @param condition Condition to assert
|
|
14
|
+
* @param message Error message
|
|
15
|
+
* @param error Error constructor
|
|
16
|
+
*/
|
|
5
17
|
export function assert<Condition extends () => boolean>(
|
|
6
18
|
condition: Condition,
|
|
7
19
|
message: string,
|
|
@@ -12,32 +24,78 @@ export function assert<Condition extends () => boolean>(
|
|
|
12
24
|
}
|
|
13
25
|
}
|
|
14
26
|
|
|
15
|
-
assert.condition =
|
|
27
|
+
assert.condition = assertCondition;
|
|
28
|
+
assert.defined = assertDefined;
|
|
29
|
+
assert.instanceOf = assertInstanceOf;
|
|
30
|
+
assert.is = assertIs;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Creates an asserter that asserts a condition is true, throwing an error if it is not
|
|
34
|
+
* @param condition Condition to assert
|
|
35
|
+
* @param message Error message
|
|
36
|
+
* @param error Error constructor
|
|
37
|
+
* @returns Asserter
|
|
38
|
+
*/
|
|
39
|
+
function assertCondition<Value>(
|
|
16
40
|
condition: (value: unknown) => boolean,
|
|
17
41
|
message: string,
|
|
18
42
|
error?: ErrorConstructor,
|
|
19
|
-
): Asserter<Value>
|
|
43
|
+
): Asserter<Value> {
|
|
20
44
|
return value => {
|
|
21
45
|
assert(() => condition(value), message, error);
|
|
22
46
|
};
|
|
23
|
-
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Asserts that a value is defined _(not `null` or `undefined`)_, throwing an error if it is not
|
|
51
|
+
* @param value Value to assert
|
|
52
|
+
* @param message Error message
|
|
53
|
+
*/
|
|
54
|
+
function assertDefined<Value>(
|
|
55
|
+
value: unknown,
|
|
56
|
+
message?: string,
|
|
57
|
+
): asserts value is Exclude<Value, null | undefined> {
|
|
58
|
+
assert(() => value != null, message ?? MESSAGE_DEFINED);
|
|
59
|
+
}
|
|
24
60
|
|
|
25
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Creates an asserter that asserts a value is an instance of a constructor, throwing an error if it is not
|
|
63
|
+
* @param constructor Constructor to check against
|
|
64
|
+
* @param message Error message
|
|
65
|
+
* @param error Error constructor
|
|
66
|
+
* @returns Asserter
|
|
67
|
+
*/
|
|
68
|
+
function assertInstanceOf<Value>(
|
|
26
69
|
constructor: Constructor<Value>,
|
|
27
70
|
message: string,
|
|
28
71
|
error?: ErrorConstructor,
|
|
29
|
-
): Asserter<Value>
|
|
72
|
+
): Asserter<Value> {
|
|
30
73
|
return value => {
|
|
31
74
|
assert(() => value instanceof constructor, message, error);
|
|
32
75
|
};
|
|
33
|
-
}
|
|
76
|
+
}
|
|
34
77
|
|
|
35
|
-
|
|
78
|
+
/**
|
|
79
|
+
* Creates an asserter that asserts a value is of a specific type, throwing an error if it is not
|
|
80
|
+
* @param condition Type guard function to check the value
|
|
81
|
+
* @param message Error message
|
|
82
|
+
* @param error Error constructor
|
|
83
|
+
* @returns Asserter
|
|
84
|
+
*/
|
|
85
|
+
function assertIs<Value>(
|
|
36
86
|
condition: (value: unknown) => value is Value,
|
|
37
87
|
message: string,
|
|
38
88
|
error?: ErrorConstructor,
|
|
39
|
-
): Asserter<Value>
|
|
89
|
+
): Asserter<Value> {
|
|
40
90
|
return value => {
|
|
41
91
|
assert(() => condition(value), message, error);
|
|
42
92
|
};
|
|
43
|
-
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// #endregion
|
|
96
|
+
|
|
97
|
+
// #region Variables
|
|
98
|
+
|
|
99
|
+
const MESSAGE_DEFINED = 'Expected value to be defined';
|
|
100
|
+
|
|
101
|
+
// #endregion
|
package/src/math.ts
CHANGED
|
@@ -154,10 +154,6 @@ export function median(array: unknown[], key?: unknown): number {
|
|
|
154
154
|
return Number.NaN;
|
|
155
155
|
}
|
|
156
156
|
|
|
157
|
-
if (length === 1) {
|
|
158
|
-
return isNumber(array[0]) ? array[0] : Number.NaN;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
157
|
let values: unknown[] = array;
|
|
162
158
|
|
|
163
159
|
const callback = getAggregateCallback(key);
|
|
@@ -170,6 +166,10 @@ export function median(array: unknown[], key?: unknown): number {
|
|
|
170
166
|
|
|
171
167
|
length = numbers.length;
|
|
172
168
|
|
|
169
|
+
if (length === 1) {
|
|
170
|
+
return numbers[0];
|
|
171
|
+
}
|
|
172
|
+
|
|
173
173
|
if (length % 2 === 0) {
|
|
174
174
|
const first = length / 2 - 1;
|
|
175
175
|
const second = length / 2;
|