@naturalcycles/js-lib 14.72.0 → 14.73.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/dist/math/stack.util.d.ts +31 -3
- package/dist/math/stack.util.js +52 -4
- package/dist/number/number.util.d.ts +1 -1
- package/dist/number/number.util.js +1 -1
- package/dist-esm/math/stack.util.js +49 -2
- package/dist-esm/number/number.util.js +1 -1
- package/package.json +1 -1
- package/src/math/stack.util.ts +58 -3
- package/src/number/number.util.ts +1 -1
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Implements a "round-robin" Stack with a limited size.
|
|
2
|
+
* Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
|
|
3
3
|
* Like an array of a fixed size. When it runs out of space - it starts writing on top of itself
|
|
4
4
|
* from index 0.
|
|
5
|
+
*
|
|
6
|
+
*
|
|
5
7
|
*/
|
|
6
|
-
export declare class
|
|
8
|
+
export declare class Stack<T> {
|
|
7
9
|
readonly size: number;
|
|
8
10
|
constructor(size: number);
|
|
9
11
|
/**
|
|
@@ -12,10 +14,36 @@ export declare class SizeLimitedStack<T> {
|
|
|
12
14
|
*/
|
|
13
15
|
private nextIndex;
|
|
14
16
|
readonly items: T[];
|
|
15
|
-
push(item: T):
|
|
17
|
+
push(item: T): this;
|
|
18
|
+
/**
|
|
19
|
+
* Fill (overwrite) the whole Stack (all its items) with the passed `item`.
|
|
20
|
+
*/
|
|
21
|
+
fill(item: T): this;
|
|
16
22
|
/**
|
|
17
23
|
* Returns last items in the right order.
|
|
18
24
|
* Unlike raw `items` property that returns "items buffer" as-is (not ordered properly).
|
|
19
25
|
*/
|
|
20
26
|
get itemsOrdered(): T[];
|
|
21
27
|
}
|
|
28
|
+
/**
|
|
29
|
+
* Fixed-size FILO stack of Numbers.
|
|
30
|
+
* Has convenience stat methods, e.g percentile, avg, etc.
|
|
31
|
+
*/
|
|
32
|
+
export declare class NumberStack extends Stack<number> {
|
|
33
|
+
avg(): number;
|
|
34
|
+
/**
|
|
35
|
+
* Returns null if Stack is empty.
|
|
36
|
+
*/
|
|
37
|
+
avgOrNull(): number | null;
|
|
38
|
+
/**
|
|
39
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
40
|
+
*/
|
|
41
|
+
percentile(pc: number): number;
|
|
42
|
+
/**
|
|
43
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
44
|
+
* Returns null if Stack is empty.
|
|
45
|
+
*/
|
|
46
|
+
percentileOrNull(pc: number): number | null;
|
|
47
|
+
median(): number;
|
|
48
|
+
medianOrNull(): number | null;
|
|
49
|
+
}
|
package/dist/math/stack.util.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.NumberStack = exports.Stack = void 0;
|
|
4
|
+
const index_1 = require("../index");
|
|
4
5
|
/**
|
|
5
|
-
* Implements a "round-robin" Stack with a limited size.
|
|
6
|
+
* Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
|
|
6
7
|
* Like an array of a fixed size. When it runs out of space - it starts writing on top of itself
|
|
7
8
|
* from index 0.
|
|
9
|
+
*
|
|
10
|
+
*
|
|
8
11
|
*/
|
|
9
|
-
class
|
|
12
|
+
class Stack {
|
|
10
13
|
constructor(size) {
|
|
11
14
|
this.size = size;
|
|
12
15
|
/**
|
|
@@ -19,6 +22,14 @@ class SizeLimitedStack {
|
|
|
19
22
|
push(item) {
|
|
20
23
|
this.items[this.nextIndex] = item;
|
|
21
24
|
this.nextIndex = this.nextIndex === this.size - 1 ? 0 : this.nextIndex + 1;
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Fill (overwrite) the whole Stack (all its items) with the passed `item`.
|
|
29
|
+
*/
|
|
30
|
+
fill(item) {
|
|
31
|
+
(0, index_1._range)(this.size).forEach(i => (this.items[i] = item));
|
|
32
|
+
return this;
|
|
22
33
|
}
|
|
23
34
|
/**
|
|
24
35
|
* Returns last items in the right order.
|
|
@@ -33,4 +44,41 @@ class SizeLimitedStack {
|
|
|
33
44
|
return [...this.items.slice(this.nextIndex), ...this.items.slice(0, this.nextIndex)];
|
|
34
45
|
}
|
|
35
46
|
}
|
|
36
|
-
exports.
|
|
47
|
+
exports.Stack = Stack;
|
|
48
|
+
/**
|
|
49
|
+
* Fixed-size FILO stack of Numbers.
|
|
50
|
+
* Has convenience stat methods, e.g percentile, avg, etc.
|
|
51
|
+
*/
|
|
52
|
+
class NumberStack extends Stack {
|
|
53
|
+
avg() {
|
|
54
|
+
// _assert(this.items.length, 'NumberStack.avg cannot be called on empty stack')
|
|
55
|
+
return (0, index_1._average)(this.items);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Returns null if Stack is empty.
|
|
59
|
+
*/
|
|
60
|
+
avgOrNull() {
|
|
61
|
+
return this.items.length === 0 ? null : (0, index_1._average)(this.items);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
65
|
+
*/
|
|
66
|
+
percentile(pc) {
|
|
67
|
+
// _assert(this.items.length, 'NumberStack.percentile cannot be called on empty stack')
|
|
68
|
+
return (0, index_1._percentile)(this.items, pc);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
72
|
+
* Returns null if Stack is empty.
|
|
73
|
+
*/
|
|
74
|
+
percentileOrNull(pc) {
|
|
75
|
+
return this.items.length === 0 ? null : (0, index_1._percentile)(this.items, pc);
|
|
76
|
+
}
|
|
77
|
+
median() {
|
|
78
|
+
return (0, index_1._percentile)(this.items, 50);
|
|
79
|
+
}
|
|
80
|
+
medianOrNull() {
|
|
81
|
+
return this.items.length === 0 ? null : (0, index_1._percentile)(this.items, 50);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
exports.NumberStack = NumberStack;
|
|
@@ -44,7 +44,7 @@ export declare function _toFixed(n: number, fractionDigits: number): number;
|
|
|
44
44
|
* _toPrecision(1634.56, 1)
|
|
45
45
|
* // 2000
|
|
46
46
|
*
|
|
47
|
-
* _toPrecision(
|
|
47
|
+
* _toPrecision(1634.56, 2)
|
|
48
48
|
* // 1600
|
|
49
49
|
*/
|
|
50
50
|
export declare function _toPrecision(n: number, precision: number): number;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
+
import { _average, _percentile, _range } from '../index';
|
|
1
2
|
/**
|
|
2
|
-
* Implements a "round-robin" Stack with a limited size.
|
|
3
|
+
* Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
|
|
3
4
|
* Like an array of a fixed size. When it runs out of space - it starts writing on top of itself
|
|
4
5
|
* from index 0.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
5
8
|
*/
|
|
6
|
-
export class
|
|
9
|
+
export class Stack {
|
|
7
10
|
constructor(size) {
|
|
8
11
|
this.size = size;
|
|
9
12
|
/**
|
|
@@ -16,6 +19,14 @@ export class SizeLimitedStack {
|
|
|
16
19
|
push(item) {
|
|
17
20
|
this.items[this.nextIndex] = item;
|
|
18
21
|
this.nextIndex = this.nextIndex === this.size - 1 ? 0 : this.nextIndex + 1;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Fill (overwrite) the whole Stack (all its items) with the passed `item`.
|
|
26
|
+
*/
|
|
27
|
+
fill(item) {
|
|
28
|
+
_range(this.size).forEach(i => (this.items[i] = item));
|
|
29
|
+
return this;
|
|
19
30
|
}
|
|
20
31
|
/**
|
|
21
32
|
* Returns last items in the right order.
|
|
@@ -30,3 +41,39 @@ export class SizeLimitedStack {
|
|
|
30
41
|
return [...this.items.slice(this.nextIndex), ...this.items.slice(0, this.nextIndex)];
|
|
31
42
|
}
|
|
32
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Fixed-size FILO stack of Numbers.
|
|
46
|
+
* Has convenience stat methods, e.g percentile, avg, etc.
|
|
47
|
+
*/
|
|
48
|
+
export class NumberStack extends Stack {
|
|
49
|
+
avg() {
|
|
50
|
+
// _assert(this.items.length, 'NumberStack.avg cannot be called on empty stack')
|
|
51
|
+
return _average(this.items);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Returns null if Stack is empty.
|
|
55
|
+
*/
|
|
56
|
+
avgOrNull() {
|
|
57
|
+
return this.items.length === 0 ? null : _average(this.items);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
61
|
+
*/
|
|
62
|
+
percentile(pc) {
|
|
63
|
+
// _assert(this.items.length, 'NumberStack.percentile cannot be called on empty stack')
|
|
64
|
+
return _percentile(this.items, pc);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
68
|
+
* Returns null if Stack is empty.
|
|
69
|
+
*/
|
|
70
|
+
percentileOrNull(pc) {
|
|
71
|
+
return this.items.length === 0 ? null : _percentile(this.items, pc);
|
|
72
|
+
}
|
|
73
|
+
median() {
|
|
74
|
+
return _percentile(this.items, 50);
|
|
75
|
+
}
|
|
76
|
+
medianOrNull() {
|
|
77
|
+
return this.items.length === 0 ? null : _percentile(this.items, 50);
|
|
78
|
+
}
|
|
79
|
+
}
|
package/package.json
CHANGED
package/src/math/stack.util.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
+
import { _average, _percentile, _range } from '../index'
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* Implements a "round-robin" Stack with a limited size.
|
|
4
|
+
* Implements a "round-robin" Stack ("first-in last-out" aka FILO) with a limited size.
|
|
3
5
|
* Like an array of a fixed size. When it runs out of space - it starts writing on top of itself
|
|
4
6
|
* from index 0.
|
|
7
|
+
*
|
|
8
|
+
*
|
|
5
9
|
*/
|
|
6
|
-
export class
|
|
10
|
+
export class Stack<T> {
|
|
7
11
|
constructor(public readonly size: number) {}
|
|
8
12
|
|
|
9
13
|
/**
|
|
@@ -14,9 +18,18 @@ export class SizeLimitedStack<T> {
|
|
|
14
18
|
|
|
15
19
|
readonly items: T[] = []
|
|
16
20
|
|
|
17
|
-
push(item: T):
|
|
21
|
+
push(item: T): this {
|
|
18
22
|
this.items[this.nextIndex] = item
|
|
19
23
|
this.nextIndex = this.nextIndex === this.size - 1 ? 0 : this.nextIndex + 1
|
|
24
|
+
return this
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fill (overwrite) the whole Stack (all its items) with the passed `item`.
|
|
29
|
+
*/
|
|
30
|
+
fill(item: T): this {
|
|
31
|
+
_range(this.size).forEach(i => (this.items[i] = item))
|
|
32
|
+
return this
|
|
20
33
|
}
|
|
21
34
|
|
|
22
35
|
/**
|
|
@@ -33,3 +46,45 @@ export class SizeLimitedStack<T> {
|
|
|
33
46
|
return [...this.items.slice(this.nextIndex), ...this.items.slice(0, this.nextIndex)]
|
|
34
47
|
}
|
|
35
48
|
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Fixed-size FILO stack of Numbers.
|
|
52
|
+
* Has convenience stat methods, e.g percentile, avg, etc.
|
|
53
|
+
*/
|
|
54
|
+
export class NumberStack extends Stack<number> {
|
|
55
|
+
avg(): number {
|
|
56
|
+
// _assert(this.items.length, 'NumberStack.avg cannot be called on empty stack')
|
|
57
|
+
return _average(this.items)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns null if Stack is empty.
|
|
62
|
+
*/
|
|
63
|
+
avgOrNull(): number | null {
|
|
64
|
+
return this.items.length === 0 ? null : _average(this.items)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
69
|
+
*/
|
|
70
|
+
percentile(pc: number): number {
|
|
71
|
+
// _assert(this.items.length, 'NumberStack.percentile cannot be called on empty stack')
|
|
72
|
+
return _percentile(this.items, pc)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* `pc` is a number from 0 to 100 inclusive.
|
|
77
|
+
* Returns null if Stack is empty.
|
|
78
|
+
*/
|
|
79
|
+
percentileOrNull(pc: number): number | null {
|
|
80
|
+
return this.items.length === 0 ? null : _percentile(this.items, pc)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
median(): number {
|
|
84
|
+
return _percentile(this.items, 50)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
medianOrNull(): number | null {
|
|
88
|
+
return this.items.length === 0 ? null : _percentile(this.items, 50)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -65,7 +65,7 @@ export function _toFixed(n: number, fractionDigits: number): number {
|
|
|
65
65
|
* _toPrecision(1634.56, 1)
|
|
66
66
|
* // 2000
|
|
67
67
|
*
|
|
68
|
-
* _toPrecision(
|
|
68
|
+
* _toPrecision(1634.56, 2)
|
|
69
69
|
* // 1600
|
|
70
70
|
*/
|
|
71
71
|
export function _toPrecision(n: number, precision: number): number {
|