mirror-creator
Another way to create an object with values equal to its key names
Last updated 5 years ago by alex.fedoseev .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mirror-creator 
SYNC missed versions from official npm registry.

mirror-creator

One more way to create an object with values equal to its key names.

npm version build status dependencies status license

Install

Get it via npm:

npm install --save mirror-creator

Usage

// actionTypes.js
import mirrorCreator from 'mirror-creator';

export default mirrorCreator([
  'SOME_ACTION_TYPE',
  'ANOTHER_ACTION_TYPE',
  'ONE_MORE_ACTION_TYPE',
]);

// Or you can specify prefix to avoid type name conflicts
export default mirrorCreator([
  'SOME_ACTION_TYPE',
  'ANOTHER_ACTION_TYPE',
  'ONE_MORE_ACTION_TYPE',
], { prefix: 'mydomain/' });

// actionCreator.js
import actionTypes from 'actionTypes';

dispatch({ type: actionTypes.SOME_ACTION_TYPE });

Pros: DRY & clean.
Cons: If you use Closure Compiler advanced mode for JS minification — keys are not optimized. Use keyMirror instead. Details.

Why

Use it if you don't like native or keyMirror ways of defining constants in flux / redux apps.

Native way:

// actionTypes.js
export const SOME_ACTION_TYPE     = 'SOME_ACTION_TYPE';
export const ANOTHER_ACTION_TYPE  = 'ANOTHER_ACTION_TYPE';
export const ONE_MORE_ACTION_TYPE = 'ONE_MORE_ACTION_TYPE';

// actionCreator.js
import * as actionTypes from 'actionTypes';

dispatch({ type: actionTypes.SOME_ACTION_TYPE });

Pros: No third-party lib required.
Cons: Duplications. Noisy.

keyMirror way:

// actionTypes.js
import keyMirror from 'keyMirror';

export default keyMirror({
  SOME_ACTION_TYPE    : null,
  ANOTHER_ACTION_TYPE : null,
  ONE_MORE_ACTION_TYPE: null,
});

// actionCreator.js
import actionTypes from 'actionTypes';

dispatch({ type: actionTypes.SOME_ACTION_TYPE });

Pros: DRY. If Closure Compiler advanced mode is used — keys are optimized.
Cons: null noise.

Current Tags

  • 1.1.0                                ...           latest (5 years ago)

3 Versions

  • 1.1.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.0.1                                ...           5 years ago

Copyright 2014 - 2016 © taobao.org |