@oscarpalmer/atoms 0.193.0 → 0.194.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.
Files changed (204) hide show
  1. package/dist/array/difference.mjs +2 -2
  2. package/dist/array/intersection.mjs +2 -2
  3. package/dist/array/match.mjs +13 -13
  4. package/dist/array/slice.mjs +5 -5
  5. package/dist/array/sort.d.mts +5 -1
  6. package/dist/array/sort.mjs +4 -2
  7. package/dist/array/union.mjs +2 -2
  8. package/dist/beacon.d.mts +8 -1
  9. package/dist/beacon.mjs +52 -60
  10. package/dist/color/constants.d.mts +1 -1
  11. package/dist/color/constants.mjs +3 -5
  12. package/dist/color/instance.mjs +34 -41
  13. package/dist/color/misc/format.d.mts +3 -2
  14. package/dist/color/misc/format.mjs +4 -1
  15. package/dist/color/misc/is.d.mts +9 -1
  16. package/dist/color/misc/is.mjs +11 -1
  17. package/dist/color/misc/state.d.mts +6 -6
  18. package/dist/color/misc/state.mjs +11 -14
  19. package/dist/color/models.d.mts +23 -68
  20. package/dist/function/assert.mjs +2 -2
  21. package/dist/function/limit.d.mts +0 -6
  22. package/dist/function/limit.mjs +2 -2
  23. package/dist/function/memoize.mjs +19 -32
  24. package/dist/function/once.d.mts +0 -3
  25. package/dist/function/once.mjs +16 -13
  26. package/dist/function/retry.d.mts +1 -24
  27. package/dist/function/retry.mjs +1 -1
  28. package/dist/function/work.mjs +13 -11
  29. package/dist/herald.d.mts +30 -31
  30. package/dist/herald.mjs +74 -56
  31. package/dist/index.d.mts +100 -185
  32. package/dist/index.mjs +680 -702
  33. package/dist/internal/abort.d.mts +10 -0
  34. package/dist/internal/abort.mjs +14 -0
  35. package/dist/internal/array/chunk.mjs +2 -2
  36. package/dist/internal/array/find.mjs +2 -2
  37. package/dist/internal/array/sets.d.mts +4 -4
  38. package/dist/internal/array/sets.mjs +7 -7
  39. package/dist/internal/function/timer.mjs +19 -15
  40. package/dist/internal/number.mjs +7 -7
  41. package/dist/internal/result.mjs +3 -3
  42. package/dist/internal/sized.mjs +4 -4
  43. package/dist/internal/string.mjs +4 -4
  44. package/dist/internal/subscription.d.mts +24 -8
  45. package/dist/internal/subscription.mjs +49 -36
  46. package/dist/internal/value/equal.mjs +6 -6
  47. package/dist/internal/value/misc.mjs +14 -14
  48. package/dist/internal/value/set.mjs +2 -2
  49. package/dist/logger.mjs +15 -39
  50. package/dist/promise/constants.d.mts +17 -0
  51. package/dist/promise/constants.mjs +14 -0
  52. package/dist/promise/delay.mjs +7 -10
  53. package/dist/promise/helpers.mjs +1 -1
  54. package/dist/promise/index.mjs +14 -17
  55. package/dist/promise/misc.d.mts +2 -1
  56. package/dist/promise/misc.mjs +8 -7
  57. package/dist/promise/models.d.mts +4 -16
  58. package/dist/promise/models.mjs +4 -13
  59. package/dist/promise/timed.mjs +9 -10
  60. package/dist/query.mjs +15 -15
  61. package/dist/queue.mjs +103 -116
  62. package/dist/random.mjs +7 -7
  63. package/dist/result/index.d.mts +0 -3
  64. package/dist/result/index.mjs +1 -1
  65. package/dist/result/match.d.mts +0 -3
  66. package/dist/result/match.mjs +4 -4
  67. package/dist/result/misc.mjs +2 -2
  68. package/dist/result/work/flow.d.mts +0 -3
  69. package/dist/result/work/flow.mjs +5 -5
  70. package/dist/result/work/pipe.d.mts +0 -3
  71. package/dist/result/work/pipe.mjs +3 -3
  72. package/dist/string/case.mjs +20 -20
  73. package/dist/string/fuzzy.mjs +23 -18
  74. package/dist/string/index.mjs +7 -7
  75. package/dist/string/normalize.mjs +14 -14
  76. package/dist/string/template.mjs +2 -2
  77. package/dist/value/clone.mjs +10 -10
  78. package/dist/value/smush.mjs +2 -2
  79. package/package.json +13 -5
  80. package/src/array/difference.ts +0 -73
  81. package/src/array/exists.ts +0 -93
  82. package/src/array/filter.ts +0 -182
  83. package/src/array/find.ts +0 -183
  84. package/src/array/first.ts +0 -196
  85. package/src/array/flatten.ts +0 -20
  86. package/src/array/from.ts +0 -129
  87. package/src/array/get.ts +0 -131
  88. package/src/array/group-by.ts +0 -369
  89. package/src/array/index.ts +0 -26
  90. package/src/array/insert.ts +0 -52
  91. package/src/array/intersection.ts +0 -72
  92. package/src/array/last.ts +0 -194
  93. package/src/array/match.ts +0 -436
  94. package/src/array/move.ts +0 -315
  95. package/src/array/partition.ts +0 -91
  96. package/src/array/push.ts +0 -23
  97. package/src/array/reverse.ts +0 -33
  98. package/src/array/select.ts +0 -260
  99. package/src/array/single.ts +0 -93
  100. package/src/array/slice.ts +0 -335
  101. package/src/array/sort.ts +0 -607
  102. package/src/array/splice.ts +0 -106
  103. package/src/array/swap.ts +0 -322
  104. package/src/array/to-map.ts +0 -371
  105. package/src/array/to-record.ts +0 -351
  106. package/src/array/to-set.ts +0 -80
  107. package/src/array/toggle.ts +0 -79
  108. package/src/array/union.ts +0 -72
  109. package/src/array/unique.ts +0 -67
  110. package/src/array/update.ts +0 -75
  111. package/src/beacon.ts +0 -378
  112. package/src/color/constants.ts +0 -137
  113. package/src/color/index.ts +0 -83
  114. package/src/color/instance.ts +0 -162
  115. package/src/color/misc/alpha.ts +0 -56
  116. package/src/color/misc/format.ts +0 -29
  117. package/src/color/misc/get.ts +0 -210
  118. package/src/color/misc/is.ts +0 -221
  119. package/src/color/misc/state.ts +0 -243
  120. package/src/color/models.ts +0 -389
  121. package/src/color/space/hex.ts +0 -184
  122. package/src/color/space/hsl.ts +0 -174
  123. package/src/color/space/hwb.ts +0 -153
  124. package/src/color/space/rgb.ts +0 -240
  125. package/src/function/assert.ts +0 -174
  126. package/src/function/index.ts +0 -2
  127. package/src/function/limit.ts +0 -83
  128. package/src/function/memoize.ts +0 -202
  129. package/src/function/once.ts +0 -200
  130. package/src/function/retry.ts +0 -175
  131. package/src/function/work.ts +0 -1007
  132. package/src/herald.ts +0 -216
  133. package/src/index.ts +0 -68
  134. package/src/internal/array/callbacks.ts +0 -46
  135. package/src/internal/array/chunk.ts +0 -51
  136. package/src/internal/array/compact.ts +0 -56
  137. package/src/internal/array/find.ts +0 -200
  138. package/src/internal/array/group.ts +0 -42
  139. package/src/internal/array/index-of.ts +0 -182
  140. package/src/internal/array/insert.ts +0 -80
  141. package/src/internal/array/overlap.ts +0 -35
  142. package/src/internal/array/sets.ts +0 -74
  143. package/src/internal/array/shuffle.ts +0 -33
  144. package/src/internal/array/update.ts +0 -52
  145. package/src/internal/function/misc.ts +0 -8
  146. package/src/internal/function/timer.ts +0 -168
  147. package/src/internal/is.ts +0 -244
  148. package/src/internal/math/aggregate.ts +0 -164
  149. package/src/internal/math/misc.ts +0 -28
  150. package/src/internal/number.ts +0 -129
  151. package/src/internal/random.ts +0 -49
  152. package/src/internal/result.ts +0 -92
  153. package/src/internal/sized.ts +0 -25
  154. package/src/internal/string.ts +0 -128
  155. package/src/internal/subscription.ts +0 -264
  156. package/src/internal/value/compare.ts +0 -170
  157. package/src/internal/value/equal.ts +0 -465
  158. package/src/internal/value/get.ts +0 -57
  159. package/src/internal/value/handlers.ts +0 -95
  160. package/src/internal/value/has.ts +0 -118
  161. package/src/internal/value/misc.ts +0 -125
  162. package/src/internal/value/partial.ts +0 -46
  163. package/src/internal/value/set.ts +0 -132
  164. package/src/is.ts +0 -182
  165. package/src/logger.ts +0 -230
  166. package/src/math.ts +0 -281
  167. package/src/models.ts +0 -286
  168. package/src/number.ts +0 -1
  169. package/src/promise/delay.ts +0 -62
  170. package/src/promise/helpers.ts +0 -96
  171. package/src/promise/index.ts +0 -300
  172. package/src/promise/misc.ts +0 -96
  173. package/src/promise/models.ts +0 -195
  174. package/src/promise/timed.ts +0 -91
  175. package/src/query.ts +0 -147
  176. package/src/queue.ts +0 -814
  177. package/src/random.ts +0 -125
  178. package/src/result/index.ts +0 -105
  179. package/src/result/match.ts +0 -124
  180. package/src/result/misc.ts +0 -129
  181. package/src/result/models.ts +0 -83
  182. package/src/result/work/flow.ts +0 -488
  183. package/src/result/work/pipe.ts +0 -496
  184. package/src/sized/map.ts +0 -110
  185. package/src/sized/set.ts +0 -107
  186. package/src/string/case.ts +0 -236
  187. package/src/string/fuzzy.ts +0 -444
  188. package/src/string/index.ts +0 -199
  189. package/src/string/match.ts +0 -80
  190. package/src/string/normalize.ts +0 -205
  191. package/src/string/template.ts +0 -161
  192. package/src/value/clone.ts +0 -319
  193. package/src/value/collection.ts +0 -63
  194. package/src/value/diff.ts +0 -232
  195. package/src/value/freeze.ts +0 -322
  196. package/src/value/handle.ts +0 -3
  197. package/src/value/index.ts +0 -5
  198. package/src/value/merge.ts +0 -278
  199. package/src/value/omit.ts +0 -20
  200. package/src/value/pick.ts +0 -20
  201. package/src/value/shake.ts +0 -45
  202. package/src/value/smush.ts +0 -89
  203. package/src/value/transform.ts +0 -153
  204. package/src/value/unsmush.ts +0 -84
