101
101 copied to clipboard
matches
thoughts: accept an object that takes keypaths and regexp values too.
Should this return a list of objects that match(1), a list of values from the objects that match(2), or a new object from the matcher(3)?
var sampleObject = {a: 1, B: 2, C: 3, d: 4}
match(sampleObject, new RegExp('[A-Z]'));
=> [{B: 2}, {C: 3}] //1
=> [2, 3] //2
=> {B:2, C:3} //3
I'm leaning towards 2 or 3, maybe specify as a third parameter?
match (obj, matcher, valuesOnly){...}
Nevermind, just saw that I was describing pick
with regex.
Example:
// support strings
[ 'a', 'aa', 'b' ].map(matches(/a/)); // [ true, true, false ]
// support objects
[ { foo: 'a' }, { foo: 'aa' }, { foo: 'b' } ].map(matches({
foo: /^a/
})); // [ true, true, false ]
matches
sounds nice. :+1:
I have something that works nicely but waiting for #55 to be merged. I could add it the request if it's alright to have added all at once.
Further thoughts:
// support strings
[ 'a', 'aa', 'b' ].map(matches(/^a/)); // [ true, true, false ]
// is equivalent to
var re = /^a/;
[ 'a', 'aa', 'b' ].map(re.test.bind(re)); // [ true, true, false ]
Also, maybe this object behavior may work well as an overloaded has-keypaths
After your comments (esp, "Further thoughts") I've had second thoughts about a full matches
. I'm going to try working it in with has-keypaths
.
var obj = {foo: '1', bar: 2};
hasKeypaths(obj, {foo: /\d/}); // true
hasKeypaths(obj, {bar: /\d/}); // true