Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

matches #5

Open
tjmehta opened this issue Sep 19, 2014 · 8 comments
Open

matches #5

tjmehta opened this issue Sep 19, 2014 · 8 comments

Comments

@tjmehta
Copy link
Owner

tjmehta commented Sep 19, 2014

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

@tristaaan
Copy link
Contributor

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
Copy link
Contributor

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

@tjmehta
Copy link
Owner Author

tjmehta commented Feb 11, 2015

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 ]

@stoeffel
Copy link
Contributor

matches sounds nice. 👍

@tristaaan tristaaan mentioned this issue Feb 11, 2015
@tristaaan
Copy link
Contributor

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.

@tjmehta
Copy link
Owner Author

tjmehta commented Feb 15, 2015

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
Copy link
Owner Author

tjmehta commented Feb 15, 2015

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

@tristaaan
Copy link
Contributor

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

@tjmehta tjmehta changed the title add a match function match Dec 11, 2015
@tjmehta tjmehta changed the title match matches Jun 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants