From ba42929d1d5495b02e90ff34cb58cf9495da824c Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Mon, 6 May 2024 09:00:49 +0800 Subject: [PATCH] set nested data with automatic subdictionary created via `nested.dictionary` --- scripts/utils/nested.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/scripts/utils/nested.js b/scripts/utils/nested.js index 1f9d4cf..48e4dcd 100644 --- a/scripts/utils/nested.js +++ b/scripts/utils/nested.js @@ -35,6 +35,36 @@ nested.dictionary = class dictionary { // Now return the data. return DATA; } + + /* + Update a data with a new value. + + @param {object} data the data to be used + @param {string} path the path to the data + @param {object} value the value to be used + @return {object} the data + */ + static set(data, path, value) { + let DATA = data, PATH = path, VALUE = value; + + // Convert path into an array if not yet set. + PATH = (Array.isArray(PATH)) ? PATH : (PATH && (typeof PATH).includes(`str`)) ? PATH.trim().split(`,`) : []; + + // Get the current path. + PATH = {"all": PATH}; + PATH[`target`] = PATH[`all`]; + PATH[`current`] = String(PATH[`target`].shift()).trim(); + + if (PATH[`target`].length > 0) { + (DATA[PATH[`current`]] == null) ? DATA[PATH[`current`]] = {} : false; + DATA[PATH[`current`]] = nested.dictionary.set(DATA[PATH[`current`]], PATH[`target`], VALUE); + } else { + DATA[PATH[`current`]] = VALUE; + } + + // Return the value. + return (DATA); + } } export {nested as default}; \ No newline at end of file