Skip to content

Commit c224bfe

Browse files
authored
Rename index object keys with alias and keeping it's positions
1 parent 0945da5 commit c224bfe

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

lib/utils.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,29 @@ exports.each = function(arr, fn) {
964964
}
965965
};
966966

967+
/**
968+
* Rename an object key, while preserving its position in the object
969+
*
970+
* @param {Object} oldObj
971+
* @param {String|Number} oldKey
972+
* @param {String|Number} newKey
973+
* @api private
974+
*/
975+
exports.renameObjKey = function (oldObj, oldKey, newKey) {
976+
const keys = Object.keys(oldObj);
977+
return keys.reduce(
978+
(acc, val) => {
979+
if (val === oldKey) {
980+
acc[newKey] = oldObj[oldKey];
981+
} else {
982+
acc[val] = oldObj[val];
983+
}
984+
return acc;
985+
},
986+
{} as Record<string, unknown>
987+
);
988+
}
989+
967990
/*!
968991
* ignore
969992
*/

0 commit comments

Comments
 (0)