https://github.com/strml/keymirror
Simple util to create an object with its keys mirrored as values. Standalone port of react/lib/keyMirror.
https://github.com/strml/keymirror
Last synced: 10 months ago
JSON representation
Simple util to create an object with its keys mirrored as values. Standalone port of react/lib/keyMirror.
- Host: GitHub
- URL: https://github.com/strml/keymirror
- Owner: STRML
- Created: 2014-06-04T13:58:59.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2022-05-09T11:33:45.000Z (almost 4 years ago)
- Last Synced: 2025-05-15T23:05:06.838Z (10 months ago)
- Language: JavaScript
- Homepage:
- Size: 211 KB
- Stars: 382
- Watchers: 11
- Forks: 45
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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.
Why?
----
From [this discussion](https://github.com/facebook/react/issues/1639#issuecomment-45188026):
> The main purpose of keyMirror is to deal with the fact that Closure Compiler advanced mode crushes keys, which allows you to write code like
> `keyMirror({monkey: null, gorilla: null})`
> and have it become something like
> `k({m:null,g:null})`
> which evaluates to
> `{m:"m",g:"g"}`
> at runtime. If it was specified as a list of strings, they wouldn't get crushed matching the property names.
Usage
-----
`npm install keymirror`
```javascript
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:
```javascript
var _ = require('lodash');
_.mixin({keyMirror: require('keymirror')});
// Can now be used as _.keyMirror(object)
```