Fixing an issue with date objects not being managed by handlebars correctly.

This commit is contained in:
mike12345567 2021-05-11 16:07:55 +01:00
parent 6f6770cef7
commit 5181ca710f
3 changed files with 24 additions and 6 deletions

View File

@ -4,7 +4,7 @@ const processors = require("./processors")
const { cloneDeep } = require("lodash/fp")
const {
removeNull,
addConstants,
updateContext,
removeHandlebarsStatements,
} = require("./utilities")
const manifest = require("../manifest.json")
@ -92,8 +92,7 @@ module.exports.processStringSync = (string, context) => {
}
// take a copy of input incase error
const input = string
let clonedContext = removeNull(cloneDeep(context))
clonedContext = addConstants(clonedContext)
const clonedContext = removeNull(updateContext(cloneDeep(context)))
// remove any null/undefined properties
if (typeof string !== "string") {
throw "Cannot process non-string types."

View File

@ -26,11 +26,24 @@ module.exports.removeNull = obj => {
return obj
}
module.exports.addConstants = obj => {
module.exports.updateContext = obj => {
if (obj.now == null) {
obj.now = new Date()
obj.now = (new Date()).toISOString()
}
return obj
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 => {

View File

@ -107,6 +107,12 @@ describe("check the utility functions", () => {
const property = makePropSafe("thing")
expect(property).toEqual("[thing]")
})
it("should be able to handle an input date object", async () => {
const date = new Date()
const output = await processString("{{ dateObj }}", { dateObj: date })
expect(date.toISOString()).toEqual(output)
})
})
describe("check manifest", () => {