Comparing version 1.0.3 to 1.0.4
@@ -90,4 +90,5 @@ "use strict"; | ||
this.right = new WeakMap(); | ||
if (iterable === undefined) | ||
if (iterable === undefined) { | ||
return; | ||
} | ||
for (var _i = 0, _a = iterable; _i < _a.length; _i++) { | ||
@@ -94,0 +95,0 @@ var _b = _a[_i], k = _b[0], v = _b[1]; |
{ | ||
"name": "bim", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"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.", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc" | ||
"build": "rm -rf lib && tsc" | ||
}, | ||
@@ -12,0 +12,0 @@ "repository": { |
126
src/bim.ts
export class BiMap<K, V> implements Map<K, V> { | ||
private left: Map<K, V>; | ||
private right: Map<V, K>; | ||
private left: Map<K, V> | ||
private right: Map<V, K> | ||
constructor(iterable?: Iterable<[K, V]>) { | ||
this.left = new Map<K, V>(iterable); | ||
this.right = new Map<V, K>(); | ||
this.left.forEach(i => this.right.set(i)); | ||
this.left = new Map<K, V>(iterable) | ||
this.right = new Map<V, K>() | ||
this.left.forEach(i => this.right.set(i)) | ||
} | ||
clear(): void { | ||
this.left.clear(); | ||
this.right.clear(); | ||
this.left.clear() | ||
this.right.clear() | ||
} | ||
delete(key: K): boolean { | ||
const val = this.left.get(key); | ||
const val = this.left.get(key) | ||
if (!this.right.has(val)) { | ||
return false; | ||
return false | ||
} | ||
this.right.delete(val); | ||
return this.left.delete(key); | ||
this.right.delete(val) | ||
return this.left.delete(key) | ||
} | ||
entries(): IterableIterator<[K, V]> { | ||
return this.left.entries(); | ||
return this.left.entries() | ||
} | ||
forEach(callbackfn: (value: V, index: K, map: Map<K, V>) => void, thisArg?: any): void { | ||
this.left.forEach(callbackfn, thisArg); | ||
this.left.forEach(callbackfn, thisArg) | ||
} | ||
get(key: K): V { | ||
return this.left.get(key); | ||
return this.left.get(key) | ||
} | ||
has(key: K): boolean { | ||
return this.left.has(key); | ||
return this.left.has(key) | ||
} | ||
keys(): IterableIterator<K> { | ||
return this.left.keys(); | ||
return this.left.keys() | ||
} | ||
set(key: K, value: V): this { | ||
const oldVal = this.left.get(key); | ||
const oldKey = this.right.get(value); | ||
const oldVal = this.left.get(key) | ||
const oldKey = this.right.get(value) | ||
if (this.left.has(key)) { | ||
this.right.delete(oldVal); | ||
this.right.delete(oldVal) | ||
} | ||
if (this.right.has(value)) { | ||
this.left.delete(oldKey); | ||
this.left.delete(oldKey) | ||
} | ||
this.left.set(key, value); | ||
this.right.set(value, key); | ||
return this; | ||
this.left.set(key, value) | ||
this.right.set(value, key) | ||
return this | ||
} | ||
get size(): number { | ||
return this.left.size; | ||
return this.left.size | ||
} | ||
values(): IterableIterator<V> { | ||
return this.left.values(); | ||
return this.left.values() | ||
} | ||
[Symbol.iterator]() { | ||
return this.left[Symbol.iterator](); | ||
return this.left[Symbol.iterator]() | ||
} | ||
get [Symbol.toStringTag]() { | ||
return this.left[Symbol.toStringTag]; | ||
return this.left[Symbol.toStringTag] | ||
} | ||
deleteValue(value: V) { | ||
const key = this.right.get(value); | ||
const key = this.right.get(value) | ||
if (!this.left.has(key)) { | ||
return false; | ||
return false | ||
} | ||
this.left.delete(key); | ||
return this.right.delete(value); | ||
this.left.delete(key) | ||
return this.right.delete(value) | ||
} | ||
getKey(value: V) { | ||
return this.right.get(value); | ||
return this.right.get(value) | ||
} | ||
hasValue(value: V) { | ||
return this.right.has(value); | ||
return this.right.has(value) | ||
} | ||
@@ -94,12 +94,14 @@ } | ||
export class WeakBiMap<K, V> implements WeakMap<K, V> { | ||
private left: WeakMap<K, V>; | ||
private right: WeakMap<V, K>; | ||
private left: WeakMap<K, V> | ||
private right: WeakMap<V, K> | ||
constructor(iterable?: Iterable<[K, V]>) { | ||
this.left = new WeakMap<K, V>(); | ||
this.right = new WeakMap<V, K>(); | ||
if (iterable === undefined) return; | ||
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); | ||
this.left.set(k, v) | ||
this.right.set(v, k) | ||
} | ||
@@ -109,57 +111,57 @@ } | ||
clear(): void { | ||
console.error('method clear is deprecated'); | ||
console.error('method clear is deprecated') | ||
} | ||
delete(key: K): boolean { | ||
const val = this.left.get(key); | ||
const val = this.left.get(key) | ||
if (!this.right.has(val)) { | ||
return false; | ||
return false | ||
} | ||
this.right.delete(val); | ||
return this.left.delete(key); | ||
this.right.delete(val) | ||
return this.left.delete(key) | ||
} | ||
get(key: K): V { | ||
return this.left.get(key); | ||
return this.left.get(key) | ||
} | ||
has(key: K): boolean { | ||
return this.left.has(key); | ||
return this.left.has(key) | ||
} | ||
set(key: K, value: V): this { | ||
const oldVal = this.left.get(key); | ||
const oldKey = this.right.get(value); | ||
const oldVal = this.left.get(key) | ||
const oldKey = this.right.get(value) | ||
if (this.left.has(key)) { | ||
this.right.delete(oldVal); | ||
this.right.delete(oldVal) | ||
} | ||
if (this.right.has(value)) { | ||
this.left.delete(oldKey); | ||
this.left.delete(oldKey) | ||
} | ||
this.left.set(key, value); | ||
this.right.set(value, key); | ||
return this; | ||
this.left.set(key, value) | ||
this.right.set(value, key) | ||
return this | ||
} | ||
get [Symbol.toStringTag]() { | ||
return this.left[Symbol.toStringTag]; | ||
return this.left[Symbol.toStringTag] | ||
} | ||
deleteValue(value: V) { | ||
const key = this.right.get(value); | ||
const key = this.right.get(value) | ||
if (!this.left.has(key)) { | ||
return false; | ||
return false | ||
} | ||
this.left.delete(key); | ||
return this.right.delete(value); | ||
this.left.delete(key) | ||
return this.right.delete(value) | ||
} | ||
getKey(value: V) { | ||
return this.right.get(value); | ||
return this.right.get(value) | ||
} | ||
hasValue(value: V) { | ||
return this.right.has(value); | ||
return this.right.has(value) | ||
} | ||
} | ||
} |
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
368
13208