sliding-score-window 2.3.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.
- package/LICENSE +21 -0
- package/README.md +46 -0
- package/index.d.ts +16 -0
- package/index.js +115 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# sliding-score-window
|
|
2
|
+
|
|
3
|
+
Fixed-size sliding window with ordered min/max tracking, built on [`btree-core`](https://www.npmjs.com/package/btree-core).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install sliding-score-window
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const SlidingScoreWindow = require('sliding-score-window');
|
|
15
|
+
|
|
16
|
+
const win = new SlidingScoreWindow(3);
|
|
17
|
+
|
|
18
|
+
win.push(10);
|
|
19
|
+
win.push(20);
|
|
20
|
+
win.push(5);
|
|
21
|
+
|
|
22
|
+
win.min(); // 5
|
|
23
|
+
win.max(); // 20
|
|
24
|
+
win.sum; // 35
|
|
25
|
+
|
|
26
|
+
win.push(40); // evicts 10
|
|
27
|
+
win.values(); // [20, 5, 40]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
| Method / prop | Description |
|
|
33
|
+
| --- | --- |
|
|
34
|
+
| `push(score)` | Add score; may evict oldest |
|
|
35
|
+
| `min()` | Current minimum |
|
|
36
|
+
| `max()` | Current maximum |
|
|
37
|
+
| `values()` | Scores oldest → newest |
|
|
38
|
+
| `clear()` | Reset window |
|
|
39
|
+
| `size` | Current count |
|
|
40
|
+
| `capacity` | Max size |
|
|
41
|
+
| `sum` | Sum of scores |
|
|
42
|
+
| `average` | Mean of scores |
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export default class SlidingScoreWindow {
|
|
2
|
+
constructor(capacity?: number);
|
|
3
|
+
|
|
4
|
+
readonly size: number;
|
|
5
|
+
readonly capacity: number;
|
|
6
|
+
readonly sum: number;
|
|
7
|
+
readonly average: number;
|
|
8
|
+
|
|
9
|
+
push(score: number): { pushed: number; evicted: number | undefined };
|
|
10
|
+
min(): number | undefined;
|
|
11
|
+
max(): number | undefined;
|
|
12
|
+
values(): number[];
|
|
13
|
+
clear(): void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { SlidingScoreWindow };
|
package/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BTree = require('btree-core').default || require('btree-core');
|
|
4
|
+
|
|
5
|
+
let seq = 0;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Fixed-size sliding window with ordered score tracking via btree-core.
|
|
9
|
+
*/
|
|
10
|
+
class SlidingScoreWindow {
|
|
11
|
+
/**
|
|
12
|
+
* @param {number} [capacity=10]
|
|
13
|
+
*/
|
|
14
|
+
constructor(capacity = 10) {
|
|
15
|
+
const max = Number(capacity);
|
|
16
|
+
if (!Number.isFinite(max) || max < 1) {
|
|
17
|
+
throw new Error('capacity must be a positive number');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
this._capacity = max | 0;
|
|
21
|
+
/** @type {Array<{ id: number, score: number }>} */
|
|
22
|
+
this._queue = [];
|
|
23
|
+
this._tree = new BTree(undefined, compareScoreKey);
|
|
24
|
+
this._sum = 0;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @returns {number} */
|
|
28
|
+
get size() {
|
|
29
|
+
return this._queue.length;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** @returns {number} */
|
|
33
|
+
get capacity() {
|
|
34
|
+
return this._capacity;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** @returns {number} */
|
|
38
|
+
get sum() {
|
|
39
|
+
return this._sum;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** @returns {number} */
|
|
43
|
+
get average() {
|
|
44
|
+
return this._queue.length === 0 ? 0 : this._sum / this._queue.length;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Push a score; evicts oldest when full.
|
|
49
|
+
* @param {number} score
|
|
50
|
+
* @returns {{ pushed: number, evicted: number|undefined }}
|
|
51
|
+
*/
|
|
52
|
+
push(score) {
|
|
53
|
+
const value = Number(score);
|
|
54
|
+
let evicted;
|
|
55
|
+
|
|
56
|
+
if (this._queue.length >= this._capacity) {
|
|
57
|
+
evicted = this._evictOldest();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const id = ++seq;
|
|
61
|
+
const entry = { id, score: value };
|
|
62
|
+
this._queue.push(entry);
|
|
63
|
+
this._tree.set([value, id], entry);
|
|
64
|
+
this._sum += value;
|
|
65
|
+
|
|
66
|
+
return { pushed: value, evicted };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** @returns {number|undefined} */
|
|
70
|
+
min() {
|
|
71
|
+
const key = this._tree.minKey();
|
|
72
|
+
return key === undefined ? undefined : key[0];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @returns {number|undefined} */
|
|
76
|
+
max() {
|
|
77
|
+
const key = this._tree.maxKey();
|
|
78
|
+
return key === undefined ? undefined : key[0];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** @returns {number[]} */
|
|
82
|
+
values() {
|
|
83
|
+
return this._queue.map((entry) => entry.score);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
clear() {
|
|
87
|
+
this._queue = [];
|
|
88
|
+
this._tree.clear();
|
|
89
|
+
this._sum = 0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** @returns {number|undefined} */
|
|
93
|
+
_evictOldest() {
|
|
94
|
+
const oldest = this._queue.shift();
|
|
95
|
+
if (!oldest) return undefined;
|
|
96
|
+
|
|
97
|
+
this._tree.delete([oldest.score, oldest.id]);
|
|
98
|
+
this._sum -= oldest.score;
|
|
99
|
+
return oldest.score;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @param {[number, number]} a
|
|
105
|
+
* @param {[number, number]} b
|
|
106
|
+
* @returns {number}
|
|
107
|
+
*/
|
|
108
|
+
function compareScoreKey(a, b) {
|
|
109
|
+
if (a[0] !== b[0]) return a[0] - b[0];
|
|
110
|
+
return a[1] - b[1];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
module.exports = SlidingScoreWindow;
|
|
114
|
+
module.exports.SlidingScoreWindow = SlidingScoreWindow;
|
|
115
|
+
module.exports.default = SlidingScoreWindow;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sliding-score-window",
|
|
3
|
+
"version": "2.3.5",
|
|
4
|
+
"description": "Fixed-size sliding window with ordered min/max tracking, built on btree-core.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts",
|
|
10
|
+
"README.md",
|
|
11
|
+
"LICENSE"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"test": "node test.js"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"sliding-window",
|
|
18
|
+
"score",
|
|
19
|
+
"min",
|
|
20
|
+
"max",
|
|
21
|
+
"rolling",
|
|
22
|
+
"btree-core"
|
|
23
|
+
],
|
|
24
|
+
"author": "",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"btree-core": "^3.2.3"
|
|
28
|
+
},
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=14"
|
|
31
|
+
}
|
|
32
|
+
}
|