Comparing version 0.0.7 to 0.1.2
13
bim.d.ts
/// <reference path="./typings/lib.es6.d.ts" /> | ||
declare module "bim" { | ||
export default class Bim<K, V> { | ||
export class BiMap<K, V> { | ||
clear(): void; | ||
@@ -20,2 +20,13 @@ delete(key: K): boolean; | ||
} | ||
export class WeakBiMap<K, V> { | ||
delete(key: K): boolean; | ||
get(key: K): V; | ||
has(key: K): boolean; | ||
set(key: K, value?: V): Map<K, V>; | ||
[Symbol.toStringTag]: string; | ||
deleteValue(value: V): boolean; | ||
getKey(value: V): K; | ||
hasValue(value: V): boolean; | ||
} | ||
} |
105
index.js
"use strict"; | ||
var Bim = (function () { | ||
function Bim(iterable) { | ||
var BiMap = (function () { | ||
function BiMap(iterable) { | ||
var _this = this; | ||
this.left = new Map(iterable); | ||
this.right = new Map(); | ||
this.left.forEach(this.right.set); | ||
this.left.forEach(function (i) { return _this.right.set(i); }); | ||
} | ||
Bim.prototype.clear = function () { | ||
BiMap.prototype.clear = function () { | ||
this.left.clear(); | ||
this.right.clear(); | ||
}; | ||
Bim.prototype.delete = function (key) { | ||
BiMap.prototype.delete = function (key) { | ||
var val = this.left.get(key); | ||
@@ -20,18 +21,18 @@ if (!this.right.has(val)) { | ||
}; | ||
Bim.prototype.entries = function () { | ||
BiMap.prototype.entries = function () { | ||
return this.left.entries(); | ||
}; | ||
Bim.prototype.forEach = function (callbackfn, thisArg) { | ||
BiMap.prototype.forEach = function (callbackfn, thisArg) { | ||
this.left.forEach(callbackfn, thisArg); | ||
}; | ||
Bim.prototype.get = function (key) { | ||
BiMap.prototype.get = function (key) { | ||
return this.left.get(key); | ||
}; | ||
Bim.prototype.has = function (key) { | ||
BiMap.prototype.has = function (key) { | ||
return this.left.has(key); | ||
}; | ||
Bim.prototype.keys = function () { | ||
BiMap.prototype.keys = function () { | ||
return this.left.keys(); | ||
}; | ||
Bim.prototype.set = function (key, value) { | ||
BiMap.prototype.set = function (key, value) { | ||
var oldVal = this.left.get(key); | ||
@@ -49,3 +50,3 @@ var oldKey = this.right.get(value); | ||
}; | ||
Object.defineProperty(Bim.prototype, "size", { | ||
Object.defineProperty(BiMap.prototype, "size", { | ||
get: function () { | ||
@@ -57,9 +58,9 @@ return this.left.size; | ||
}); | ||
Bim.prototype.values = function () { | ||
BiMap.prototype.values = function () { | ||
return this.left.values(); | ||
}; | ||
Bim.prototype[Symbol.iterator] = function () { | ||
BiMap.prototype[Symbol.iterator] = function () { | ||
return this.left[Symbol.iterator](); | ||
}; | ||
Object.defineProperty(Bim.prototype, Symbol.toStringTag, { | ||
Object.defineProperty(BiMap.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
@@ -71,3 +72,3 @@ return this.left[Symbol.toStringTag]; | ||
}); | ||
Bim.prototype.deleteValue = function (value) { | ||
BiMap.prototype.deleteValue = function (value) { | ||
var key = this.right.get(value); | ||
@@ -80,11 +81,73 @@ if (!this.left.has(key)) { | ||
}; | ||
Bim.prototype.getKey = function (value) { | ||
BiMap.prototype.getKey = function (value) { | ||
return this.right.get(value); | ||
}; | ||
Bim.prototype.hasValue = function (value) { | ||
BiMap.prototype.hasValue = function (value) { | ||
return this.right.has(value); | ||
}; | ||
return Bim; | ||
return BiMap; | ||
}()); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Bim; | ||
exports.BiMap = BiMap; | ||
var WeakBiMap = (function () { | ||
function WeakBiMap(iterable) { | ||
this.left = new WeakMap(); | ||
this.right = new WeakMap(); | ||
if (iterable === undefined) | ||
return; | ||
for (var _i = 0, _a = iterable; _i < _a.length; _i++) { | ||
var _b = _a[_i], k = _b[0], v = _b[1]; | ||
this.left.set(k, v); | ||
this.right.set(v, k); | ||
} | ||
} | ||
WeakBiMap.prototype.delete = function (key) { | ||
var val = this.left.get(key); | ||
if (!this.right.has(val)) { | ||
return false; | ||
} | ||
this.right.delete(val); | ||
return this.left.delete(key); | ||
}; | ||
WeakBiMap.prototype.get = function (key) { | ||
return this.left.get(key); | ||
}; | ||
WeakBiMap.prototype.has = function (key) { | ||
return this.left.has(key); | ||
}; | ||
WeakBiMap.prototype.set = function (key, value) { | ||
var oldVal = this.left.get(key); | ||
var oldKey = this.right.get(value); | ||
if (this.left.has(key)) { | ||
this.right.delete(oldVal); | ||
} | ||
if (this.right.has(value)) { | ||
this.left.delete(oldKey); | ||
} | ||
this.left.set(key, value); | ||
this.right.set(value, key); | ||
return this; | ||
}; | ||
Object.defineProperty(WeakBiMap.prototype, Symbol.toStringTag, { | ||
get: function () { | ||
return this.left[Symbol.toStringTag]; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
WeakBiMap.prototype.deleteValue = function (value) { | ||
var key = this.right.get(value); | ||
if (!this.left.has(key)) { | ||
return false; | ||
} | ||
this.left.delete(key); | ||
return this.right.delete(value); | ||
}; | ||
WeakBiMap.prototype.getKey = function (value) { | ||
return this.right.get(value); | ||
}; | ||
WeakBiMap.prototype.hasValue = function (value) { | ||
return this.right.has(value); | ||
}; | ||
return WeakBiMap; | ||
}()); | ||
exports.WeakBiMap = WeakBiMap; |
{ | ||
"name": "bim", | ||
"version": "0.0.7", | ||
"version": "0.1.2", | ||
"description": "A bidirectional map based on the ES6 Map object containing additional methods to retrive keys by values, delete key-value pairs by values and check the existence of keys by values.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
130
src/bim.ts
/// <reference path="../typings/tsd.d.ts" />; | ||
export default class Bim<K, V> implements Map<K, V> { | ||
export class BiMap<K, V> implements Map<K, V> { | ||
private left: Map<K, V>; | ||
@@ -95,80 +95,72 @@ private right: Map<V, K>; | ||
// function generateId(collision?: (temp: string) => boolean) { | ||
// const id = Math.random().toString(36).slice(2); | ||
// return collision !== undefined && collision(id) ? generateId(collision) : id; | ||
// } | ||
export class WeakBiMap<K, V> implements WeakMap<K, V> { | ||
private left: WeakMap<K, V>; | ||
private right: WeakMap<V, K>; | ||
// export class WeakBiMap<K, V> implements WeakMap<K, V> { | ||
// private keyUids = new Set<string>(); | ||
// private valUids = new Set<string>(); | ||
constructor(iterable?: Iterable<[K, V]>) { | ||
this.left = new WeakMap<K, V>(); | ||
this.right = new WeakMap<V, K>(); | ||
if (iterable === undefined) return; | ||
for (let [k, v] of iterable as any) { | ||
this.left.set(k, v); | ||
this.right.set(v, k); | ||
} | ||
} | ||
// constructor(iterable?: Iterable<[K, V]>) { | ||
// if (iterable === undefined) return; | ||
// for (let [k, v] of iterable as any) { | ||
// this.set(k, v); | ||
// } | ||
// } | ||
clear(): void { | ||
console.error('method clear is deprecated'); | ||
} | ||
// private getHiddenProperty(obj: any): string { | ||
// for (let prop of Object.keys(obj).filter(k => k[0] === '_')) { | ||
// if (this.keyUids.has(prop)) { | ||
// return prop; | ||
// } | ||
// } | ||
// return undefined; | ||
// } | ||
delete(key: K): boolean { | ||
const val = this.left.get(key); | ||
if (!this.right.has(val)) { | ||
return false; | ||
} | ||
this.right.delete(val); | ||
return this.left.delete(key); | ||
} | ||
// clear(): void { | ||
// console.error('method clear is deprecated'); | ||
// } | ||
get(key: K): V { | ||
return this.left.get(key); | ||
} | ||
// delete(key: K): boolean { | ||
// const hiddenProp = this.getHiddenProperty(key); | ||
// if (hiddenProp === undefined) { | ||
// return false; | ||
// } | ||
// key[hiddenProp] = undefined; | ||
// this.keyUids.delete(hiddenProp); | ||
// return true; | ||
// } | ||
has(key: K): boolean { | ||
return this.left.has(key); | ||
} | ||
// get(key: K): V { | ||
// return key[this.getHiddenProperty(key)]; | ||
// } | ||
set(key: K, value: V): this { | ||
const oldVal = this.left.get(key); | ||
const oldKey = this.right.get(value); | ||
if (this.left.has(key)) { | ||
this.right.delete(oldVal); | ||
} | ||
if (this.right.has(value)) { | ||
this.left.delete(oldKey); | ||
} | ||
this.left.set(key, value); | ||
this.right.set(value, key); | ||
return this; | ||
} | ||
// has(key: K): boolean { | ||
// return this.keyUids.has(this.getHiddenProperty(key)); | ||
// } | ||
get [Symbol.toStringTag](): string { | ||
return this.left[Symbol.toStringTag]; | ||
} | ||
// set(key: K, value: V): this { | ||
// const keyId = '_' + generateId(id => this.keyUids.has('_' + id) || id in key); | ||
// Object.defineProperty(key, keyId, { value }); | ||
// this.keyUids.add(keyId); | ||
// const valId = '_' + generateId(id => this.valUids.has('_' + id) || id in value); | ||
// Object.defineProperty(value, valId, { key }); | ||
// this.keyUids.add(keyId); | ||
// return this; | ||
// } | ||
deleteValue(value: V) { | ||
const key = this.right.get(value); | ||
if (!this.left.has(key)) { | ||
return false; | ||
} | ||
this.left.delete(key); | ||
return this.right.delete(value); | ||
} | ||
// get [Symbol.toStringTag](): string { | ||
// return {}[Symbol.toStringTag]; | ||
// } | ||
getKey(value: V) { | ||
return this.right.get(value); | ||
} | ||
// deleteValue(value: V) { | ||
// const key = this.right.get(value); | ||
// if (!this.left.has(key)) { | ||
// return false; | ||
// } | ||
// this.left.delete(key); | ||
// return this.right.delete(value); | ||
// } | ||
hasValue(value: V) { | ||
return this.right.has(value); | ||
} | ||
// getKey(value: V) { | ||
// return this.right.get(value); | ||
// } | ||
// hasValue(value: V) { | ||
// return this.right.has(value); | ||
// } | ||
// } | ||
} |
@@ -1,4 +0,4 @@ | ||
import Bim from '../src/bim'; | ||
import { BiMap } from '../src/bim'; | ||
const m = new Bim<string, number>(); | ||
const m = new BiMap<string, number>(); | ||
m.set("foo", 3); | ||
@@ -5,0 +5,0 @@ m.set("bar", 12); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
789224
17241