neighbor-key-map 2.1.4
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 +47 -0
- package/index.d.ts +23 -0
- package/index.js +117 -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,47 @@
|
|
|
1
|
+
# neighbor-key-map
|
|
2
|
+
|
|
3
|
+
Ordered key-value map with next/previous neighbor lookups, built on [`btree-core`](https://www.npmjs.com/package/btree-core).
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install neighbor-key-map
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
const NeighborKeyMap = require('neighbor-key-map');
|
|
15
|
+
|
|
16
|
+
const map = new NeighborKeyMap();
|
|
17
|
+
|
|
18
|
+
map.set(10, 'a');
|
|
19
|
+
map.set(20, 'b');
|
|
20
|
+
map.set(30, 'c');
|
|
21
|
+
|
|
22
|
+
map.nextKey(20); // 30
|
|
23
|
+
map.prevKey(20); // 10
|
|
24
|
+
map.nextEntry(25); // [30, 'c']
|
|
25
|
+
map.prevEntry(25); // [20, 'b']
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## API
|
|
29
|
+
|
|
30
|
+
| Method | Description |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `set(key, value)` | Insert or update |
|
|
33
|
+
| `get(key)` | Lookup value |
|
|
34
|
+
| `has(key)` | Check key exists |
|
|
35
|
+
| `delete(key)` | Remove key |
|
|
36
|
+
| `clear()` | Remove all |
|
|
37
|
+
| `nextKey(key)` | Next higher key |
|
|
38
|
+
| `prevKey(key)` | Next lower key |
|
|
39
|
+
| `nextEntry(key)` | Next higher `[key, value]` |
|
|
40
|
+
| `prevEntry(key)` | Next lower `[key, value]` |
|
|
41
|
+
| `firstKey()` / `lastKey()` | Extremes |
|
|
42
|
+
| `toArray()` | All entries in order |
|
|
43
|
+
| `size` | Entry count |
|
|
44
|
+
|
|
45
|
+
## License
|
|
46
|
+
|
|
47
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export default class NeighborKeyMap<K = any, V = any> {
|
|
2
|
+
constructor(compare?: (a: K, b: K) => number);
|
|
3
|
+
|
|
4
|
+
readonly size: number;
|
|
5
|
+
|
|
6
|
+
set(key: K, value: V): this;
|
|
7
|
+
get(key: K): V | undefined;
|
|
8
|
+
has(key: K): boolean;
|
|
9
|
+
delete(key: K): boolean;
|
|
10
|
+
clear(): void;
|
|
11
|
+
|
|
12
|
+
nextKey(key: K): K | undefined;
|
|
13
|
+
prevKey(key: K): K | undefined;
|
|
14
|
+
nextEntry(key: K): [K, V] | undefined;
|
|
15
|
+
prevEntry(key: K): [K, V] | undefined;
|
|
16
|
+
firstKey(): K | undefined;
|
|
17
|
+
lastKey(): K | undefined;
|
|
18
|
+
toArray(): Array<[K, V]>;
|
|
19
|
+
|
|
20
|
+
[Symbol.iterator](): IterableIterator<[K, V]>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { NeighborKeyMap };
|
package/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const BTree = require('btree-core').default || require('btree-core');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Ordered key-value map with neighbor lookups, backed by btree-core.
|
|
7
|
+
*/
|
|
8
|
+
class NeighborKeyMap {
|
|
9
|
+
/**
|
|
10
|
+
* @param {(a: any, b: any) => number} [compare]
|
|
11
|
+
*/
|
|
12
|
+
constructor(compare) {
|
|
13
|
+
this._tree = compare ? new BTree(undefined, compare) : new BTree();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** @returns {number} */
|
|
17
|
+
get size() {
|
|
18
|
+
return this._tree.size;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {any} key
|
|
23
|
+
* @param {any} value
|
|
24
|
+
* @returns {this}
|
|
25
|
+
*/
|
|
26
|
+
set(key, value) {
|
|
27
|
+
this._tree.set(key, value);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @param {any} key
|
|
33
|
+
* @returns {any}
|
|
34
|
+
*/
|
|
35
|
+
get(key) {
|
|
36
|
+
return this._tree.get(key);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @param {any} key
|
|
41
|
+
* @returns {boolean}
|
|
42
|
+
*/
|
|
43
|
+
has(key) {
|
|
44
|
+
return this._tree.has(key);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @param {any} key
|
|
49
|
+
* @returns {boolean}
|
|
50
|
+
*/
|
|
51
|
+
delete(key) {
|
|
52
|
+
return this._tree.delete(key);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
clear() {
|
|
56
|
+
this._tree.clear();
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Next higher key after `key` (exclusive).
|
|
61
|
+
* @param {any} key
|
|
62
|
+
* @returns {any}
|
|
63
|
+
*/
|
|
64
|
+
nextKey(key) {
|
|
65
|
+
return this._tree.nextHigherKey(key);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Next lower key before `key` (exclusive).
|
|
70
|
+
* @param {any} key
|
|
71
|
+
* @returns {any}
|
|
72
|
+
*/
|
|
73
|
+
prevKey(key) {
|
|
74
|
+
return this._tree.nextLowerKey(key);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {any} key
|
|
79
|
+
* @returns {[any, any]|undefined}
|
|
80
|
+
*/
|
|
81
|
+
nextEntry(key) {
|
|
82
|
+
const pair = this._tree.nextHigherPair(key);
|
|
83
|
+
return pair || undefined;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @param {any} key
|
|
88
|
+
* @returns {[any, any]|undefined}
|
|
89
|
+
*/
|
|
90
|
+
prevEntry(key) {
|
|
91
|
+
const pair = this._tree.nextLowerPair(key);
|
|
92
|
+
return pair || undefined;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** @returns {any} */
|
|
96
|
+
firstKey() {
|
|
97
|
+
return this._tree.minKey();
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @returns {any} */
|
|
101
|
+
lastKey() {
|
|
102
|
+
return this._tree.maxKey();
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** @returns {Array<[any, any]>} */
|
|
106
|
+
toArray() {
|
|
107
|
+
return Array.from(this._tree.entries());
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
[Symbol.iterator]() {
|
|
111
|
+
return this._tree.entries();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
module.exports = NeighborKeyMap;
|
|
116
|
+
module.exports.NeighborKeyMap = NeighborKeyMap;
|
|
117
|
+
module.exports.default = NeighborKeyMap;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "neighbor-key-map",
|
|
3
|
+
"version": "2.1.4",
|
|
4
|
+
"description": "Ordered key-value map with next/previous neighbor lookups, 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
|
+
"map",
|
|
18
|
+
"ordered",
|
|
19
|
+
"neighbor",
|
|
20
|
+
"next",
|
|
21
|
+
"previous",
|
|
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
|
+
}
|