Socket
Socket
Sign inDemoInstall

multimap

Package Overview
Dependencies
0
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.1 to 0.0.2

test/es6map.js

71

index.js

@@ -8,9 +8,9 @@ "use strict";

if(typeof Map != 'undefined') {
self._map = Map;
if (Multimap.Map) {
self._map = Multimap.Map;
}
self._ = self._map ? new self._map() : {};
if(iterable) {
if (iterable) {
iterable.forEach(function(i) {

@@ -40,5 +40,5 @@ self.set(i[0], i[1]);

var entry = this.get(key);
if(!entry) {
if (!entry) {
entry = [];
if(this._map)
if (this._map)
this._.set(key, entry);

@@ -59,12 +59,12 @@ else

Multimap.prototype.delete = function(key, val) {
if(!this.has(key))
if (!this.has(key))
return false;
if(arguments.length == 1) {
if (arguments.length == 1) {
this._map ? (this._.delete(key)) : (delete this._[key]);
return true;
}else {
} else {
var entry = this.get(key);
var idx = entry.indexOf(val);
if(idx != -1){
if (idx != -1) {
entry.splice(idx, 1);

@@ -84,5 +84,5 @@ return true;

Multimap.prototype.has = function(key, val) {
var hasKey = this._map ? this._.has(key) : this._.hasOwnProperty(key);
var hasKey = this._map ? this._.has(key) : this._.hasOwnProperty(key);
if(arguments.length == 1 || !hasKey)
if (arguments.length == 1 || !hasKey)
return hasKey;

@@ -98,3 +98,6 @@

Multimap.prototype.keys = function() {
return this._map ? this._.keys() : Object.keys(this._);
if (this._map)
return this._.keys();
return makeIterator(Object.keys(this._));
};

@@ -111,3 +114,3 @@

return vals;
return makeIterator(vals);
};

@@ -120,12 +123,15 @@

var self = this;
self.keys().forEach(function(key) {
iter(self.get(key), key);
});
var keys = self.keys();
var next;
while(!(next = keys.next()).done) {
iter(self.get(next.value), next.value, self);
}
};
Multimap.prototype.forEach = function(iter){
Multimap.prototype.forEach = function(iter) {
var self = this;
self.forEachEntry(function(entry, key) {
entry.forEach(function(item) {
iter(item, key);
iter(item, key, self);
});

@@ -137,5 +143,5 @@ });

Multimap.prototype.clear = function() {
if(this._map) {
if (this._map) {
this._.clear();
}else {
} else {
this._ = {};

@@ -146,3 +152,3 @@ }

Object.defineProperty(
Multimap.prototype,
Multimap.prototype,
"size", {

@@ -153,5 +159,8 @@ configurable: false,

var self = this;
return self.keys().reduce(function(total, key) {
return total + self.get(key).length;
}, 0);
var keys = self.keys();
var next, total = 0;
while(!(next = keys.next()).done) {
total += self.get(next.value).length;
}
return total;
}

@@ -161,1 +170,13 @@ });

function makeIterator(array){
var nextIndex = 0;
return {
next: function(){
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{done: true};
}
}
}
{
"name": "multimap",
"version": "0.0.1",
"version": "0.0.2",
"description": "multi-map which allow multi values for the same key",

@@ -8,3 +8,3 @@ "main": "index.js",

"lint": "./node_modules/.bin/jshint *.js test/*.js",
"test": "npm run lint; node test/index.js"
"test": "npm run lint; node test/index.js;node test/es6map.js"
},

@@ -25,4 +25,5 @@ "repository": {

"devDependencies": {
"jshint": "~2.1.9",
"chai": "~1.7.2"
"chai": "~1.7.2",
"es6-shim": "^0.13.0",
"jshint": "~2.1.9"
},

@@ -29,0 +30,0 @@ "readmeFilename": "README.md",

@@ -19,4 +19,19 @@ # Multimap - Map which Allow Multiple Values for the same Key

If `Map` is available in global, `Multimap` will use `Map` as inner store, that means Object can be used as key.
If `Multimap.Map` is set, `Multimap` will use the `Map` as inner store, that means Object can be used as key.
```javascript
var Multimap = require('multimap');
// if harmony is on
Multimap.Map = Map;
// or if you are using es6-shim
Multimap.Map = ShimMap;
var m = new Multimap();
var key = {};
m.set(key, 'one');
```
Otherwise, an object will be used, all the keys will be transformed into string.

@@ -71,4 +86,5 @@

var keys = map.keys(); // ['a', 'b']
var values = map.values(); // ['one', 'two', 1, 2]
var keys = map.keys(); // iterator with ['a', 'b']
keys.next().value; // 'a'
var values = map.values(); // iterator ['one', 'two', 1, 2]

@@ -75,0 +91,0 @@ map.clear(); // undefined

@@ -0,15 +1,22 @@

"use strict";
var assert = require('chai').assert;
var Multimap = require('..');
var map = new Multimap([['a', 'one'], ['b', 1], ['a', 'two'], ['b', 2]]);
var map = new Multimap([
['a', 'one'],
['b', 1],
['a', 'two'],
['b', 2]
]);
assert.equal(map.size, 4);
assert.equal(map.get('a').length, 2);
assert.equal(map.get('a')[0], 'one'); // ['one', 'two']
assert.equal(map.get('a')[1], 'two'); // ['one', 'two']
assert.equal(map.get('a').length, 2);
assert.equal(map.get('a')[0], 'one'); // ['one', 'two']
assert.equal(map.get('a')[1], 'two'); // ['one', 'two']
assert.equal(map.get('b').length, 2);
assert.equal(map.get('b')[0], 1); // [1, 2]
assert.equal(map.get('b')[1], 2); // [1, 2]
assert.equal(map.get('b')[0], 1); // [1, 2]
assert.equal(map.get('b')[1], 2); // [1, 2]

@@ -26,3 +33,3 @@

assert.equal(map.size, 5);
assert.equal(map.get('a').length, 3); // ['one', 'two', 'three']
assert.equal(map.get('a').length, 3); // ['one', 'two', 'three']

@@ -41,6 +48,6 @@ map.set('b', 3, 4);

map.set('b', 1, 2);
assert.equal(map.size, 4); // 4
assert.equal(map.size, 4); // 4
var cnt = 0;
map.forEach(function (value, key) {
map.forEach(function(value, key) {
// iterates { 'a', 'one' }, { 'a', 'two' }, { 'b', 1 }, { 'b', 2 }

@@ -54,7 +61,7 @@ cnt++;

cnt = 0;
map.forEachEntry(function (entry, key) {
map.forEachEntry(function(entry, key) {
// iterates { 'a', ['one', 'two'] }, { 'b', [1, 2] }
cnt++;
assert(key == 'a' || key == 'b', "key must be either 'a' or 'b'");
assert.equal(entry.length , 2);
assert.equal(entry.length, 2);
});

@@ -65,13 +72,17 @@

var keys = map.keys(); // ['a', 'b']
assert.equal(keys.length, 2);
assert.equal(keys[0], 'a');
assert.equal(keys[1], 'b');
var keys = map.keys(); // ['a', 'b']
assert.equal(keys.next().value, 'a');
assert.equal(keys.next().value, 'b');
assert(keys.next().done);
var values = map.values(); // ['one', 'two', 1, 2]
assert.equal(values.length, 4);
var values = map.values(); // ['one', 'two', 1, 2]
assert.equal(values.next().value, 'one');
assert.equal(values.next().value, 'two');
assert.equal(values.next().value, 1);
assert.equal(values.next().value, 2);
assert(values.next().done);
map.clear();
assert.equal(map.size, 0);
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc