tonybranfort / filter-objects Goto Github PK
View Code? Open in Web Editor NEWFilter an array of objects
License: MIT License
Filter an array of objects
License: MIT License
Inherited properties are considered missing; ie, not being checked. This affects both filter and matches. Example:
var props = ["prop1"];
var o = {"prop1":"abc"};
var pObj = Object.create(o);
var tObj = {"prop1":"abc"};
var f = fos.makeMatchFn(props);
f(pObj, tObj).should.equal(true); // === > equals FALSE
Depending on your implementation, this may be an issue or may be expected behavior if you don't want inherited properties to be checked.
Inherited properties are considered 'missing' so they match with options matchIfPObjPropMissing
, matchIfTObjPropMissing
and matchIfBothPropMissing
in those respective situations.
The getVariablesFn option is a function that takes a callback currently. It should not take a callback as filter-objects is not asynchronous. filter-objects will be modified with a patch version to log a warning if getVariablesFn is not undefined.
The next major version will replace getVariablesFn
with variables
which will be an object of the form that should be returned by the current getVariablesFn.
If using variables, the values may not be replaced correctly if you have multiple variables to be replaced in the same property.
it('it should replace multiple occurrences of different variables',
function() {
// var props = {"prop1":{"variablesInPObj":true,"variablesStartStr":"+"}};
var props = {
"prop1":{"variablesInPObj":true,
"variablesEndStr":"~",
"variablesStartStr":"+"
}
};
var options =
{getVariables: function(cb) {
return cb(null,
{youngDog: "puppy", youngCat: "kitten", youngSheep:"ewe"});
}};
var pObj = {"prop1":"+youngDog~WHAT+youngCat~"}; // ISSUE: TWO VARIABLES HERE
var tObj = {"prop1":"puppyWHATkitten"};
var f = fos.makeMatchFn(props, options);
f(pObj, tObj).should.equal(true); // CURRENTLY RETURNS false
});
it('it should replace multiple occurances of different variables',
function() {
// var props = {"prop1":{"variablesInPObj":true,"variablesStartStr":"+"}};
var props = {
"prop1":{"variablesInPObj":true,
"variablesEndStr":"~",
"variablesStartStr":"~"
}
};
var options =
{getVariables: function(cb) {
return cb(null,
{youngDog: "puppy", youngCat: "kitten", youngSheep:"ewe"});
}};
var pObj = {"prop1":"~youngDog~WHAT~youngCat~"}; // ISSUE: TWO VARIABLES HERE
var tObj = {"prop1":"puppyWHATkitten"};
var f = fos.makeMatchFn(props, options);
f(pObj, tObj).should.equal(true); // CURRENTLY RETURNS false
});
These two options combinations can incorrectly return a positive match if the tObj or pObj is missing respectively:
(default for matchIfTObjPropMissing & matchIfPObjPropMissing is false
)
Affects both match and filter (makeFilterFn, makeMatchFn).
it('should return false if a chosen poperty is missing in tObj ' +
'including when regExpMatch and pObj has .*', function() {
var options = {
regExpMatch:true,
// matchIfTObjPropMissing: false // default is false
};
var props = ["prop1",'prop2.cat.tail'];
var pObj = {"prop1":"abc","prop2":{"cat":{"tail":".*"}}};
var tObj = {"prop1":"abc","prop2":{"cat":{"nose":"brown"}}};
var f = fos.makeMatchFn(props,options);
f(pObj, tObj).should.equal(false); // IS INCORRECTLY RETURNING true
});
it('should return false if a chosen property is missing in pObj ' +
'including when regExpMatch, regExpReverse and tObj has .*',
function() {
var options = {
regExpMatch:true,
regExpReverse:true,
// matchIfPObjPropMissing: false // default is false
};
var props = ["prop1",'prop2.cat.tail'];
var pObj = {"prop1":"abc","prop2":{"cat":{"nose":"brown"}}};
var tObj = {"prop1":"abc","prop2":{"cat":{"tail":".*"}}};
var f = fos.makeMatchFn(props, options);
f(pObj, tObj).should.equal(false); // IS INCORRECTLY RETURNING true
});
{ zzz: [ 'a', 'b', 'c' ] }
how can I filter/match those?
zzz.0
zzz.1
zzz.2
?
As a user I would love to be able within the array of nested objects to be able to also have this search through arrays of non primitive types, arrays of more nested objects.
IE. [Object: {name: test, colors: [ Object: {first: blue, second: green} ] } ]
This would be really helpful when having complex array of objects within other objects.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.