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

array-keys

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-keys - npm Package Compare versions

Comparing version 1.1.1 to 1.2.0

browser/array-keys.js

149

array-keys.js
/*!
* array-keys
* version 1.1.1
* version 1.2.0
* http://github.com/silverbucket/array-keys

@@ -19,76 +19,109 @@ *

(function (global, factory, undefined) {
function ArrayKeys(p) {
if (typeof p !== 'object') { p = {}; }
this.identifier = p.identifier || 'id';
this.store = [];
this.idx = []; // array of identifier strings for quick lookup
}
if ( typeof module === 'object' && typeof module.exports === 'object' ) {
module.exports = (global.document) ? factory(global) : factory({});
} else {
factory(global);
ArrayKeys.prototype.getIdentifiers = function () {
var ids = [];
for (var i = this.store.length - 1; i >= 0; i = i - 1) {
ids.push(this.store[i][this.identifier]);
}
return ids;
};
}((typeof window !== 'undefined') ? window : this, function (scope, undefined) {
ArrayKeys.prototype.getRecord = function (id) {
for (var i = this.store.length - 1; i >= 0; i = i - 1) {
if ('' + this.store[i][this.identifier] === '' + id) {
return this.store[i];
}
}
return undefined;
};
function ArrayKeys(p) {
if (typeof p !== 'object') { p = {}; }
this.identifier = p.identifier || 'id';
this.idx = [];
ArrayKeys.prototype.exists = function (id) {
if (this.idx.getIndex(id) >= 0) {
return true;
} else {
return false;
}
};
ArrayKeys.prototype.getIdentifiers = function () {
var ids = [];
for (var i = this.idx.length - 1; i >= 0; i--) {
ids.push(this.idx[i][this.identifier]);
// faster than using indexOf
ArrayKeys.prototype.getIndex = function (id) {
for (i = this.idx.length - 1; i >= 0; i = i - 1) {
if (this.idx[i] === id) {
return i;
}
return ids;
};
}
return -1;
};
ArrayKeys.prototype.getRecord = function (id) {
for (var i = this.idx.length - 1; i >= 0; i--) {
if ('' + this.idx[i][this.identifier] === '' + id) {
return this.idx[i];
}
ArrayKeys.prototype.addRecord = function (record) {
if (typeof record !== 'object') {
throw new Error('cannot add non-object records.');
} else if (!record[this.identifier]) {
throw new Error('cannot add a record with no `' + this.identifier +
'` property specified.');
}
this.removeRecord(record[this.identifier]);
this.idx.push(record[this.identifier]);
this.store.push(record);
return true;
};
ArrayKeys.prototype.removeRecord = function (id) {
var i;
var idx = this.getIndex(id);
if (idx < 0) {
return false;
}
// start looking for the record at the same point as the idx entry
for (i = idx; i >= 0; i = i - 1) {
if (this.store[i][this.identifier] === id) {
this.store.splice(i, 1);
this.idx.splice(idx, 1);
return true;
}
return undefined;
};
}
ArrayKeys.prototype.addRecord = function (record) {
if (typeof record !== 'object') {
throw new Error('cannot add non-object records.');
} else if (!record[this.identifier]) {
throw new Error('cannot add a record with no `' + this.identifier +
'` property specified.');
// if it was not found, start at the end and break at the idx number
for (i = this.store.length - 1; i >= idx; i = i - 1) {
if (this.store[i][this.identifier] === id) {
this.store.splice(i, 1);
this.idx.splice(idx, 1);
return true;
}
this.removeRecord(record[this.identifier]);
this.idx.push(record);
return true;
};
}
return false;
};
ArrayKeys.prototype.removeRecord = function (id) {
for (var i = this.idx.length - 1; i >= 0; i--) {
if ('' + this.idx[i][this.identifier] === '' + id) {
this.idx.splice(i, 1);
return true;
}
ArrayKeys.prototype.forEachRecord = function (cb) {
var count = 0;
var self = this;
var finished = function () {};
setTimeout(function () {
for (var i = self.store.length - 1; i >= 0; i = i - 1) {
count += 1;
cb(self.store[i]);
}
return false;
};
finished(count);
}, 0);
ArrayKeys.prototype.forEachRecord = function (cb) {
for (var i = this.idx.length - 1; i >= 0; i--) {
cb(this.idx[i]);
return {
finally: function (func) {
finished = func;
}
};
};
ArrayKeys.prototype.getCount = function () {
return this.idx.length;
};
ArrayKeys.prototype.getCount = function () {
return this.store.length;
};
if ( typeof define === 'function' && define.amd ) {
define([], function() {
return ArrayKeys;
});
}
scope.ArrayKeys = ArrayKeys;
return ArrayKeys;
}));
module.exports = ArrayKeys;
{
"name": "array-keys",
"version": "1.1.1",
"version": "1.2.0",
"description": "a simple interface to manage large arrays of objects easily",

@@ -20,2 +20,5 @@ "license": "LGPL",

"gulp": "^3.8.8",
"gulp-browserify": "^0.5.0",
"gulp-rename": "^1.2.0",
"gulp-uglify": "^1.0.1",
"jaribu": "^0.2.2",

@@ -22,0 +25,0 @@ "minify": "^1.0.4"

@@ -39,2 +39,5 @@ # array-keys

// this function is called once for each record
}).finally(function (count) {
// function called after the above callback is called for each record
// count is the total number of records processed
});

@@ -41,0 +44,0 @@

@@ -1,98 +0,217 @@

if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(['require', './../array-keys.js'], function (require, ArrayKeysAMD) {
var suites = [];
function getTests() {
return [
{
desc: 'ensure amd module is loaded correctly',
run: function (env, test) {
if (typeof amdMod !== 'undefined') {
test.assertTypeAnd(amdMod, 'function');
var amdmod = new amdMod();
test.assertType(amdmod.addRecord, 'function');
} else{
test.done();
}
}
},
suites.push({
desc: "basic tests",
setup: function (env, test) {
env.ArrayKeys = require('./../array-keys.js');
test.assertTypeAnd(env.ArrayKeys, 'function');
env.ak = new env.ArrayKeys();
test.assertType(env.ak.addRecord, 'function');
{
desc: '# getRecordIFExists - with no params returns undefined',
run: function (env, test) {
test.assert(env.mod.getRecord('thingy'), undefined);
}
},
tests: [
{
desc: 'ensure amd module is loaded correctly',
run: function (env, test) {
test.assertTypeAnd(ArrayKeysAMD, 'function');
var amdak = new ArrayKeysAMD();
test.assertType(amdak.addRecord, 'function');
}
},
{
desc: '# getRecordIFExists - with no params returns undefined',
run: function (env, test) {
test.assert(env.ak.getRecord('thingy'), undefined);
}
},
{
desc: '# addRecord 1',
run: function (env, test) {
test.assert(env.mod.addRecord({id:'thingy1'}), true);
}
},
{
desc: '# addRecord 1',
run: function (env, test) {
test.assert(env.ak.addRecord({id:'thingy1'}), true);
}
},
{
desc: '# getRecordIFExists',
run: function (env, test) {
test.assert(env.mod.getRecord('thingy1'), {id:'thingy1'});
}
},
{
desc: '# getRecordIFExists',
run: function (env, test) {
test.assert(env.ak.getRecord('thingy1'), {id:'thingy1'});
}
},
{
desc: '# addRecord with no identifier',
run: function (env, test) {
test.throws(env.mod.addRecord, Error, 'caught thrown exception');
}
},
{
desc: '# addRecord with no identifier',
run: function (env, test) {
test.throws(env.ak.addRecord, Error, 'caught thrown exception');
}
},
{
desc: '# addRecord 2',
run: function (env, test) {
test.assert(env.mod.addRecord({id:'thingy2'}), true);
}
},
{
desc: '# addRecord 2',
run: function (env, test) {
test.assert(env.ak.addRecord({id:'thingy2'}), true);
}
},
{
desc: '# addRecord 3',
run: function (env, test) {
test.assert(env.mod.addRecord({id:'thingy3'}), true);
}
},
{
desc: '# addRecord 3',
run: function (env, test) {
test.assert(env.ak.addRecord({id:'thingy3'}), true);
}
},
{
desc: '# getCount (3)',
run: function (env, test) {
test.assert(env.mod.getCount(), 3);
}
},
{
desc: '# getCount (3)',
run: function (env, test) {
test.assert(env.ak.getCount(), 3);
}
},
{
desc: '# forEachRecord',
run: function (env, test) {
var processed = 0;
env.mod.forEachRecord(function (obj) {
processed += 1;
}).finally(function (count) {
test.assert(count, processed);
});
}
},
{
desc: '# getIndexes',
run: function (env, test) {
test.assert(env.ak.getIdentifiers(), ['thingy3', 'thingy2', 'thingy1']);
{
desc: '# getIndexes',
run: function (env, test) {
test.assert(env.mod.getIdentifiers(), ['thingy3', 'thingy2', 'thingy1']);
}
},
{
desc: '# removeRecord 2',
run: function (env, test) {
test.assert(env.mod.removeRecord('thingy2'), true);
}
},
{
desc: '# getCount (2)',
run: function (env, test) {
test.assert(env.mod.getCount(), 2);
}
},
{
desc: '# addRecord (30000) + getCount',
run: function (env, test) {
var currentCount = env.mod.getCount();
var recordCount = 30000;
var start = new Date().getTime();
var _pre = new Date().getTime();
for (var i = 0, len = recordCount; i < len; i += 1) {
env.mod.addRecord({id: 'test' + i});
if ((i % 1000) === 0) {
var _post = new Date().getTime();
test.write('added ' + i + ' records in ' + (_post - _pre) + 'ms');
_pre = new Date().getTime();
}
}
},
var end = new Date().getTime();
test.write('finished adding ' + recordCount + ' records in ' + (end - start) + 'ms');
test.write('getCount: ' + env.mod.getCount());
test.assert(env.mod.getCount(), recordCount + currentCount);
}
},
{
desc: '# removeRecord 2',
run: function (env, test) {
test.assert(env.ak.removeRecord('thingy2'), true);
{
desc: '# removeRecord (20000)',
run: function (env, test) {
var currentCount = env.mod.getCount();
var recordCount = 20000;
var start = new Date().getTime();
var _pre = new Date().getTime();
for (var i = 0, len = recordCount; i < len; i += 1) {
env.mod.removeRecord('test' + (i + 10000));
if ((i % 1000) === 0) {
var _post = new Date().getTime();
test.write('removed ' + i + ' records in ' + (_post - _pre) + 'ms');
_pre = new Date().getTime();
}
}
},
var end = new Date().getTime();
test.write('finished removing ' + recordCount + ' records in ' + (end - start) + 'ms');
test.write('getCount: ' + env.mod.getCount());
test.assert(env.mod.getCount(), currentCount - recordCount);
}
},
{
desc: '# getCount (2)',
run: function (env, test) {
test.assert(env.ak.getCount(), 2);
{
desc: '# addRecord (10000) + getCount',
run: function (env, test) {
var currentCount = env.mod.getCount();
var recordCount = 10000;
var start = new Date().getTime();
var _pre = new Date().getTime();
for (var i = 0, len = recordCount; i < len; i += 1) {
var result = env.mod.addRecord({id: 'test' + i+9999999});
if (!result) {
test.fail('failed added record: ', {id: 'test' + i});
}
if ((i % 1000) === 0) {
var _post = new Date().getTime();
test.write('added ' + i + ' records in ' + (_post - _pre) + 'ms');
_pre = new Date().getTime();
}
}
},
]
});
var end = new Date().getTime();
test.write('finished adding ' + recordCount + ' records in ' + (end - start) + 'ms');
test.write('getCount: ' + env.mod.getCount());
test.assert(env.mod.getCount(), currentCount + recordCount);
}
},
return suites;
{
desc: '# forEachRecord',
run: function (env, test) {
var processed = 0;
env.mod.forEachRecord(function (obj) {
processed += 1;
}).finally(function (count) {
test.write('count: ' + count + ' processed: ' + processed);
test.assert(count, processed);
});
}
},
];
}
if (typeof define !== 'function') {
var define = require('amdefine')(module);
}
define(['require'], function (require) {
return [{
desc: "basic tests",
setup: function (env, test) {
var Mod = require('./../array-keys');
test.assertTypeAnd(Mod, 'function');
env.mod = new Mod();
test.assertType(env.mod, 'object');
},
tests: getTests(),
},
{
desc: "basic tests (browserify)",
setup: function (env, test) {
var Mod = require('./../browser/array-keys.js');
test.assertTypeAnd(Mod, 'function');
env.mod = new Mod();
test.assertType(env.mod, 'object');
},
tests: getTests(),
},
{
desc: "basic tests (browserify minified)",
setup: function (env, test) {
var Mod = require('./../browser/array-keys.min.js');
test.assertTypeAnd(Mod, 'function');
env.mod = new Mod();
test.assertType(env.mod, 'object');
},
tests: getTests(),
}];
});

Sorry, the diff of this file is not supported yet

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