Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule keyMirror
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
var invariant = require('invariant');
16
17
/**
18
* Constructs an enumeration with keys equal to their value.
19
*
20
* For example:
21
*
22
* var COLORS = keyMirror({blue: null, red: null});
23
* var myColor = COLORS.blue;
24
* var isColorValid = !!COLORS[myColor];
25
*
26
* The last line could not be performed if the values of the generated enum were
27
* not equal to their keys.
28
*
29
* Input: {key1: val1, key2: val2}
30
* Output: {key1: key1, key2: key2}
31
*
32
* @param {object} obj
33
* @return {object}
34
*/
35
var keyMirror = function(obj) {
36
var ret = {};
37
var key;
38
invariant(
39
obj instanceof Object && !Array.isArray(obj),
40
'keyMirror(...): Argument must be an object.'
41
);
42
for (key in obj) {
43
if (!obj.hasOwnProperty(key)) {
44
continue;
45
}
46
ret[key] = key;
47
}
48
return ret;
49
};
50
51
module.exports = keyMirror;
52
53