list.ts
 1  import { date, difference, duration, durationFromNow } from "./date"
 2  
 3  /* 
 4  @budibase/handlebars-helpers is not treeshakeable, so we can't use the barrel files. 
 5  Otherwise, we have issues when generating the isolated-vm bundle because of the treeshaking
 6  */
 7  /* eslint-disable local-rules/no-budibase-imports */
 8  // @ts-expect-error
 9  import math from "@budibase/handlebars-helpers/lib/math"
10  // @ts-expect-error
11  import array from "@budibase/handlebars-helpers/lib/array"
12  // @ts-expect-error
13  import number from "@budibase/handlebars-helpers/lib/number"
14  // @ts-expect-error
15  import url from "@budibase/handlebars-helpers/lib/url"
16  // @ts-expect-error
17  import string from "@budibase/handlebars-helpers/lib/string"
18  // @ts-expect-error
19  import comparison from "@budibase/handlebars-helpers/lib/comparison"
20  // @ts-expect-error
21  import object from "@budibase/handlebars-helpers/lib/object"
22  // @ts-expect-error
23  import regex from "@budibase/handlebars-helpers/lib/regex"
24  // @ts-expect-error
25  import uuid from "@budibase/handlebars-helpers/lib/uuid"
26  /* eslint-enable local-rules/no-budibase-imports */
27  
28  // https://github.com/evanw/esbuild/issues/56
29  const externalCollections = {
30    math,
31    array,
32    number,
33    url,
34    string,
35    comparison,
36    object,
37    regex,
38    uuid,
39  }
40  
41  export const helpersToRemoveForJs = ["sortBy", "map"]
42  
43  const addedHelpers = {
44    date: date,
45    difference: difference,
46    duration: duration,
47    durationFromNow: durationFromNow,
48  }
49  
50  let helpers: Record<string, any>
51  
52  export function getJsHelperList() {
53    if (helpers) {
54      return helpers
55    }
56  
57    helpers = {}
58    for (let collection of Object.values(externalCollections)) {
59      for (let [key, func] of Object.entries<any>(collection)) {
60        // Handlebars injects the hbs options to the helpers by default. We are adding an empty {} as a last parameter to simulate it
61        helpers[key] = (...props: any) => func(...props, {})
62      }
63    }
64    helpers = {
65      ...helpers,
66      ...addedHelpers,
67    }
68  
69    for (const toRemove of helpersToRemoveForJs) {
70      delete helpers[toRemove]
71    }
72    Object.freeze(helpers)
73    return helpers
74  }