object-key-mirror
Version:
Object key mirror is a zero-dependency Node.js module that lets you easily create objects with values equal to their key names.
29 lines (27 loc) • 842 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
/**
* takes in an array and returns an object with
* keys and values equal to the items in the array.
*
* @param {object} arr - An array of primitive types
*
* @returns {object} - an object with keys and values equal to the items in the parameter supplied.
*/
exports.default = function (arr) {
try {
if (arr === undefined) {
throw new Error("objectKeyMirror: Error! Please supply and argument.");
} else if (!Array.isArray(arr)) {
throw new Error("objectKeyMirror: Error! Argument must be an array. " + arr + " is not an array.");
}
return arr.reduce(function (mirroredObject, token) {
mirroredObject[token] = "" + token;
return mirroredObject;
}, {});
} catch (err) {
console.error(err.message);
}
};