{"_id":"gaia-filter","_rev":"240923","name":"gaia-filter","description":"Array filtering inspired by MongoDB's query language.","dist-tags":{"latest":"0.1.0"},"maintainers":[{"name":"jakeluer","email":""}],"time":{"modified":"2021-06-03T15:56:36.000Z","created":"2012-11-18T22:42:08.116Z","0.1.0":"2012-11-18T22:42:08.116Z"},"users":{},"author":{"name":"Jake Luer","email":"jake@qualiancy.com","url":"http://qualiancy.com"},"repository":{"type":"git","url":"git@github.com:qualiancy/gaia-filter.git"},"versions":{"0.1.0":{"name":"gaia-filter","version":"0.1.0","description":"Array filtering inspired by MongoDB's query language.","author":{"name":"Jake Luer","email":"jake@qualiancy.com","url":"http://qualiancy.com"},"license":"MIT","keywords":[],"repository":{"type":"git","url":"git@github.com:qualiancy/gaia-filter.git"},"engines":{"node":"*"},"main":"./index","scripts":{"test":"make test"},"dependencies":{"tea-properties":"0.1.0"},"devDependencies":{"chai":"*","component":"*","mocha":"*","mocha-phantomjs":"*"},"_id":"gaia-filter@0.1.0","dist":{"shasum":"e029ab04aafaa0137885b8e161e78a48f4eeaf6c","size":5185,"noattachment":false,"key":"/gaia-filter/-/gaia-filter-0.1.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/gaia-filter/download/gaia-filter-0.1.0.tgz"},"_npmVersion":"1.1.63","_npmUser":{"name":"jakeluer","email":"jake@alogicalparadox.com"},"maintainers":[{"name":"jakeluer","email":""}],"directories":{},"publish_time":1353278528116,"_cnpm_publish_time":1353278528116,"_hasShrinkwrap":false}},"readme":"# gaia-filter [![Build Status](https://secure.travis-ci.org/qualiancy/gaia-filter.png?branch=master)](https://travis-ci.org/qualiancy/gaia-filter)\n\n> Array filtering inspired by MongoDB's query language.\n\n## Installation\n\n### Node.js\n\n`gaia-filter` is available on [npm](http://npmjs.org).\n\n    $ npm install gaia-filter\n\n### Component\n\n`gaia-filter` is available as a [component](https://github.com/component/component).\n\n    $ component install qualiancy/gaia-filter\n\n## Usage\n\n### filter (query)\n\n* **@param** _{Object}_ query \n* **@return** _{filter}_  \n\nThe primary export is a function that will memoize a query\nso that it can be used to filter many arrays. Furthermore,\nthere are several methods to adjust the output of the result\nset.\n\nThe filter query language is similiar MongoDB's query language\nin that it allows for nested statements, deep object property\ndetection, and conditionals such as `$and`, `$or`, and `$nor`.\n\n```js\nvar filter = require('gaia-filter');\n\nvar dataComplex = [\n    { a: { b: 100 }\n    , c: 'testC'\n    , d:\n      [ { e: 'world' } ]\n   }\n , { a: { b: 50 }\n   , c: 'testC'\n   , d:\n     [ { e: 'universe' }\n     , { e: 'galaxy' } ]\n   }\n];\n\nvar query1 = filter({ 'a.b': { $and: [ { $gt: 75 }, { $lt: 125 } ] }});\n  , query2 = filter({ 'a.b': { $gt: 25, $lt: 75 }, 'd[0].e': { $eq: 'universe' } });\n\nvar res1 = query1.subset(dataComplex)  // result array will have the first item\n  , res2 = query1.subset(dataComplex); // result array will have the second item\n```\n\nFilter allows for some flexibility when composing queries by making assumptions\nbased on sane defaults.\n\nWhen a comparator is omitted when comparing a value, it assumes `$eq`.\n\n```js\n// the following are the same.\nfilter({ hello: { $eq: 'universe' } });\nfilter({ hello: 'universe' });\n```\n\nWhen multiple statements are listed, it assumes `$and`.\n\n```js\n// the following are the same.\nfilter({ $and: [ { hello: { $gt: 42 } }, { hello: { $lt: 84 } } ] });\nfilter({ hello: { $and: [ { $gt: 42 }, { $lt: 84 } ] } });\nfilter({ hello: { $gt: 42, $lt: 84 } });\n```\n\n\n### .test (data)\n\n* **@param** _{Mixed}_ data \n* **@return** _{Boolean}_  result\n\nTest a single data point against the query. Will return a\nboolean indicating whether data passes query criteria.\n\n```js\nvar query = filter({ hello: { $eq: 'universe' }})\n  , pass = query.test({ hello: 'universe' });\n\npass.should.be.true;\n```\n\n\n### .subset (data)\n\n* **@param** _{Array}_ data \n* **@return** _{Array}_  result\n\nTest an array of data points against the query. Will return\nan array of all data points that pass the query criteria.\n\n```js\nvar query = { $or : [ { 'hello': true }, { 'universe': true } ] }\n  , q = filter(query)\n  , result = q.subset([\n      { hello: true }\n    , { universe: false }\n    , { hello: false, universe: true }\n    , { hello: false, universe: false }\n  ]);\n\nresult.should\n  .be.an('array')\n  .and.have.length(2)\n  .and.deep.equal([\n      { hello: true }\n    , { hello: false, universe: true }\n  ]);\n```\n\n\n### .pass (data)\n\n* **@param** _{Array}_ data \n* **@return** _{Array}_  result\n\nTest an array of data points against the query. Will return\nan array of boolean in the order of the original array that\nindicate if the data at that index passed the query criteria.\n\n```js\nvar query = { $or : [ { 'hello': true }, { 'universe': true } ] }\n  , q = filter(query)\n  , result = q.pass([\n        { hello: true }\n      , { universe: false }\n      , { hello: false, universe: true }\n      , { hello: false, universe: false }\n    ]);\n\nresult.should\n  .be.an('array')\n  .and.have.length(4)\n  .and.deep.equal([\n      true\n    , false\n    , true\n    , false\n  ]);\n```\n\n\n### .index (data)\n\n* **@param** _{Array}_ data \n* **@return** _{Array}_  result\n\nTest an array of data points against the query. Will return\nan array of numbers indicating the indexes of the original\narray that passed the query criteria.\n\n```js\nvar query = { $or : [ { 'hello': true }, { 'universe': true } ] }\n  , q = filter(query)\n  , result = q.index([\n        { hello: true }\n      , { universe: false }\n      , { hello: false, universe: true }\n      , { hello: false, universe: false }\n    ]);\n\nresult.should\n  .be.an('array')\n  .and.have.length(2)\n  .and.deep.equal([ 0, 2 ]);\n```\n\n\n### .comparators\n\nThe following are the comparators implemented\nfor use as part of a query. They can also be used\nfor simple comparation, such as `filter.comparators.$eq(2,2)`,\nwhich would return `true`.\n\n##### $gt (a, b)\n\n* **@param** _{Number}_ a \n* **@param** _{Number}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` is greater than `b`.\n\n\n##### $gte (a, b)\n\n* **@param** _{Number}_ a \n* **@param** _{Number}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` is greater than or equal to `b`.\n\n\n##### $lt (a, b)\n\n* **@param** _{Number}_ a \n* **@param** _{Number}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` is less than `b`.\n\n\n##### $lte (a, b)\n\n* **@param** _{Number}_ a \n* **@param** _{Number}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` is less than or equal to `b`.\n\n\n##### $all (a, b)\n\n* **@param** _{Array}_ a \n* **@param** _{Array}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` contains at least all items in `b`.\n\n\n##### $exists (a, b)\n\n* **@param** _{Mixed}_ a \n* **@param** _{Boolean}_ b \n* **@return** _{Boolean}_  \n\nAssert truthiness of `a` equals `b`.\n\n\n##### $mod (a, b)\n\n* **@param** _{Number}_ a \n* **@param** _{Array}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` mod (`%`) `b[0]` equals `b[1]`.\n\n\n##### $eq (a, b)\n\n* **@param** _{Mixed}_ a \n* **@param** _{Mixed}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` equals (`===`) `b`.\n\n\n##### $eq (a, b)\n\n* **@param** _{Mixed}_ a \n* **@param** _{Mixed}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` does not equal (`!==`) `b`.\n\n\n##### $in (a, b)\n\n* **@param** _{Mixed}_ a \n* **@param** _{Array}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` is in `b` using `indexOf`.\n\n\n##### $nin (a, b)\n\n* **@param** _{Mixed}_ a \n* **@param** _{Array}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` is not in `b` using `indexOf`.\n\n\n##### $size (a, b)\n\n* **@param** _{Mixed}_ a \n* **@param** _{Number}_ b \n* **@return** _{Boolean}_  \n\nAssert `a` has length of `b`. Returns\n`false` if `a` does not have a length.\nproperty.\n\n\n##### $or (a)\n\n* **@param** _{Array}_ a \n* **@return** _{Boolean}_  \n\nAssert `a` has at least one truthy value.\n\n\n##### $nor (a)\n\n* **@param** _{Array}_ a \n* **@return** _{Boolean}_  \n\nAssert `a` has zero truthy values.\n\n\n##### $and (a)\n\n* **@param** _{Array}_ a \n* **@return** _{Boolean}_  \n\nAssert `a` has all truthy values.\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":{},"license":"MIT"}