Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

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

install

npm i constant-keys

Downloadsweekly downloads

4

version

0.0.2

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability