Fix typings
This commit is contained in:
parent
0b292f2e82
commit
b05dc6ab49
|
@ -79,7 +79,7 @@ function createTemplate(string, opts) {
|
||||||
* @param {object|undefined} [opts] optional - specify some options for processing.
|
* @param {object|undefined} [opts] optional - specify some options for processing.
|
||||||
* @returns {Promise<object|array>} The structure input, as fully updated as possible.
|
* @returns {Promise<object|array>} The structure input, as fully updated as possible.
|
||||||
*/
|
*/
|
||||||
export async function processObject(object, context, opts) {
|
export async function processObject(object, context, opts?) {
|
||||||
testObject(object)
|
testObject(object)
|
||||||
for (let key of Object.keys(object || {})) {
|
for (let key of Object.keys(object || {})) {
|
||||||
if (object[key] != null) {
|
if (object[key] != null) {
|
||||||
|
@ -214,7 +214,7 @@ export function makePropSafe(property) {
|
||||||
* @param [opts] optional - specify some options for processing.
|
* @param [opts] optional - specify some options for processing.
|
||||||
* @returns {boolean} Whether or not the input string is valid.
|
* @returns {boolean} Whether or not the input string is valid.
|
||||||
*/
|
*/
|
||||||
export function isValid(string, opts) {
|
export function isValid(string, opts?) {
|
||||||
const validCases = [
|
const validCases = [
|
||||||
"string",
|
"string",
|
||||||
"number",
|
"number",
|
||||||
|
@ -256,11 +256,8 @@ export function isValid(string, opts) {
|
||||||
* This manifest provides information about each of the helpers and how it can be used.
|
* This manifest provides information about each of the helpers and how it can be used.
|
||||||
* @returns The manifest JSON which has been generated from the helpers.
|
* @returns The manifest JSON which has been generated from the helpers.
|
||||||
*/
|
*/
|
||||||
let manifest
|
import manifest from "../manifest.json"
|
||||||
export function getManifest() {
|
export function getManifest() {
|
||||||
if (!manifest) {
|
|
||||||
manifest = fs.readFileSync(require.resolve("../manifest.json"), "utf-8")
|
|
||||||
}
|
|
||||||
return manifest
|
return manifest
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -188,9 +188,7 @@ describe("test the date helpers", () => {
|
||||||
time: date.toUTCString(),
|
time: date.toUTCString(),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
const formatted = new dayjs(date)
|
const formatted = dayjs(date).tz("America/New_York").format("HH-mm-ss Z")
|
||||||
.tz("America/New_York")
|
|
||||||
.format("HH-mm-ss Z")
|
|
||||||
expect(output).toBe(formatted)
|
expect(output).toBe(formatted)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -200,7 +198,7 @@ describe("test the date helpers", () => {
|
||||||
time: date.toUTCString(),
|
time: date.toUTCString(),
|
||||||
})
|
})
|
||||||
const timezone = dayjs.tz.guess()
|
const timezone = dayjs.tz.guess()
|
||||||
const offset = new dayjs(date).tz(timezone).format("Z")
|
const offset = dayjs(date).tz(timezone).format("Z")
|
||||||
expect(output).toBe(offset)
|
expect(output).toBe(offset)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -7,7 +7,7 @@ const {
|
||||||
} = require("../src/index")
|
} = require("../src/index")
|
||||||
const { UUID_REGEX } = require("./constants")
|
const { UUID_REGEX } = require("./constants")
|
||||||
|
|
||||||
const processJS = (js, context) => {
|
const processJS = (js, context?) => {
|
||||||
return processStringSync(encodeJSBinding(js), context)
|
return processStringSync(encodeJSBinding(js), context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const vm = require("vm")
|
import vm from "vm"
|
||||||
|
|
||||||
jest.mock("@budibase/handlebars-helpers/lib/math", () => {
|
jest.mock("@budibase/handlebars-helpers/lib/math", () => {
|
||||||
const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math")
|
const actual = jest.requireActual("@budibase/handlebars-helpers/lib/math")
|
||||||
|
@ -17,10 +17,10 @@ jest.mock("@budibase/handlebars-helpers/lib/uuid", () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const { processString, setJSRunner } = require("../src/index")
|
import { processString, setJSRunner } from "../src/index"
|
||||||
|
|
||||||
const tk = require("timekeeper")
|
import tk from "timekeeper"
|
||||||
const { getParsedManifest, runJsHelpersTests } = require("./utils")
|
import { getParsedManifest, runJsHelpersTests } from "./utils"
|
||||||
|
|
||||||
tk.freeze("2021-01-21T12:00:00")
|
tk.freeze("2021-01-21T12:00:00")
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,7 @@
|
||||||
const { getManifest } = require("../src")
|
import { getManifest } from "../src"
|
||||||
const { getJsHelperList } = require("../src/helpers")
|
import { getJsHelperList } from "../src/helpers"
|
||||||
|
|
||||||
const {
|
import { convertToJS, processStringSync, encodeJSBinding } from "../src/index"
|
||||||
convertToJS,
|
|
||||||
processStringSync,
|
|
||||||
encodeJSBinding,
|
|
||||||
} = require("../src/index.js")
|
|
||||||
|
|
||||||
function tryParseJson(str) {
|
function tryParseJson(str) {
|
||||||
if (typeof str !== "string") {
|
if (typeof str !== "string") {
|
||||||
|
@ -19,23 +15,35 @@ function tryParseJson(str) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getParsedManifest = () => {
|
type ExampleType = [
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
hbs: string
|
||||||
|
js: string
|
||||||
|
requiresHbsBody: boolean
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
export const getParsedManifest = () => {
|
||||||
const manifest = getManifest()
|
const manifest = getManifest()
|
||||||
const collections = Object.keys(manifest)
|
const collections = Object.keys(manifest)
|
||||||
|
|
||||||
const examples = collections.reduce((acc, collection) => {
|
const examples = collections.reduce((acc, collection) => {
|
||||||
const functions = Object.entries(manifest[collection])
|
const functions = Object.entries<{
|
||||||
.filter(([_, details]) => details.example)
|
example: string
|
||||||
.map(([name, details]) => {
|
requiresBlock: boolean
|
||||||
|
}>(manifest[collection])
|
||||||
|
.filter(
|
||||||
|
([_, details]) =>
|
||||||
|
details.example?.split("->").map(x => x.trim()).length > 1
|
||||||
|
)
|
||||||
|
.map(([name, details]): ExampleType => {
|
||||||
const example = details.example
|
const example = details.example
|
||||||
let [hbs, js] = example.split("->").map(x => x.trim())
|
let [hbs, js] = example.split("->").map(x => x.trim())
|
||||||
if (!js) {
|
|
||||||
// The function has no return value
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Trim 's
|
// Trim 's
|
||||||
js = js.replace(/^'|'$/g, "")
|
js = js.replace(/^'|'$/g, "")
|
||||||
let parsedExpected
|
let parsedExpected: string
|
||||||
if ((parsedExpected = tryParseJson(js))) {
|
if ((parsedExpected = tryParseJson(js))) {
|
||||||
if (Array.isArray(parsedExpected)) {
|
if (Array.isArray(parsedExpected)) {
|
||||||
if (typeof parsedExpected[0] === "object") {
|
if (typeof parsedExpected[0] === "object") {
|
||||||
|
@ -48,19 +56,23 @@ const getParsedManifest = () => {
|
||||||
const requiresHbsBody = details.requiresBlock
|
const requiresHbsBody = details.requiresBlock
|
||||||
return [name, { hbs, js, requiresHbsBody }]
|
return [name, { hbs, js, requiresHbsBody }]
|
||||||
})
|
})
|
||||||
.filter(x => !!x)
|
|
||||||
|
|
||||||
if (Object.keys(functions).length) {
|
if (functions.length) {
|
||||||
acc[collection] = functions
|
acc[collection] = functions
|
||||||
}
|
}
|
||||||
return acc
|
return acc
|
||||||
}, {})
|
}, {} as Record<string, ExampleType[]>)
|
||||||
|
|
||||||
return examples
|
return examples
|
||||||
}
|
}
|
||||||
module.exports.getParsedManifest = getParsedManifest
|
|
||||||
|
|
||||||
module.exports.runJsHelpersTests = ({ funcWrap, testsToSkip } = {}) => {
|
export const runJsHelpersTests = ({
|
||||||
|
funcWrap,
|
||||||
|
testsToSkip,
|
||||||
|
}: {
|
||||||
|
funcWrap?: any
|
||||||
|
testsToSkip?: any
|
||||||
|
} = {}) => {
|
||||||
funcWrap = funcWrap || (delegate => delegate())
|
funcWrap = funcWrap || (delegate => delegate())
|
||||||
const manifest = getParsedManifest()
|
const manifest = getParsedManifest()
|
||||||
|
|
||||||
|
@ -73,7 +85,7 @@ module.exports.runJsHelpersTests = ({ funcWrap, testsToSkip } = {}) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
describe("can be parsed and run as js", () => {
|
describe("can be parsed and run as js", () => {
|
||||||
const jsHelpers = getJsHelperList()
|
const jsHelpers = getJsHelperList()!
|
||||||
const jsExamples = Object.keys(manifest).reduce((acc, v) => {
|
const jsExamples = Object.keys(manifest).reduce((acc, v) => {
|
||||||
acc[v] = manifest[v].filter(([key]) => jsHelpers[key])
|
acc[v] = manifest[v].filter(([key]) => jsHelpers[key])
|
||||||
return acc
|
return acc
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
"allowJs": true,
|
"allowJs": true,
|
||||||
"outDir": "dist",
|
"outDir": "dist",
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
"types": ["node", "jest"]
|
"types": ["node", "jest"],
|
||||||
|
"resolveJsonModule": true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue