@naturalcycles/js-lib 15.59.1 → 15.61.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.
@@ -158,6 +158,20 @@ export declare function _intersectsWith<T>(a1: T[], a2: T[] | Set<T>): boolean;
158
158
  * Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
159
159
  */
160
160
  export declare function _difference<T>(a1: T[], a2: T[] | Set<T>): T[];
161
+ /**
162
+ * Does NOT mutate the array, returns a filtered array instead.
163
+ */
164
+ export declare function _arrayRemove<T>(a: T[], itemToRemove: T): T[];
165
+ /**
166
+ * "Toggles" an item to be present or absent in the array,
167
+ * based on the predicate. Respects uniqueness.
168
+ *
169
+ * If predicate==false - item gets removed from the array.
170
+ * If predicate==true - item gets pushed to the array (unless it was already present).
171
+ *
172
+ * Pushing the item DOES MUTATE the array, same if you would do array.push manually.
173
+ */
174
+ export declare function _arrayPushOrRemove<T>(a: T[], item: T, predicate: boolean): T[];
161
175
  /**
162
176
  * Returns the sum of items, or 0 for empty array.
163
177
  */
@@ -282,6 +282,30 @@ export function _difference(a1, a2) {
282
282
  const a2set = a2 instanceof Set ? a2 : new Set(a2);
283
283
  return a1.filter(v => !a2set.has(v));
284
284
  }
285
+ /**
286
+ * Does NOT mutate the array, returns a filtered array instead.
287
+ */
288
+ export function _arrayRemove(a, itemToRemove) {
289
+ return a.filter(r => r !== itemToRemove);
290
+ }
291
+ /**
292
+ * "Toggles" an item to be present or absent in the array,
293
+ * based on the predicate. Respects uniqueness.
294
+ *
295
+ * If predicate==false - item gets removed from the array.
296
+ * If predicate==true - item gets pushed to the array (unless it was already present).
297
+ *
298
+ * Pushing the item DOES MUTATE the array, same if you would do array.push manually.
299
+ */
300
+ export function _arrayPushOrRemove(a, item, predicate) {
301
+ if (predicate) {
302
+ if (!a.includes(item)) {
303
+ a.push(item);
304
+ }
305
+ return a;
306
+ }
307
+ return a.filter(r => r !== item);
308
+ }
285
309
  /**
286
310
  * Returns the sum of items, or 0 for empty array.
287
311
  */
@@ -11,6 +11,19 @@ export declare function _average(values: number[]): number;
11
11
  * Same as _average, but safely returns null if input array is empty or nullish.
12
12
  */
13
13
  export declare function _averageOrNull(values: number[] | undefined | null): number | null;
14
+ /**
15
+ * @returns Population standard deviation of the array of numbers
16
+ *
17
+ * @example
18
+ *
19
+ * _stdDev([2, 4, 4, 4, 5, 5, 7, 9])
20
+ * // 2
21
+ */
22
+ export declare function _stdDev(values: number[]): number;
23
+ /**
24
+ * Same as _stdDev, but safely returns null if input array is empty or nullish.
25
+ */
26
+ export declare function _stdDevOrNull(values: number[] | undefined | null): number | null;
14
27
  /**
15
28
  * valuesArray and weightsArray length is expected to be the same.
16
29
  */
@@ -21,6 +21,30 @@ export function _average(values) {
21
21
  export function _averageOrNull(values) {
22
22
  return values?.length ? _average(values) : null;
23
23
  }
24
+ /**
25
+ * @returns Population standard deviation of the array of numbers
26
+ *
27
+ * @example
28
+ *
29
+ * _stdDev([2, 4, 4, 4, 5, 5, 7, 9])
30
+ * // 2
31
+ */
32
+ export function _stdDev(values) {
33
+ _assert(values.length, '_stdDev is called on empty array');
34
+ const avg = _average(values);
35
+ let sumSquaredDiff = 0;
36
+ for (const n of values) {
37
+ const diff = n - avg;
38
+ sumSquaredDiff += diff * diff;
39
+ }
40
+ return Math.sqrt(sumSquaredDiff / values.length);
41
+ }
42
+ /**
43
+ * Same as _stdDev, but safely returns null if input array is empty or nullish.
44
+ */
45
+ export function _stdDevOrNull(values) {
46
+ return values?.length ? _stdDev(values) : null;
47
+ }
24
48
  /**
25
49
  * valuesArray and weightsArray length is expected to be the same.
26
50
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.59.1",
4
+ "version": "15.61.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2",
7
7
  "undici": "^7",
@@ -326,6 +326,33 @@ export function _difference<T>(a1: T[], a2: T[] | Set<T>): T[] {
326
326
  return a1.filter(v => !a2set.has(v))
327
327
  }
328
328
 
329
+ /**
330
+ * Does NOT mutate the array, returns a filtered array instead.
331
+ */
332
+ export function _arrayRemove<T>(a: T[], itemToRemove: T): T[] {
333
+ return a.filter(r => r !== itemToRemove)
334
+ }
335
+
336
+ /**
337
+ * "Toggles" an item to be present or absent in the array,
338
+ * based on the predicate. Respects uniqueness.
339
+ *
340
+ * If predicate==false - item gets removed from the array.
341
+ * If predicate==true - item gets pushed to the array (unless it was already present).
342
+ *
343
+ * Pushing the item DOES MUTATE the array, same if you would do array.push manually.
344
+ */
345
+ export function _arrayPushOrRemove<T>(a: T[], item: T, predicate: boolean): T[] {
346
+ if (predicate) {
347
+ if (!a.includes(item)) {
348
+ a.push(item)
349
+ }
350
+ return a
351
+ }
352
+
353
+ return a.filter(r => r !== item)
354
+ }
355
+
329
356
  /**
330
357
  * Returns the sum of items, or 0 for empty array.
331
358
  */
@@ -23,6 +23,32 @@ export function _averageOrNull(values: number[] | undefined | null): number | nu
23
23
  return values?.length ? _average(values) : null
24
24
  }
25
25
 
26
+ /**
27
+ * @returns Population standard deviation of the array of numbers
28
+ *
29
+ * @example
30
+ *
31
+ * _stdDev([2, 4, 4, 4, 5, 5, 7, 9])
32
+ * // 2
33
+ */
34
+ export function _stdDev(values: number[]): number {
35
+ _assert(values.length, '_stdDev is called on empty array')
36
+ const avg = _average(values)
37
+ let sumSquaredDiff = 0
38
+ for (const n of values) {
39
+ const diff = n - avg
40
+ sumSquaredDiff += diff * diff
41
+ }
42
+ return Math.sqrt(sumSquaredDiff / values.length)
43
+ }
44
+
45
+ /**
46
+ * Same as _stdDev, but safely returns null if input array is empty or nullish.
47
+ */
48
+ export function _stdDevOrNull(values: number[] | undefined | null): number | null {
49
+ return values?.length ? _stdDev(values) : null
50
+ }
51
+
26
52
  /**
27
53
  * valuesArray and weightsArray length is expected to be the same.
28
54
  */