This commit is contained in:
Adria Navarro 2024-02-21 21:10:53 +01:00
parent 046e27c737
commit a572cc980f
12 changed files with 50 additions and 28 deletions

View File

@ -18,7 +18,7 @@ export default [
file: "./dist/bundle.cjs",
},
plugins: [
typescript({ tsconfig: "tsconfig.build.json" }),
typescript({ tsconfig: "tsconfig.json" }),
resolve({
preferBuiltins: true,
browser: true,

View File

@ -114,7 +114,7 @@ export function convertHBSBlock(block, blockNumber) {
const parts = splitBySpace(layer)
if (value || parts.length > 1 || list[parts[0]]) {
// first of layer should always be the helper
const helper = parts.splice(0, 1)
const [helper] = parts.splice(0, 1)
if (list[helper]) {
value = `helpers.${helper}(${buildList(parts, value)})`
}

View File

@ -1,7 +1,3 @@
export class JsErrorTimeout extends Error {
code = "ERR_SCRIPT_EXECUTION_TIMEOUT"
constructor() {
super()
}
}

View File

@ -1,4 +1,8 @@
export default class Helper {
private name
private fn
private useValueFallback
constructor(name, fn, useValueFallback = true) {
this.name = name
this.fn = fn

View File

@ -1,12 +1,22 @@
import dayjs from "dayjs"
dayjs.extend(require("dayjs/plugin/duration"))
dayjs.extend(require("dayjs/plugin/advancedFormat"))
dayjs.extend(require("dayjs/plugin/isoWeek"))
dayjs.extend(require("dayjs/plugin/weekYear"))
dayjs.extend(require("dayjs/plugin/weekOfYear"))
dayjs.extend(require("dayjs/plugin/relativeTime"))
dayjs.extend(require("dayjs/plugin/utc"))
dayjs.extend(require("dayjs/plugin/timezone"))
import dayjsDurationPlugin from "dayjs/plugin/duration"
import dayjsAdvancedFormatPlugin from "dayjs/plugin/advancedFormat"
import dayjsIsoWeekPlugin from "dayjs/plugin/isoWeek"
import dayjsWeekYearPlugin from "dayjs/plugin/weekYear"
import dayjsWeekOfYearPlugin from "dayjs/plugin/weekOfYear"
import dayjsRelativeTimePlugin from "dayjs/plugin/relativeTime"
import dayjsUtcPlugin from "dayjs/plugin/utc"
import dayjsTimezonePlugin from "dayjs/plugin/timezone"
dayjs.extend(dayjsDurationPlugin)
dayjs.extend(dayjsAdvancedFormatPlugin)
dayjs.extend(dayjsIsoWeekPlugin)
dayjs.extend(dayjsWeekYearPlugin)
dayjs.extend(dayjsWeekOfYearPlugin)
dayjs.extend(dayjsRelativeTimePlugin)
dayjs.extend(dayjsUtcPlugin)
dayjs.extend(dayjsTimezonePlugin)
/**
* This file was largely taken from the helper-date package - we did this for two reasons:
@ -58,7 +68,7 @@ function getContext(thisArg, locals, options) {
return context
}
function initialConfig(str, pattern, options) {
function initialConfig(str, pattern, options = {}) {
if (isOptions(pattern)) {
options = pattern
pattern = null
@ -72,7 +82,7 @@ function initialConfig(str, pattern, options) {
return { str, pattern, options }
}
function setLocale(str, pattern, options) {
function setLocale(str, pattern, options = {}) {
// if options is null then it'll get updated here
const config = initialConfig(str, pattern, options)
const defaults = { lang: "en", date: new Date(config.str) }

View File

@ -44,8 +44,9 @@ const HELPERS = [
if (value == null || typeof value !== "string") {
return value == null ? "" : value
}
if (value && value.string) {
value = value.string
// TODO: check, this should always be false
if (value && (value as any).string) {
value = (value as any).string
}
let text = value
if (__opts && __opts.escapeNewlines) {

View File

@ -1,7 +1,7 @@
import { date, duration } from "./date"
// https://github.com/evanw/esbuild/issues/56
const externalCollections = {
const externalCollections: Record<string, () => any> = {
math: require("@budibase/handlebars-helpers/lib/math"),
array: require("@budibase/handlebars-helpers/lib/array"),
number: require("@budibase/handlebars-helpers/lib/number"),

View File

@ -2,7 +2,7 @@ import { FIND_HBS_REGEX } from "../utilities"
import * as preprocessor from "./preprocessor"
import * as postprocessor from "./postprocessor"
function process(output, processors, opts) {
function process(output, processors, opts = {}) {
for (let processor of processors) {
// if a literal statement has occurred stop
if (typeof output !== "string") {

View File

@ -6,6 +6,9 @@ export const PostProcessorNames = {
/* eslint-disable no-unused-vars */
class Postprocessor {
name
private fn
constructor(name, fn) {
this.name = name
this.fn = fn

View File

@ -11,7 +11,7 @@ export const PreprocessorNames = {
/* eslint-disable no-unused-vars */
class Preprocessor {
private name: string
name: string
private fn: any
constructor(name, fn) {

View File

@ -15,14 +15,14 @@ export const isJSAllowed = () => {
// originally this could be done with a single regex using look behinds
// but safari does not support this feature
// original regex: /(?<!{){{[^{}]+}}(?!})/g
export const findDoubleHbsInstances = string => {
export const findDoubleHbsInstances = (string: string): string[] => {
let copied = string
const doubleRegex = new RegExp(FIND_HBS_REGEX)
const regex = new RegExp(FIND_TRIPLE_HBS_REGEX)
const tripleMatches = copied.match(regex)
// remove triple braces
if (tripleMatches) {
tripleMatches.forEach(match => {
tripleMatches.forEach((match: string) => {
copied = copied.replace(match, "")
})
}
@ -30,16 +30,21 @@ export const findDoubleHbsInstances = string => {
return doubleMatches ? doubleMatches : []
}
export const isAlphaNumeric = char => {
export const isAlphaNumeric = (char: string) => {
return char.match(ALPHA_NUMERIC_REGEX)
}
export const swapStrings = (string, start, length, swap) => {
export const swapStrings = (
string: string,
start: number,
length: number,
swap: string
) => {
return string.slice(0, start) + swap + string.slice(start + length)
}
export const removeHandlebarsStatements = (
string,
string: string,
replacement = "Invalid binding"
) => {
let regexp = new RegExp(FIND_HBS_REGEX)
@ -54,10 +59,10 @@ export const removeHandlebarsStatements = (
return string
}
export const btoa = plainText => {
export const btoa = (plainText: string) => {
return Buffer.from(plainText, "utf-8").toString("base64")
}
export const atob = base64 => {
export const atob = (base64: string) => {
return Buffer.from(base64, "base64").toString("utf-8")
}

View File

@ -1,6 +1,9 @@
{
"include": ["src/**/*"],
"compilerOptions": {
"target": "es6",
"moduleResolution": "node",
"lib": ["dom"],
"allowJs": true,
"declaration": true,
"emitDeclarationOnly": true,