101 icon indicating copy to clipboard operation
101 copied to clipboard

matches

Open tjmehta opened this issue 10 years ago • 8 comments

thoughts: accept an object that takes keypaths and regexp values too.

tjmehta avatar Sep 19 '14 19:09 tjmehta

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){...}

tristaaan avatar Feb 04 '15 17:02 tristaaan

Nevermind, just saw that I was describing pick with regex.

tristaaan avatar Feb 09 '15 22:02 tristaaan

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 ]

tjmehta avatar Feb 11 '15 08:02 tjmehta

matches sounds nice. :+1:

stoeffel avatar Feb 11 '15 08:02 stoeffel

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.

tristaaan avatar Feb 15 '15 04:02 tristaaan

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 ]

tjmehta avatar Feb 15 '15 05:02 tjmehta

Also, maybe this object behavior may work well as an overloaded has-keypaths

tjmehta avatar Feb 15 '15 07:02 tjmehta

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

tristaaan avatar Feb 20 '15 00:02 tristaaan