1
0
Fork 0
mirror of synced 2024-10-02 10:08:09 +13:00

Fix typings

This commit is contained in:
Adria Navarro 2024-02-21 23:43:12 +01:00
parent 0b292f2e82
commit b05dc6ab49
6 changed files with 46 additions and 38 deletions

View file

@ -79,7 +79,7 @@ function createTemplate(string, opts) {
* @param {object|undefined} [opts] optional - specify some options for processing.
* @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)
for (let key of Object.keys(object || {})) {
if (object[key] != null) {
@ -214,7 +214,7 @@ export function makePropSafe(property) {
* @param [opts] optional - specify some options for processing.
* @returns {boolean} Whether or not the input string is valid.
*/
export function isValid(string, opts) {
export function isValid(string, opts?) {
const validCases = [
"string",
"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.
* @returns The manifest JSON which has been generated from the helpers.
*/
let manifest
import manifest from "../manifest.json"
export function getManifest() {
if (!manifest) {
manifest = fs.readFileSync(require.resolve("../manifest.json"), "utf-8")
}
return manifest
}

View file

@ -188,9 +188,7 @@ describe("test the date helpers", () => {
time: date.toUTCString(),
}
)
const formatted = new dayjs(date)
.tz("America/New_York")
.format("HH-mm-ss Z")
const formatted = dayjs(date).tz("America/New_York").format("HH-mm-ss Z")
expect(output).toBe(formatted)
})
@ -200,7 +198,7 @@ describe("test the date helpers", () => {
time: date.toUTCString(),
})
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)
})
})

View file

@ -7,7 +7,7 @@ const {
} = require("../src/index")
const { UUID_REGEX } = require("./constants")
const processJS = (js, context) => {
const processJS = (js, context?) => {
return processStringSync(encodeJSBinding(js), context)
}

View file

@ -1,4 +1,4 @@
const vm = require("vm")
import vm from "vm"
jest.mock("@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")
const { getParsedManifest, runJsHelpersTests } = require("./utils")
import tk from "timekeeper"
import { getParsedManifest, runJsHelpersTests } from "./utils"
tk.freeze("2021-01-21T12:00:00")

View file

@ -1,11 +1,7 @@
const { getManifest } = require("../src")
const { getJsHelperList } = require("../src/helpers")
import { getManifest } from "../src"
import { getJsHelperList } from "../src/helpers"
const {
convertToJS,
processStringSync,
encodeJSBinding,
} = require("../src/index.js")
import { convertToJS, processStringSync, encodeJSBinding } from "../src/index"
function tryParseJson(str) {
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 collections = Object.keys(manifest)
const examples = collections.reduce((acc, collection) => {
const functions = Object.entries(manifest[collection])
.filter(([_, details]) => details.example)
.map(([name, details]) => {
const functions = Object.entries<{
example: string
requiresBlock: boolean
}>(manifest[collection])
.filter(
([_, details]) =>
details.example?.split("->").map(x => x.trim()).length > 1
)
.map(([name, details]): ExampleType => {
const example = details.example
let [hbs, js] = example.split("->").map(x => x.trim())
if (!js) {
// The function has no return value
return
}
// Trim 's
js = js.replace(/^'|'$/g, "")
let parsedExpected
let parsedExpected: string
if ((parsedExpected = tryParseJson(js))) {
if (Array.isArray(parsedExpected)) {
if (typeof parsedExpected[0] === "object") {
@ -48,19 +56,23 @@ const getParsedManifest = () => {
const requiresHbsBody = details.requiresBlock
return [name, { hbs, js, requiresHbsBody }]
})
.filter(x => !!x)
if (Object.keys(functions).length) {
if (functions.length) {
acc[collection] = functions
}
return acc
}, {})
}, {} as Record<string, ExampleType[]>)
return examples
}
module.exports.getParsedManifest = getParsedManifest
module.exports.runJsHelpersTests = ({ funcWrap, testsToSkip } = {}) => {
export const runJsHelpersTests = ({
funcWrap,
testsToSkip,
}: {
funcWrap?: any
testsToSkip?: any
} = {}) => {
funcWrap = funcWrap || (delegate => delegate())
const manifest = getParsedManifest()
@ -73,7 +85,7 @@ module.exports.runJsHelpersTests = ({ funcWrap, testsToSkip } = {}) => {
}
describe("can be parsed and run as js", () => {
const jsHelpers = getJsHelperList()
const jsHelpers = getJsHelperList()!
const jsExamples = Object.keys(manifest).reduce((acc, v) => {
acc[v] = manifest[v].filter(([key]) => jsHelpers[key])
return acc

View file

@ -7,6 +7,7 @@
"allowJs": true,
"outDir": "dist",
"esModuleInterop": true,
"types": ["node", "jest"]
"types": ["node", "jest"],
"resolveJsonModule": true
}
}