Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ Input: `{key1: val1, key2: val2}`

Output: `{key1: key1, key2: key2}`

You can specify the prefix for all values:

```javascript
keyMirror({
OPEN: null,
CLOSE: null
}, 'WINDOW_');
```

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

```javascript
Expand Down
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@
* @param {object} obj
* @return {object}
*/
var keyMirror = function(obj) {
var keyMirror = function(obj, prefix) {
prefix = prefix || '';
var ret = {};
var key;
if (!(obj instanceof Object && !Array.isArray(obj))) {
throw new Error('keyMirror(...): Argument must be an object.');
}
for (key in obj) {
if (obj.hasOwnProperty(key)) {
ret[key] = key;
ret[key] = prefix + key;
}
}
return ret;
Expand Down