Comments (5)
@williamtran29 The are indeed very similar. Here are the differences that I can come up with (having never used _.get
before):
_.get
returnsundefined
for unresolved values, whereasidx
preservesnull
where applicable._.get
supports a default value, whereasidx
does not. This is becauseidx
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.
_.get
supports a default value, whereasidx
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.
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.
@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.
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)
- Deprecate? HOT 3
- [TS] v2.5.0 DeepRequired generic breaks input.focus() HOT 3
- Consider to support nullable type in `idx` first argument
- Replace `NonUndefinedOrnNull` with TypeScript standard type `NonNullable` HOT 1
- discussion for setIn implementation? HOT 1
- Typescript types do not work on undefined as first parameter HOT 5
- Upgrade to latest version of Flow HOT 3
- Typescript types do not work on undefined as first parameter (new test case) HOT 4
- Change return value from `T2 | null | undefined` to `T2 | null` and `T2 | undefined`? HOT 1
- Doesn't work with TS compiler HOT 3
- Add support for Map.get in .ts files HOT 2
- Cannot find name 'bigint' HOT 6
- `UnboxDeepRequired doesn't support enum type. HOT 1
- CI is not running test for PRs HOT 1
- Outdated CHANGELOG HOT 1
- New release? HOT 1
- @babel/typescript support HOT 3
- Merge in the macro? HOT 1
- DeepRequiredObject causing issues in Typescript 3.5.1 HOT 2
- Accessing Data on Void HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from idx.