0.0.2 • Published 9 years ago

constant-keys v0.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
9 years ago

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
    }
  }
}
0.0.2

9 years ago

0.0.1

9 years ago