From 16768eb0016e3ad18f3fbbf757779713d07391b9 Mon Sep 17 00:00:00 2001 From: sarayourfriend <24264157+sarayourfriend@users.noreply.github.com> Date: Wed, 27 Apr 2022 09:18:50 +0200 Subject: [PATCH] Remove lodash.set --- package.json | 1 - pnpm-lock.yaml | 6 ------ src/locales/scripts/jed1x-json-to-json.js | 4 ++-- src/locales/scripts/ngx-json-to-json.js | 4 ++-- src/locales/scripts/utils.js | 21 +++++++++++++++++++++ 5 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 src/locales/scripts/utils.js diff --git a/package.json b/package.json index 1c60faba8c..397323ef08 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,6 @@ "lodash.clonedeep": "^4.5.0", "lodash.debounce": "^4.0.8", "lodash.isempty": "^4.4.0", - "lodash.set": "^4.3.2", "lodash.sortby": "^4.7.0", "lodash.throttle": "^4.1.1", "node-html-parser": "^5.3.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 00078bc636..3c931c4ef4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -67,7 +67,6 @@ specifiers: lodash.clonedeep: ^4.5.0 lodash.debounce: ^4.0.8 lodash.isempty: ^4.4.0 - lodash.set: ^4.3.2 lodash.sortby: ^4.7.0 lodash.throttle: ^4.1.1 module-alias: ^2.2.2 @@ -125,7 +124,6 @@ dependencies: lodash.clonedeep: 4.5.0 lodash.debounce: 4.0.8 lodash.isempty: 4.4.0 - lodash.set: 4.3.2 lodash.sortby: 4.7.0 lodash.throttle: 4.1.1 node-html-parser: 5.3.3 @@ -12722,10 +12720,6 @@ packages: resolution: {integrity: sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==} dev: false - /lodash.set/4.3.2: - resolution: {integrity: sha1-2HV7HagH3eJIFrDWqEvqGnYjCyM=} - dev: false - /lodash.sortby/4.7.0: resolution: {integrity: sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=} dev: false diff --git a/src/locales/scripts/jed1x-json-to-json.js b/src/locales/scripts/jed1x-json-to-json.js index fceb24491b..d621c6a257 100644 --- a/src/locales/scripts/jed1x-json-to-json.js +++ b/src/locales/scripts/jed1x-json-to-json.js @@ -1,4 +1,4 @@ -const set = require('lodash.set') +const { setToValue } = require('./utils') /** * Convert a Jed1x-Translate object to a nested JSON object. @@ -37,7 +37,7 @@ function jed1xJsonToJson(jed1xObject) { const cleanedKey = key.slice(0, key.indexOf(SPLIT_CHAR)) if (value.length > 0) { const cleanedValue = value.length === 1 ? value[0] : value.join('|') - return set(result, cleanedKey, cleanedValue) + return setToValue(result, cleanedKey, cleanedValue) } }) return result diff --git a/src/locales/scripts/ngx-json-to-json.js b/src/locales/scripts/ngx-json-to-json.js index 6718c10b72..af4e9bf56e 100644 --- a/src/locales/scripts/ngx-json-to-json.js +++ b/src/locales/scripts/ngx-json-to-json.js @@ -1,4 +1,4 @@ -const set = require('lodash.set') +const { setToValue } = require('./utils') /** * Convert an NGX-Translate object to a nested JSON object @@ -26,7 +26,7 @@ const set = require('lodash.set') function ngxJsonToJson(ngxObject) { const result = {} Object.entries(ngxObject).forEach( - ([key, value]) => value && set(result, key, value) + ([key, value]) => value && setToValue(result, key, value) ) return result } diff --git a/src/locales/scripts/utils.js b/src/locales/scripts/utils.js new file mode 100644 index 0000000000..c5cb921c0d --- /dev/null +++ b/src/locales/scripts/utils.js @@ -0,0 +1,21 @@ +/** + * Mutates an object at the path with the value. If the path + * does not exist, it is created by nesting objects along the + * path segments. + * + * From https://stackoverflow.com/a/20240290 + * + * @param {any} obj - The object to mutate. + * @param {string} path - The dot delimited path on the object to mutate. + * @param {unknown} value - The value to set at the path. + */ +exports.setToValue = function setValue(obj, path, value) { + var a = path.split('.') + var o = obj + while (a.length - 1) { + var n = a.shift() + if (!(n in o)) o[n] = {} + o = o[n] + } + o[a[0]] = value +}