Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Change Lodash `get` functions to ES7 optional member expressions

NotificationsYou must be signed in to change notification settings

eschaefer/codemod-get-to-optional-member-expressions

Repository files navigation

This repository contains a codemod script for use withJSCodeshift.

Overview

The codemod replaces calls to Lodash'sget method with the nativeoptional chaining proposal for ECMAScript. This is a novel new way to reliably access deeply nested object properties, and can be used now with@babel/plugin-proposal-optional-chaining. It is important to note that this will currently only work withBabel 7. If you are using Babel 6.x, then you will need to update your project to Babel 7 to use this codemod.

constfoo=get(data,'crate.box.present.wrapping.color');constbar=get(data,'crate.box.present.wrapping.color','green');// 👇 Becomes 👇constfoo=data?.crate?.box?.present?.wrapping?.color;constbar=data?.crate?.box?.present?.wrapping?.color||'green';

Setup & Run

npm install -g jscodeshiftgit clone https://github.com/eschaefer/codemod-get-to-optional-member-expressions.gitcd codemod-get-to-optional-member-expressions/jscodeshift -t ./lodash-get-to-optional-member-expressions.js<file(s)>

Use the-d option for a dry-run and use-p to print the output forcomparison.

Run tests for this codemod

npm runtest

TODO

  • Add support forrequire;
  • Add support for string literal member expressions
  • Add support for array paths

About

Change Lodash `get` functions to ES7 optional member expressions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp