You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

ts-treemap

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-treemap - npm Package Compare versions

Comparing version

to
1.1.0

dist/Types.d.ts

1

.eslintrc.js

@@ -37,2 +37,3 @@ module.exports = {

'import/no-unresolved': 'off',
'import/named': 'off',
'no-var': 'error',

@@ -39,0 +40,0 @@ 'prefer-const': 'warn',

3

.prettierrc.js

@@ -5,3 +5,4 @@ module.exports = {

singleQuote: true,
printWidth: 120
printWidth: 120,
trailingComma: "none"
}

@@ -9,4 +9,3 @@ export default class TreeMap<K, V> extends Map {

private specifiedCompareFn;
readonly comparator: (a: K, b: K) => number;
private isIterable;
get comparator(): (a: K, b: K) => number;
private isCompareFn;

@@ -19,6 +18,16 @@ private compare;

/**
* @param entries entries
* @param compareFn A function that defines the sort order of the keys.
*/
constructor(entries?: readonly (readonly [K, V])[] | null, compareFn?: (a: K, b: K) => number);
/**
* @param iterable Iterable object
* @param compareFn A function that defines the sort order of the keys.
*/
constructor(iterable?: Iterable<readonly [K, V]> | null, compareFn?: (a: K, b: K) => number);
constructor(iterable?: IterableIterator<[K, V]>, compareFn?: (a: K, b: K) => number);
/**
* @param map `Map` object
* @param compareFn A function that defines the sort order of the keys.
*/
constructor(map?: Map<K, V>, compareFn?: (a: K, b: K) => number);
private _constructor;

@@ -40,2 +49,3 @@ /**

reverseKeys(): IterableIterator<K>;
get(key: K): V | undefined;
/**

@@ -159,3 +169,4 @@ * Adds or updates entry with the specified value with the specified key in this map.

splitHigher(key: K, include?: boolean): TreeMap<K, V>;
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void;
forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: unknown): void;
}
export * from './Types';
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const Util_1 = require("./Util");
/* eslint-disable no-dupe-class-members */

@@ -26,6 +28,10 @@ const numberComparator = (a, b) => a - b;

}
const toString = Object.prototype.toString;
if (toString.call(value).endsWith('Date]')) {
if (Util_1.isDate(value)) {
return comparators.Date;
}
if (Util_1.isComparable(value)) {
return (o1, o2) => {
return o1.compare(o2);
};
}
throw new Error('Cannot sort keys in this map. You have to specify compareFn if the type of key in this map is not number, string, or Date.');

@@ -37,13 +43,3 @@ };

this.specifiedCompareFn = false;
this.isIterable = (value) => {
if (value == null) {
return false;
}
const itr = value;
if (itr[Symbol.iterator] == null) {
return false;
}
return true;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types
this.isCompareFn = (value) => {

@@ -54,3 +50,3 @@ return typeof value === 'function';

this.sortedKeys = [];
if (this.isIterable(iterableOrCompareFn)) {
if (Util_1.isIterable(iterableOrCompareFn)) {
this._constructor(iterableOrCompareFn, compareFn);

@@ -108,5 +104,9 @@ }

const normalMap = new Map();
this.sortedKeys.forEach(key => {
normalMap.set(key, this.get(key));
const entries = Array.from(super.entries());
entries.sort((a, b) => {
return this.compareFn(a[0], b[0]);
});
entries.forEach(([k, v]) => {
normalMap.set(k, v);
});
return normalMap;

@@ -118,2 +118,9 @@ }

}
get(key) {
const resultKey = this.sortedKeys.find((key0) => this.comparator(key0, key) === 0);
if (resultKey == null) {
return undefined;
}
return super.get(resultKey);
}
/**

@@ -125,13 +132,15 @@ * Adds or updates entry with the specified value with the specified key in this map.

set(key, value) {
const before = Array.from(super.keys());
super.set(key, value);
const sortedKeys = [...this.sortedKeys];
if (before.length !== Array.from(super.keys()).length) {
sortedKeys.push(key);
}
if (sortedKeys.length === 1 && !this.specifiedCompareFn) {
this.compareFn = decideCompareFn(sortedKeys[0]);
if (this.sortedKeys.length === 0 && !this.specifiedCompareFn) {
this.compareFn = decideCompareFn(key);
this.specifiedCompareFn = true;
}
this.sortedKeys = sortedKeys.sort(this.compareFn);
const actualKey = this.sortedKeys.find((k) => this.compareFn(k, key) === 0);
if (actualKey == null) {
this.sortedKeys.push(key);
super.set(key, value);
}
else {
super.set(actualKey, value);
}
this.sortedKeys.sort(this.compareFn);
return this;

@@ -155,3 +164,3 @@ }

if (super.delete(key)) {
this.sortedKeys = this.sortedKeys.filter(existKey => this.compare(existKey, key) !== 0);
this.sortedKeys = this.sortedKeys.filter((existKey) => this.compare(existKey, key) !== 0);
return true;

@@ -178,3 +187,3 @@ }

values() {
return this.sortedKeys.map(k => super.get(k)).values();
return this.sortedKeys.map((k) => super.get(k)).values();
}

@@ -195,3 +204,4 @@ /**

}
return [key, this.get(key)];
const value = this.get(key);
return value === undefined ? undefined : [key, value];
}

@@ -212,3 +222,4 @@ /**

}
return [key, this.get(key)];
const value = this.get(key);
return value === undefined ? undefined : [key, value];
}

@@ -253,3 +264,4 @@ /**

if (resultKey != null) {
return [resultKey, this.get(resultKey)];
const value = this.get(resultKey);
return value === undefined ? undefined : [resultKey, value];
}

@@ -264,3 +276,3 @@ return undefined;

floorKey(key) {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) <= 0);
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) <= 0);
return filtered.reverse()[0];

@@ -276,3 +288,4 @@ }

if (resultKey != null) {
return [resultKey, this.get(resultKey)];
const value = this.get(resultKey);
return value === undefined ? undefined : [resultKey, value];
}

@@ -287,3 +300,3 @@ return undefined;

ceilingKey(key) {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) >= 0);
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) >= 0);
return filtered[0];

@@ -299,3 +312,4 @@ }

if (resultKey != null) {
return [resultKey, this.get(resultKey)];
const value = this.get(resultKey);
return value === undefined ? undefined : [resultKey, value];
}

@@ -310,3 +324,3 @@ return undefined;

lowerKey(key) {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) < 0);
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) < 0);
return filtered.reverse()[0];

@@ -322,3 +336,4 @@ }

if (resultKey != null) {
return [resultKey, this.get(resultKey)];
const value = this.get(resultKey);
return value === undefined ? undefined : [resultKey, value];
}

@@ -333,3 +348,3 @@ return undefined;

higherKey(key) {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) > 0);
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) > 0);
return filtered[0];

@@ -343,5 +358,5 @@ }

splitLower(key, include = true) {
const entries = Array.from(this.entries()).filter(e => {
const than = this.compare(e[0], key) < 0;
return include ? than || this.compare(e[0], key) === 0 : than;
const entries = Array.from(this.entries()).filter((e) => {
const range = this.compare(e[0], key) < 0;
return include ? range || this.compare(e[0], key) === 0 : range;
});

@@ -356,12 +371,11 @@ return new TreeMap(entries, this.compareFn);

splitHigher(key, include = true) {
const entries = Array.from(this.entries()).filter(e => {
const than = this.compare(e[0], key) > 0;
return include ? than || this.compare(e[0], key) === 0 : than;
const entries = Array.from(this.entries()).filter((e) => {
const range = this.compare(e[0], key) > 0;
return include ? range || this.compare(e[0], key) === 0 : range;
});
return new TreeMap(entries, this.compareFn);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
forEach(callbackfn, thisArg) {
this.sortedKeys.forEach(k => {
callbackfn(this.get(k), k, this);
Array.from(this.entries()).forEach(([k, v]) => {
callbackfn(v, k, this);
}, thisArg);

@@ -371,1 +385,2 @@ }

exports.default = TreeMap;
tslib_1.__exportStar(require("./Types"), exports);
{
"name": "ts-treemap",
"version": "1.0.0",
"version": "1.1.0",
"description": "a TypeScript implementation of TreeMap",

@@ -24,17 +24,17 @@ "main": "dist/TreeMap.js",

"devDependencies": {
"@types/jest": "^24.0.15",
"@typescript-eslint/eslint-plugin": "^1.11.0",
"@typescript-eslint/parser": "^1.11.0",
"codecov": "^3.5.0",
"@types/jest": "^25.2.3",
"@typescript-eslint/eslint-plugin": "^3.0.2",
"@typescript-eslint/parser": "^3.0.2",
"codecov": "^3.6.5",
"eslint": "^6.0.1",
"eslint-config-prettier": "^6.0.0",
"eslint-config-standard": "^12.0.0",
"eslint-config-standard": "^14.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-node": "^9.1.0",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.1.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"jest": "^24.8.0",
"jest": "^26.0.0",
"prettier": "^1.18.2",
"ts-jest": "^24.0.2",
"ts-jest": "^26.0.0",
"typescript": "^3.5.2"

@@ -41,0 +41,0 @@ },

@@ -64,12 +64,51 @@ # ts-treemap

# 注意!
# 重要!
キーをソートするためには、比較を行うための関数を定義する必要があります。TreeMap は内部で比較関数を持っており、キーを追加するたびに、比較関数によって自動でキーをソートします。
TreeMap にエントリを追加したり、キーをソートするためには、**比較を行うための関数**を定義する必要があります。TreeMap は内部で比較関数を持っており、キーを追加するたびに、比較関数によって自動でキーをソートします。
ES2015 の Map は、エントリの追加時、キーが重複しているかの判定は[“same-value-zero”アルゴリズム](https://developer.mozilla.org/ja/docs/Web/JavaScript/Equality_comparisons_and_sameness)を用いています。“same-value-zero”アルゴリズムは、オブジェクト同士を比較する際、“**===**” を用いて等価性を判断します(+0 と-0 は同じ)。これは、キーが同一のエントリを複数回追加する際、それらのキーの型が`Date`型のようなオブジェクト型である場合、正常に重複判定が行われないことを意味します。
この問題を避けるため、TreeMap ではキーの追加時に“same-value”アルゴリズムは使わず、**比較関数**を用いて、登録済みのキーと追加時のキーを比較し、比較関数の返却値が 0 かどうかでキーの重複を判断します。
比較関数は、Array.prototype.sort()で用いられる[比較関数](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#説明)に準拠しています。
キーの型が`number`, `string`, `Date`のどれかに該当する場合は、デフォルトで用意されている関数で比較を行うので、比較関数を定義する必要はありません。(ご自身で定義することも出来ます)
キーの型が`number`, `string`, `Date`のいずれかである場合は、デフォルトで用意されている関数で比較を行うので、比較関数を定義する必要はありません。
キーの型が上記のいずれにも該当しない場合、比較関数を与えずに TreeMap を生成してから**1 つ目のエントリを追加した時にエラーが発生します。**
上記以外の型をキーとしたい場合は、次のいずれかの方法で TreeMap を生成します:
**方法1:コンストラクタに比較関数を渡してマップを生成**
```typescript
import TreeMap from 'ts-treemap'
import Day from 'dayjs'
const objectMap = new TreeMap<Day.Dayjs, string>((a, b) => a.unix() - b.unix())
objectMap.set(Day('2019-01-01'), 'foo') // OK
```
**方法2:比較関数`compare()`を持っているクラスをキーにする**
```typescript
import TreeMap, { Comparable } from 'ts-treemap'
class ExampleObject implements Comparable<ExampleObject> {
value: number
constructor(value: number) {
this.value = value
}
compare(object: ExampleObject) {
return this.value - object.value
}
}
const map = new TreeMap<ExampleObject, string>()
map.set(new ExampleObject(1), 'a') // OK
```
(なお、両方とも満たした場合は方法1が優先されます)
上記の場合で比較関数を渡さずに TreeMap を生成した場合は、**1 つ目のエントリを追加した時にエラーがスローされます。**
**✅ Do:**

@@ -76,0 +115,0 @@

@@ -1,2 +0,2 @@

[🇯🇵 日本語はここ](https://github.com/yuyasvx/ts-treemap/blob/master/README.md)
[🇯🇵 日本語はここ](https://github.com/yuyasvx/ts-treemap/blob/master/README-ja.md)

@@ -66,8 +66,52 @@ # ts-treemap

To sort the keys, you need to define a function to compare keys in the map. Once TreeMap is constructed with comparator function, the keys are sorted by this function each time an entry is added.
In order to sort keys, you need to define a comparison function, and TreeMap has an internal comparison function that automatically sorts keys each time you add them.
The comparator function conforms to the [compare function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description) used in `Array.prototype.sort()`.
The ES2015 Map uses the "Same-value-zero" algorithm to determine if there are duplicate keys when an entry is added. The algorithm uses "===" to determine equivalence when comparing objects (but +0 and -0 are considered equal). This means that when you add multiple entries with the same key, the duplicate check will not work correctly if the type of key is object (such as `Date`).
You don’t have to define the compare function if the type of the key is `number`, `string` or `Date`. Otherwise, when you construct a new TreeMap without supplying a compare function and add the first entry, an `Error` will be thrown.
To avoid this problem, TreeMap does not use that algorithm when adding keys, but uses a comparison function. If the return value of the comparison function is 0, the key is considered to be a duplicate.
This comparison function conforms to the [compare function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Description) used in `Array.prototype.sort()`.
You don’t have to define the compare function if the type of the key is `number`, `string` or `Date`.
If you want to use other types as keys, you can use one of the following methods to generate a TreeMap.
**Method 1: Pass the comparison function to the constructor to create a map**
```typescript
import TreeMap from 'ts-treemap'
interface IKeyObject {
value: number
}
const objectMap = new TreeMap<IKeyObject, string>((a, b) => a.value - b.value)
objectMap.set({ value: 1 }, 'foo') // OK
```
**Method 2: Use the class which has the comparison function `compare()` as a key**
```typescript
import TreeMap, { Comparable } from 'ts-treemap'
class ExampleObject implements Comparable<ExampleObject> {
value: number
constructor(value: number) {
this.value = value
}
compare(object: ExampleObject) {
return this.value - object.value
}
}
const map = new TreeMap<ExampleObject, string>()
map.set(new ExampleObject(1), 'a') // OK
```
(If both are satisfied, method 1 takes precedence.)
If TreeMap is created without passing parameters in the above case, `Error` will be thrown **when the first entry is added.**
**✅ Do:**

@@ -74,0 +118,0 @@

@@ -0,1 +1,4 @@

import { isComparable, isDate, isIterable } from './Util'
import { Comparable } from './Types'
/* eslint-disable no-dupe-class-members */

@@ -26,6 +29,10 @@ const numberComparator = (a: number, b: number): number => a - b

}
const toString = Object.prototype.toString
if (toString.call(value).endsWith('Date]')) {
if (isDate(value)) {
return comparators.Date
}
if (isComparable(value)) {
return (o1: Comparable<unknown>, o2: Comparable<unknown>): number => {
return o1.compare(o2)
}
}
throw new Error(

@@ -45,3 +52,3 @@ 'Cannot sort keys in this map. You have to specify compareFn if the type of key in this map is not number, string, or Date.'

private specifiedCompareFn: boolean = false
private specifiedCompareFn = false

@@ -54,14 +61,3 @@ // private reverseOrder: boolean = false

private isIterable = (value: unknown): value is Iterable<readonly [K, V]> => {
if (value == null) {
return false
}
const itr = value as Iterable<readonly [K, V]>
if (itr[Symbol.iterator] == null) {
return false
}
return true
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
// eslint-disable-next-line @typescript-eslint/no-explicit-any,@typescript-eslint/explicit-module-boundary-types
private isCompareFn = (value: any): value is (a: K, b: K) => number => {

@@ -82,11 +78,29 @@ return typeof value === 'function'

/**
* @param entries entries
* @param compareFn A function that defines the sort order of the keys.
*/
constructor(entries?: readonly (readonly [K, V])[] | null, compareFn?: (a: K, b: K) => number)
/**
* @param iterable Iterable object
* @param compareFn A function that defines the sort order of the keys.
*/
constructor(iterable?: Iterable<readonly [K, V]> | null, compareFn?: (a: K, b: K) => number)
constructor(iterableOrCompareFn?: unknown, compareFn?: (a: K, b: K) => number) {
constructor(iterable?: IterableIterator<[K, V]>, compareFn?: (a: K, b: K) => number)
/**
* @param map `Map` object
* @param compareFn A function that defines the sort order of the keys.
*/
constructor(map?: Map<K, V>, compareFn?: (a: K, b: K) => number)
constructor(
iterableOrCompareFn?:
| readonly (readonly [K, V])[]
| IterableIterator<[K, V]>
| Map<K, V>
| ((a: K, b: K) => number)
| null,
compareFn?: (a: K, b: K) => number
) {
super()
this.compareFn = comparators.none
this.sortedKeys = []
if (this.isIterable(iterableOrCompareFn)) {
if (isIterable<K, V>(iterableOrCompareFn)) {
this._constructor(iterableOrCompareFn, compareFn)

@@ -105,3 +119,3 @@ }

private _constructor(iterable?: Iterable<readonly [K, V]> | null, compareFn?: (a: K, b: K) => number): void {
private _constructor(iterable: Iterable<readonly [K, V]> | null, compareFn?: (a: K, b: K) => number): void {
this.compareFn = compareFn == null ? comparators.none : compareFn

@@ -140,5 +154,9 @@ this.specifiedCompareFn = compareFn != null

const normalMap: Map<K, V> = new Map()
this.sortedKeys.forEach(key => {
normalMap.set(key, this.get(key))
const entries = Array.from(super.entries())
entries.sort((a: [K, V], b: [K, V]) => {
return this.compareFn(a[0], b[0])
})
entries.forEach(([k, v]) => {
normalMap.set(k, v)
})
return normalMap

@@ -152,2 +170,10 @@ }

public get(key: K): V | undefined {
const resultKey = this.sortedKeys.find((key0) => this.comparator(key0, key) === 0)
if (resultKey == null) {
return undefined
}
return super.get(resultKey)
}
/**

@@ -159,15 +185,17 @@ * Adds or updates entry with the specified value with the specified key in this map.

public set(key: K, value: V): this {
const before = Array.from(super.keys())
super.set(key, value)
const sortedKeys = [...this.sortedKeys]
if (before.length !== Array.from(super.keys()).length) {
sortedKeys.push(key)
}
if (sortedKeys.length === 1 && !this.specifiedCompareFn) {
this.compareFn = decideCompareFn(sortedKeys[0])
if (this.sortedKeys.length === 0 && !this.specifiedCompareFn) {
this.compareFn = decideCompareFn(key)
this.specifiedCompareFn = true
}
this.sortedKeys = sortedKeys.sort(this.compareFn)
const actualKey = this.sortedKeys.find((k) => this.compareFn(k, key) === 0)
if (actualKey == null) {
this.sortedKeys.push(key)
super.set(key, value)
} else {
super.set(actualKey, value)
}
this.sortedKeys.sort(this.compareFn)
return this

@@ -193,3 +221,3 @@ }

if (super.delete(key)) {
this.sortedKeys = this.sortedKeys.filter(existKey => this.compare(existKey, key) !== 0)
this.sortedKeys = this.sortedKeys.filter((existKey) => this.compare(existKey, key) !== 0)
return true

@@ -219,3 +247,3 @@ }

public values(): IterableIterator<V> {
return this.sortedKeys.map(k => super.get(k)).values()
return this.sortedKeys.map((k) => super.get(k)).values()
}

@@ -238,3 +266,4 @@

}
return [key, this.get(key)]
const value = this.get(key)
return value === undefined ? undefined : [key, value]
}

@@ -257,3 +286,4 @@

}
return [key, this.get(key)]
const value = this.get(key)
return value === undefined ? undefined : [key, value]
}

@@ -302,3 +332,4 @@

if (resultKey != null) {
return [resultKey, this.get(resultKey)]
const value = this.get(resultKey)
return value === undefined ? undefined : [resultKey, value]
}

@@ -314,3 +345,3 @@ return undefined

public floorKey(key: K): K | undefined {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) <= 0)
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) <= 0)
return filtered.reverse()[0]

@@ -327,3 +358,4 @@ }

if (resultKey != null) {
return [resultKey, this.get(resultKey)]
const value = this.get(resultKey)
return value === undefined ? undefined : [resultKey, value]
}

@@ -339,3 +371,3 @@ return undefined

public ceilingKey(key: K): K | undefined {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) >= 0)
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) >= 0)
return filtered[0]

@@ -352,3 +384,4 @@ }

if (resultKey != null) {
return [resultKey, this.get(resultKey)]
const value = this.get(resultKey)
return value === undefined ? undefined : [resultKey, value]
}

@@ -364,3 +397,3 @@ return undefined

public lowerKey(key: K): K | undefined {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) < 0)
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) < 0)
return filtered.reverse()[0]

@@ -377,3 +410,4 @@ }

if (resultKey != null) {
return [resultKey, this.get(resultKey)]
const value = this.get(resultKey)
return value === undefined ? undefined : [resultKey, value]
}

@@ -389,3 +423,3 @@ return undefined

public higherKey(key: K): K | undefined {
const filtered = this.sortedKeys.filter(existKey => this.compare(existKey, key) > 0)
const filtered = this.sortedKeys.filter((existKey) => this.compare(existKey, key) > 0)
return filtered[0]

@@ -399,6 +433,6 @@ }

*/
public splitLower(key: K, include: boolean = true): TreeMap<K, V> {
const entries = Array.from(this.entries()).filter(e => {
const than = this.compare(e[0], key) < 0
return include ? than || this.compare(e[0], key) === 0 : than
public splitLower(key: K, include = true): TreeMap<K, V> {
const entries = Array.from(this.entries()).filter((e) => {
const range = this.compare(e[0], key) < 0
return include ? range || this.compare(e[0], key) === 0 : range
})

@@ -413,6 +447,6 @@ return new TreeMap(entries, this.compareFn)

*/
public splitHigher(key: K, include: boolean = true): TreeMap<K, V> {
const entries = Array.from(this.entries()).filter(e => {
const than = this.compare(e[0], key) > 0
return include ? than || this.compare(e[0], key) === 0 : than
public splitHigher(key: K, include = true): TreeMap<K, V> {
const entries = Array.from(this.entries()).filter((e) => {
const range = this.compare(e[0], key) > 0
return include ? range || this.compare(e[0], key) === 0 : range
})

@@ -422,8 +456,9 @@ return new TreeMap(entries, this.compareFn)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
public forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void {
this.sortedKeys.forEach(k => {
callbackfn(this.get(k), k, this)
public forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: unknown): void {
Array.from(this.entries()).forEach(([k, v]) => {
callbackfn(v, k, this)
}, thisArg)
}
}
export * from './Types'

@@ -1,3 +0,15 @@

import TreeMap from '../../src/TreeMap'
import TreeMap, { Comparable } from '../../src/TreeMap'
class ComparableTest implements Comparable<ComparableTest> {
value: number
constructor(val: number) {
this.value = val
}
compare(object: ComparableTest): number {
return this.value - object.value
}
}
const getTreeMap = (): TreeMap<number, string> => {

@@ -21,4 +33,4 @@ const treeMap = new TreeMap<number, string>()

it('construct TreeMap', () => {
// create custom comparator
const compareFn = (a: number, b: number): number => b - a
const iterableArray: [number, string][] = [[1, 'a'], [2, 'b']]

@@ -32,2 +44,3 @@ const map1 = new TreeMap<number, number>(compareFn)

const iterableArray: [number, string][] = [[1, 'a'], [2, 'b']]
const map3 = new TreeMap<number, string>(iterableArray, compareFn)

@@ -52,2 +65,16 @@ const map4 = new TreeMap<number, string>(iterableArray)

expect(Array.from(map7.entries())).toStrictEqual([[1, 'a'], [2, 'b']])
const comparableObjectMap: TreeMap<ComparableTest, string> = new TreeMap()
comparableObjectMap.set(new ComparableTest(3), 'c')
comparableObjectMap.set(new ComparableTest(1), 'a')
comparableObjectMap.set(new ComparableTest(2), 'b')
comparableObjectMap.set(new ComparableTest(1), 'd')
expect(Array.from(comparableObjectMap.values())).toStrictEqual(['d', 'b', 'c'])
const comparableObjectMap2: TreeMap<ComparableTest, string> = new TreeMap((a, b) => b.value - a.value)
comparableObjectMap2.set(new ComparableTest(3), 'c')
comparableObjectMap2.set(new ComparableTest(1), 'a')
comparableObjectMap2.set(new ComparableTest(2), 'b')
comparableObjectMap2.set(new ComparableTest(1), 'd')
expect(Array.from(comparableObjectMap2.values())).toStrictEqual(['c', 'b', 'd'])
})

@@ -63,8 +90,19 @@

const treeMap = getTreeMap()
const keys = [20, 15, 10, 5, 0]
const values = ['e', 'd', 'c', 'b', 'a']
keys.forEach((k, i) => {
expect(treeMap.get(k)).toBe(values[i])
})
})
it('get', () => {
const treeMap = getTreeMap()
expect(treeMap.size).toBe(5)
expect(Array.from(treeMap.keys())).toStrictEqual([0, 5, 10, 15, 20])
expect(treeMap.get(999)).toBeUndefined()
})
it('add entries with the same key', () => {
it('overwrites value', () => {
// primitive key
const treeMap = getTreeMap()

@@ -77,2 +115,9 @@ treeMap.set(0, '123')

expect(treeMap.get(0)).toBe('456')
// object key
const treeMap2 = new TreeMap<Date, string>()
treeMap2.set(new Date('2020-01-01'), 'a')
treeMap2.set(new Date('2020-01-01'), 'b')
expect(treeMap2.size).toBe(1)
expect(treeMap2.get(new Date('2020-01-01'))).toBe('b')
})

@@ -223,5 +268,5 @@

const treeMap = new TreeMap<Date, string>()
treeMap.set(new Date('2019-02-11'), '建国記念の日')
treeMap.set(new Date('2019-02-11'), 'holiday 2')
treeMap.set(new Date('2019-01-01'), "new year's day")
treeMap.set(new Date('2019-01-14'), '成人の日')
treeMap.set(new Date('2019-01-14'), 'holiday 1')

@@ -228,0 +273,0 @@ expect(Array.from(treeMap.keys())).toStrictEqual([

{
"compilerOptions": {
"target": "esnext",
"target": "ES2018",
"strict": true,

@@ -5,0 +5,0 @@ "jsx": "preserve",