babel-plugin-transform-object-from-destructuring

1.0.3 • Public • Published

babel-plugin-transform-object-from-destructuring

Babel plugin that allows you to extract an object/array from a destructuring expression.

Installation

$ npm install --save-dev babel-plugin-transform-object-from-destructuring

Why do I need this?

It can be very exhausting when you create an object from a destructuring expression, especially when you have to rename, add or remove a property. With this plugin maintaining such scenarios gets much more easier for you.

Object example

let myObject = {
  test1: "stringTest1",
  test2: "stringTest2",
  test3: "stringTest3"
};
let { test1, test3 } = myObject,
  myTest = { test1, test3 };

With this plugin activated you can simply write:

let myObject = {
  test1: "stringTest1",
  test2: "stringTest2",
  test3: "stringTest3"
};
let myTest = { test1, test3 } = myObject;

Array example

It also works great with arrays:

let myArray = ["stringTest1", "stringTest2", "stringTest3"];
let [ test1, , test3 ] = myArray,
  myTest = [ test1, test3 ];

Can be written as follow:

let myArray = ["stringTest1", "stringTest2", "stringTest3"];
let myTest = [ test1, , test3 ] = myArray;

Note: You are not allowed to use a rest spread operator: let myTest = { test1, ...rest } = myObject.

Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["transform-object-from-destructuring"]
}

Via CLI

$ babel --plugins transform-object-from-destructuring script.js

Via Node API

require('babel').transform('code', {
  plugins: ['transform-object-from-destructuring']
});

Package Sidebar

Install

npm i babel-plugin-transform-object-from-destructuring

Weekly Downloads

6

Version

1.0.3

License

MIT

Unpacked Size

15.4 kB

Total Files

7

Last publish

Collaborators

  • matzeeable