key-mirror.ts 🪞
July 23, 2020•139 words
A quick function to construct an enumeration which has keys the same as their value
This used to be part of React, but since it's now been removed (See commit: #56f5115), and some older packages throw an error without it, I've updated it, and uploading in case anyone else needs it while working with older React packages. A copy of React's original, JavaScript version can be found here.
/**
* (C) Alicia Sykes <https://aliciasykes.com>
* Licensed under MIT X11: https://git.io/Jew4i
*
* Constructs an enumeration with keys equal to their value.
* @param {object} obj
* @return {object}
*/
export function keyMirror(originObj: object) {
if (typeof originObj !== 'object')
throw new Error('keMirror(...): Argument must be an object');
const obj: any = {};
for (const key in originObj) {
if (originObj.hasOwnProperty(key)) obj[key] = key;
}
return obj
}