From df7834a8805aa813fe44bc20ae6d24f6bbe70dde Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Fri, 18 Dec 2020 15:24:12 -0500 Subject: [PATCH] More build script improvements (#1123) * Update set action to support an assignment index * Add 'add' modification --- dev/build.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/dev/build.js b/dev/build.js index 3fd62893..72dce283 100644 --- a/dev/build.js +++ b/dev/build.js @@ -82,10 +82,16 @@ function applyModifications(manifest, modifications) { switch (action) { case 'set': { - const {value: newValue} = modification; - const value = getObjectProperties(manifest, path2, path2.length - 1); - const last = path2[path2.length - 1]; - value[last] = clone(newValue); + const {value, before, after} = modification; + const object = getObjectProperties(manifest, path2, path2.length - 1); + const key = path2[path2.length - 1]; + + let {index} = modification; + if (typeof index !== 'number') { index = -1; } + if (typeof before === 'string') { index = getObjectKeyIndex(object, before); } + if (typeof after === 'string') { index = getObjectKeyIndex(object, after); } + + setObjectKeyAtIndex(object, key, value, index); } break; case 'replace': @@ -143,6 +149,14 @@ function applyModifications(manifest, modifications) { } } break; + case 'add': + { + const {items} = modification; + const value = getObjectProperties(manifest, path2, path2.length); + const itemsNew = items.map((v) => clone(v)); + value.push(...itemsNew); + } + break; } } } @@ -165,7 +179,7 @@ function getObjectKeyIndex(object, key) { } function setObjectKeyAtIndex(object, key, value, index) { - if (index < 0 || Object.prototype.hasOwnProperty.call(object, key)) { + if (index < 0 || typeof key === 'number' || Object.prototype.hasOwnProperty.call(object, key)) { object[key] = value; return; }