{"_id":"gaia-hash","_rev":"234954","name":"gaia-hash","description":"Hash tables for javascript.","dist-tags":{"latest":"0.1.1"},"maintainers":[{"name":"jakeluer","email":""}],"time":{"modified":"2021-06-03T15:49:31.000Z","created":"2012-12-08T22:40:07.937Z","0.1.1":"2013-02-10T19:01:12.625Z","0.1.0":"2012-12-08T22:40:07.937Z"},"users":{},"author":{"name":"Jake Luer","email":"jake@qualiancy.com","url":"http://qualiancy.com"},"repository":{"type":"git","url":"git@github.com:qualiancy/gaia-hash.git"},"versions":{"0.1.1":{"name":"gaia-hash","version":"0.1.1","description":"Hash tables for javascript.","author":{"name":"Jake Luer","email":"jake@qualiancy.com","url":"http://qualiancy.com"},"license":"MIT","keywords":[],"repository":{"type":"git","url":"git@github.com:qualiancy/gaia-hash.git"},"engines":{"node":"*"},"main":"./index","scripts":{"test":"make test"},"dependencies":{"gaia-filter":"0.1.0","tea-extend":"0.2.0","tea-properties":"0.1.0"},"devDependencies":{"chai":"*","component":"*","mocha":"*","mocha-phantomjs":"*"},"readmeFilename":"README.md","_id":"gaia-hash@0.1.1","dist":{"shasum":"d969acbe16a2e5e4dc3d74ac28b24c87d36cf12d","size":7079,"noattachment":false,"key":"/gaia-hash/-/gaia-hash-0.1.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/gaia-hash/download/gaia-hash-0.1.1.tgz"},"_from":".","_npmVersion":"1.2.10","_npmUser":{"name":"jakeluer","email":"jake@alogicalparadox.com"},"maintainers":[{"name":"jakeluer","email":""}],"directories":{},"publish_time":1360522872625,"_cnpm_publish_time":1360522872625,"_hasShrinkwrap":false},"0.1.0":{"name":"gaia-hash","version":"0.1.0","description":"Hash tables for javascript.","author":{"name":"Jake Luer","email":"jake@qualiancy.com","url":"http://qualiancy.com"},"license":"MIT","keywords":[],"repository":{"type":"git","url":"git@github.com:qualiancy/gaia-hash.git"},"engines":{"node":"*"},"main":"./index","scripts":{"test":"make test"},"dependencies":{"gaia-filter":"0.1.0","tea-extend":"0.1.0","tea-properties":"0.1.0"},"devDependencies":{"chai":"*","component":"*","mocha":"*","mocha-phantomjs":"*"},"readmeFilename":"README.md","_id":"gaia-hash@0.1.0","dist":{"shasum":"5c37579ba392eaa1e7abfb09ed7106a43249fdc4","size":7034,"noattachment":false,"key":"/gaia-hash/-/gaia-hash-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/gaia-hash/download/gaia-hash-0.1.0.tgz"},"_npmVersion":"1.1.66","_npmUser":{"name":"jakeluer","email":"jake@alogicalparadox.com"},"maintainers":[{"name":"jakeluer","email":""}],"directories":{},"publish_time":1355006407937,"_cnpm_publish_time":1355006407937,"_hasShrinkwrap":false}},"readme":"# gaia-hash [![Build Status](https://secure.travis-ci.org/qualiancy/gaia-hash.png?branch=master)](https://travis-ci.org/qualiancy/gaia-hash)\n\n> Hash tables for javascript.\n\n## Installation\n\n### Node.js\n\n`gaia-hash` is available on [npm](http://npmjs.org).\n\n    $ npm install gaia-hash\n\n### Component\n\n`gaia-hash` is available as a [component](https://github.com/component/component).\n\n    $ component install qualiancy/gaia-hash\n\n## Usage\n\n### Hash (constructor)\n\n* **@param** _{Object}_ key/value pairs to insert\n* **@param** _{Object}_ options \n\nCreates a new hash given a set\nof values and options.\n\n```js\nvar hash = require('gaia-hash')\n  , h = hash();\n```\n\nThe `Hash` constructor is also available\nfor extending.\n\n```js\nvar Hash = require('gaia-hash').Hash\n  , inhertis = require('tea-inherits');\n\nfunction MyHash (data) {\n  Hash.call(this, data);\n}\n\ninherits(MyHash, Hash);\n```\n\nIn this scenarios, any method that returns a\nnew hash (such as `.clone()`) will return a\nnew instance of `MyHash`.\n\n\n### .length\n\n* **@return** _{Number}_  count of keys\n\nProperty that returns the the number of keys\nin the hash.\n\n\n### .keys\n\n* **@return** _{Array}_  keys\n\nProperty that returns an array of all of the\nkeys with defined values in the hash.\n\n\n### .values\n\n* **@return** _{Array}_  values\n\nProperty that return an array of all of the\ndefined values in the hash. Order will be maintained.\n`null` is considered a defined value, but `undefined` is not.\n\n\n### .set(key, value)\n\n* **@param** _{String}_ key \n* **@param** _{Object}_ value \n* **@return** _{this}_  chainable\n\nSets the value at a given key.\n\n```js\nh.set('hello', 'universe');\n```\n\n\n### .get(key)\n\n* **@param** _{String}_ key \n* **@return** _{Mixed}_  value\n\nGets the value of at a specific key.\n\n```js\nh.get('hello'); // 'universe'\n```\n\n\n### .has(key)\n\n* **@param** _{String}_ key \n* **@return** _{Boolean}_  existence\n\nChecks for existence of key in hash.\n\n```js\nif (h.has('hello')) {\n  // do something cool\n}\n```\n\n\n### .del(key, [silent])\n\n* **@param** _{String}_ key \n\n```js\nh.del('hello');\n```\n\nRemoves a key from the Hash by setting it's\nvalue to `undefined`. The key will no longer\nbe included in `h.keys` or `h.toArray()`. Will\nbe completely removed from internal storage\nupon the next invocation of `h.clean()`.\n\n\n### .clone()\n\n* **@return** _{Hash}_  cloned\n\nReturns a copy of the hash.\n\n```js\nvar h2 = h.clone();\n```\n\n\n### .at(index)\n\n* **@param** _{Number}_ index \n* **@return** _{Object}_  value at index\n\nReturns the data at a given index,\nas if the hash as an array. Respects\nthe last `.sort()` or the order the\nkeys were defined.\n\n```js\nvar first = h.at(0);\n```\n\n\n### .index(key)\n\n* **@param** _{String}_ key \n* **@return** _{Number}_  index\n\nReturns the index for a given\nkey, as if the hash was an array.\nRespects the last `.sort()` or the\norder the keys were defined. Returns\n`-1` if the key is not defined.\n\n```js\nvar pos = h.index('key');\n```\n\n\n### .toArray()\n\n* **@return** _{Array}_  \n\nReturns the hash as javascript array with\neach entry being an _{Object}_ of `key`/`value`\npairs.\n\n```js\nvar h = hash({ 'hello': 'world' })\n  , arr = h.toArray();\n\n// [ { key: 'hello', value: 'world' } ]\n```\n\n\n### .fromArray(arr)\n\n* **@return** _{this}_  for chaining\n\nSets values in this Hash from the result of\nof a `sol.toArray` call. Array must have entries in\nthe format `{ key: [key], value: [value] }`.\n\n```js\n// the long clone\nvar arr = h1.toArray()\n  , h2 = hash();\n\nh2.fromArray(arr);\n```\n\nAny existing values with the same key wil be\noverwritten. Any keys with the value of `undefined`\nwill be skipped.\n\n\n### .flush()\n\n* **@return** _{this}_  for chaining\n\nRemove all key/value pairs from a hash.\n\n```js\nh.flush();\n```\n\n\n### .clean()\n\n* **@return** _{this}_  for chaining\n\nHelper function to remove all keys that have an\n`undefined` value. Useful, as `del` leaves keys\nin memory.\n\n```js\nh.clean();\n```\n\n\n### .each(iterator, [context])\n\n* **@param** _{Function}_ iterator \n* **@param** _{Object}_ context to apply as `this` to iterator. Defaults to current hash.\n* **@return** _{this}_  for chaining\n\nApply a given iteration function to\neach value in the hash.\n\n```js\nh.each(function (value, key, index) {\n  console.log(index, key, value);\n});\n```\n\n\n### .map(iterator, [context])\n\n* **@param** _{Function}_ iterator \n* **@param** _{Object}_ content to apply as `this` to iterator. Defults to current hash.\n* **@return** _{Hash}_  modified new Hash\n\nMap a given function to every value\nin the Hash. Iterator must return new value.\nReturns a new Hash.\n\n```js\nh.set('hello', 'world');\n\nvar h2 = h.map(function (value, key, index) {\n  return value.toUpperCase();\n});\n\nconsole.log(h2.get('hello')); // \"WORLD\"\n```\n\n\n### .reduce(iterator, [context])\n\n* **@param** _{Function}_ iterator \n* **@param** _{Object}_ context to apply as `this` to the iterator. Defaults to current hash.\n* **@return** _{Mixed}_  result\n\n```js\nh.set('one', 1);\nh.set('two', 2);\n\nvar sum = h.reduce(function (value, key, index) {\n  return value;\n});\n\nconsole.log(sum); // 3\n```\n\n\n### .mapReduce(mapFn, reduceFn)\n\n* **@param** _{Function}_ map function\n* **@param** _{Function}_ reduce function\n* **@return** _{Hash}_  new Hash of results\n\nDivide and aggregate a hash based on arbitrary criteria.\n\nThe `mapFunction` will be applied to each key/value\npair in the original hash. A group key and the value\nto be added to that key's array may be emitted by\nby this iterator.\n\nThe `reduceFunction` will be applied to each group\nkey emitted by the `mapFunction`. This function should\nreturn the new value for that key to be used in the final\nHash.\n\nThis method will return a new Hash with unique keys emitted\nby the `mapFunction` with values returned by the `reduceFunction`.\n\nThe following example will return a hash with count of\nlast names that start with a specific letter.\n\n```js\nh.set('smith', { first: 'The', 'Doctor' });\nh.set('amy', { first: 'Amy', last: 'Williams' });\nh.set('rory', { first: 'Rory', last: 'Williams' });\n\nvar byLetter = h.mapReduce(\n    function map (key, value, emit) {\n      var first = value.last.charAt(0).toUpperCase();\n      emit(first, 1);\n    }\n  , function reduce (key, arr) {\n      var sum = 0;\n      for (var i = 0, i < arr.length; i++) sum += arr[i];\n      return sum;\n    }\n);\n\nbyLetter.get('D'); // 1\nbyLetter.get('W'); // 2\n```\n\n\n### .filter(iterator, [context])\n\n* **@param** _{Function}_ iterator \n* **@param** _{Object}_ context to apply as `this` to iterator. Defaults to current hash.\n* **@return** _{Hash}_  new Hash of results\n\nCreates a new hash with all key/values\nthat pass a given iterator test. Iterator\nshould return `true` or `false`.\n\n```js\nh.set('smith', { first: 'The', 'Doctor' });\nh.set('amy', { first: 'Amy', last: 'Williams' });\nh.set('rory', { first: 'Rory', last: 'Williams' });\n\nvar williams = h.filter(function (value, key) {\n  return value.last.toUpperCase() === 'WILLIAMS';\n});\n\nwilliams.length; // 2\n```\n\n\n### .find(query)\n\n* **@param** _{Object}_ query \n* **@return** _{Hash}_  new Hash of results\n\nCreates a new hash with all elements that pass\na given query. Query language is provided by\n[gaia-filter](https://github.com/qualiancy/gaia-filter).\n\n```js\nh.set('smith', { first: 'The', 'Doctor' });\nh.set('amy', { first: 'Amy', last: 'Williams' });\nh.set('rory', { first: 'Rory', last: 'Williams' });\n\nvar williams = h.find({ 'name': { $eq: 'Williams' }});\n\nwilliams.length; // 2\n```\n\nIf the hash is a storage mechanism for multiple _{Object}_\ninstances instances (such as database models}, the `findRoot`\noption may be defined for the Hash. This option will be\ntransferred to all new result Hashes.\n\n```js\nfunction Model (first, last) {\n  this.attributes = { first: first, last: last };\n}\n\nvar h = hash(null, { findRoot: 'attributes' });\n\nh.set('smith', new Model('Amy', 'Williams'));\n\n// now functions same as above\n```\n\n\n### .sort(iterator)\n\n* **@param** _{String|Function}_ comparator \n* **@return** _{this}_  for chaining\n\nCreates a new hash sorted with an iterator. Can use one\nof the provided iterators or provide a custom function.\n\nComparators:\n- `'kasc'`: key alphabetical (default)\n- `'kdesc'`: key alphabetical descending\n- `'asc'`: value ascending, only if value is not object\n- `'desc'`: value descending, only if value is not object\n- _{Function}_: provide custom iterator\n\nCustom Comparator:\n\nThe parameters for each to compare are an object\nwith `key` and `value` properties. See the `.toArray()`\nmethod for more information.\n\n```js\nhash.sort(function (a, b) {\n  var akey = a.key\n    , avalue = a.value;\n  // etc..\n  return 1 || -1 || 0; // choose\n});\n```\n\n\n### .sortBy (path, comparator)\n\n* **@param** _{String}_ path \n* **@param** _{String|Function}_ comparator \n\nHelper for sorting hash of objects by a path.\n\n```js\nhash.sortBy('stats.age', 'asc');\n```\n\nComparators:\n- `'asc'`: value ascending\n- `'desc'`: value descending\n- _{Function}_: custom function to receive the value at path\n\n\n\n## License\n\n(The MIT License)\n\nCopyright (c) 2012 Jake Luer <jake@qualiancy.com> (http://qualiancy.com)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n","_attachments":{},"readmeFilename":"README.md","license":"MIT"}