GithubHelp home page GithubHelp logo

Comments (15)

atomiks avatar atomiks commented on April 28, 2024 2

Also as it stands, it's quite useless because it takes longer with more overhead than just doing || d.

If we just don't want undefined, null, or NaN, but DO want to use the other falsy values:

const valueOrDefault = (value, d) => 
  (value || [false,0,''].includes(value)) ? value : d

from 30-seconds-of-code.

skatcat31 avatar skatcat31 commented on April 28, 2024 1

also many prior arts call it defaultTo and make it a closure so you can use it in many similar cases:

const defaultTo = d => v => v === undefined ?  d : v

More robust case with optional param for allowing you to tell it to use some falsey values

const defaulTo = (default, ...allowed) => value =>  value === undefined || !allowed.includes(value) ?  default : value

I'm a little tired ATM so someone may want to check my allowed version

from 30-seconds-of-code.

Chalarangelo avatar Chalarangelo commented on April 28, 2024 1

It is kind of weird and messes up falsey values. We either remove it entirely or refactor it into something useful. As it stands it's a bit of a mess and it's my fault for merging it.

@skatcat31 might be onto something with the first snippet in the last comment, the second one is a bit too excessive in my opinion.

from 30-seconds-of-code.

prodigic avatar prodigic commented on April 28, 2024 1

yeah it does have a different goal really, I'll PR it as a separate snippet

from 30-seconds-of-code.

skatcat31 avatar skatcat31 commented on April 28, 2024 1

valueOrDefault is kind of a weird function, I'll agree. coalesce is more of a first valid which is really nice, and extremely useful in promise race conditions.

from 30-seconds-of-code.

skatcat31 avatar skatcat31 commented on April 28, 2024

I agree that valueOrDefault is kind of... weird. You have many other tools to accomplish it and should really only be checking for undefined(null in some of these cases is considered a specific value you'd want, same with NaN) so that only if something didn't return something when it should have would you need a default. If it returned null, and you wanted null you can't have it with this version.

from 30-seconds-of-code.

prodigic avatar prodigic commented on April 28, 2024

i like it as coalesce similar to the SQL use

const coalesce = (...args) => args.find(_ => ![NaN,undefined,null].includes(_))

or closed over a set of excluded values

const coalesce = (...excludes) => (...args) => args.find(_ => !excludes.includes(_))

from 30-seconds-of-code.

Chalarangelo avatar Chalarangelo commented on April 28, 2024

Can you provide a quick example of how this works @prodigic? It might be worthwhile to add it in.

from 30-seconds-of-code.

skatcat31 avatar skatcat31 commented on April 28, 2024

@Chalarangelo I agree that the second one is a bit to excessive. It was meant to be a more in depth example of something that behaved a little more like a whitelist. Realistically you should only ever default if undefined since undefined is not a lack of data, but a lack of reference:

  • undefined : no reference, cannot define
  • null : specifically a lack of value at reference
  • NaN : it's Not a Number, this could be a valid value as it's at the reference
  • false : a Boolean. In this case it's a value at the reference
  • 0 : a Number. In this case it's a value at the reference
  • '' : A String. In this case it's a value at the reference

So I think the first one is a little bit more along the lines of what the function describes it doing

If a value does not exist, it will return the default

Contexts are weird man

from 30-seconds-of-code.

prodigic avatar prodigic commented on April 28, 2024
coalesce(myNickName, myFullName, "");

would return the first value not filtered from the args, so if myNickName===null it return myFullName moving along the parameters til it finds the first item that passes the excludes test.

kind of works like this https://stackoverflow.com/questions/18528468/what-is-the-difference-bewteen-ifnull-and-coalesce-in-mysql except with a broader definition of excluded values.

const coalesce = (...args) => args.find(_ => ![undefined,null].includes(_))
const coalesceFactory = (...excludes = [null,undefined]) => (...args) => args.find(_ => !excludes.includes(_))

taking @skatcat31's observation and defaulting to skipping null/undefined by defualt.

from 30-seconds-of-code.

skatcat31 avatar skatcat31 commented on April 28, 2024

@prodigic It's a cool method, but I think it's a little different than if one does not exist. coalesce is more of a filter function isn't it?

from 30-seconds-of-code.

skatcat31 avatar skatcat31 commented on April 28, 2024

@prodigic one last question... are you sure you want to rest and then spread it, or do it against a collection(iterables should be okay) without a rest input so that it isn't something you always have to spread too or collect for, just use an array?

from 30-seconds-of-code.

Chalarangelo avatar Chalarangelo commented on April 28, 2024

I think we should remove value or default and stick with coalesce. It's a more robust snippet with a clear purpose and the current value or default is a little problematic. Anyone against this?

from 30-seconds-of-code.

Chalarangelo avatar Chalarangelo commented on April 28, 2024

I am closing the issue for now as it is not about valueOrDefault anymore. The snippet will be removed as it's not really useful right now. If anyone wants to add a coalesce function, I'd be ok with that.

from 30-seconds-of-code.

lock avatar lock commented on April 28, 2024

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for any follow-up tasks.

from 30-seconds-of-code.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.