1.0.3 • Published 1 year ago

babel-plugin-transforms-undefined-object-destructuring v1.0.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

babel-plugin-transforms-undefined-object-destructuring

Automatically converts the destructuring object to an empty object ({}) when the destructuring object is undefined.

Install

Using npm:

npm install --save-dev babel-plugin-transforms-undefined-object-destructuring

or using yarn:

yarn add babel-plugin-transforms-undefined-object-destructuring --dev

Examples

For objects

In

function test(params) {
  const { anyKeyName, ...restKeys } = params;
}

test();

Out

var _excluded = ["anyKeyName"];
function _objectWithoutProperties(source, excluded) { ... }
function _objectWithoutPropertiesLoose(source, excluded) { ... }
function test(params) {
    var _ref = params || {},
        anyKeyName = _ref.anyKeyName,
        restKeys = _objectWithoutProperties(_ref, _excluded);
}
test();

For arrays

In

function test(params) {
  const [anyKeyName, ...restKeys] = params;
}

test();

Out

function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
function _nonIterableRest() { ... }
function _unsupportedIterableToArray(o, minLen) { ... }
function _arrayLikeToArray(arr, len) { ... }
function _iterableToArray(iter) { ... }
function _arrayWithHoles(arr) { ... }
function test(params) {
  var _ref = params || [],
    _ref2 = _toArray(_ref),
    anyKeyName = _ref2[0],
    restKeys = _ref2.slice(1);
}
test();

Usage

With a configuration file (Recommended)

{
  "plugins": ["babel-plugin-transforms-undefined-object-destructuring"]
}

Via CLI

babel --plugins babel-plugin-transforms-undefined-object-destructuring script.js

Via Node API

require("@babel/core").transformSync("code", {
  plugins: ["babel-plugin-transforms-undefined-object-destructuring"],
});
1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago