-
Notifications
You must be signed in to change notification settings - Fork 634
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
Should findSingle
has the default predicate?
#1224
Comments
@kt3k yes there is no need to add this default selector of |
That is a very opinionated statement without stating reasons^^ Keep in mind that the default selector has been dorectly copied from the inspired module - which seems to see a use case for it (one I have used amd encountered myself several times). I do not see how rempving it adds any value right now - at least not without actually talking about why. |
@LionC thanks for feedback, let's take this example and discuss: const bookings = [
{ month: 'January', active: false },
{ month: 'March', active: false },
{ month: 'June', active: true },
];
const dd = findSingle(bookings); It will return
even if we pass one value like const bookings = ["January", "March", "June"];
const dd = findSingle(bookings); it does not make sense @kt3k any feedback ? |
The default selector (predicate) can be used for getting the single element from a signleton array const bookings = [{ month: 'January', active: false }];
const item = findSingle(bookings);
console.log(item);
// => { month: "January", active: false } This might be useful sometimes in my view, but there was an argument that if we do this, the name He suggests we should use |
findSingle
has the default selector?findSingle
has the default predicate?
isn't const bookings = [{ month: 'January', active: false }];
const item = findSingle(bookings);
console.log(item);
// => { month: "January", active: false } same as native javascript if the method was something like this: // Returns an random element. the return value matches the given condition if provieded.
declare function random<T>(
array: readonly T[],
predicate?: (el: T) => boolean,
): T | undefined the default value would make sense to me. |
Just for completeness here the reasons why I find we should remove the default predicate
findSingle([]) // returns undefined
findSingle([undefined]) // returns undefined
findSingle([undefined, undefined]) // returns undefined |
We named the function with the default predicate being there - using the name as an argument afterwards seems very circular.
I think this is a discussion about personal style and preference. Neither decision will have a lot of impact. I am not sure if this is important enough to introduce a breaking change to a function right after it was released though @kt3k I don't think there is a lot to add from my side here, I will leave the decision to the std authorities and focus on adding some more things. |
I feel the community is slightly in favor of removing the default predicate (timreichen, zhangyuannie, getspooky, majidsajadi seem in favor). So let's merge #1232 |
@kt3k I think this issue can be closed since the PR got merged. |
findSingle
incollections
module has the defaultselectorpredicate of(_) => true
. Does this make sense? What's the use case?context of this discussion: #1166 (comment)
The text was updated successfully, but these errors were encountered: