Skip to content

kentor/babel-plugin-symbol-to-string

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Symbol to String Build Status npm

A Babel plugin to convert assigned symbols to strings.

Note: Babel 6 only.

Use Cases

Redux

Very useful for reducing boilerplate when creating action type constants.

In:

// TodoActions.js
export const CREATE = Symbol();
export const CREATE_SUCCESS = Symbol();
export const CREATE_FAILURE = Symbol();

Out (something like this):

// TodoActions.js
export const CREATE = 'TodoActions.CREATE';
export const CREATE_SUCCESS = 'TodoActions.CREATE_SUCCESS';
export const CREATE_FAILURE = 'TodoActions.CREATE_FAILURE';

Then use these constants in a reducer:

// TodoReducer.js
import * as TodoActions from '../actions/TodoActions';

export function TodoReducer(state, action) {
  switch (action) {
  case TodoActions.CREATE:
    // Handle create
    break;
  case TodoActions.CREATE_SUCCESS:
    // Handle create success
    break;
  case TodoActions.CREATE_FAILURE:
    // Handle create failure
    break;
  default:
    return state;
  }
}

Installation

npm install -D babel-plugin-symbol-to-string

.babelrc:

{
  "plugins": ["symbol-to-string"]
}

About

A Babel plugin to convert assigned symbols to strings.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published