This allows to filter and find contributors by the account entries.
For example:
Contributor.filterByAccount({site:'github.com'});// returns an array of all contributors with github account
Contributor.filterByAccount({site:'github.com',username:'bumi'});// returns an array with bumi as single entry
Contributor.findByAccount({site:'github.com',username:'bumi'});// returns the matching contributor
Contributor.findByAccount({site:'github.com'});// returns the matching contributor - the first with a github.com account entry
This allows to filter and find contributors by the account entries.
For example:
```js
Contributor.filterByAccount({site: 'github.com'}); // returns an array of all contributors with github account
Contributor.filterByAccount({site: 'github.com', username: 'bumi'}); // returns an array with bumi as single entry
Contributor.findByAccount({site: 'github.com', username: 'bumi'}); // returns the matching contributor
Contributor.findByAccount({site: 'github.com'}); // returns the matching contributor - the first with a github.com account entry
```
fsmanuel
(Migrated from github.com)
requested changes 2018-04-25 21:53:41 +00:00
fsmanuel
(Migrated from github.com)
left a comment
We should always return an array like the native `filter` does: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
the return value of the find is used in the filter callback. if the callback returns a truthy value the contributor is kept otherwise removed. the return value of filter is returned by the promise.
probably
returnfind(...)!=undefined;
would be better instead of simply returning the return value of the find method.
the return value of the find is used in the filter callback. if the callback returns a truthy value the contributor is kept otherwise removed. the return value of filter is returned by the promise.
probably
```js
return find(...) != undefined;
```
would be better instead of simply returning the return value of the find method.
Why not just have an explicit findBy that actually returns a single contributor? I think it's a common use case to look up one, so checking array length and calling [0] in the client would be a bit clunky anyway.
Why not just have an explicit `findBy` that actually returns a single contributor? I think it's a common use case to look up one, so checking array length and calling `[0]` in the client would be a bit clunky anyway.
not sure if I understand what you mean.
I try to get contributors by their account entry. that's why I filter the contributors array and find in the accounts array for matching entries.
the function is only doing one thing.
a findBy.. would be a filterBy()[0] ?
not sure if I understand what you mean.
I try to get contributors by their account entry. that's why I filter the contributors array and find in the accounts array for matching entries.
the function is only doing one thing.
a `findBy..` would be a `filterBy()[0]` ?
I think both @fsmanuel and I read this as "returns bumi's contributor object", but it actually meant "returns an array with bumi as the only entry". So if it returns the latter, as you explained, then the original comment about it was invalid and just a misunderstanding.
However, what I mean by "why not have a findBy" was: a normal use case in clients is to actually find a single contributor's data. Then, filtering for exact values, implicitly expecting a single-entry array, is a bit clunky.
(I think the "give me all users with GitHub accounts" doesn't currently appear in kredits-web or hubot-kredits, but the "give me the data for @bumi on GitHub" does.)
> Contributor.filterByAccount(...); // returns bumi
I think both @fsmanuel and I read this as "returns bumi's contributor object", but it actually meant "returns an array with bumi as the only entry". So if it returns the latter, as you explained, then the original comment about it was invalid and just a misunderstanding.
However, what I mean by "why not have a findBy" was: a normal use case in clients is to actually find a single contributor's data. Then, filtering for exact values, implicitly expecting a single-entry array, is a bit clunky.
(I think the "give me all users with GitHub accounts" doesn't currently appear in kredits-web or hubot-kredits, but the "give me the data for @bumi on GitHub" does.)
@bumi It's just about the semantics filter vs find. If I see a filter in JS land I expect it to return an array. If I see a find I expect it to return a value or null.
I think your last example:
Contributor.filterByAccount({site:'github.com',username:'bumi'});// returns bumi: { id: 123, ... }
// should be
Contributor.findByAccount({site:'github.com',username:'bumi'});// returns bumi: { id: 123, ... }
We can use a filterByAccount that returns an array in findByAccount that returns a value.
@bumi It's just about the semantics `filter` vs `find`. If I see a `filter` in JS land I expect it to return an array. If I see a `find` I expect it to return a value or `null`.
I think your last example:
```js
Contributor.filterByAccount({site: 'github.com', username: 'bumi'}); // returns bumi: { id: 123, ... }
// should be
Contributor.findByAccount({site: 'github.com', username: 'bumi'}); // returns bumi: { id: 123, ... }
```
We can use a `filterByAccount` that returns an array in `findByAccount` that returns a value.
yes I see. the comment in the PR description is missleading. I've updated that.
The method that this PR adds always returns an array - it returns the return value of the native Array.filter function.
I tried to implement a reusable function to find a single contributor but thought this logic is more flexible and used filter instead of find. (line 41)
should I change it to findByAccount and use find instead of filter?
or add a findByAccont function that returns the fist value of the filterByAccount function?
or refactor it that it is possible to call filter or find?
(in the code right now, with this implementation, I'd use filterByAccount({})[0] )
yes I see. the comment in the PR description is missleading. I've updated that.
The method that this PR adds always returns an array - it returns the return value of the native `Array.filter` function.
I tried to implement a reusable function to find a single contributor but thought this logic is more flexible and used `filter` instead of `find`. (line 41)
should I change it to `findByAccount` and use `find` instead of `filter`?
or add a `findByAccont` function that returns the fist value of the `filterByAccount` function?
or refactor it that it is possible to call filter or find?
(in the code right now, with this implementation, I'd use `filterByAccount({})[0]` )
I'd add a findBy, but actually only call find in that one, and call filter in the filter one. It's not clear that a filter function would include a find, thus restricting results to a single match. If we have both, then filter can filter, and find can find. :)
I'd add a `findBy`, but actually only call find in that one, and call `filter` in the filter one. It's not clear that a `filter` function would include a `find`, thus restricting results to a single match. If we have both, then filter can filter, and find can find. :)
I would replace the `reduce` with an `every` [Docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every)
```js
searchEntries.every((item) => {
const [ key, value ] = item;
return account[key] === value;
});
```
bumi
(Migrated from github.com)
reviewed 2018-04-26 13:36:36 +00:00
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
This allows to filter and find contributors by the account entries.
For example:
We should always return an array like the native
filterdoes: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/filterhow would we do that? because of the promise that is returned by
all()wouldn't we always need to also return a promise?I think he meant the promise should be fulfilled with an array, if it's called
filterinstead offind, so it's not misleading.it is returning the filter, line 41... no?
It looks like it's returning the result of a
find, which would be the first matching object in the filtered array.the return value of the find is used in the filter callback. if the callback returns a truthy value the contributor is kept otherwise removed. the return value of filter is returned by the promise.
probably
would be better instead of simply returning the return value of the find method.
Why not just have an explicit
findBythat actually returns a single contributor? I think it's a common use case to look up one, so checking array length and calling[0]in the client would be a bit clunky anyway.because this way you can filter and not only get one single contributor.
Yes, but you can have both
filterByandfindBy. Why only have one function to do it all?not sure if I understand what you mean.
I try to get contributors by their account entry. that's why I filter the contributors array and find in the accounts array for matching entries.
the function is only doing one thing.
a
findBy..would be afilterBy()[0]?I think both @fsmanuel and I read this as "returns bumi's contributor object", but it actually meant "returns an array with bumi as the only entry". So if it returns the latter, as you explained, then the original comment about it was invalid and just a misunderstanding.
However, what I mean by "why not have a findBy" was: a normal use case in clients is to actually find a single contributor's data. Then, filtering for exact values, implicitly expecting a single-entry array, is a bit clunky.
(I think the "give me all users with GitHub accounts" doesn't currently appear in kredits-web or hubot-kredits, but the "give me the data for @bumi on GitHub" does.)
@bumi It's just about the semantics
filtervsfind. If I see afilterin JS land I expect it to return an array. If I see afindI expect it to return a value ornull.I think your last example:
We can use a
filterByAccountthat returns an array infindByAccountthat returns a value.yes I see. the comment in the PR description is missleading. I've updated that.
The method that this PR adds always returns an array - it returns the return value of the native
Array.filterfunction.I tried to implement a reusable function to find a single contributor but thought this logic is more flexible and used
filterinstead offind. (line 41)should I change it to
findByAccountand usefindinstead offilter?or add a
findByAccontfunction that returns the fist value of thefilterByAccountfunction?or refactor it that it is possible to call filter or find?
(in the code right now, with this implementation, I'd use
filterByAccount({})[0])I'd add a
findBy, but actually only call find in that one, and callfilterin the filter one. It's not clear that afilterfunction would include afind, thus restricting results to a single match. If we have both, then filter can filter, and find can find. :)what do you think of this now?
Nice! 👏
@@ -37,0 +42,4 @@return this._byAccount(search, 'find');}_byAccount(search, method = 'filter') {That's a master piece of functional programming 😀
I would replace the
reducewith aneveryDocsoh, reads nice. thanks.
♥️