Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bim

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bim - npm Package Compare versions

Comparing version 0.1.4 to 1.0.0

lib/bim.d.ts

150

index.js

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

"use strict";
var BiMap = (function () {
function BiMap(iterable) {
var _this = this;
this.left = new Map(iterable);
this.right = new Map();
this.left.forEach(function (i) { return _this.right.set(i); });
}
BiMap.prototype.clear = function () {
this.left.clear();
this.right.clear();
};
BiMap.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);
};
BiMap.prototype.entries = function () {
return this.left.entries();
};
BiMap.prototype.forEach = function (callbackfn, thisArg) {
this.left.forEach(callbackfn, thisArg);
};
BiMap.prototype.get = function (key) {
return this.left.get(key);
};
BiMap.prototype.has = function (key) {
return this.left.has(key);
};
BiMap.prototype.keys = function () {
return this.left.keys();
};
BiMap.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(BiMap.prototype, "size", {
get: function () {
return this.left.size;
},
enumerable: true,
configurable: true
});
BiMap.prototype.values = function () {
return this.left.values();
};
BiMap.prototype[Symbol.iterator] = function () {
return this.left[Symbol.iterator]();
};
Object.defineProperty(BiMap.prototype, Symbol.toStringTag, {
get: function () {
return this.left[Symbol.toStringTag];
},
enumerable: true,
configurable: true
});
BiMap.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);
};
BiMap.prototype.getKey = function (value) {
return this.right.get(value);
};
BiMap.prototype.hasValue = function (value) {
return this.right.has(value);
};
return BiMap;
}());
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;
'use strict'
module.exports = require('./lib/bim')
{
"name": "bim",
"version": "0.1.4",
"version": "1.0.0",
"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.",
"main": "index.js",
"types": "lib/bim.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc"
},

@@ -17,3 +19,4 @@ "repository": {

"bidirectional",
"map"
"map",
"weakmap"
],

@@ -20,0 +23,0 @@ "author": "Anton Veselyev <inker.gh@gmail.com> (https://github.com/inker)",

# bim
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.
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. The module contains two classes: `BiMap` & `WeakBiMap` based on `Map` & `WeakMap` respectively.
## Installation
```
npm install bim
npm install --save bim
```
## Usage
The same as normal `Map` & `WeakMap`, plus the `getKey`, `hasValue` & `deleteValue` methods.
```javascript
import { BiMap } from 'bim';
const bim = new BiMap();
bim.set(5, "foo");
bim.set(6, "bar");
bim.get(6); // "bar"
bim.delete(6);
bim.set(1, "qux");
bim.getKey("qux");
bim.set(2, "aaa");
bim.set(1, "foo");
import { WeakBiMap } from 'bim'
// create objects to use as keys
const a = {
i: 'foo',
j: 8,
}
const b = {
k: 'bar',
p: 11,
}
// create the bidirectional weak map
const wbm = new WeakBiMap()
wbm.set(a, 5)
wbm.set(b, 6)
wbm.deleteValue(5) // now only has { k: 'bar', p: 11 } => 6
wbm.hasValue(5) // false
const c = {
h: 'quux',
z: 100,
}
wbm.set(c, 7)
wbm.hasValue(7) // true
wbm.getKey(7) // { h: 'quux', z: 100 }
```

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

/// <reference path="../typings/tsd.d.ts" />;
export class BiMap<K, V> implements Map<K, V> {

@@ -69,7 +67,7 @@ private left: Map<K, V>;

[Symbol.iterator]():IterableIterator<[K,V]> {
[Symbol.iterator]() {
return this.left[Symbol.iterator]();
}
get [Symbol.toStringTag](): string {
get [Symbol.toStringTag]() {
return this.left[Symbol.toStringTag];

@@ -145,3 +143,3 @@ }

get [Symbol.toStringTag](): string {
get [Symbol.toStringTag]() {
return this.left[Symbol.toStringTag];

@@ -148,0 +146,0 @@ }

@@ -7,5 +7,9 @@ {

"watch": true,
"noLib": true
"pretty": true,
"lib": [ "dom", "es7" ],
"declaration": true,
"alwaysStrict": true,
"outDir": "lib"
},
"exclude": [ "node_modules" ]
}
"exclude": [ "node_modules", "test" ]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc