Adding a manifest of all the useful helpers which our system provides.
This commit is contained in:
parent
0fabfccb19
commit
56c7f8094c
File diff suppressed because it is too large
Load Diff
|
@ -20,6 +20,7 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@rollup/plugin-json": "^4.1.0",
|
"@rollup/plugin-json": "^4.1.0",
|
||||||
|
"doctrine": "^3.0.0",
|
||||||
"jest": "^26.6.3",
|
"jest": "^26.6.3",
|
||||||
"rollup": "^2.36.2",
|
"rollup": "^2.36.2",
|
||||||
"rollup-plugin-commonjs": "^10.1.0",
|
"rollup-plugin-commonjs": "^10.1.0",
|
||||||
|
|
|
@ -0,0 +1,143 @@
|
||||||
|
const helpers = require("handlebars-helpers")
|
||||||
|
const { HelperFunctionBuiltin } = require("../src/helpers/constants")
|
||||||
|
const fs = require("fs")
|
||||||
|
const doctrine = require("doctrine")
|
||||||
|
|
||||||
|
const FILENAME = "../manifest.json"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* full list of supported helpers can be found here:
|
||||||
|
* https://github.com/helpers/handlebars-helpers
|
||||||
|
*/
|
||||||
|
|
||||||
|
const COLLECTIONS = [
|
||||||
|
"math",
|
||||||
|
"array",
|
||||||
|
"number",
|
||||||
|
"url",
|
||||||
|
"string",
|
||||||
|
"comparison",
|
||||||
|
]
|
||||||
|
|
||||||
|
const outputJSON = {}
|
||||||
|
|
||||||
|
function fixSpecialCases(name, obj) {
|
||||||
|
const args = obj.args
|
||||||
|
if (name === "ifNth") {
|
||||||
|
args[0] = "a"
|
||||||
|
args[1] = "b"
|
||||||
|
}
|
||||||
|
if (name === "eachIndex") {
|
||||||
|
obj.description = "Iterates the array, listing an item and the index of it."
|
||||||
|
}
|
||||||
|
if (name === "toFloat") {
|
||||||
|
obj.description = "Convert input to a float."
|
||||||
|
}
|
||||||
|
if (name === "toInt") {
|
||||||
|
obj.description = "Convert input to an integer."
|
||||||
|
}
|
||||||
|
// add the date helper
|
||||||
|
obj
|
||||||
|
return obj
|
||||||
|
}
|
||||||
|
|
||||||
|
function lookForward(lines, funcLines, idx) {
|
||||||
|
const funcLen = funcLines.length
|
||||||
|
for (let i = idx, j = 0; i < idx + funcLen; ++i, j++) {
|
||||||
|
if (!lines[i].includes(funcLines[j])) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCommentInfo(file, func) {
|
||||||
|
const lines = file.split("\n")
|
||||||
|
const funcLines = func.split("\n")
|
||||||
|
let comment = null
|
||||||
|
for (let idx = 0; idx < lines.length; ++idx) {
|
||||||
|
// from here work back until we have the comment
|
||||||
|
if (lookForward(lines, funcLines, idx)) {
|
||||||
|
let fromIdx = idx
|
||||||
|
let start = 0, end = 0
|
||||||
|
do {
|
||||||
|
if (lines[fromIdx].includes("*/")) {
|
||||||
|
end = fromIdx
|
||||||
|
} else if (lines[fromIdx].includes("/*")) {
|
||||||
|
start = fromIdx
|
||||||
|
}
|
||||||
|
if (start && end) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
fromIdx--
|
||||||
|
} while (fromIdx > 0)
|
||||||
|
comment = lines.slice(start, end + 1).join("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (comment == null) {
|
||||||
|
return { description: "" }
|
||||||
|
}
|
||||||
|
const docs = doctrine.parse(comment, { unwrap: true })
|
||||||
|
// some hacky fixes
|
||||||
|
docs.description = docs.description.replace(/\n/g, " ")
|
||||||
|
docs.description = docs.description.replace(/[ ]{2,}/g, " ")
|
||||||
|
docs.description = docs.description.replace(/is is/g, "is")
|
||||||
|
const example = docs.description.split("```")
|
||||||
|
if (example.length > 1) {
|
||||||
|
docs.example = example[1]
|
||||||
|
}
|
||||||
|
docs.description = example[0].trim()
|
||||||
|
return docs
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This script is very specific to purpose, parsing the handlebars-helpers files to attempt to get information about them.
|
||||||
|
*/
|
||||||
|
function run() {
|
||||||
|
const foundNames = []
|
||||||
|
for (let collection of COLLECTIONS) {
|
||||||
|
const collectionFile = fs.readFileSync(`../node_modules/handlebars-helpers/lib/${collection}.js`, "utf8")
|
||||||
|
const collectionInfo = {}
|
||||||
|
// collect information about helper
|
||||||
|
let hbsHelperInfo = helpers[collection]()
|
||||||
|
for (let entry of Object.entries(hbsHelperInfo)) {
|
||||||
|
const name = entry[0]
|
||||||
|
// skip built in functions and ones seen already
|
||||||
|
if (
|
||||||
|
HelperFunctionBuiltin.indexOf(name) !== -1 ||
|
||||||
|
foundNames.indexOf(name) !== -1
|
||||||
|
) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
foundNames.push(name)
|
||||||
|
// this is ridiculous, but it parse the function header
|
||||||
|
const fnc = entry[1].toString()
|
||||||
|
const jsDocInfo = getCommentInfo(collectionFile, fnc)
|
||||||
|
let args = jsDocInfo.tags
|
||||||
|
.filter(tag => tag.title === "param")
|
||||||
|
.map(tag => tag.description && tag.description.replace(/`/g, "").split(" ")[0].trim())
|
||||||
|
collectionInfo[name] = fixSpecialCases(name, {
|
||||||
|
args,
|
||||||
|
numArgs: args.length,
|
||||||
|
example: jsDocInfo.example || undefined,
|
||||||
|
description: jsDocInfo.description,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
outputJSON[collection] = collectionInfo
|
||||||
|
}
|
||||||
|
// add the date helper
|
||||||
|
outputJSON["date"] = {
|
||||||
|
"date": {
|
||||||
|
args: [
|
||||||
|
"datetime",
|
||||||
|
"format"
|
||||||
|
],
|
||||||
|
numArgs: 2,
|
||||||
|
example: "{{date now \"YYYY\"}}",
|
||||||
|
description: "Format a date using moment.js data formatting."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fs.writeFileSync(FILENAME, JSON.stringify(outputJSON, null, 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
run()
|
|
@ -35,6 +35,14 @@ describe("test the math helpers", () => {
|
||||||
})
|
})
|
||||||
expect(parseInt(output)).toBe(2)
|
expect(parseInt(output)).toBe(2)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("should be able to times", async () => {
|
||||||
|
const output = await processString("{{times a b}}", {
|
||||||
|
a: 5,
|
||||||
|
b: 5,
|
||||||
|
})
|
||||||
|
expect(parseInt(output)).toBe(25)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("test the array helpers", () => {
|
describe("test the array helpers", () => {
|
||||||
|
|
|
@ -1665,6 +1665,13 @@ diffie-hellman@^5.0.0:
|
||||||
miller-rabin "^4.0.0"
|
miller-rabin "^4.0.0"
|
||||||
randombytes "^2.0.0"
|
randombytes "^2.0.0"
|
||||||
|
|
||||||
|
doctrine@^3.0.0:
|
||||||
|
version "3.0.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-3.0.0.tgz#addebead72a6574db783639dc87a121773973961"
|
||||||
|
integrity sha512-yS+Q5i3hBf7GBkd4KG8a7eBNNWNGLTaEwwYWUijIYM7zrlYDM0BFXHjjPWlWZ1Rg7UaddZeIDmi9jF3HmqiQ2w==
|
||||||
|
dependencies:
|
||||||
|
esutils "^2.0.2"
|
||||||
|
|
||||||
domexception@^2.0.1:
|
domexception@^2.0.1:
|
||||||
version "2.0.1"
|
version "2.0.1"
|
||||||
resolved "https://registry.yarnpkg.com/domexception/-/domexception-2.0.1.tgz#fb44aefba793e1574b0af6aed2801d057529f304"
|
resolved "https://registry.yarnpkg.com/domexception/-/domexception-2.0.1.tgz#fb44aefba793e1574b0af6aed2801d057529f304"
|
||||||
|
|
Loading…
Reference in New Issue