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

FirstOrDefault #20

Closed
NiallArkEnergy opened this issue Dec 12, 2016 · 3 comments
Closed

FirstOrDefault #20

NiallArkEnergy opened this issue Dec 12, 2016 · 3 comments

Comments

@NiallArkEnergy
Copy link

FirstOrDefault doesn't appear to work.
Enumerable.from([]).firstOrDefault(15) // null

@M0ns1gn0r
Copy link
Collaborator

Yep, its current implementation is broken. It doesn't expect that instead of a predicate, a default value can be passed as the first argument:

Enumerable.prototype.firstOrDefault = function (predicate, defaultValue) {
    if (defaultValue === undefined) defaultValue = null;
    if (predicate != null) return this.where(predicate).firstOrDefault(null, defaultValue);
    ...

I would fix it that way:

Enumerable.prototype.firstOrDefault = function (predicate, defaultValue) {
    if (predicate) {
        if (typeof predicate === Types.Function)
             return this.where(predicate).firstOrDefault(null, defaultValue);
        defaultValue = predicate;
    }
    defaultValue = defaultValue || null;
    ...

@NiallArkEnergy
Copy link
Author

I like the change. Sorry I was an idiot and using the function incorrectly. I'm too used to javascript libraries doing the heavy lifting of checking input and adapting. C# isn't nearly as adaptable and this library is emulating C#

@M0ns1gn0r
Copy link
Collaborator

Don't blame yourself, the authors of linq.js changed the signature at some moment and introduced this breaking change. See http://neue.cc/reference.htm, here your code sample works as expected.

@mihaifm mihaifm closed this as completed in 6d75e23 Feb 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants