Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
Remove lodash.set
Browse files Browse the repository at this point in the history
  • Loading branch information
sarayourfriend committed Apr 27, 2022
1 parent af82534 commit 16768eb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 0 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/locales/scripts/jed1x-json-to-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const set = require('lodash.set')
const { setToValue } = require('./utils')

/**
* Convert a Jed1x-Translate object to a nested JSON object.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/locales/scripts/ngx-json-to-json.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const set = require('lodash.set')
const { setToValue } = require('./utils')

/**
* Convert an NGX-Translate object to a nested JSON object
Expand Down Expand Up @@ -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
}
Expand Down
21 changes: 21 additions & 0 deletions src/locales/scripts/utils.js
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 16768eb

Please sign in to comment.