shelving 1.36.0 → 1.37.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 +1 -1
- package/stream/ArrayState.d.ts +2 -2
- package/stream/ArrayState.js +4 -4
- package/util/iterate.d.ts +0 -5
- package/util/iterate.js +3 -10
- package/util/number.d.ts +5 -1
- package/util/number.js +16 -1
package/package.json
CHANGED
package/stream/ArrayState.d.ts
CHANGED
|
@@ -3,8 +3,8 @@ import { State } from "./State.js";
|
|
|
3
3
|
/** State that stores an array and has additional methods to help with that. */
|
|
4
4
|
export declare class ArrayState<T> extends State<ImmutableArray<T>> implements Iterable<T> {
|
|
5
5
|
_value: never[];
|
|
6
|
-
/**
|
|
7
|
-
get
|
|
6
|
+
/** Get the length of the current value of this state. */
|
|
7
|
+
get length(): number;
|
|
8
8
|
/** Add an item to this array. */
|
|
9
9
|
add(item: T): void;
|
|
10
10
|
/** Remove an item from this array. */
|
package/stream/ArrayState.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { swapItem, toggleItem, withItem, withoutItem
|
|
1
|
+
import { swapItem, toggleItem, withItem, withoutItem } from "../util/index.js";
|
|
2
2
|
import { State } from "./State.js";
|
|
3
3
|
/** State that stores an array and has additional methods to help with that. */
|
|
4
4
|
export class ArrayState extends State {
|
|
@@ -7,9 +7,9 @@ export class ArrayState extends State {
|
|
|
7
7
|
// Set default value to be empty array.
|
|
8
8
|
this._value = [];
|
|
9
9
|
}
|
|
10
|
-
/**
|
|
11
|
-
get
|
|
12
|
-
return
|
|
10
|
+
/** Get the length of the current value of this state. */
|
|
11
|
+
get length() {
|
|
12
|
+
return this.value.length;
|
|
13
13
|
}
|
|
14
14
|
/** Add an item to this array. */
|
|
15
15
|
add(item) {
|
package/util/iterate.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { ImmutableMap } from "./map.js";
|
|
2
1
|
import type { ImmutableArray } from "./array.js";
|
|
3
2
|
import type { Entry } from "./entry.js";
|
|
4
3
|
import { DONE } from "./constants.js";
|
|
@@ -23,8 +22,6 @@ export declare const isIterable: <T extends Iterable<unknown>>(value: unknown) =
|
|
|
23
22
|
* - Note: Array and Map instances etc will return true because they implement `Symbol.iterator`
|
|
24
23
|
*/
|
|
25
24
|
export declare const isAsyncIterable: <T extends AsyncIterable<unknown>>(value: unknown) => value is T;
|
|
26
|
-
/** Get the known size or length of an object (e.g. `Array`, `Map`, and `Set` have known size), or return `undefined` if the size cannot be established. */
|
|
27
|
-
export declare const getSize: (obj: Iterable<unknown> | ImmutableMap | ImmutableArray) => number | undefined;
|
|
28
25
|
/**
|
|
29
26
|
* Count the number items of an iterable.
|
|
30
27
|
* - Checks `items.size` or `items.length` first, or consumes the iterable and counts its iterations.
|
|
@@ -35,8 +32,6 @@ export declare function countItems(items: Iterable<unknown>): number;
|
|
|
35
32
|
* - Note: this consumes the iterable so you won't be able to use it again.
|
|
36
33
|
*/
|
|
37
34
|
export declare function countIterations(items: Iterable<unknown>): number;
|
|
38
|
-
/** Sum an iterable set of numbers and return the total. */
|
|
39
|
-
export declare function sumItems(input: Iterable<number>): number;
|
|
40
35
|
/**
|
|
41
36
|
* Yield a range of numbers from `start` to `end`
|
|
42
37
|
* - Yields in descending order if `end` is lower than `start`
|
package/util/iterate.js
CHANGED
|
@@ -13,14 +13,14 @@ export const isIterable = (value) => typeof value === "object" && !!value && Sym
|
|
|
13
13
|
*/
|
|
14
14
|
export const isAsyncIterable = (value) => typeof value === "object" && !!value && Symbol.asyncIterator in value;
|
|
15
15
|
/** Get the known size or length of an object (e.g. `Array`, `Map`, and `Set` have known size), or return `undefined` if the size cannot be established. */
|
|
16
|
-
|
|
16
|
+
const getKnownSize = (obj) => ("size" in obj && typeof obj.size === "number" ? obj.size : "length" in obj && typeof obj.length === "number" ? obj.length : undefined);
|
|
17
17
|
/**
|
|
18
18
|
* Count the number items of an iterable.
|
|
19
19
|
* - Checks `items.size` or `items.length` first, or consumes the iterable and counts its iterations.
|
|
20
20
|
*/
|
|
21
21
|
export function countItems(items) {
|
|
22
22
|
var _a;
|
|
23
|
-
return (_a =
|
|
23
|
+
return (_a = getKnownSize(items)) !== null && _a !== void 0 ? _a : countIterations(items);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* Count the number of iterations an iterable does.
|
|
@@ -32,13 +32,6 @@ export function countIterations(items) {
|
|
|
32
32
|
count++; // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
33
33
|
return count;
|
|
34
34
|
}
|
|
35
|
-
/** Sum an iterable set of numbers and return the total. */
|
|
36
|
-
export function sumItems(input) {
|
|
37
|
-
let sum = 0;
|
|
38
|
-
for (const num of input)
|
|
39
|
-
sum += num;
|
|
40
|
-
return sum;
|
|
41
|
-
}
|
|
42
35
|
/**
|
|
43
36
|
* Yield a range of numbers from `start` to `end`
|
|
44
37
|
* - Yields in descending order if `end` is lower than `start`
|
|
@@ -57,7 +50,7 @@ export function* yieldRange(start, end) {
|
|
|
57
50
|
*/
|
|
58
51
|
export function limitItems(items, limit) {
|
|
59
52
|
var _a;
|
|
60
|
-
const size = (_a =
|
|
53
|
+
const size = (_a = getKnownSize(items)) !== null && _a !== void 0 ? _a : Infinity;
|
|
61
54
|
return size <= limit ? items : yieldUntilLimit(items, limit);
|
|
62
55
|
}
|
|
63
56
|
/** Yield items from a source iterable until we hit a maximum iteration count. */
|
package/util/number.d.ts
CHANGED
|
@@ -49,10 +49,14 @@ export declare const formatNumber: (num: number, precision?: number) => string;
|
|
|
49
49
|
*/
|
|
50
50
|
export declare const isBetween: (num: number, start: number, end: number) => boolean;
|
|
51
51
|
/**
|
|
52
|
-
*
|
|
52
|
+
* Apply a min/max to a number to return a number that's definitely in the specified range.
|
|
53
53
|
*
|
|
54
54
|
* @param num The number to apply the min/max to, e.g. `17`
|
|
55
55
|
* @param start The start of the range, e.g. `10`
|
|
56
56
|
* @param end The end of the range, e.g. `20`
|
|
57
57
|
*/
|
|
58
58
|
export declare const getBetween: (num: number, start: number, end: number) => number;
|
|
59
|
+
/** Sum an iterable set of numbers and return the total. */
|
|
60
|
+
export declare function sumNumbers(nums: Iterable<number>): number;
|
|
61
|
+
/** Find the number that's closest to a target in an iterable set of numbers. */
|
|
62
|
+
export declare function getClosestNumber<T extends number>(nums: Iterable<T>, target: number): T | undefined;
|
package/util/number.js
CHANGED
|
@@ -68,10 +68,25 @@ export const formatNumber = (num, precision = 10) => new Intl.NumberFormat(undef
|
|
|
68
68
|
*/
|
|
69
69
|
export const isBetween = (num, start, end) => num >= start && num <= end;
|
|
70
70
|
/**
|
|
71
|
-
*
|
|
71
|
+
* Apply a min/max to a number to return a number that's definitely in the specified range.
|
|
72
72
|
*
|
|
73
73
|
* @param num The number to apply the min/max to, e.g. `17`
|
|
74
74
|
* @param start The start of the range, e.g. `10`
|
|
75
75
|
* @param end The end of the range, e.g. `20`
|
|
76
76
|
*/
|
|
77
77
|
export const getBetween = (num, start, end) => Math.max(start, Math.min(end, num));
|
|
78
|
+
/** Sum an iterable set of numbers and return the total. */
|
|
79
|
+
export function sumNumbers(nums) {
|
|
80
|
+
let sum = 0;
|
|
81
|
+
for (const num of nums)
|
|
82
|
+
sum += num;
|
|
83
|
+
return sum;
|
|
84
|
+
}
|
|
85
|
+
/** Find the number that's closest to a target in an iterable set of numbers. */
|
|
86
|
+
export function getClosestNumber(nums, target) {
|
|
87
|
+
let closest = undefined;
|
|
88
|
+
for (const item of nums)
|
|
89
|
+
if (closest === undefined || Math.abs(item - target) < Math.abs(closest - target))
|
|
90
|
+
closest = item;
|
|
91
|
+
return closest;
|
|
92
|
+
}
|