JSPM

keymirror

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 213869
  • Score
    100M100P100Q188745F

A simple utility for creating an object with values equal to its keys. Identical to react/lib/keyMirror

Package Exports

  • keymirror

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (keymirror) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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)