Code Monkey home page Code Monkey logo

babel-plugin-transform-safely's Introduction

babel-plugin-transform-safely

This babel plugin will transform expressions that use a safely keyword/call to produce safe property access and modification.

Installation

$ npm install babel-plugin-transform-safely

Usage

The basic format of using the transform is to write object-checked, safe expressions (existential property access) in the form:

safely(expression)

We can then access properties on variables that may be set to null or undefined, and they won't error out. For example:

safely(object.subObject.subProperty) // will check for each object's existence before accessing property

Will be rewritten to:

var _object
(_object = object == null ? void 0 : object.subObject) == null ? void 0 : _object.subProperty

So if object or subObject is not an object, the entire expression will return undefined (without an error).

And we can assign properties to objects thay may not exist yet, and they will be created:

safely(object.subObject.subProperty = 4) // will create the any missing objects in order to assign property

If object or subObject are not objects, they will be assigned an object.

And we can make function or method calls on functions may or may not exist as well:

safely(object.method(args)) // will only call if method exists

Again, this will return undefined if the method doesn't exist.

If you use a numeric index or the push or unshift method, the code will be transformed to create an array as necessary (rather than just doing an existence check). For example:

object.arrayProperty.push('hi')

If arrayProperty is not defined, an array will be created (and push called on it).

And of course you can combine any permutation of the above (with any other valid expression or operator):

safely(empty.b.c = object[a]() || object[b](something.c.d))

Transform Usage

Via .babelrc (Recommended)

.babelrc

{
  "plugins": ["transform-safely"]
}

Via CLI

$ babel --plugins transform-safely

Via Node API

require("babel-core").transform("code", {
  plugins: ["transform-safely"]
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.