Coder Social home page Coder Social logo

Compare this with Lodash.get about idx HOT 5 CLOSED

facebook avatar facebook commented on April 28, 2024 2
Compare this with Lodash.get

from idx.

Comments (5)

yungsters avatar yungsters commented on April 28, 2024 10

@williamtran29 The are indeed very similar. Here are the differences that I can come up with (having never used _.get before):

  • _.get returns undefined for unresolved values, whereas idx preserves null where applicable.
  • _.get supports a default value, whereas idx does not. This is because idx is an intermediate solution until an operator is added to ECMAScript (which does not currently propose a default value mechanism).
  • idx ships with a Flow definition for type safety (e.g. so that the return type is preserved and so that you can access properties on a nullable object, but only if those properties are otherwise valid on the object).
  • babel-plugin-idx allows stripping most of the runtime performance implications of the function invocation and intermediate value checks.

I hope this is helpful. Thanks for the question!

from idx.

Daniel15 avatar Daniel15 commented on April 28, 2024 6

_.get supports a default value, whereas idx does not.

I think this is a non-issue as you can just do idx(foo, x => x.bar.baz) || defaultValue, which also ensures the default value is lazily computed if it's something expensive (imagine something like (idx(foo, x => x.bar.baz) || someExpensiveFunction())

from idx.

brettjashford avatar brettjashford commented on April 28, 2024

one thing to keep in mind about the default value (3rd parameter) to lodash.get is that it's only used if the expression evaluates to undefined, if it's null the default value will not be used, which is not what i want 99% of the time.

anyone migrating from lodash.get to idx may find this codemod that i wrote useful https://github.com/brettjashford/codemods

from idx.

Daniel15 avatar Daniel15 commented on April 28, 2024

@brettjashford For what it's worth, the optional chaining proposal just reached stage 3 recently, so in the near future I think that'll totally replace idx.

// Old
idx(foo, x => x.bar.baz)

// New
x?.bar?.baz

Babel supports it, and TypeScript support is coming.

However, do note that there's subtle differences between _.get, idx and optional chanining around handling of null and undefined, so definitely test thoroughly when converting between them. For example, idx(foo, x => x.bar.baz) will return null if x.bar is null or undefined if x.bar is undefined, whereas the optional chaining operator will return undefined in both cases.

from idx.

brettjashford avatar brettjashford commented on April 28, 2024

interesting, thanks for pointing that out!

i'm eagerly awaiting typescript 3.7 with optional chaining support. planning to write an idx -> optional chaining codemod then, although it probably won't take into account the null vs undefined differences you mentioned.

from idx.

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.