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

filter-where

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-where - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

156

lib/filter-where.js

@@ -5,84 +5,84 @@ 'use strict'

/**
* Filter an array using any combination of scalars, object queries, functions or regular expressions.
*
* @module filter-where
*/
module.exports = whereReducer
module.exports = where
/**
Deep query an array.
@param {any | any[]} - one or more queries
@returns {function}
@alias module:filter-where
@example
Say you have a recordset:
```js
> data = [
{ name: 'Dana', age: 30 },
{ name: 'Yana', age: 20 },
{ name: 'Zhana', age: 10 }
]
```
You can return records with properties matching an exact value:
```js
> a.where(data, { age: 10 })
[ { name: 'Zhana', age: 10 } ]
```
or where NOT the value (prefix the property name with `!`)
```js
> a.where(data, { '!age': 10 })
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
```
match using a function:
```js
> function over10(age){ return age > 10; }
> a.where(data, { age: over10 })
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
```
match using a regular expression
```js
> a.where(data, { name: /ana/ })
[ { name: 'Dana', age: 30 },
{ name: 'Yana', age: 20 },
{ name: 'Zhana', age: 10 } ]
```
You can query to any arbitrary depth. So with deeper data, like this:
```js
> deepData = [
{ name: 'Dana', favourite: { colour: 'light red' } },
{ name: 'Yana', favourite: { colour: 'dark red' } },
{ name: 'Zhana', favourite: { colour: [ 'white', 'red' ] } }
]
```
get records with `favourite.colour` values matching `/red/`
```js
> a.where(deepData, { favourite: { colour: /red/ } })
[ { name: 'Dana', favourite: { colour: 'light red' } },
{ name: 'Yana', favourite: { colour: 'dark red' } } ]
```
if the value you're looking for _maybe_ part of an array, prefix the property name with `+`. Now Zhana is included:
```js
> a.where(deepData, { favourite: { '+colour': /red/ } })
[ { name: 'Dana', favourite: { colour: 'light red' } },
{ name: 'Yana', favourite: { colour: 'dark red' } },
{ name: 'Zhana', favourite: { colour: [ 'white', 'red' ] } } ]
```
you can combine any of the above by supplying an array of queries. Records will be returned if _any_ of the queries match:
```js
> var nameBeginsWithY = { name: /^Y/ }
> var faveColourIncludesWhite = { favourite: { '+colour': 'white' } }
> a.where(deepData, [ nameBeginsWithY, faveColourIncludesWhite ])
[ { name: 'Yana', favourite: { colour: 'dark red' } },
{ name: 'Zhana', favourite: { colour: [ 'white', 'red' ] } } ]
```
*/
function whereReducer (query) {
* @param {any | any[]} - one or more queries
* @returns {function}
* @alias module:filter-where
* @example
* Say you have a recordset:
* ```js
* > data = [
* { name: 'Dana', age: 30 },
* { name: 'Yana', age: 20 },
* { name: 'Zhana', age: 10 }
* ]
* ```
*
* You can return records with properties matching an exact value:
* ```js
* > data.filter(where({ age: 10 }))
* [ { name: 'Zhana', age: 10 } ]
* ```
*
* or where NOT the value (prefix the property name with `!`)
* ```js
* > data.filter(where({ '!age': 10 }))
* [ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
* ```
*
* match using a function:
* ```js
* > function over10(age){ return age > 10; }
* > data.filter(where({ age: over10 }))
* [ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
* ```
*
* match using a regular expression
* ```js
* > data.filter(where({ name: /ana/ }))
* [ { name: 'Dana', age: 30 },
* { name: 'Yana', age: 20 },
* { name: 'Zhana', age: 10 } ]
* ```
*
* You can query to any arbitrary depth. So with deeper data, like this:
* ```js
* > deepData = [
* { name: 'Dana', favourite: { colour: 'light red' } },
* { name: 'Yana', favourite: { colour: 'dark red' } },
* { name: 'Zhana', favourite: { colour: [ 'white', 'red' ] } }
* ]
* ```
*
* get records with `favourite.colour` values matching `/red/`
* ```js
* > deepData.filter(where({ favourite: { colour: /red/ } }))
* [ { name: 'Dana', favourite: { colour: 'light red' } },
* { name: 'Yana', favourite: { colour: 'dark red' } } ]
* ```
*
* if the value you're looking for _maybe_ part of an array, prefix the property name with `+`. Now Zhana is included:
* ```js
* > deepData.filter(where({ favourite: { '+colour': /red/ } }))
* [ { name: 'Dana', favourite: { colour: 'light red' } },
* { name: 'Yana', favourite: { colour: 'dark red' } },
* { name: 'Zhana', favourite: { colour: [ 'white', 'red' ] } } ]
* ```
*
* you can combine any of the above by supplying an array of queries. Records will be returned if _any_ of the queries match:
* ```js
* > var nameBeginsWithY = { name: /^Y/ }
* > var faveColourIncludesWhite = { favourite: { '+colour': 'white' } }
*
* > deepData.filter(where([ nameBeginsWithY, faveColourIncludesWhite ]))
* [ { name: 'Yana', favourite: { colour: 'dark red' } },
* { name: 'Zhana', favourite: { colour: [ 'white', 'red' ] } } ]
* ```
*/
function where (query) {
return function (element) {

@@ -89,0 +89,0 @@ return testValue(element, query)

{
"name": "filter-where",
"author": "Lloyd Brookes <75pound@gmail.com>",
"version": "1.0.0",
"description": "filter-where",
"version": "1.0.1",
"description": "Filter an array using any combination of scalars, object queries, functions or regular expressions.",
"repository": "https://github.com/75lb/filter-where.git",

@@ -7,0 +7,0 @@ "license": "MIT",

@@ -9,8 +9,7 @@ [![view on npm](http://img.shields.io/npm/v/filter-where.svg)](https://www.npmjs.org/package/filter-where)

## filter-where
Filter an array using any combination of scalars, object queries, functions or regular expressions.
<a name="exp_module_filter-where--whereReducer"></a>
### whereReducer(query) ⇒ `function` ⏏
Deep query an array.
<a name="exp_module_filter-where--where"></a>
### where(query) ⇒ `function` ⏏
**Kind**: Exported function

@@ -35,3 +34,3 @@

```js
> a.where(data, { age: 10 })
> data.filter(where({ age: 10 }))
[ { name: 'Zhana', age: 10 } ]

@@ -42,3 +41,3 @@ ```

```js
> a.where(data, { '!age': 10 })
> data.filter(where({ '!age': 10 }))
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]

@@ -50,3 +49,3 @@ ```

> function over10(age){ return age > 10; }
> a.where(data, { age: over10 })
> data.filter(where({ age: over10 }))
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]

@@ -57,3 +56,3 @@ ```

```js
> a.where(data, { name: /ana/ })
> data.filter(where({ name: /ana/ }))
[ { name: 'Dana', age: 30 },

@@ -75,3 +74,3 @@ { name: 'Yana', age: 20 },

```js
> a.where(deepData, { favourite: { colour: /red/ } })
> deepData.filter(where({ favourite: { colour: /red/ } }))
[ { name: 'Dana', favourite: { colour: 'light red' } },

@@ -83,3 +82,3 @@ { name: 'Yana', favourite: { colour: 'dark red' } } ]

```js
> a.where(deepData, { favourite: { '+colour': /red/ } })
> deepData.filter(where({ favourite: { '+colour': /red/ } }))
[ { name: 'Dana', favourite: { colour: 'light red' } },

@@ -95,3 +94,3 @@ { name: 'Yana', favourite: { colour: 'dark red' } },

> a.where(deepData, [ nameBeginsWithY, faveColourIncludesWhite ])
> deepData.filter(where([ nameBeginsWithY, faveColourIncludesWhite ]))
[ { name: 'Yana', favourite: { colour: 'dark red' } },

@@ -98,0 +97,0 @@ { name: 'Zhana', favourite: { colour: [ 'white', 'red' ] } } ]

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