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

array-tools

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array-tools - npm Package Compare versions

Comparing version 1.8.6 to 2.0.0-0

bower.json

28

bin/cli.js

@@ -5,20 +5,22 @@ #!/usr/bin/env node

var a = require("../");
var tr = require("transform-tools");
if (process.argv.length < 4){
console.error("Usage:");
console.error("$ cat <json array> | array-tools <method> <args...>");
process.exit(1);
}
process.argv.splice(0, 2);
var input = new Buffer(0);
var method = process.argv.shift();
var args = process.argv.slice(0);
process.stdin.on("readable", function(){
var chunk = this.read();
if (chunk) input = Buffer.concat([ input, chunk ]);
});
process.stdin.on("end", function(){
var array = JSON.parse(input);
function processInput(array){
args.unshift(array);
if (method){
var result = a[method].apply(null, args);
console.log(JSON.stringify(result, null, " "))
}
});
var result = a[method].apply(null, args);
return JSON.stringify(result, null, " ") + "\n";
}
process.stdin
.pipe(tr.collectJson({ transform: processInput }))
.pipe(process.stdout);

@@ -80,4 +80,4 @@ "use strict";

- converts array-like objects (e.g. `arguments`) to a real array
- converts `null` or `undefined` to an empty array
- converts any another other, singular value into an array containing that value
- converts `undefined` to an empty array
- converts any another other, singular value (including `null`) into an array containing that value
- ignores input which is already an array

@@ -90,5 +90,8 @@

@example
> a.arrayify(null)
> a.arrayify(undefined)
[]
> a.arrayify(null)
[ null ]
> a.arrayify(0)

@@ -105,3 +108,3 @@ [ 0 ]

