Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Hash tables for javascript.
gaia-hash
is available on npm.
$ npm install gaia-hash
gaia-hash
is available as a component.
$ component install qualiancy/gaia-hash
Creates a new hash given a set of values and options.
var hash = require('gaia-hash')
, h = hash();
The Hash
constructor is also available
for extending.
var Hash = require('gaia-hash').Hash
, inhertis = require('tea-inherits');
function MyHash (data) {
Hash.call(this, data);
}
inherits(MyHash, Hash);
In this scenarios, any method that returns a
new hash (such as .clone()
) will return a
new instance of MyHash
.
Property that returns the the number of keys in the hash.
Property that returns an array of all of the keys with defined values in the hash.
Property that return an array of all of the
defined values in the hash. Order will be maintained.
null
is considered a defined value, but undefined
is not.
Sets the value at a given key.
h.set('hello', 'universe');
Gets the value of at a specific key.
h.get('hello'); // 'universe'
Checks for existence of key in hash.
if (h.has('hello')) {
// do something cool
}
h.del('hello');
Removes a key from the Hash by setting it's
value to undefined
. The key will no longer
be included in h.keys
or h.toArray()
. Will
be completely removed from internal storage
upon the next invocation of h.clean()
.
Returns a copy of the hash.
var h2 = h.clone();
Returns the data at a given index,
as if the hash as an array. Respects
the last .sort()
or the order the
keys were defined.
var first = h.at(0);
Returns the index for a given
key, as if the hash was an array.
Respects the last .sort()
or the
order the keys were defined. Returns
-1
if the key is not defined.
var pos = h.index('key');
Returns the hash as javascript array with
each entry being an {Object} of key
/value
pairs.
var h = hash({ 'hello': 'world' })
, arr = h.toArray();
// [ { key: 'hello', value: 'world' } ]
Sets values in this Hash from the result of
of a sol.toArray
call. Array must have entries in
the format { key: [key], value: [value] }
.
// the long clone
var arr = h1.toArray()
, h2 = hash();
h2.fromArray(arr);
Any existing values with the same key wil be
overwritten. Any keys with the value of undefined
will be skipped.
Remove all key/value pairs from a hash.
h.flush();
Helper function to remove all keys that have an
undefined
value. Useful, as del
leaves keys
in memory.
h.clean();
this
to iterator. Defaults to current hash.Apply a given iteration function to each value in the hash.
h.each(function (value, key, index) {
console.log(index, key, value);
});
this
to iterator. Defults to current hash.Map a given function to every value in the Hash. Iterator must return new value. Returns a new Hash.
h.set('hello', 'world');
var h2 = h.map(function (value, key, index) {
return value.toUpperCase();
});
console.log(h2.get('hello')); // "WORLD"
this
to the iterator. Defaults to current hash.h.set('one', 1);
h.set('two', 2);
var sum = h.reduce(function (value, key, index) {
return value;
});
console.log(sum); // 3
Divide and aggregate a hash based on arbitrary criteria.
The mapFunction
will be applied to each key/value
pair in the original hash. A group key and the value
to be added to that key's array may be emitted by
by this iterator.
The reduceFunction
will be applied to each group
key emitted by the mapFunction
. This function should
return the new value for that key to be used in the final
Hash.
This method will return a new Hash with unique keys emitted
by the mapFunction
with values returned by the reduceFunction
.
The following example will return a hash with count of last names that start with a specific letter.
h.set('smith', { first: 'The', 'Doctor' });
h.set('amy', { first: 'Amy', last: 'Williams' });
h.set('rory', { first: 'Rory', last: 'Williams' });
var byLetter = h.mapReduce(
function map (key, value, emit) {
var first = value.last.charAt(0).toUpperCase();
emit(first, 1);
}
, function reduce (key, arr) {
var sum = 0;
for (var i = 0, i < arr.length; i++) sum += arr[i];
return sum;
}
);
byLetter.get('D'); // 1
byLetter.get('W'); // 2
this
to iterator. Defaults to current hash.Creates a new hash with all key/values
that pass a given iterator test. Iterator
should return true
or false
.
h.set('smith', { first: 'The', 'Doctor' });
h.set('amy', { first: 'Amy', last: 'Williams' });
h.set('rory', { first: 'Rory', last: 'Williams' });
var williams = h.filter(function (value, key) {
return value.last.toUpperCase() === 'WILLIAMS';
});
williams.length; // 2
Creates a new hash with all elements that pass a given query. Query language is provided by gaia-filter.
h.set('smith', { first: 'The', 'Doctor' });
h.set('amy', { first: 'Amy', last: 'Williams' });
h.set('rory', { first: 'Rory', last: 'Williams' });
var williams = h.find({ 'name': { $eq: 'Williams' }});
williams.length; // 2
If the hash is a storage mechanism for multiple {Object}
instances instances (such as database models}, the findRoot
option may be defined for the Hash. This option will be
transferred to all new result Hashes.
function Model (first, last) {
this.attributes = { first: first, last: last };
}
var h = hash(null, { findRoot: 'attributes' });
h.set('smith', new Model('Amy', 'Williams'));
// now functions same as above
Creates a new hash sorted with an iterator. Can use one of the provided iterators or provide a custom function.
Comparators:
'kasc'
: key alphabetical (default)'kdesc'
: key alphabetical descending'asc'
: value ascending, only if value is not object'desc'
: value descending, only if value is not objectCustom Comparator:
The parameters for each to compare are an object
with key
and value
properties. See the .toArray()
method for more information.
hash.sort(function (a, b) {
var akey = a.key
, avalue = a.value;
// etc..
return 1 || -1 || 0; // choose
});
Helper for sorting hash of objects by a path.
hash.sortBy('stats.age', 'asc');
Comparators:
'asc'
: value ascending'desc'
: value descending(The MIT License)
Copyright (c) 2012 Jake Luer jake@qualiancy.com (http://qualiancy.com)
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.
FAQs
Hash tables for javascript.
We found that gaia-hash demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.