Socket
Socket
Sign inDemoInstall

array-indexer

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.2 to 1.0.0

2

bench.js

@@ -23,3 +23,3 @@ var index = require('./');

iterations = 1000;
iterations = 100000;

@@ -26,0 +26,0 @@ start = +new Date();

@@ -31,7 +31,11 @@ module.exports = index;

if (!idx[field].hasOwnProperty(val)) {
idx[field][val] = [];
}
idx[field][val].push(obj);
val = Array.isArray(val) ? val : [val]
val.forEach(function (v) {
if (!idx[field].hasOwnProperty(v)) {
idx[field][v] = [];
}
idx[field][v].push(obj);
})
});

@@ -45,5 +49,6 @@ });

Object.keys(obj).forEach(function (field) {
//avoid indexing fields whose values are
//objects
if (typeof obj[field] === 'object') {
val = obj[field];
//avoid indexing fields whose values are objects
if (typeof val === 'object' && !Array.isArray(val)) {
return;

@@ -55,10 +60,12 @@ }

}
val = obj[field];
if (!idx[field].hasOwnProperty(val)) {
idx[field][val] = [];
}
idx[field][val].push(obj);
val = Array.isArray(val) ? val : [val]
val.forEach(function (v) {
if (!idx[field].hasOwnProperty(v)) {
idx[field][v] = [];
}
idx[field][v].push(obj);
})
});

@@ -65,0 +72,0 @@ });

