Resolve the problem of the default value only trigged by its value is undefined in destructuring, Reduce time spent communicating with backends. (中文文档)
$ npm i babel-plugin-destructuring-with-null -D
const { name = 'tom' } = null
const { list: [, first = 'foo', second = 'bar'] } = { list: [false, null, undefined] };
↓ ↓ ↓ ↓ ↓ ↓
const _ref = null,
name = _destructuring_with_null(_ref, 'name', 'tom'); // name === 'tom'
const _list = {list: [false, null, undefined]},
_list$list = _slicedToArray(_list.list, 3),
first = _destructuring_with_null(_list$list, '1', 'foo'), // first === 'foo',
second = _destructuring_with_null(_list$list, '2', 'bar');// second === 'bar';
.babelrc
{
"plugins": [
"babel-plugin-destructuring-with-null"
]
}
Because ES6 destructuring default values only kick in if the value is undefined. null, false and 0 are all still values. But other languages don't has undefined this thing, null means does not exist. The purpose of this plugin is to avoid multiple judgments of the deconstructed object is null before destructuring.
Copyright (c) 2017-present, Army-U. Released under the MIT License.