@@ -1,244 +0,0 @@
1
- import type {
2
- ArrayOrPlainObject,
3
- Constructor,
4
- Key,
5
- PlainObject,
6
- Primitive,
7
- TypedArray,
8
- } from '../models';
9
-
10
- // #region Functions
11
-
12
- /**
13
- * Is the value an array or a plain object?
14
- *
15
- * @param value Value to check
16
- * @returns `true` if the value is an array or a plain object, otherwise `false`
17
- */
18
- export function isArrayOrPlainObject(value: unknown): value is ArrayOrPlainObject {
19
- return Array.isArray(value) || isPlainObject(value);
20
- }
21
-
22
- /**
23
- * Is the value a constructor function?
24
- *
25
- * @param value Value to check
26
- * @returns `true` if the value is a constructor function, otherwise `false`
27
- */
28
- export function isConstructor(value: unknown): value is Constructor {
29
- return typeof value === 'function' && value.prototype?.constructor === value;
30
- }
31
-
32
- /**
33
- * Is the value an instance of the constructor?
34
- *
35
- * @param constructor Class constructor
36
- * @param value Value to check
37
- * @returns `true` if the value is an instance of the constructor, otherwise `false`
38
- */
39
- export function isInstanceOf<Instance>(
40
- constructor: Constructor<Instance>,
41
- value: unknown,
42
- ): value is Instance {
43
- return isConstructor(constructor) && value instanceof constructor;
44
- }
45
-
46
- /**
47
- * Is the value a _Key_?
48
- *
49
- * @param value Value to check
50
- * @returns `true` if the value is a _Key_ _(`number` or `string`)_, otherwise `false`
51
- */
52
- export function isKey(value: unknown): value is Key {
53
- return typeof value === 'number' || typeof value === 'string';
54
- }
55
-
56
- /**
57
- * Is the value not an array or a plain object?
58
- *
59
- * @param value Value to check
60
- * @returns `true` if the value is not an array or a plain object, otherwise `false`
61
- */
62
- export function isNonArrayOrPlainObject<Value>(
63
- value: Value,
64
- ): value is Exclude<Value, ArrayOrPlainObject> {
65
- return !isArrayOrPlainObject(value);
66
- }
67
-
68
- /**
69
- * Is the value not a constructor function?
70
- *
71
- * @param value Value to check
72
- * @returns `true` if the value is not a constructor function, otherwise `false`
73
- */
74
- export function isNonConstructor<Value>(value: Value): value is Exclude<Value, Constructor> {
75
- return !isConstructor(value);
76
- }
77
-
78
- /**
79
- * Is the value not an instance of the constructor?
80
- *
81
- * @param constructor Class constructor
82
- * @param value Value to check
83
- * @returns `true` if the value is not an instance of the constructor, otherwise `false`
84
- */
85
- export function isNonInstanceOf<Instance, Value>(
86
- constructor: Constructor<Instance>,
87
- value: Value,
88
- ): value is Exclude<Value, Instance> {
89
- return !isInstanceOf(constructor, value);
90
- }
91
-
92
- /**
93
- * Is the value not a _Key_?
94
- *
95
- * @param value Value to check
96
- * @returns `true` if the value is not a _Key_ _(`number` or `string`)_, otherwise `false`
97
- */
98
- export function isNonKey<Value>(value: Value): value is Exclude<Value, Key> {
99
- return !isKey(value);
100
- }
101
-
102
- /**
103
- * Is the value not a number?
104
- *
105
- * @param value Value to check
106
- * @returns `true` if the value is not a `number`, otherwise `false`
107
- */
108
- export function isNonNumber<Value>(value: Value): value is Exclude<Value, number> {
109
- return !isNumber(value);
110
- }
111
-
112
- /**
113
- * Is the value not a plain object?
114
- *
115
- * @param value Value to check
116
- * @returns `true` if the value is not a plain object, otherwise `false`
117
- */
118
- export function isNonPlainObject<Value>(value: Value): value is Exclude<Value, PlainObject> {
119
- return !isPlainObject(value);
120
- }
121
-
122
- /**
123
- * Is the value not a primitive value?
124
- *
125
- * @param value Value to check
126
- * @returns `true` if the value is not a primitive value, otherwise `false`
127
- */
128
- export function isNonPrimitive<Value>(value: Value): value is Exclude<Value, Primitive> {
129
- return !isPrimitive(value);
130
- }
131
-
132
- /**
133
- * Is the value not a template strings array?
134
- *
135
- * @param value Value to check
136
- * @returns `true` if the value is not a `TemplateStringsArray`, otherwise `false`
137
- */
138
- export function isNonTemplateStringsArray<Value>(
139
- value: Value,
140
- ): value is Exclude<Value, TemplateStringsArray> {
141
- return !isTemplateStringsArray(value);
142
- }
143
-
144
- /**
145
- * Is the value not a typed array?
146
- *
147
- * @param value Value to check
148
- * @returns `true` if the value is not a typed array, otherwise `false`
149
- */
150
- export function isNonTypedArray<Value>(value: Value): value is Exclude<Value, TypedArray> {
151
- return !isTypedArray(value);
152
- }
153
-
154
- /**
155
- * Is the value a number?
156
- *
157
- * @param value Value to check
158
- * @returns `true` if the value is a `number`, otherwise `false`
159
- */
160
- export function isNumber(value: unknown): value is number {
161
- return typeof value === 'number' && !Number.isNaN(value);
162
- }
163
-
164
- /**
165
- * Is the value a plain object?
166
- *
167
- * @param value Value to check
168
- * @returns `true` if the value is a plain object, otherwise `false`
169
- */
170
- export function isPlainObject(value: unknown): value is PlainObject {
171
- if (value === null || typeof value !== 'object') {
172
- return false;
173
- }
174
-
175
- if (Symbol.toStringTag in value || Symbol.iterator in value) {
176
- return false;
177
- }
178
-
179
- const prototype = Object.getPrototypeOf(value);
180
-
181
- return (
182
- prototype === null ||
183
- prototype === Object.prototype ||
184
- Object.getPrototypeOf(prototype) === null
185
- );
186
- }
187
-
188
- /**
189
- * Is the value a primitive value?
190
- *
191
- * @param value Value to check
192
- * @returns `true` if the value is a primitive value, otherwise `false`
193
- */
194
- export function isPrimitive(value: unknown): value is Primitive {
195
- if (value == null) {
196
- return true;
197
- }
198
-
199
- const type = typeof value;
200
-
201
- return (
202
- type === 'bigint' ||
203
- type === 'boolean' ||
204
- type === 'number' ||
205
- type === 'string' ||
206
- type === 'symbol'
207
- );
208
- }
209
-
210
- /**
211
- * Is the value a template strings array?
212
- *
213
- * @param value Value to check
214
- * @returns `true` if the value is a `TemplateStringsArray`, otherwise `false`
215
- */
216
- export function isTemplateStringsArray(value: unknown): value is TemplateStringsArray {
217
- return Array.isArray(value) && Array.isArray((value as unknown as TemplateStringsArray).raw);
218
- }
219
-
220
- /**
221
- * Is the value a typed array?
222
- *
223
- * @param value Value to check
224
- * @returns `true` if the value is a typed array, otherwise `false`
225
- */
226
- export function isTypedArray(value: unknown): value is TypedArray {
227
- (isTypedArray as any).types ??= new Set<unknown>([
228
- Int8Array,
229
- Uint8Array,
230
- Uint8ClampedArray,
231
- Int16Array,
232
- Uint16Array,
233
- Int32Array,
234
- Uint32Array,
235
- Float32Array,
236
- Float64Array,
237
- BigInt64Array,
238
- BigUint64Array,
239
- ]);
240
-
241
- return (isTypedArray as any).types.has((value as TypedArray)?.constructor);
242
- }
243
-
244
- // #endregion
@@ -1,164 +0,0 @@
1
- import type {NumericalValues, PlainObject} from '../../models';
2
- import {isNonNumber} from '../is';
3
-
4
- // #region Types
5
-
6
- type Aggregation = {
7
- count: number;
8
- value: number;
9
- };
10
-
11
- type AggregationCallback = (current: number, value: number, notNumber: boolean) => number;
12
-
13
- export type AggregationType = 'average' | 'max' | 'min' | 'sum';
14
-
15
- type NonAverageAggregationType = 'max' | 'min' | 'sum';
16
-
17
- // #endregion
18
-
19
- // #region Functions
20
-
21
- export function aggregate(type: AggregationType, array: unknown[], key: unknown): Aggregation {
22
- const length = Array.isArray(array) ? array.length : 0;
23
-
24
- if (length === 0) {
25
- return {
26
- count: 0,
27
- value: Number.NaN,
28
- };
29
- }
30
-
31
- const aggregator = aggregators[type];
32
- const callback = getAggregateCallback(key);
33
-
34
- let counted = 0;
35
- let aggregated = Number.NaN;
36
- let notNumber = true;
37
-
38
- for (let index = 0; index < length; index += 1) {
39
- const item = array[index];
40
-
41
- const value = callback == null ? item : callback(item as never, index, array);
42
-
43
- if (isNonNumber(value)) {
44
- continue;
45
- }
46
-
47
- aggregated = aggregator(aggregated, value, notNumber);
48
-
49
- counted += 1;
50
- notNumber = false;
51
- }
52
-
53
- return {
54
- count: counted,
55
- value: aggregated,
56
- };
57
- }
58
-
59
- export function getAggregateCallback(key: unknown): Function | undefined {
60
- if (key == null) {
61
- return;
62
- }
63
-
64
- return typeof key === 'function' ? key : (item: PlainObject): unknown => item[key as never];
65
- }
66
-
67
- /**
68
- * Get the maximum value from a list of items
69
- *
70
- * @example
71
- * ```typescript
72
- * max(
73
- * [{id: 1, value: 10}, {id: 2, value: 20}],
74
- * item => item.value,
75
- * ); // => 20
76
- *
77
- * max([], item => item.value); // => Number.NaN
78
- * ```
79
- *
80
- * @param items List of items
81
- * @param callback Callback to get an item's value
82
- * @returns Maximum value, or `Number.NaN` if no maximum can be found
83
- */
84
- export function max<Item>(
85
- items: Item[],
86
- callback: (item: Item, index: number, array: Item[]) => number,
87
- ): number;
88
-
89
- /**
90
- * Get the maximum value from a list of items
91
- *
92
- * @example
93
- * ```typescript
94
- * max(
95
- * [{id: 1, value: 10}, {id: 2, value: 20}],
96
- * 'value',
97
- * ); // => 20
98
- *
99
- * max([], 'value'); // => Number.NaN
100
- * ```
101
- *
102
- * @param items List of items
103
- * @param key Key to use for value
104
- * @returns Maximum value, or `Number.NaN` if no maximum can be found
105
- */
106
- export function max<Item extends PlainObject, ItemKey extends keyof NumericalValues<Item>>(
107
- items: Item[],
108
- key: ItemKey,
109
- ): number;
110
-
111
- /**
112
- * Get the maximum value from a list of numbers
113
- *
114
- * @example
115
- * ```typescript
116
- * max([10, 20]); // => 20
117
- * max([]); // => Number.NaN
118
- * ```
119
- *
120
- * @param values List of numbers
121
- * @returns Maximum value, or `Number.NaN` if no maximum can be found
122
- */
123
- export function max(values: number[]): number;
124
-
125
- export function max(array: unknown[], key?: unknown): number {
126
- return getAggregated(AGGREGATION_MAX as NonAverageAggregationType, array, key);
127
- }
128
-
129
- function calculateSum(current: number, value: number, notNumber: boolean): number {
130
- return notNumber ? value : current + value;
131
- }
132
-
133
- export function getAggregated(
134
- type: NonAverageAggregationType,
135
- array: unknown[],
136
- key?: unknown,
137
- ): number {
138
- const aggregated = aggregate(type, array, key);
139
-
140
- return aggregated.count > 0 ? aggregated.value : Number.NaN;
141
- }
142
-
143
- // #endregion
144
-
145
- // #region Variables
146
-
147
- export const AGGREGATION_AVERAGE: AggregationType = 'average';
148
-
149
- export const AGGREGATION_MAX = 'max';
150
-
151
- export const AGGREGATION_MIN = 'min';
152
-
153
- export const AGGREGATION_SUM = 'sum';
154
-
155
- const aggregators: Record<AggregationType, AggregationCallback> = {
156
- average: calculateSum,
157
- max: (current: number, value: number, notNumber: boolean) =>
158
- notNumber || value > current ? value : current,
159
- min: (current: number, value: number, notNumber: boolean) =>
160
- notNumber || value < current ? value : current,
161
- sum: calculateSum,
162
- };
163
-
164
- // #endregion
@@ -1,28 +0,0 @@
1
- /**
2
- * Round a number
3
- *
4
- * @param value Number to round
5
- * @param decimals Number of decimal places to round to _(defaults to `0`)_
6
- * @returns Rounded number, or `Number.NaN` if the value if unable to be rounded
7
- */
8
- export function round(value: number, decimals?: number): number {
9
- return roundNumber(Math.round, value, decimals);
10
- }
11
-
12
- export function roundNumber(
13
- callback: (value: number) => number,
14
- value: number,
15
- decimals?: number,
16
- ): number {
17
- if (typeof value !== 'number') {
18
- return Number.NaN;
19
- }
20
-
21
- if (typeof decimals !== 'number' || decimals < 1) {
22
- return callback(value);
23
- }
24
-
25
- const mod = 10 ** decimals;
26
-
27
- return callback((value + Number.EPSILON) * mod) / mod;
28
- }
@@ -1,129 +0,0 @@
1
- import {isNumber} from './is';
2
-
3
- // #region Functions
4
-
5
- /**
6
- * Is the number between a minimum and maximum value?
7
- *
8
- * @param value Value to check
9
- * @param minimum Minimum value
10
- * @param maximum Maximum value
11
- * @returns `true` if the value is between the minimum and maximum, otherwise `false`
12
- */
13
- export function between(value: number, minimum: number, maximum: number): boolean {
14
- if (![value, minimum, maximum].every(isNumber)) {
15
- return false;
16
- }
17
-
18
- if (Object.is(minimum, maximum)) {
19
- return Object.is(value, minimum);
20
- }
21
-
22
- const max = maximum > minimum ? maximum : minimum;
23
- const min = maximum > minimum ? minimum : maximum;
24
-
25
- return value >= min && value <= max;
26
- }
27
-
28
- /**
29
- * Clamp a number between a minimum and maximum value
30
- *
31
- * @param value Value to clamp
32
- * @param minimum Minimum value
33
- * @param maximum Maximum value
34
- * @param loop If `true`, the value will loop around when smaller than the minimum or larger than the maximum _(defaults to `false`)_
35
- * @returns Clamped value
36
- *
37
- * @example
38
- * ```typescript
39
- * clamp(10, 0, 5); // => 5
40
- * clamp(10, 0, 5, true); // => 0
41
- * ```
42
- */
43
- export function clamp(value: number, minimum: number, maximum: number, loop?: boolean): number {
44
- if (![value, minimum, maximum].every(isNumber)) {
45
- return Number.NaN;
46
- }
47
-
48
- if (value < minimum) {
49
- return loop === true ? maximum : minimum;
50
- }
51
-
52
- const next = loop === true ? minimum : maximum;
53
-
54
- return value > maximum ? next : value;
55
- }
56
-
57
- /**
58
- * Get the number value from an unknown value
59
- *
60
- * _(Based on Lodash)_
61
- *
62
- * @param value Original value
63
- * @returns Original value as a number, or `Number.NaN` if the value is unable to be parsed
64
- */
65
- export function getNumber(value: unknown): number {
66
- if (typeof value === 'number') {
67
- return value;
68
- }
69
-
70
- if (typeof value === 'bigint' || typeof value === 'boolean') {
71
- return Number(value);
72
- }
73
-
74
- if (value == null || typeof value === 'symbol') {
75
- return Number.NaN;
76
- }
77
-
78
- if (typeof value === 'function') {
79
- return getNumber(value());
80
- }
81
-
82
- let parsed = value.valueOf();
83
-
84
- if (typeof parsed === 'object') {
85
- parsed = parsed.toString();
86
- }
87
-
88
- if (typeof parsed !== 'string') {
89
- return getNumber(parsed);
90
- }
91
-
92
- const trimmed = parsed.trim();
93
-
94
- if (trimmed.length === 0) {
95
- return Number.NaN;
96
- }
97
-
98
- if (EXPRESSION_ZEROISH.test(parsed)) {
99
- return 0;
100
- }
101
-
102
- const isBinary = EXPRESSION_BINARY.test(trimmed);
103
-
104
- if (isBinary || EXPRESSION_OCTAL.test(trimmed)) {
105
- return Number.parseInt(trimmed.slice(2), isBinary ? 2 : OCTAL_VALUE);
106
- }
107
-
108
- return Number(trimmed);
109
- }
110
-
111
- export function getNumberOrDefault(value: unknown, defaultValue: number, minimum?: number): number {
112
- return typeof value === 'number' && !Number.isNaN(value) && value >= (minimum ?? 0)
113
- ? Math.floor(value)
114
- : defaultValue;
115
- }
116
-
117
- // #endregion
118
-
119
- // #region Variables
120
-
121
- const EXPRESSION_BINARY = /^0b[01]+$/i;
122
-
123
- const EXPRESSION_OCTAL = /^0o[0-7]+$/i;
124
-
125
- const EXPRESSION_ZEROISH = /^\s*0+\s*$/;
126
-
127
- const OCTAL_VALUE = 8;
128
-
129
- // #endregion
@@ -1,49 +0,0 @@
1
- import {isNumber} from './is';
2
-
3
- // #region Functions
4
-
5
- function getRandomFloatingNumberValue(
6
- inclusive: boolean,
7
- minimum?: number,
8
- maximum?: number,
9
- ): number {
10
- let maxFloat =
11
- isNumber(maximum) && maximum <= Number.MAX_SAFE_INTEGER ? maximum : Number.MAX_SAFE_INTEGER;
12
-
13
- let minFloat =
14
- isNumber(minimum) && minimum >= Number.MIN_SAFE_INTEGER ? minimum : Number.MIN_SAFE_INTEGER;
15
-
16
- if (minFloat === maxFloat) {
17
- return minFloat;
18
- }
19
-
20
- if (minFloat > maxFloat) {
21
- [minFloat, maxFloat] = [maxFloat, minFloat];
22
- }
23
-
24
- return Math.random() * (maxFloat + (inclusive ? 1 : 0) - minFloat) + minFloat;
25
- }
26
-
27
- /**
28
- * Get a random floating-point number
29
- *
30
- * @param minimum Minimum value
31
- * @param maximum Maximum value
32
- * @returns Random floating-point number
33
- */
34
- export function getRandomFloatingNumber(minimum?: number, maximum?: number): number {
35
- return getRandomFloatingNumberValue(false, minimum, maximum);
36
- }
37
-
38
- /**
39
- * Get a random integer
40
- *
41
- * @param minimum Minimum value
42
- * @param maximum Maximum value
43
- * @returns Random integer
44
- */
45
- export function getRandomInteger(minimum?: number, maximum?: number): number {
46
- return Math.floor(getRandomFloatingNumberValue(true, minimum, maximum));
47
- }
48
-
49
- // #endregion
@@ -1,92 +0,0 @@
1
- import type {PlainObject} from '../models';
2
- import type {Err, ExtendedErr, Ok, Result} from '../result/models';
3
- import {isNonPlainObject} from './is';
4
-
5
- // #region Functions
6
-
7
- /**
8
- * Is the _Result_ an extended error?
9
- *
10
- * @param result _Result_ to check
11
- * @returns `true` if the _Result_ is an extended error, otherwise `false`
12
- */
13
- export function isError<Value, E = Error>(
14
- result: ExtendedErr<E> | Result<Value, E>,
15
- extended: true,
16
- ): result is ExtendedErr<E>;
17
-
18
- /**
19
- * Is the _Result_ an error?
20
- *
21
- * @param result _Result_ to check
22
- * @returns `true` if the _Result_ is an error, otherwise `false`
23
- */
24
- export function isError<Value, E = Error>(result: Result<Value, E>): result is Err<E>;
25
-
26
- /**
27
- * Is the value an error?
28
- *
29
- * @param value Value to check
30
- * @returns `true` if the value is an error, otherwise `false`
31
- */
32
- export function isError(value: unknown): value is Err<unknown> | ExtendedErr<unknown>;
33
-
34
- export function isError(
35
- value: unknown,
36
- extended?: boolean,
37
- ): value is Err<unknown> | ExtendedErr<unknown> {
38
- return (
39
- isResultValue(value, false) &&
40
- (extended === true ? (value as ExtendedErr<unknown>).original instanceof Error : true)
41
- );
42
- }
43
-
44
- /**
45
- * Is the _Result_ ok?
46
- *
47
- * @param value _Result_ to check
48
- * @returns `true` if the _Result_ is ok, otherwise `false`
49
- */
50
- export function isOk<Value, E = Error>(value: Result<Value, E>): value is Ok<Value>;
51
-
52
- /**
53
- * Is the value ok?
54
- *
55
- * @param value Value to check
56
- * @returns `true` if the value is ok, otherwise `false`
57
- */
58
- export function isOk(value: unknown): value is Ok<unknown>;
59
-
60
- export function isOk(value: unknown): value is Ok<unknown> {
61
- return isResultValue(value, true);
62
- }
63
-
64
- /**
65
- * Is the value a _Result_?
66
- *
67
- * @param value Value to check
68
- * @returns `true` if the value is a _Result_, otherwise `false`
69
- */
70
- export function isResult(value: unknown): value is ExtendedErr<unknown> | Result<unknown, unknown> {
71
- return isResultValue(value, true) || isResultValue(value, false);
72
- }
73
-
74
- function isResultValue(value: unknown, okValue: boolean): value is Result<unknown, unknown> {
75
- if (isNonPlainObject(value)) {
76
- return false;
77
- }
78
-
79
- return (
80
- (value as PlainObject).ok === okValue && (okValue ? PROPERTY_VALUE : PROPERTY_ERROR) in value
81
- );
82
- }
83
-
84
- // #endregion
85
-
86
- // #region Variable
87
-
88
- const PROPERTY_ERROR = 'error';
89
-
90
- const PROPERTY_VALUE = 'value';
91
-
92
- // #endregion