{
"name": "array-indexer",
"version": "0.0.2",
"version": "1.0.0",
"description": "Create indexes based on arrays of objects",
"main": "index.js",
"scripts": {
"test": "nodeunit test.js"
},
"repository": {

@@ -19,3 +16,9 @@ "type": "git",

"object"
]
}
],
"devDependencies": {
"nodeunit": "^0.11.3"
},
"scripts": {
"test": "nodeunit test.js"
}
}
var index = require('./');
var data = [
{ id : 1, name : 'Dan', sex : 'male', child : { a : 1 } }
, { id : 2, name : 'Sam', sex : 'female', child : { a : 2 } }
, { id : 3, name : 'Pat', sex : 'female', child : { a : 3 } }
, { id : 4, name : 'Jon', sex : 'male', child : { a : 1 } }
, { id : 5, name : 'Dug', sex : 'male', child : { a : 2 } }
{ id: 1, name: 'Dan', sex: 'male', child: { a: 1 }, colors: ['red', 'green'] }
, { id: 2, name: 'Sam', sex: 'female', child: { a: 2 }, colors: ['brown', 'orange'] }
, { id: 3, name: 'Pat', sex: 'female', child: { a: 3 }, colors: ['yellow', 'blue'] }
, { id: 4, name: 'Jon', sex: 'male', child: { a: 1 }, colors: ['black', 'orange'] }
, { id: 5, name: 'Dug', sex: 'male', child: { a: 2 }, colors: ['blue', 'brown'] }
];
module.exports = {
"test data array with field list array " : function (test) {
"test data array with field list array ": function (test) {
test.deepEqual(
index(data, ['sex'])
, {
, {
sex: {
male: [
{ id: 1, name: 'Dan', sex: 'male', child : { a : 1 } }
, { id: 4, name: 'Jon', sex: 'male', child : { a : 1 } }
, { id: 5, name: 'Dug', sex: 'male', child : { a : 2 } }
male: [
{ id: 1, name: 'Dan', sex: 'male', child: { a: 1 }, colors: ['red', 'green'] }
, { id: 4, name: 'Jon', sex: 'male', child: { a: 1 }, colors: ['black', 'orange'] }
, { id: 5, name: 'Dug', sex: 'male', child: { a: 2 }, colors: ['blue', 'brown'] }
]
, female: [
{ id: 2, name: 'Sam', sex: 'female', child : { a : 2 } }
, { id: 3, name: 'Pat', sex: 'female', child : { a : 3 } }
]
}
, female: [
{ id: 2, name: 'Sam', sex: 'female', child: { a: 2 }, colors: ['brown', 'orange'] }
, { id: 3, name: 'Pat', sex: 'female', child: { a: 3 }, colors: ['yellow', 'blue'] }
]
}
}
);
test.done();
}
, "test data array with field string " : function (test) {
, "test data array with field string ": function (test) {
test.deepEqual(
index(data, 'sex')
, {
sex : {
male: [
{ id: 1, name: 'Dan', sex: 'male', child : { a : 1 } }
, { id: 4, name: 'Jon', sex: 'male', child : { a : 1 } }
, { id: 5, name: 'Dug', sex: 'male', child : { a : 2 } }
, {
sex: {
male: [
{ id: 1, name: 'Dan', sex: 'male', child: { a: 1 }, colors: ['red', 'green'] }
, { id: 4, name: 'Jon', sex: 'male', child: { a: 1 }, colors: ['black', 'orange'] }
, { id: 5, name: 'Dug', sex: 'male', child: { a: 2 }, colors: ['blue', 'brown'] }
]
, female: [
{ id: 2, name: 'Sam', sex: 'female', child : { a : 2 } }
, { id: 3, name: 'Pat', sex: 'female', child : { a : 3 } }
]
}
, female: [
{ id: 2, name: 'Sam', sex: 'female', child: { a: 2 }, colors: ['brown', 'orange'] }
, { id: 3, name: 'Pat', sex: 'female', child: { a: 3 }, colors: ['yellow', 'blue'] }
]
}
}
);
test.done();
}
, "test data array, index all fields" : function (test) {
, "test data array, index all fields": function (test) {
test.deepEqual(
index(data)
, { id: {
'1': [ { id: 1, name: 'Dan', sex: 'male', child : { a : 1 } } ]
, '2': [ { id: 2, name: 'Sam', sex: 'female', child : { a : 2 } } ]
, '3': [ { id: 3, name: 'Pat', sex: 'female', child : { a : 3 } } ]
, '4': [ { id: 4, name: 'Jon', sex: 'male', child : { a : 1 } } ]
, '5': [ { id: 5, name: 'Dug', sex: 'male', child : { a : 2 } } ]
, {
id: {
'1': [{ id: 1, name: 'Dan', sex: 'male', child: { a: 1 }, colors: ['red', 'green'] }]
, '2': [{ id: 2, name: 'Sam', sex: 'female', child: { a: 2 }, colors: ['brown', 'orange'] }]
, '3': [{ id: 3, name: 'Pat', sex: 'female', child: { a: 3 }, colors: ['yellow', 'blue'] }]
, '4': [{ id: 4, name: 'Jon', sex: 'male', child: { a: 1 }, colors: ['black', 'orange'] }]
, '5': [{ id: 5, name: 'Dug', sex: 'male', child: { a: 2 }, colors: ['blue', 'brown'] }]
}
, name: {
Dan: [ { id: 1, name: 'Dan', sex: 'male', child : { a : 1 } } ]
, Sam: [ { id: 2, name: 'Sam', sex: 'female', child : { a : 2 } } ]
, Pat: [ { id: 3, name: 'Pat', sex: 'female', child : { a : 3 } } ]
, Jon: [ { id: 4, name: 'Jon', sex: 'male', child : { a : 1 } } ]
, Dug: [ { id: 5, name: 'Dug', sex: 'male', child : { a : 2 } } ]
, name: {
Dan: [{ id: 1, name: 'Dan', sex: 'male', child: { a: 1 }, colors: ['red', 'green'] }]
, Sam: [{ id: 2, name: 'Sam', sex: 'female', child: { a: 2 }, colors: ['brown', 'orange'] }]
, Pat: [{ id: 3, name: 'Pat', sex: 'female', child: { a: 3 }, colors: ['yellow', 'blue'] }]
, Jon: [{ id: 4, name: 'Jon', sex: 'male', child: { a: 1 }, colors: ['black', 'orange'] }]
, Dug: [{ id: 5, name: 'Dug', sex: 'male', child: { a: 2 }, colors: ['blue', 'brown'] }]
}
, sex: {
male: [
{ id: 1, name: 'Dan', sex: 'male', child : { a : 1 } }
, { id: 4, name: 'Jon', sex: 'male', child : { a : 1 } }
, { id: 5, name: 'Dug', sex: 'male', child : { a : 2 } }
male: [
{ id: 1, name: 'Dan', sex: 'male', child: { a: 1 }, colors: ['red', 'green'] }
, { id: 4, name: 'Jon', sex: 'male', child: { a: 1 }, colors: ['black', 'orange'] }
, { id: 5, name: 'Dug', sex: 'male', child: { a: 2 }, colors: ['blue', 'brown'] }
]
, female: [
{ id: 2, name: 'Sam', sex: 'female', child : { a : 2 } }
, { id: 3, name: 'Pat', sex: 'female', child : { a : 3 } }
]
, female: [
{ id: 2, name: 'Sam', sex: 'female', child: { a: 2 }, colors: ['brown', 'orange'] }
, { id: 3, name: 'Pat', sex: 'female', child: { a: 3 }, colors: ['yellow', 'blue'] }
]
}
, colors: {
"red": [
{ "id": 1, "name": "Dan", "sex": "male", "child": { "a": 1 }, "colors": ["red", "green"] }
]
, "green": [
{ "id": 1, "name": "Dan", "sex": "male", "child": { "a": 1 }, "colors": ["red", "green"] }
]
, "brown": [
{ "id": 2, "name": "Sam", "sex": "female", "child": { "a": 2 }, "colors": ["brown", "orange"] }
, { "id": 5, "name": "Dug", "sex": "male", "child": { "a": 2 }, "colors": ["blue", "brown"] }
]
, "orange": [
{ "id": 2, "name": "Sam", "sex": "female", "child": { "a": 2 }, "colors": ["brown", "orange"] }
, { "id": 4, "name": "Jon", "sex": "male", "child": { "a": 1 }, "colors": ["black", "orange"] }
]
, "yellow": [
{ "id": 3, "name": "Pat", "sex": "female", "child": { "a": 3 }, "colors": ["yellow", "blue"] }
]
, "blue": [
{ "id": 3, "name": "Pat", "sex": "female", "child": { "a": 3 }, "colors": ["yellow", "blue"] }
, { "id": 5, "name": "Dug", "sex": "male", "child": { "a": 2 }, "colors": ["blue", "brown"] }
]
, "black": [
{ "id": 4, "name": "Jon", "sex": "male", "child": { "a": 1 }, "colors": ["black", "orange"] }
]
}
}
);
test.done();
}
, "test data array, index single deep field" : function (test) {
, "test data array, index single deep field": function (test) {
test.deepEqual(
index(data, 'child.a')
, { 'child.a' :
{
1: [
{ id: 1, name: 'Dan', sex: 'male', child : { a : 1 } }
, { id: 4, name: 'Jon', sex: 'male', child : { a : 1 } }
, {
'child.a':
{
1: [
{ id: 1, name: 'Dan', sex: 'male', child: { a: 1 }, colors: ['red', 'green'] }
, { id: 4, name: 'Jon', sex: 'male', child: { a: 1 }, colors: ['black', 'orange'] }
]
, 2 : [
{ id: 2, name: 'Sam', sex: 'female', child : { a : 2 } }
, { id: 5, name: 'Dug', sex: 'male', child : { a : 2 } }
, 2: [
{ id: 2, name: 'Sam', sex: 'female', child: { a: 2 }, colors: ['brown', 'orange'] }
, { id: 5, name: 'Dug', sex: 'male', child: { a: 2 }, colors: ['blue', 'brown'] }
]
, 3 : [
{ id: 3, name: 'Pat', sex: 'female', child : { a : 3 } }
]
, 3: [
{ id: 3, name: 'Pat', sex: 'female', child: { a: 3 }, colors: ['yellow', 'blue'] }
]
}
}
);
test.done();
}
};
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