constant-keys

0.0.2 • Public • Published

constant-keys

Utility function that takes an array of strings and returns an object with the array values as keys, and each value being the same as a key. Very useful for using as action or event name constants in something like React.

Example:

import constantKeys from 'constant-keys';
 
const actions = constantKeys(['ADD_TODO', 'DELETE_TODO']);

'actions' will now contain:

{
  ADD_TODO: 'ADD_TODO',
  DELETE_TODO: 'DELETE_TODO'
}

Requirements

All code is ES6/2015 so you'll need to compile with someting like Babel.

Installation

npm install constant-keys

Example

This is how you might go about using this with something like react / flux:

// constants/todo-constants.js
import constantKeys from 'constant-keys';
 
export default constantKeys(['ADD_TODO', 'DELETE_TODO']);
 
// actions/todo-actions.js
import types from '../constants/action-constants';
import dispather from '../dispatcher/dispatcher';
 
export default {
  createTodo (title) {
    dispatcher.dispatch {
      type: types.ADD_TODO,
      title
    }
  }
}

Dependencies (2)

Dev Dependencies (2)

Package Sidebar

Install

npm i constant-keys

Weekly Downloads

1

Version

0.0.2

License

ISC

Last publish

Collaborators

  • robinmbarnes