Gathering detailed insights and metrics for hasharray
Gathering detailed insights and metrics for hasharray
npm install hasharray
Typescript
Module System
Node Version
NPM Version
96.7
Supply Chain
100
Quality
75.9
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
5,689,049
Last Day
4,024
Last Week
22,232
Last Month
181,933
Last Year
1,486,981
17 Stars
44 Commits
3 Forks
3 Watching
1 Branches
3 Contributors
Minified
Minified + Gzipped
Latest Version
1.1.2
Package Id
hasharray@1.1.2
Unpacked Size
62.45 kB
Size
12.63 kB
File Count
7
NPM Version
6.14.10
Node Version
12.20.1
Cumulative downloads
Total Downloads
Last day
45%
4,024
Compared to previous day
Last week
-36.1%
22,232
Compared to previous week
Last month
3.7%
181,933
Compared to previous month
Last year
-23.4%
1,486,981
Compared to previous year
1
HashArray is a data structure that combines the best feature of a hash (O(1) retrieval) and an array (length and ordering). Think of it as a super-lightweight, extensible, self-indexing set database in memory.
npm install hasharray
So as not to scare the faint of heart with all the technical goodness contained therein:
var HashArray = require('hasharray'),
ha = new HashArray('id');
ha.add({id: 'someId0', name: 'Josh'},
{id: 'someId1', name: 'Joseph'},
{id: 'someId2', name: 'Kuba'},
{id: 'someId3', name: 'Ty'});
console.log(ha.get('someId0').name); // 'Kuba'
HashArray(keyfields, callback, options)
keyfields
var HashArray = require('HashArray');
new HashArray('firstname'); // one key, depth of 1 (e.g. `item.firstname`)
new HashArray(['firstname']); // same as above
// one key, depth of 2 (e.g. `item.first.name`)
new HashArray([['first', 'name']]);
// two keys, depth of 1 (e.g. `item.firstname` AND `item.lastname`)
new HashArray(['firstname', 'lastname']);
// multiple keys, depth of 2 (e.g. `item.name.first` AND `item.name.last`)
new HashArray([['name', 'first'], ['name', 'last']]);
callback
A callback function can be specified to monitor changes to the HashArray as they occur:
var ha = new HashArray('someKey', function(type, whatChanged) {
// type will be 'add', 'addMap', 'remove', 'removeByKey', or 'construct'
// whatChanged will be the items that were changed
});
options
{
ignoreDuplicates: false; // When true, any attempt to add items that collide
// with any items in the HashArray will be ignored.
// default is false.
}
add(...items)
: insert all arguments.addAll(Array of items)
: insert all items in the passed in Array.addMap(key, item)
: adds a single mapping from a key to a item.addOne(item)
: adds a single item, skipping dispatch of event.get(key)
: if a single item exists for key
, returns that item. If no item exists, returns undefined
. If multiple items exist for the key, returns an Array
.getAll(keys)
: unions all items for all provided keys and returns an Array. Ensures no duplicates even if keys
independently return sets that intersect.getAsArray(key)
: like get()
except if no item exists, returns an empty Array
.sample(count, keys)
: samples all items in the HashArray, returning a random Array
of size count
. If count
is larger than the HashArray
, returns all items in the the HashArray
remove(...items)
: removes all items in arguments.removeByKey(...keys)
: removes all items that match the keys
provided.removeAll()
: clears out all items in the HashArray
intersection(HashArray)
: returns a cloned HashArray
whose items are the intersection between this
and the passed in HashArray
(this
^ argument
).complement(HashArray)
: returns a cloned HashArray
whose items are the complement between this
and the passed in HashArray
(this
\ argument
).has(key)
: returns true
if any items exist for the provided key.hasMultiple(key)
: returns true
if multiple items exist for the provided key (e.g. get(key)
would return an Array
)collides(item)
: returns true
if the argument would collide with any other item in this HashArray
for any key in the HashArray
.forEach(keys, callback)
: iterates through a union of all items that match the provided keys argument and calls the callback passing in each item as an argument.forEachDeep(keys, key, callback)
: iterates through a union of all items that match the provided keys argument and passes the value (at the provided key argument) as an argument to the callback.sum(keys, key, weightKey)
: sums all values for a union of all objects found for the keys
argument provided at the key
you provide. Weights the summation by item[weightKey]
or by 1.0
if no weightKey
is provided.average(keys, key, weightKey)
: similar to sum()
except returns the average.filter(keys, callbackOrKey)
: returns a new HashArray
that is a clone of the current one but filtered by the provided keys
.objectAt(item, key)
: internally used to find a value on item
at key
. For example, objectAt(obj, 'firstname')
would return obj['firstname']
. objectAt(obj, ['first', 'name'])
would return obj['first']['name']
. Returns undefined if the key does not map properly to the provided object.clone(callback, ignoreItems)
: shallow clones the HashArray
. If ignoreItems
is true, does not clone the items just the settings.My goal with this data structure was to attempt to get the ordered features of an Array while keeping lookup O(1) for any arbitrary keys. The cost is a small loss of memory.
In addition HashArray works with deep keys. Consider the following array of customer objects:
var customers = [{
id: 1337,
name: {
first: 'Bob',
last: 'Winkle',
},
dob: new Date(1985, 1, 4),
address: {
city: 'Chicago',
zip: 60616
}
}...]
If we had multiple people who lived in the zip code 60616
, ideally we would want to index the data by zip code so that if we had to rapidly retrieve all those people we could do so.
With HashArray, we could index the above data for O(1) retrieval by id
, ['name', 'first']
, and ['name', 'last]
like so:
var HashArray = require('hasharray'),
ha = new HashArray(['id', ['name', 'first'], ['name', 'last']]);
ha.addAll(customers);
// At this point we have indexed everything by ['name', 'first'] so there is already an array built internal to `ha` that
// contains all the 'Bob' customers. So this operation is O(1).
var bobs = ha.get('Bob');
Note: the order of the bobs
array above will be the order in which they were inserted.
Normally when you use a standard JavaScript Object to map keys to values, the only way to retrieve the count of objects is to loop over all the keys which is O(n). However, with HashArray if you want to determine the length of all customers in O(1), it is as simple as:
ha.all.length; // in addition, ha.all is an ordered array of all customers in the order in which they were added!
At this time, I am also working on adding functions for statistical analysis, like sum(...)
. See the tests for more information. I'll be adding more to this documentation as I go.
var HashArray = require ('hasharray');
// Create new hasharray with two key mappings.
var ha = new HashArray(['name', 'zip']);
// Add 2 objects to the hash.
var item1 = {name: 'Josh', zip: '54321'};
var item2 = {name: 'Josh', zip: '12345'};
ha.add(item1, item2);
if (ha.has('Josh'))
console.log(ha.get('Josh')); // Will output two objects to the console
// Display the number of unique objects. In this case, 2.
console.log(ha.all.length);
// Remove an element by one of the keys
ha.removeByKey('54321'); // This removes item1
// Remove item2 directly
ha.remove(item2);
var HashArray = require ('hasharray');
var ha = new HashArray([
['name', 'last'], // Internally maps obj.name.last -> obj
['name', 'first'], // Internally maps obj.name.first -> obj
'zip'
]);
ha.add({
name: {
first: 'Josh',
last: 'Jung'
},
zip: 60616
});
console.log(ha.get(60616) === ha.get('Josh') == ha.get('Jung')); // true
var ha = new HashArray(['firstName', 'lastName']);
var person1 = {firstName: 'Bill', lastName: 'William'},
person2 = {firstName: 'Bob', lastName: 'William'};
ha.add(person1, person2);
console.log(ha.getAsArray('William')); // [person1, person2]
var ha = new HashArray(['firstName', 'lastName']);
var person1 = {firstName: 'Victor', lastName: 'Victor'},
person2 = {firstName: 'Victor', lastName: 'Manning'},
person3 = {firstName: 'Manning', lastName: 'Victor'};
person4 = {firstName: 'John', lastName: 'Smith'};
ha.add(person1, person2, person3, person4);
console.log(ha.getAll(['Victor', 'Smith'])); // [person1, person2, person3, person4]
console.log(ha.getAll(['John', 'Smith'])); // [person4]
If two items contain the same key, they are appended to an array at that key location.
var HashArray = require ('hasharray');
var ha = new HashArray([
['name', 'last'],
['name', 'first']
]);
ha.add({
name: {
first: 'Josh',
last: 'Jung'
}
},
{
name: {
first: 'Josh',
last: 'Mills'
}
},
{
name: {
first: 'Josh',
last: 'Willis'
}
});
console.log(ha.get('Josh').length); // Will be 3
console.log(ha.get('Willis')); // Will be {name: {first: 'Josh', last: 'Willis'} }
If you need to check if an item already exists for a given key, simply use has(...)
:
ha.has('someKeyValue');
// Here we index by item.type and item.data.speed
var ha = new HashArray(['type', ['data', 'speed']]);
var a = {type: 'airplane', data: {speed: 100, weight: 10000}},
b = {type: 'airplane', data: {speed: 100, weight: 20000}},
c = {type: 'airplane', data: {speed: 25, weight: 50000}};
d = {type: 'boat', data: {speed: 10, weight: 100000}};
e = {type: 'boat', data: {speed: 5, weight: 200000}};
ha.add(a, b, c, d, e);
// Loop through just airplanes
ha.forEach('airplane', function (airplane) {console.log(airplane);});
// Loop through airplanes AND boats
ha.forEach(['airplane', 'boat'], function (airplane) {console.log(airplane);});
// Loop through all items that have a speed of 100
ha.forEach(100, function (airplane) {console.log(airplane);});
forEachDeep()
differs from forEach()
in that it passes a value by key you specify to the callback
:
// Here we index by item.type and item.data.speed
var ha = new HashArray(['type', ['data', 'speed']]);
var a = {type: 'airplane', data: {speed: 100, weight: 10000}},
b = {type: 'airplane', data: {speed: 100, weight: 20000}},
c = {type: 'airplane', data: {speed: 25, weight: 50000}};
d = {type: 'boat', data: {speed: 10, weight: 100000}};
e = {type: 'boat', data: {speed: 5, weight: 200000}};
ha.add(a, b, c, d, e);
// Loop through all items that have a speed of 100 and only pass the speed to the callback
ha.forEachDeep(100, ['data', 'speed'], function (speed) {
console.log('Speed is: ' + speed);
});
Cloning makes a new HashArray clone of the original, ensuring that no Array objects are shared.
Keep in mind that cloning does deep clone objects in the collection. Therefore if you clone an object with three Object items, the clonee will be a new HashArray but will contain references to the original objects.
var HashArray = require ('hasharray');
...
var ha = new HashArray(['someKey']);
...
var clonee = ha.clone();
HashArray uses jclass, which is an implementation of John Resig's simple inheritance model.
You can easily extend HashArray:
var MyCustomHashArray = HashArray._extend({
...
init: function init(keyFields)
{
console.log('My custom hash array!');
init._super(keyFields);
}
...
});
var myCustomHashArray = new MyCustomHashArray();
See the jclass
documentation for more information.
>mocha
START
․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․
76 passing (25ms)
The MIT License (MIT)
Copyright (c) 2014 Joshua Jung
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/28 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-06
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More