array-tools
Advanced tools
Comparing version 1.5.2 to 1.5.3
@@ -117,2 +117,3 @@ "use strict"; | ||
@returns {Array} | ||
@category General | ||
@example | ||
@@ -145,2 +146,3 @@ > a.arrayify(null) | ||
@returns {boolean} | ||
@category General | ||
@example | ||
@@ -223,2 +225,3 @@ > a.exists([ 1, 2, 3 ], 2) | ||
@returns {Array} | ||
@category General | ||
@example | ||
@@ -244,2 +247,3 @@ > a.without([ 1, 2, 3 ], 2) | ||
@returns {Array} | ||
@category General | ||
@example | ||
@@ -283,2 +287,3 @@ > var array1 = [ 1, 2 ], array2 = [ 2, 3 ]; | ||
@returns {Array} | ||
@category General | ||
@example | ||
@@ -305,2 +310,3 @@ > a.commonSequence([1,2,3], [1,2,4]) | ||
@returns {Array} | ||
@category General | ||
@example | ||
@@ -327,2 +333,3 @@ > n = [1,6,6,7,1] | ||
@returns {Array} | ||
@category General | ||
@example | ||
@@ -351,2 +358,3 @@ > letters = ["a", "a", "b"] | ||
@returns {Array} the extracted items. | ||
@category General | ||
@alias module:array-tools.extract | ||
@@ -382,2 +390,3 @@ */ | ||
@returns {Array} | ||
@category General | ||
@example | ||
@@ -384,0 +393,0 @@ > numbers = [ 1, 2, [ 3, 4 ], 5 ] |
{ | ||
"name": "array-tools", | ||
"author": "Lloyd Brookes <75pound@gmail.com>", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "Useful functions for working with arrays", | ||
@@ -6,0 +6,0 @@ "repository": "https://github.com/75lb/array-tools.git", |
323
README.md
@@ -16,86 +16,21 @@ [![view on npm](http://img.shields.io/npm/v/array-tools.svg)](https://www.npmjs.org/package/array-tools) | ||
* [array-tools](#module_array-tools) | ||
* [.pluck(arrayOfObjects, ...property)](#module_array-tools.pluck) ⇒ <code>Array</code> | ||
* [.pick(arrayOfObjects, ...property)](#module_array-tools.pick) ⇒ <code>Array.<object></code> | ||
* [.arrayify(input)](#module_array-tools.arrayify) ⇒ <code>Array</code> | ||
* [.exists(array, value)](#module_array-tools.exists) ⇒ <code>boolean</code> | ||
* [.where(arrayOfObjects, query)](#module_array-tools.where) ⇒ <code>Array</code> | ||
* [.findWhere(arrayOfObjects, query)](#module_array-tools.findWhere) ⇒ <code>object</code> | ||
* [.without(input, toRemove)](#module_array-tools.without) ⇒ <code>Array</code> | ||
* [.union(array1, array2, idKey)](#module_array-tools.union) ⇒ <code>Array</code> | ||
* [.commonSequence(a, b)](#module_array-tools.commonSequence) ⇒ <code>Array</code> | ||
* [.unique(array)](#module_array-tools.unique) ⇒ <code>Array</code> | ||
* [.spliceWhile(array, index, test, ...elementN)](#module_array-tools.spliceWhile) ⇒ <code>Array</code> | ||
* [.extract(array, query)](#module_array-tools.extract) ⇒ <code>Array</code> | ||
* [.flatten()](#module_array-tools.flatten) ⇒ <code>Array</code> | ||
* [.sortBy(arrayOfObject, ...columns)](#module_array-tools.sortBy) ⇒ <code>array</code> | ||
* _General_ | ||
* [.arrayify(any)](#module_array-tools.arrayify) ⇒ <code>Array</code> | ||
* [.exists(array, value)](#module_array-tools.exists) ⇒ <code>boolean</code> | ||
* [.without(array, toRemove)](#module_array-tools.without) ⇒ <code>Array</code> | ||
* [.union(array1, array2, idKey)](#module_array-tools.union) ⇒ <code>Array</code> | ||
* [.commonSequence(a, b)](#module_array-tools.commonSequence) ⇒ <code>Array</code> | ||
* [.unique(array)](#module_array-tools.unique) ⇒ <code>Array</code> | ||
* [.spliceWhile(array, index, test, ...elementN)](#module_array-tools.spliceWhile) ⇒ <code>Array</code> | ||
* [.extract(array, query)](#module_array-tools.extract) ⇒ <code>Array</code> | ||
* [.flatten()](#module_array-tools.flatten) ⇒ <code>Array</code> | ||
* _Record sets_ | ||
* [.pluck(arrayOfObjects, ...property)](#module_array-tools.pluck) ⇒ <code>Array</code> | ||
* [.pick(arrayOfObjects, ...property)](#module_array-tools.pick) ⇒ <code>Array.<object></code> | ||
* [.where(arrayOfObjects, query)](#module_array-tools.where) ⇒ <code>Array</code> | ||
* [.findWhere(arrayOfObjects, query)](#module_array-tools.findWhere) ⇒ <code>object</code> | ||
* [.sortBy(arrayOfObjects, columns, customOrder)](#module_array-tools.sortBy) ⇒ <code>Array</code> | ||
<a name="module_array-tools.pluck"></a> | ||
## a.pluck(arrayOfObjects, ...property) ⇒ <code>Array</code> | ||
Plucks the value of the specified property from each object in the input array | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array.<object></code></td><td>the input array of objects</td> | ||
</tr><tr> | ||
<td>...property</td><td><code>string</code></td><td>the property(s) to pluck</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> var data = [ | ||
{one: 1, two: 2}, | ||
{two: "two"}, | ||
{one: "one", two: "zwei"}, | ||
]; | ||
> a.pluck(data, "one"); | ||
[ 1, 'one' ] | ||
> a.pluck(data, "two"); | ||
[ 2, 'two', 'zwei' ] | ||
> a.pluck(data, "one", "two"); | ||
[ 1, 'two', 'one' ] | ||
``` | ||
<a name="module_array-tools.pick"></a> | ||
## a.pick(arrayOfObjects, ...property) ⇒ <code>Array.<object></code> | ||
return a copy of the input `arrayOfObjects` containing objects having only the cherry-picked properties | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array.<object></code></td><td>the input</td> | ||
</tr><tr> | ||
<td>...property</td><td><code>string</code></td><td>the properties to include in the result</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> data = [ | ||
{ one: "un", two: "deux", three: "trois" }, | ||
{ two: "two", one: "one" }, | ||
{ four: "quattro" }, | ||
{ two: "zwei" } | ||
] | ||
> a.pick(data, "two") | ||
[ { two: 'deux' }, | ||
{ two: 'two' }, | ||
{ two: 'zwei' } ] | ||
``` | ||
<a name="module_array-tools.arrayify"></a> | ||
## a.arrayify(input) ⇒ <code>Array</code> | ||
## a.arrayify(any) ⇒ <code>Array</code> | ||
Takes input and guarantees an array back. Result can be one of three things: | ||
@@ -107,2 +42,3 @@ | ||
**Category**: General | ||
@@ -117,3 +53,3 @@ <table> | ||
<tr> | ||
<td>input</td><td><code>*</code></td><td>the input value to convert to an array</td> | ||
<td>any</td><td><code>*</code></td><td>the input value to convert to an array</td> | ||
</tr> | ||
@@ -139,2 +75,3 @@ </tbody> | ||
**Category**: General | ||
@@ -167,67 +104,7 @@ <table> | ||
``` | ||
<a name="module_array-tools.where"></a> | ||
## a.where(arrayOfObjects, query) ⇒ <code>Array</code> | ||
returns an array containing items from `arrayOfObjects` where key/value pairs | ||
from `query` are matched identically | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array</code></td><td>the array to search</td> | ||
</tr><tr> | ||
<td>query</td><td><code>query</code></td><td>an object containing the key/value pairs you want to match</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> dudes = [{ name: "Jim", age: 8}, { name: "Clive", age: 8}, { name: "Hater", age: 9}] | ||
[ { name: 'Jim', age: 8 }, | ||
{ name: 'Clive', age: 8 }, | ||
{ name: 'Hater', age: 9 } ] | ||
> a.where(dudes, { age: 8}) | ||
[ { name: 'Jim', age: 8 }, | ||
{ name: 'Clive', age: 8 } ] | ||
``` | ||
<a name="module_array-tools.findWhere"></a> | ||
## a.findWhere(arrayOfObjects, query) ⇒ <code>object</code> | ||
returns the first item from `arrayOfObjects` where key/value pairs | ||
from `query` are matched identically | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array</code></td><td>the array to search</td> | ||
</tr><tr> | ||
<td>query</td><td><code>query</code></td><td>an object containing the key/value pairs you want to match</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> dudes = [{ name: "Jim", age: 8}, { name: "Clive", age: 8}, { name: "Hater", age: 9}] | ||
[ { name: 'Jim', age: 8 }, | ||
{ name: 'Clive', age: 8 }, | ||
{ name: 'Hater', age: 9 } ] | ||
> a.findWhere(dudes, { age: 8}) | ||
{ name: 'Jim', age: 8 } | ||
``` | ||
<a name="module_array-tools.without"></a> | ||
## a.without(input, toRemove) ⇒ <code>Array</code> | ||
## a.without(array, toRemove) ⇒ <code>Array</code> | ||
Returns the input minus the specified values. | ||
**Category**: General | ||
@@ -242,3 +119,3 @@ <table> | ||
<tr> | ||
<td>input</td><td><code>Array</code></td><td>the input array</td> | ||
<td>array</td><td><code>Array</code></td><td>the input array</td> | ||
</tr><tr> | ||
@@ -261,2 +138,3 @@ <td>toRemove</td><td><code>*</code></td><td>a single, or array of values to omit</td> | ||
**Category**: General | ||
@@ -301,2 +179,3 @@ <table> | ||
**Category**: General | ||
@@ -325,4 +204,5 @@ <table> | ||
## a.unique(array) ⇒ <code>Array</code> | ||
reduces an array to unique values | ||
returns an array of unique values | ||
**Category**: General | ||
@@ -353,2 +233,3 @@ <table> | ||
**Category**: General | ||
@@ -388,2 +269,3 @@ <table> | ||
**Returns**: <code>Array</code> - the extracted items. | ||
**Category**: General | ||
@@ -409,2 +291,3 @@ <table> | ||
**Category**: General | ||
**Since**: 1.4.0 | ||
@@ -421,6 +304,140 @@ **Todo** | ||
``` | ||
<a name="module_array-tools.pluck"></a> | ||
## a.pluck(arrayOfObjects, ...property) ⇒ <code>Array</code> | ||
Plucks the value of the specified property from each object in the input array | ||
**Category**: Record sets | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array.<object></code></td><td>the input array of objects</td> | ||
</tr><tr> | ||
<td>...property</td><td><code>string</code></td><td>the property(s) to pluck</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> var data = [ | ||
{one: 1, two: 2}, | ||
{two: "two"}, | ||
{one: "one", two: "zwei"}, | ||
]; | ||
> a.pluck(data, "one"); | ||
[ 1, 'one' ] | ||
> a.pluck(data, "two"); | ||
[ 2, 'two', 'zwei' ] | ||
> a.pluck(data, "one", "two"); | ||
[ 1, 'two', 'one' ] | ||
``` | ||
<a name="module_array-tools.pick"></a> | ||
## a.pick(arrayOfObjects, ...property) ⇒ <code>Array.<object></code> | ||
return a copy of the input `arrayOfObjects` containing objects having only the cherry-picked properties | ||
**Category**: Record sets | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array.<object></code></td><td>the input</td> | ||
</tr><tr> | ||
<td>...property</td><td><code>string</code></td><td>the properties to include in the result</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> data = [ | ||
{ one: "un", two: "deux", three: "trois" }, | ||
{ two: "two", one: "one" }, | ||
{ four: "quattro" }, | ||
{ two: "zwei" } | ||
] | ||
> a.pick(data, "two") | ||
[ { two: 'deux' }, | ||
{ two: 'two' }, | ||
{ two: 'zwei' } ] | ||
``` | ||
<a name="module_array-tools.where"></a> | ||
## a.where(arrayOfObjects, query) ⇒ <code>Array</code> | ||
returns an array containing items from `arrayOfObjects` where key/value pairs | ||
from `query` are matched identically | ||
**Category**: Record sets | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array.<object></code></td><td>the array to search</td> | ||
</tr><tr> | ||
<td>query</td><td><code>query</code></td><td>an object containing the key/value pairs you want to match</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> dudes = [{ name: "Jim", age: 8}, { name: "Clive", age: 8}, { name: "Hater", age: 9}] | ||
[ { name: 'Jim', age: 8 }, | ||
{ name: 'Clive', age: 8 }, | ||
{ name: 'Hater', age: 9 } ] | ||
> a.where(dudes, { age: 8}) | ||
[ { name: 'Jim', age: 8 }, | ||
{ name: 'Clive', age: 8 } ] | ||
``` | ||
<a name="module_array-tools.findWhere"></a> | ||
## a.findWhere(arrayOfObjects, query) ⇒ <code>object</code> | ||
returns the first item from `arrayOfObjects` where key/value pairs | ||
from `query` are matched identically | ||
**Category**: Record sets | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Param</th><th>Type</th><th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<tr> | ||
<td>arrayOfObjects</td><td><code>Array.<object></code></td><td>the array to search</td> | ||
</tr><tr> | ||
<td>query</td><td><code>object</code></td><td>an object containing the key/value pairs you want to match</td> | ||
</tr> | ||
</tbody> | ||
</table> | ||
**Example** | ||
```js | ||
> dudes = [{ name: "Jim", age: 8}, { name: "Clive", age: 8}, { name: "Hater", age: 9}] | ||
[ { name: 'Jim', age: 8 }, | ||
{ name: 'Clive', age: 8 }, | ||
{ name: 'Hater', age: 9 } ] | ||
> a.findWhere(dudes, { age: 8}) | ||
{ name: 'Jim', age: 8 } | ||
``` | ||
<a name="module_array-tools.sortBy"></a> | ||
## a.sortBy(arrayOfObject, ...columns) ⇒ <code>array</code> | ||
## a.sortBy(arrayOfObjects, columns, customOrder) ⇒ <code>Array</code> | ||
Sort an array of objects by one or more fields | ||
**Category**: Record sets | ||
**Since**: 1.5.0 | ||
@@ -435,5 +452,7 @@ <table> | ||
<tr> | ||
<td>arrayOfObject</td><td><code>array</code></td><td>input array</td> | ||
<td>arrayOfObjects</td><td><code>Array.<object></code></td><td>input array</td> | ||
</tr><tr> | ||
<td>...columns</td><td><code>string</code></td><td>column names to sort by</td> | ||
<td>columns</td><td><code>string</code> | <code>Array.<string></code></td><td>column name(s) to sort by</td> | ||
</tr><tr> | ||
<td>customOrder</td><td><code>object</code></td><td>specific sort orders, per columns</td> | ||
</tr> | ||
@@ -454,5 +473,5 @@ </tbody> | ||
{ a: 3, b: 3, c: 3}, | ||
{ a: 4, b: 3, c: 1} | ||
{ a: 4, b: 3, c: 1} | ||
]; | ||
> a.sortBy(fixture, "a", "b", "c") | ||
> a.sortBy(fixture, ["a", "b", "c"]) | ||
[ { a: 1, b: 1, c: 4 }, | ||
@@ -459,0 +478,0 @@ { a: 1, b: 2, c: 4 }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44666
951
479