@rimbu/common 2.0.3 → 2.0.5

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.
@@ -1,58 +0,0 @@
1
- /**
2
- * A range definition for any type of (orderable) value.
3
- * If a start or end is defined, a tuple can be used where the second item is a boolean
4
- * indicating whether that end is inclusive (true) or exclusive (false).<br/>
5
- * A Range of type T can have one of the following forms:<br/>
6
- * <br/>
7
- * - { end: T }<br/>
8
- * - { end: [T, boolean] }<br/>
9
- * - { start: T }<br/>
10
- * - { start: T, end: T }<br/>
11
- * - { start: T, end: [T, boolean] }<br/>
12
- * - { start: [T, boolean] }<br/>
13
- * - { start: [T, boolean], end: T }<br/>
14
- * - { start: [T, boolean], end: [T, boolean] }<br/>
15
- */
16
- export type Range<T> =
17
- | { start: T | [T, boolean]; end?: T | [T, boolean]; amount?: undefined }
18
- | { start?: T | [T, boolean]; end: T | [T, boolean]; amount?: undefined };
19
-
20
- export namespace Range {
21
- /**
22
- * Simplifies a given `range` `Range` input for easier processing, by returning optional
23
- * start and end ranges including whether they are inclusive or exclusive
24
- * @param range - the `Range` to use
25
- */
26
- export function getNormalizedRange<T>(range: Range<T>): {
27
- start?: [T, boolean] | undefined;
28
- end?: [T, boolean] | undefined;
29
- } {
30
- let start: [T, boolean] | undefined = undefined;
31
- let end: [T, boolean] | undefined = undefined;
32
-
33
- if (`start` in range && undefined !== range.start) {
34
- if (
35
- Array.isArray(range.start) &&
36
- range.start.length === 2 &&
37
- typeof range.start[1] === 'boolean'
38
- ) {
39
- start = range.start;
40
- } else {
41
- start = [range.start as T, true];
42
- }
43
- }
44
- if (`end` in range && undefined !== range.end) {
45
- if (
46
- Array.isArray(range.end) &&
47
- range.end.length === 2 &&
48
- typeof range.end[1] === 'boolean'
49
- ) {
50
- end = range.end;
51
- } else {
52
- end = [range.end as T, true];
53
- }
54
- }
55
-
56
- return { start, end };
57
- }
58
- }
@@ -1,59 +0,0 @@
1
- /**
2
- * An object used to track the state of a traversal, e.g. a `forEach`.
3
- */
4
- export interface TraverseState {
5
- /**
6
- * Returns true if the traversal has been halted by the user.
7
- */
8
- readonly halted: boolean;
9
- /**
10
- * Returns the current index of the traversal.
11
- */
12
- readonly currentIndex: number;
13
-
14
- /**
15
- * Increases the `currentIndex` of this traversal
16
- * @returns the next index that the traversal should use
17
- */
18
- nextIndex(): number;
19
- /**
20
- * Sets the `halted` value to true.
21
- */
22
- halt(): void;
23
- /**
24
- * Sets the `halted` value to false, and resets the `currentIndex`
25
- * value to its start value.
26
- */
27
- reset(): void;
28
- }
29
-
30
- class TraverseStateImpl implements TraverseState {
31
- currentIndex: number;
32
- halted = false;
33
-
34
- constructor(readonly startIndex: number) {
35
- this.currentIndex = startIndex;
36
- }
37
-
38
- nextIndex(): number {
39
- return this.currentIndex++;
40
- }
41
-
42
- readonly halt = (): void => {
43
- this.halted = true;
44
- };
45
-
46
- reset(): void {
47
- this.currentIndex = this.startIndex;
48
- this.halted = false;
49
- }
50
- }
51
-
52
- /**
53
- * Returns a new `TraverseState` instance, using optionally given `startIndex` as a start
54
- * index value.
55
- * @param startIndex - (default: 0) the start index to use
56
- */
57
- export function TraverseState(startIndex = 0): TraverseState {
58
- return new TraverseStateImpl(startIndex);
59
- }
@@ -1,40 +0,0 @@
1
- /**
2
- * Accepts all types of T and U where T extends U,
3
- * and result in the upper bound U.
4
- */
5
- export type SuperOf<U, T> = T extends U ? U : never;
6
-
7
- /**
8
- * Accepts all types of T and S where S extends T,
9
- * and results in the lower bound S.
10
- */
11
- export type SubOf<S, T> = S extends T ? S : never;
12
-
13
- /**
14
- * Accepts all types of T and U where T extends U or U extends T.
15
- */
16
- export type RelatedTo<T, U> = T | SuperOf<U, T>;
17
-
18
- /**
19
- * Accepts all arrays with at least one element.
20
- */
21
- export type ArrayNonEmpty<T> = [T, ...T[]];
22
-
23
- /**
24
- * Accepts all strings with at least one character.
25
- */
26
- export type StringNonEmpty<T> = T extends string
27
- ? '' extends T
28
- ? never
29
- : T
30
- : never;
31
-
32
- /**
33
- * Utility type to convert some object to a JSON serializable format.
34
- * @typeparam V - the `value` type
35
- * @typeparam D - the `dataType` tag string type
36
- */
37
- export interface ToJSON<V, D extends string = string> {
38
- readonly dataType: D;
39
- readonly value: V;
40
- }
@@ -1,24 +0,0 @@
1
- /**
2
- * A value of type T, or a function taking a value of type T and returning a new value of type T.
3
- */
4
- export type Update<T> = T | ((value: T) => T);
5
-
6
- /**
7
- * Returns the result of given `update` parameter, where it can either directly give a new value,
8
- * or it is a function receiving the given `value`, and returns a new value.
9
- * @param value - the current value
10
- * @param update - an `Update` value, either a new value or a function receiving the old value
11
- * and returning a new one.
12
- * @example
13
- * ```ts
14
- * Update(1, 2) // => 2
15
- * Update(1, () => 10) // => 10
16
- * Update(1, v => v + 1) // => 2
17
- * ```
18
- */
19
- export function Update<T>(value: T, update: Update<T>): T {
20
- if (typeof update === 'function') {
21
- return (update as (value: T) => T)(value);
22
- }
23
- return update;
24
- }