function arrayify(any){
if (any === null || any === undefined){
if (any === undefined){
return [];

@@ -124,2 +127,4 @@ } else if (t.isArrayLike(any)){

@example
Say you have a recordset:
```js
> data = [

@@ -130,16 +135,25 @@ { name: "Dana", age: 30 },

]
```
> // match on an exact value
You can return records with properties matching an exact value:
```js
> a.where(data, { age: 10 })
[ { name: 'Zhana', age: 10 } ]
```
> // match records which satisfy the function testing the value of the `age` field
> a.where(data, { age: function(ageValue){ return ageValue > 10; } })
All query expressions can be negated (where NOT the value). Prefix the property name with `!`:
```js
> a.where(data, { "!age": 10 })
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
```
> // match if NOT the value
> a.where(data, { "!age": 10 })
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 on regular expression
match using a regular expression
```js
> a.where(data, { name: /ana/ })

@@ -149,4 +163,6 @@ [ { name: 'Dana', age: 30 },

{ name: 'Zhana', age: 10 } ]
```
> // you can perform deep queries
You can query to any arbitrary depth. So with deeper data, like:
```js
> deepData = [

@@ -157,10 +173,13 @@ { name: "Dana", favourite: { colour: "light red" } },

]
```
> // match values of `person.favourite.colour` which match the regex `/red/`
return records containing `favourite.colour` values matching the regex `/red/`
```js
> a.where(deepData, { favourite: { colour: /red/ } })
[ { name: 'Dana', favourite: { colour: 'light red' } },
{ name: 'Yana', favourite: { colour: 'dark red' } } ]
```
> // if there are one or more values for colour (i.e. the field may contain a singular
> // or array of values) then search inside arrays too
Prefix the property name with `+` if the value you want to match could be singular _or_ a member of an array.
```js
> a.where(deepData, { favourite: { "+colour": /red/ } })

@@ -170,5 +189,7 @@ [ { name: 'Dana', favourite: { colour: 'light red' } },

{ name: 'Zhana', favourite: { colour: [ "white", "red" ] } } ]
```
*/
function where(arrayOfObjects, query){
return arrayify(arrayOfObjects).filter(function(item){
function where(recordset, query){
return arrayify(recordset).filter(function(item){
return o.exists(item, query);

@@ -179,5 +200,51 @@ });

/**
returns true if a value, or nested object value exists in an array.. If value is a plain object, it is considered to be a query. If `value` is a plain object and you want to search for it by reference, use `.contains`.
@param {Array} - the array to search
@param {*} - the value to search for
@returns {boolean}
@category not chainable
@static
@example
> a.exists([ 1, 2, 3 ], 2)
true
> a.exists([ 1, 2, 3 ], [ 2, 3 ])
true
> a.exists([ { result: false }, { result: false } ], { result: true })
false
> a.exists([ { result: true }, { result: false } ], { result: true })
> a.exists([ { n: 1 }, { n: 2 }, { n: 3 } ], [ { n: 1 }, { n: 3 } ])
true
*/
function exists(array, value){
if (t.isPlainObject(value)){
var query = value;
return array.some(function(item){
return o.exists(item, query);
});
} else if (Array.isArray(value)){
return value.every(function(val){
return exists(array, val);
});
} else if (value instanceof RegExp){
return array.some(function(item){
return value.test(item);
});
} else if (typeof value === "function"){
return array.some(function(item){
return value(item);
});
} else {
return contains(array, value);
}
}
/**
Plucks the value of the specified property from each object in the input array
@param arrayOfObjects {object[]} - The input recordset
@param recordset {object[]} - The input recordset
@param property {...string} - Up to three property names - the first one found will be returned.

@@ -200,6 +267,6 @@ @returns {Array}

*/
function pluck(arrayOfObjects, property, property2, property3){
if (!Array.isArray(arrayOfObjects)) throw new Error(".pluck() input must be an array");
function pluck(recordset, property, property2, property3){
if (!Array.isArray(recordset)) throw new Error(".pluck() input must be an array");
return arrayOfObjects
return recordset
.filter(function(obj){

@@ -220,4 +287,4 @@ var one = eval("obj." + property);

/**
return a copy of the input `arrayOfObjects` containing objects having only the cherry-picked properties
@param arrayOfObjects {object[]} - the input
return a copy of the input `recordset` containing objects having only the cherry-picked properties
@param recordset {object[]} - the input
@param property {...string} - the properties to include in the result

@@ -244,8 +311,8 @@ @return {object[]}

var args = arrayify(arguments);
var arrayOfObjects = args.shift();
var recordset = args.shift();
var properties = args;
if (!Array.isArray(arrayOfObjects)) throw new Error(".pick() input must be an array");
if (!Array.isArray(recordset)) throw new Error(".pick() input must be an array");
return arrayOfObjects
return recordset
.filter(function(obj){

@@ -268,39 +335,3 @@ return properties.some(function(prop){

/**
returns true if a value, or nested object value exists in an array
@param {Array} - the array to search
@param {*} - the value to search for
@returns {boolean}
@category not chainable
@static
@example
> a.exists([ 1, 2, 3 ], 2)
true
> a.exists([ { result: false }, { result: false } ], { result: true })
false
> a.exists([ { result: true }, { result: false } ], { result: true })
true
> a.exists([ { result: true }, { result: true } ], { result: true })
true
*/
function exists(array, value){
if (t.isPlainObject(value)){
var query = value;
var found = false;
var index = 0;
var item;
while(!found && (item = array[index++])){
found = o.exists(item, query);
}
return found;
} else {
return array.indexOf(value) > -1;
}
}
/**
returns the first item from `arrayOfObjects` where key/value pairs
returns the first item from `recordset` where key/value pairs
from `query` are matched identically

@@ -321,4 +352,4 @@ @param {object[]} - the array to search

*/
function findWhere(arrayOfObjects, query){
var result = where(arrayOfObjects, query);
function findWhere(recordset, query){
var result = where(recordset, query);
return result.length ? result[0] : null;

@@ -329,3 +360,4 @@ }

/**
Returns the input minus the specified values.
Returns a new array with the same content as the input minus the specified values.
@param {Array} - the input array

@@ -543,4 +575,4 @@ @param {*} - a single, or array of values to omit

*/
function sortBy(arrayOfObjects, columns, customOrder){
return arrayOfObjects.sort(sortByFunc(arrayify(columns), customOrder));
function sortBy(recordset, columns, customOrder){
return recordset.sort(sortByFunc(arrayify(columns), customOrder));
}

@@ -547,0 +579,0 @@

{
"name": "array-tools",
"author": "Lloyd Brookes <75pound@gmail.com>",
"version": "1.8.6",
"description": "Lightweight tool-kit for working with arrays",
"version": "2.0.0-0",
"description": "Lightweight, use-anywhere toolkit for working with array data. 1.5k, compressed.",
"repository": "https://github.com/75lb/array-tools.git",

@@ -38,5 +38,6 @@ "main": "./lib/array-tools.js",

"dependencies": {
"object-tools": "^1.6.1",
"typical": "^2.1"
"object-tools": "^2",
"typical": "^2.1",
"transform-tools": "~0.0"
}
}

@@ -8,3 +8,3 @@ [![view on npm](http://img.shields.io/npm/v/array-tools.svg)](https://www.npmjs.org/package/array-tools)

# array-tools
Lightweight tool-kit for working with array data. 1.5k, compressed.
Lightweight, use-anywhere toolkit for working with array data. 1.5k, compressed.

@@ -37,2 +37,3 @@ ```js

// this class will inherit all array-tools methods
function CarCollection(cars){

@@ -70,3 +71,6 @@ ArrayTools.call(this, cars);

#### More on chaining
Each method returning an `Array` (e.g. `where`, `without`) can be chained. Methods not returning an array (`exists`, `contains`) cannot be chained. If the final operation in your chain is chainable (returns an array), append `.val()` to terminate the chain and retrieve the output.
* Each method returning an `Array` (e.g. `where`, `without`) can be chained.
* Methods not returning an array (`exists`, `contains`) cannot be chained.
* All methods from `Array.prototype` (e.g. `.join`, `.forEach` etc.) are also available in the chain. The same rules, regarding what can and cannot be chained, apply as above.
* If the final operation in your chain is "chainable" (returns an array), append `.val()` to terminate the chain and retrieve the output.

@@ -80,2 +84,4 @@ ```js

[ 2, 3 ]
> a([ 1, 2, 2, 3 ]).without(1).unique().join("-")
'2-3'
```

@@ -105,5 +111,5 @@

* [.arrayify(any)](#module_array-tools.arrayify) ⇒ <code>Array</code>
* [.where(arrayOfObjects, query)](#module_array-tools.where) ⇒ <code>Array</code>
* [.pluck(arrayOfObjects, ...property)](#module_array-tools.pluck) ⇒ <code>Array</code>
* [.pick(arrayOfObjects, ...property)](#module_array-tools.pick) ⇒ <code>Array.&lt;object&gt;</code>
* [.where(recordset, query)](#module_array-tools.where) ⇒ <code>Array</code>
* [.pluck(recordset, ...property)](#module_array-tools.pluck) ⇒ <code>Array</code>
* [.pick(recordset, ...property)](#module_array-tools.pick) ⇒ <code>Array.&lt;object&gt;</code>
* [.without(array, toRemove)](#module_array-tools.without) ⇒ <code>Array</code>

@@ -116,6 +122,6 @@ * [.union(array1, array2, idKey)](#module_array-tools.union) ⇒ <code>Array</code>

* [.flatten(array)](#module_array-tools.flatten) ⇒ <code>Array</code>
* [.sortBy(arrayOfObjects, columns, customOrder)](#module_array-tools.sortBy) ⇒ <code>Array</code>
* [.sortBy(recordset, columns, customOrder)](#module_array-tools.sortBy) ⇒ <code>Array</code>
* _not chainable_
* [.exists(array, value)](#module_array-tools.exists) ⇒ <code>boolean</code>
* [.findWhere(arrayOfObjects, query)](#module_array-tools.findWhere) ⇒ <code>object</code>
* [.findWhere(recordset, query)](#module_array-tools.findWhere) ⇒ <code>object</code>
* [.last(arr)](#module_array-tools.last) ⇒ <code>\*</code>

@@ -130,4 +136,4 @@ * [.remove(arr, toRemove)](#module_array-tools.remove) ⇒ <code>\*</code>

- converts array-like objects (e.g. `arguments`) to a real array
- converts `null` or `undefined` to an empty array
- converts any another other, singular value into an array containing that value
- converts `undefined` to an empty array
- converts any another other, singular value (including `null`) into an array containing that value
- ignores input which is already an array

@@ -144,5 +150,8 @@

```js
> a.arrayify(null)
> a.arrayify(undefined)
[]
> a.arrayify(null)
[ null ]
> a.arrayify(0)

@@ -159,3 +168,3 @@ [ 0 ]

<a name="module_array-tools.where"></a>
### a.where(arrayOfObjects, query) ⇒ <code>Array</code>
### a.where(recordset, query) ⇒ <code>Array</code>
Query a recordset, at any depth..

@@ -168,6 +177,7 @@

| --- | --- | --- |
| arrayOfObjects | <code>Array.&lt;object&gt;</code> | the recordset to query |
| recordset | <code>Array.&lt;object&gt;</code> | the recordset to query |
| query | <code>query</code> | the query definition |
**Example**
Say you have a recordset:
```js

@@ -179,16 +189,25 @@ > data = [

]
```
> // match on an exact value
You can return records with properties matching an exact value:
```js
> a.where(data, { age: 10 })
[ { name: 'Zhana', age: 10 } ]
```
> // match records which satisfy the function testing the value of the `age` field
> a.where(data, { age: function(ageValue){ return ageValue > 10; } })
All query expressions can be negated (where NOT the value). Prefix the property name with `!`:
```js
> a.where(data, { "!age": 10 })
[ { name: 'Dana', age: 30 }, { name: 'Yana', age: 20 } ]
```
> // match if NOT the value
> a.where(data, { "!age": 10 })
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 on regular expression
match using a regular expression
```js
> a.where(data, { name: /ana/ })

@@ -198,4 +217,6 @@ [ { name: 'Dana', age: 30 },

{ name: 'Zhana', age: 10 } ]
```
> // you can perform deep queries
You can query to any arbitrary depth. So with deeper data, like:
```js
> deepData = [

@@ -206,10 +227,13 @@ { name: "Dana", favourite: { colour: "light red" } },

]
```
> // match values of `person.favourite.colour` which match the regex `/red/`
return records containing `favourite.colour` values matching the regex `/red/`
```js
> a.where(deepData, { favourite: { colour: /red/ } })
[ { name: 'Dana', favourite: { colour: 'light red' } },
{ name: 'Yana', favourite: { colour: 'dark red' } } ]
```
> // if there are one or more values for colour (i.e. the field may contain a singular
> // or array of values) then search inside arrays too
Prefix the property name with `+` if the value you want to match could be singular _or_ a member of an array.
```js
> a.where(deepData, { favourite: { "+colour": /red/ } })

@@ -221,3 +245,3 @@ [ { name: 'Dana', favourite: { colour: 'light red' } },

<a name="module_array-tools.pluck"></a>
### a.pluck(arrayOfObjects, ...property) ⇒ <code>Array</code>
### a.pluck(recordset, ...property) ⇒ <code>Array</code>
Plucks the value of the specified property from each object in the input array

@@ -230,3 +254,3 @@

| --- | --- | --- |
| arrayOfObjects | <code>Array.&lt;object&gt;</code> | The input recordset |
| recordset | <code>Array.&lt;object&gt;</code> | The input recordset |
| ...property | <code>string</code> | Up to three property names - the first one found will be returned. |

@@ -249,4 +273,4 @@

<a name="module_array-tools.pick"></a>
### a.pick(arrayOfObjects, ...property) ⇒ <code>Array.&lt;object&gt;</code>
return a copy of the input `arrayOfObjects` containing objects having only the cherry-picked properties
### a.pick(recordset, ...property) ⇒ <code>Array.&lt;object&gt;</code>
return a copy of the input `recordset` containing objects having only the cherry-picked properties

@@ -258,3 +282,3 @@ **Kind**: static method of <code>[array-tools](#module_array-tools)</code>

| --- | --- | --- |
| arrayOfObjects | <code>Array.&lt;object&gt;</code> | the input |
| recordset | <code>Array.&lt;object&gt;</code> | the input |
| ...property | <code>string</code> | the properties to include in the result |

@@ -280,3 +304,3 @@

### a.without(array, toRemove) ⇒ <code>Array</code>
Returns the input minus the specified values.
Returns a new array with the same content as the input minus the specified values.

@@ -424,3 +448,3 @@ **Kind**: static method of <code>[array-tools](#module_array-tools)</code>

<a name="module_array-tools.sortBy"></a>
### a.sortBy(arrayOfObjects, columns, customOrder) ⇒ <code>Array</code>
### a.sortBy(recordset, columns, customOrder) ⇒ <code>Array</code>
Sort an array of objects by one or more fields

@@ -434,3 +458,3 @@

| --- | --- | --- |
| arrayOfObjects | <code>Array.&lt;object&gt;</code> | input array |
| recordset | <code>Array.&lt;object&gt;</code> | input array |
| columns | <code>string</code> &#124; <code>Array.&lt;string&gt;</code> | column name(s) to sort by |

@@ -465,3 +489,3 @@ | customOrder | <code>object</code> | specific sort orders, per columns |

### a.exists(array, value) ⇒ <code>boolean</code>
returns true if a value, or nested object value exists in an array
returns true if a value, or nested object value exists in an array.. If value is a plain object, it is considered to be a query. If `value` is a plain object and you want to search for it by reference, use `.contains`.

@@ -481,2 +505,5 @@ **Kind**: static method of <code>[array-tools](#module_array-tools)</code>

> a.exists([ 1, 2, 3 ], [ 2, 3 ])
true
> a.exists([ { result: false }, { result: false } ], { result: true })

@@ -486,10 +513,9 @@ false

> a.exists([ { result: true }, { result: false } ], { result: true })
true
> a.exists([ { result: true }, { result: true } ], { result: true })
> a.exists([ { n: 1 }, { n: 2 }, { n: 3 } ], [ { n: 1 }, { n: 3 } ])
true
```
<a name="module_array-tools.findWhere"></a>
### a.findWhere(arrayOfObjects, query) ⇒ <code>object</code>
returns the first item from `arrayOfObjects` where key/value pairs
### a.findWhere(recordset, query) ⇒ <code>object</code>
returns the first item from `recordset` where key/value pairs
from `query` are matched identically

@@ -502,3 +528,3 @@

| --- | --- | --- |
| arrayOfObjects | <code>Array.&lt;object&gt;</code> | the array to search |
| recordset | <code>Array.&lt;object&gt;</code> | the array to search |
| query | <code>object</code> | an object containing the key/value pairs you want to match |

@@ -505,0 +531,0 @@

@@ -6,4 +6,4 @@ "use strict";

test(".arrayify()", function(t){
t.deepEqual(a.arrayify(null), []);
t.deepEqual(a.arrayify(undefined), []);
t.deepEqual(a.arrayify(null), [ null ]);
t.deepEqual(a.arrayify(0), [ 0 ]);

@@ -10,0 +10,0 @@ t.deepEqual(a.arrayify([ 1, 2 ]), [ 1, 2 ]);

var test = require("tape");
var a = require("../");
test("exists: query a recordset", function(t){
var arr = [
var f = {
recordset: [
{ result: false, number: 1 },
{ result: false, number: 2 }
];
t.equal(a.exists(arr, { result: true }), false);
t.equal(a.exists(arr, { result: false }), true);
t.equal(a.exists(arr, { result: false, number: 3 }), false);
t.equal(a.exists(arr, { result: false, number: 2 }), true);
],
arr: [ 1, 2, "three" ]
};
test(".exists(recordset, query)", function(t){
t.equal(a.exists(f.recordset, { result: true }), false);
t.equal(a.exists(f.recordset, { result: false }), true);
t.equal(a.exists(f.recordset, { result: false, number: 3 }), false);
t.equal(a.exists(f.recordset, { result: false, number: 2 }), true);
t.end();
});
test("exists: search for scalar", function(t){
var arr = [ 1, 2, "three" ];
t.equal(a.exists(arr, 0), false);
t.equal(a.exists(arr, 1), true);
t.equal(a.exists(arr, "1"), false);
t.equal(a.exists(arr, "three"), true);
test(".exists(array, primitive)", function(t){
t.equal(a.exists(f.arr, 0), false);
t.equal(a.exists(f.arr, 1), true);
t.equal(a.exists(f.arr, "1"), false);
t.equal(a.exists(f.arr, "three"), true);
t.end();
});
test.skip("exists: search an object", function(t){
var obj1 = {};
var obj2 = {};
var arr = [ obj1, {} ];
t.equal(a.exists(arr, obj1), true);
t.equal(a.exists(arr, obj2), false);
test(".exists(array, primitive[])", function(t){
t.equal(a.exists(f.arr, [ 1, 2 ]), true);
t.equal(a.exists(f.arr, [ 1, 2, 3 ]), false);
t.equal(a.exists(f.arr, [ 1, 2, "three" ]), true);
t.end();
});
var test = require("tape");
var a = require("../");
var fixture = {
arr: [ 1, 2, 3, 4 ]
};
test("where", function(t){

@@ -63,1 +67,27 @@ var arr = [

});
test(".where(array, primitive)", function(t){
t.deepEqual(a.where(fixture.arr, 1 ), [ 1 ]);
t.end();
});
test(".where(array, regex)", function(t){
t.deepEqual(a.where(fixture.arr, /2/ ), [ 2 ]);
t.end();
});
test(".where(array, function)", function(t){
function over3(val){ return val > 3; }
t.deepEqual(a.where(fixture.arr, over3 ), [ 4 ]);
t.end();
});
test(".where(array, array)", function(t){
function over1(val){ return val > 1; }
function under4(val){ return val < 4; }
t.deepEqual(a.where(fixture.arr, [ over1, under4 ] ), [ 2, 3 ]);
t.deepEqual(a.where(fixture.arr, [ /2/, /4/ ] ), [ 2, 4 ]);
t.deepEqual(a.where(fixture.arr, [ 1, 3 ] ), [ 1, 3 ]);
t.end();
});

@@ -1,8 +0,34 @@

var test = require("tape"),
a = require("../");
var test = require("tape");
var a = require("../");
test("without (array)", function(t){
var array = [ 1,2,3,4 ];
t.deepEqual(a.without(array, [ 2, 3 ]), [ 1, 4 ]);
var fixture = {
num: [ 1, 2, 3, 4 ]
};
test(".without(array, toRemove)", function(t){
t.deepEqual(a.without(fixture.num, [ 2, 3 ]), [ 1, 4 ]);
t.end();
});
test(".without does not return the input array", function(t){
var result = a.without(fixture.num, [ 2, 3 ]);
t.notStrictEqual(fixture.num, result);
t.end();
});
test(".without(array, toRemove)", function(t){
t.deepEqual(a.without(fixture.num, [ 2, 3 ]), [ 1, 4 ]);
t.end();
});
test(".without(array, regex)", function(t){
t.deepEqual(a.without(fixture.num, /2|3/), [ 1, 4 ]);
t.end();
});
test(".without(array, function)", function(t){
function over3(val){ return val > 3; }
t.deepEqual(a.without(fixture.num, over3), [ 1, 2, 3 ]);
t.end();
});

Sorry, the diff of this file is not supported yet

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