pixl-tools
Advanced tools
Comparing version 1.1.5 to 1.1.6
{ | ||
"name": "pixl-tools", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "A set of miscellaneous utility functions for Node.js.", | ||
@@ -5,0 +5,0 @@ "author": "Joseph Huckaby <jhuckaby@gmail.com>", |
@@ -30,2 +30,4 @@ <details><summary>Table of Contents</summary> | ||
* [findObjects](#findobjects) | ||
* [findObjectDeep](#findobjectdeep) | ||
* [findObjectsDeep](#findobjectsdeep) | ||
* [deleteObject](#deleteobject) | ||
@@ -497,2 +499,42 @@ * [deleteObjects](#deleteobjects) | ||
## findObjectDeep | ||
``` | ||
OBJECT findObjectDeep( ARRAY, CRITERIA ) | ||
``` | ||
This function iterates over an array of hashes, and returns the first item whose object has deep properties which match a given criteria hash with `dot.path.properties`. If no objects match, `null` is returned. The format of the criteria properties should be compatible with [getPath()](#getpath). | ||
```js | ||
let list = [ | ||
{ id: 12345, params: { name: "Joe", eyes: "blue" } }, | ||
{ id: 12346, params: { name: "Frank", eyes: "brown" } }, | ||
{ id: 12347, params: { name: "Cynthia", eyes: "blue" } } | ||
]; | ||
let criteria = { 'params.eyes': "blue" }; | ||
let obj = Tools.findObjecDeep( list, criteria ); | ||
// --> { id: 12345, params: { name: "Joe", eyes: "blue" } } | ||
``` | ||
## findObjectsDeep | ||
``` | ||
ARRAY findObjectsDeep( ARRAY, CRITERIA ) | ||
``` | ||
This function iterates over an array of hashes, and returns all the items whose objects have deep properties which match a given criteria hash with `dot.path.properties`. If none match, an empty array is returned. The format of the criteria properties should be compatible with [getPath()](#getpath). | ||
```js | ||
let list = [ | ||
{ id: 12345, params: { name: "Joe", eyes: "blue" } }, | ||
{ id: 12346, params: { name: "Frank", eyes: "brown" } }, | ||
{ id: 12347, params: { name: "Cynthia", eyes: "blue" } } | ||
]; | ||
let criteria = { 'params.eyes': "blue" }; | ||
let objs = Tools.findObjectsDeep( list, criteria ); | ||
// --> [{ id: 12345, params: { name: "Joe", eyes: "blue" } }, { id: 12347, params: { name: "Cynthia", eyes: "blue" } }] | ||
``` | ||
## deleteObject | ||
@@ -499,0 +541,0 @@ |
30
tools.js
@@ -57,2 +57,6 @@ // Misc Tools for Node.js | ||
NEVER_MATCH: /(?!)/, | ||
noop: function() {}, | ||
timeNow: function(floor) { | ||
@@ -270,2 +274,28 @@ // return current epoch time | ||
findObjectsDeep: function(arr, crit, max) { | ||
// find and return all objects that match crit paths/values | ||
var results = []; | ||
var num_crit = 0; | ||
for (var a in crit) num_crit++; | ||
for (var idx = 0, len = arr.length; idx < len; idx++) { | ||
var matches = 0; | ||
for (var key in crit) { | ||
if (this.getPath(arr[idx], key) == crit[key]) matches++; | ||
} | ||
if (matches == num_crit) { | ||
results.push(arr[idx]); | ||
if (max && (results.length >= max)) return results; | ||
} | ||
} // foreach elem | ||
return results; | ||
}, | ||
findObjectDeep: function(arr, crit) { | ||
// return first found object matching crit paths/values, or null if not found | ||
var results = this.findObjectsDeep(arr, crit, 1); | ||
return results.length ? results[0] : null; | ||
}, | ||
deleteObject: function(arr, crit) { | ||
@@ -272,0 +302,0 @@ // walk array looking for nested object matching criteria object |
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
96718
1203
1538