Remove lodash functions and recursive context mutation in string templates to increase performance
This commit is contained in:
parent
be2ec9b427
commit
6d9a1b8382
|
@ -92,8 +92,6 @@ module.exports.processStringSync = (string, context) => {
|
||||||
}
|
}
|
||||||
// take a copy of input incase error
|
// take a copy of input incase error
|
||||||
const input = string
|
const input = string
|
||||||
const clonedContext = removeNull(updateContext(cloneDeep(context)))
|
|
||||||
// remove any null/undefined properties
|
|
||||||
if (typeof string !== "string") {
|
if (typeof string !== "string") {
|
||||||
throw "Cannot process non-string types."
|
throw "Cannot process non-string types."
|
||||||
}
|
}
|
||||||
|
@ -103,7 +101,10 @@ module.exports.processStringSync = (string, context) => {
|
||||||
const template = hbsInstance.compile(string, {
|
const template = hbsInstance.compile(string, {
|
||||||
strict: false,
|
strict: false,
|
||||||
})
|
})
|
||||||
return processors.postprocess(template(clonedContext))
|
return processors.postprocess(template({
|
||||||
|
now: new Date().toISOString(),
|
||||||
|
...context,
|
||||||
|
}))
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return removeHandlebarsStatements(input)
|
return removeHandlebarsStatements(input)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,38 +11,6 @@ module.exports.swapStrings = (string, start, length, swap) => {
|
||||||
return string.slice(0, start) + swap + string.slice(start + length)
|
return string.slice(0, start) + swap + string.slice(start + length)
|
||||||
}
|
}
|
||||||
|
|
||||||
// removes null and undefined
|
|
||||||
module.exports.removeNull = obj => {
|
|
||||||
obj = _(obj).omitBy(_.isUndefined).omitBy(_.isNull).value()
|
|
||||||
for (let [key, value] of Object.entries(obj)) {
|
|
||||||
// only objects
|
|
||||||
if (typeof value === "object" && !Array.isArray(value)) {
|
|
||||||
obj[key] = module.exports.removeNull(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return obj
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.updateContext = obj => {
|
|
||||||
if (obj.now == null) {
|
|
||||||
obj.now = new Date().toISOString()
|
|
||||||
}
|
|
||||||
function recurse(obj) {
|
|
||||||
for (let key of Object.keys(obj)) {
|
|
||||||
if (!obj[key]) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if (obj[key] instanceof Date) {
|
|
||||||
obj[key] = obj[key].toISOString()
|
|
||||||
} else if (typeof obj[key] === "object") {
|
|
||||||
obj[key] = recurse(obj[key])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return obj
|
|
||||||
}
|
|
||||||
return recurse(obj)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.removeHandlebarsStatements = string => {
|
module.exports.removeHandlebarsStatements = string => {
|
||||||
let regexp = new RegExp(exports.FIND_HBS_REGEX)
|
let regexp = new RegExp(exports.FIND_HBS_REGEX)
|
||||||
let matches = string.match(regexp)
|
let matches = string.match(regexp)
|
||||||
|
|
Loading…
Reference in New Issue