2021-01-25 18:08:21 +01:00
|
|
|
const _ = require("lodash")
|
2021-01-21 12:30:53 +01:00
|
|
|
const ALPHA_NUMERIC_REGEX = /^[A-Za-z0-9]+$/g
|
|
|
|
|
2021-01-25 19:14:45 +01:00
|
|
|
module.exports.FIND_HBS_REGEX = /{{([^{].*?)}}/g
|
2021-01-21 12:30:53 +01:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
module.exports.isAlphaNumeric = char => {
|
2021-01-21 12:30:53 +01:00
|
|
|
return char.match(ALPHA_NUMERIC_REGEX)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.swapStrings = (string, start, length, swap) => {
|
|
|
|
return string.slice(0, start) + swap + string.slice(start + length)
|
|
|
|
}
|
|
|
|
|
2021-01-21 14:48:23 +01:00
|
|
|
// removes null and undefined
|
2021-05-04 12:32:22 +02:00
|
|
|
module.exports.removeNull = obj => {
|
2021-05-03 09:31:09 +02:00
|
|
|
obj = _(obj).omitBy(_.isUndefined).omitBy(_.isNull).value()
|
2021-01-25 18:08:21 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-11 17:07:55 +02:00
|
|
|
module.exports.updateContext = obj => {
|
2021-01-25 18:08:21 +01:00
|
|
|
if (obj.now == null) {
|
2021-05-11 17:07:55 +02:00
|
|
|
obj.now = (new Date()).toISOString()
|
2021-01-25 18:08:21 +01:00
|
|
|
}
|
2021-05-11 17:07:55 +02:00
|
|
|
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)
|
2021-01-21 14:48:23 +01:00
|
|
|
}
|
2021-02-03 14:04:19 +01:00
|
|
|
|
2021-05-04 12:32:22 +02:00
|
|
|
module.exports.removeHandlebarsStatements = string => {
|
2021-02-03 14:04:19 +01:00
|
|
|
let regexp = new RegExp(exports.FIND_HBS_REGEX)
|
|
|
|
let matches = string.match(regexp)
|
|
|
|
if (matches == null) {
|
|
|
|
return string
|
|
|
|
}
|
|
|
|
for (let match of matches) {
|
|
|
|
const idx = string.indexOf(match)
|
|
|
|
string = exports.swapStrings(string, idx, match.length, "Invalid Binding")
|
|
|
|
}
|
|
|
|
return string
|
|
|
|
}
|