Java-style Set and Map collections (on `{ hashCode(): int, equals(x: any): boolean}` objects) written in TypeScript.
npm install javasetmap.ts



{ hashCode(): int, equals(x: any): boolean} objects) written in TypeScript.npm install javasetmap.ts --savejavasetmap_ts ts
import {JavaMap} from 'javasetmap.ts'
// node: const {JavaMap} = require('javasetmap.ts')
// browser: const {JavaMap} = nladeclare global { // remove this block if not using TypeScript
interface Array extends Equalable {}
interface Number extends Equalable {}
}
Array.prototype.equals = function (o) {
return this == o || this.length == o.length && this.every((el, i) => el.equals(o[i]))
}
Array.prototype.hashCode = function () {
return this.reduce((acc, current) => acc * 31 + current.hashCode(), 0)
}
Number.prototype.equals = function (o) { return this == o }
Number.prototype.hashCode = function () { return this | 0 }
const myMap = new JavaMap<[number, number], string>() // new JavaMap() if not using TypeScript
myMap.set([1, 2], "foo")
myMap.has([1, 2]) // === true
myMap.get([1, 2]) // === "foo"
myMap.has([1, 3]) // === false
myMap.get([2, 1]) // === undefined
``
LICENSE
MIT