Skip to content
forked from STRML/keyMirror

Simple util to create an object with its keys mirrored as values. Standalone port of react/lib/keyMirror.

Notifications You must be signed in to change notification settings

vizath/keyMirror

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

KeyMirror

Create an object with values equal to its key names.

I thought react/lib/keyMirror was useful and wanted to reuse it without any dependencies.

This is not my code, this is property of Facebook.

Usage

npm install keymirror

var keyMirror = require('keyMirror');
var COLORS = keyMirror({blue: null, red: null});
var myColor = COLORS.blue;
var isColorValid = !!COLORS[myColor];

The last line could not be performed if the values of the generated enum were not equal to their keys.

Input: {key1: val1, key2: val2}

Output: {key1: key1, key2: key2}

I sometimes use this with lodash - use the following upon your first use of lodash to mix it in:

var _ = require('lodash');
_.mixin({keyMirror: require('keyMirror')});
// Can now be used as _.keyMirror(object)

Fork reason

Allow nested declarations. This allow you to have nice constants nesting.

var keyMirror = require('keyMirror');
var COLORS = keyMirror({
    blue: null,
    red: null,
    CYMK: {
        cyan:null,
        yellow:null
    }
});
var myColor = COLORS.blue;
var isColorValid = !!COLORS[myColor];
COLORS.CYMK.cyan === 'CYMK_cyan';

About

Simple util to create an object with its keys mirrored as values. Standalone port of react/lib/keyMirror.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%