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", file: "./dist/bundle.cjs",
}, },
plugins: [ plugins: [
typescript({ tsconfig: "tsconfig.build.json" }), typescript({ tsconfig: "tsconfig.json" }),
resolve({ resolve({
preferBuiltins: true, preferBuiltins: true,
browser: true, browser: true,

View File

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

View File

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

View File

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

View File

@ -1,12 +1,22 @@
import dayjs from "dayjs" import dayjs from "dayjs"
dayjs.extend(require("dayjs/plugin/duration"))
dayjs.extend(require("dayjs/plugin/advancedFormat")) import dayjsDurationPlugin from "dayjs/plugin/duration"
dayjs.extend(require("dayjs/plugin/isoWeek")) import dayjsAdvancedFormatPlugin from "dayjs/plugin/advancedFormat"
dayjs.extend(require("dayjs/plugin/weekYear")) import dayjsIsoWeekPlugin from "dayjs/plugin/isoWeek"
dayjs.extend(require("dayjs/plugin/weekOfYear")) import dayjsWeekYearPlugin from "dayjs/plugin/weekYear"
dayjs.extend(require("dayjs/plugin/relativeTime")) import dayjsWeekOfYearPlugin from "dayjs/plugin/weekOfYear"
dayjs.extend(require("dayjs/plugin/utc")) import dayjsRelativeTimePlugin from "dayjs/plugin/relativeTime"
dayjs.extend(require("dayjs/plugin/timezone")) 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: * 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 return context
} }
function initialConfig(str, pattern, options) { function initialConfig(str, pattern, options = {}) {
if (isOptions(pattern)) { if (isOptions(pattern)) {
options = pattern options = pattern
pattern = null pattern = null
@ -72,7 +82,7 @@ function initialConfig(str, pattern, options) {
return { 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 // if options is null then it'll get updated here
const config = initialConfig(str, pattern, options) const config = initialConfig(str, pattern, options)
const defaults = { lang: "en", date: new Date(config.str) } const defaults = { lang: "en", date: new Date(config.str) }

View File

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

View File

@ -1,7 +1,7 @@
import { date, duration } from "./date" import { date, duration } from "./date"
// https://github.com/evanw/esbuild/issues/56 // https://github.com/evanw/esbuild/issues/56
const externalCollections = { const externalCollections: Record<string, () => any> = {
math: require("@budibase/handlebars-helpers/lib/math"), math: require("@budibase/handlebars-helpers/lib/math"),
array: require("@budibase/handlebars-helpers/lib/array"), array: require("@budibase/handlebars-helpers/lib/array"),
number: require("@budibase/handlebars-helpers/lib/number"), 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 preprocessor from "./preprocessor"
import * as postprocessor from "./postprocessor" import * as postprocessor from "./postprocessor"
function process(output, processors, opts) { function process(output, processors, opts = {}) {
for (let processor of processors) { for (let processor of processors) {
// if a literal statement has occurred stop // if a literal statement has occurred stop
if (typeof output !== "string") { if (typeof output !== "string") {

View File

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

View File

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

View File

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

View File

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