All files / src handlebars.ts

0% Statements 0/60
0% Branches 0/39
0% Functions 0/15
0% Lines 0/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                                                                                                                                                                                                                               
import Handlebars from "handlebars";
import {FacetDetail} from "./templater";
 
export function prefix(values: string[], prefix: string) {
  return values.map(value => `${prefix || ""}${value}`);
}
 
export function stringUnion(values: string[], includeQuotes: boolean) {
  if (!Array.isArray(values)) {
      throw new Error("Invalid context");
  }
  return values
    .map(value => includeQuotes ? `"${value}"` : value)
    .join(" | ");
}
 
export function union(values: string[]) {
  if (!Array.isArray(values)) {
    throw new Error("Invalid context");
  }
  return values.join(" | ");
}
 
type IndexType = {name: string, type: string};
 
export function getFacetPermutations(values: FacetDetail[]): IndexType[][] {
  if (!Array.isArray(values)) {
    throw new Error("Invalid context");
  }
  let permutations: IndexType[][] = [];
  for (let i = 0; i < values.length; i++) {
    if (values[i].key === "pk" && values[i+1] !== undefined && values[i+1].key === "pk") {
        continue;
    }
    let facets: IndexType[] = [];
    for (let j = 0; j <= i; j++) {
        let item = values[j];
        facets.push({
            name: item.name,
            type: item.type
        });
    }
    permutations.push(facets);
  }
  return permutations;
}
 
export function buildIndexType(values: FacetDetail[], facetStart?: string) {
  if (!Array.isArray(values)) {
      throw new Error("Invalid context");
  }
  if (typeof facetStart !== "string") {
    facetStart = values[0] && values[0].name;
  }
  let facets: FacetDetail[] = [];
  let hasBegun = false;
  for (let value of values) {
    if (hasBegun) {
      facets.push(value);
    } else if (value.name === facetStart) {
      facets.push(value);
      hasBegun = true;
    }
  }
  let tableIndex = getFacetPermutations(facets);
  return concatIndexType(tableIndex);
}
 
export function concatIndexType(values: IndexType[][] = []) {
  return values.map(facets => {
      return `{ ${facets.map(facet => `${facet.name}: ${facet.type}`).join(", ")} }`
  }).join(" | ") || "{}";
}
 
export function filterIndexType(values: FacetDetail[], key: 'pk' | 'sk' | 'all') {
  return values.filter(indexType => {
      return key === "all" || indexType.key === key
  })
}
 
export function eq(this: typeof Handlebars, type: any, value: any, options: Handlebars.HelperOptions) {
  return type === value
    ? options.fn(this)
    : options.inverse(this);
}
 
export function ne(this: typeof Handlebars, type: any, value: any, options: Handlebars.HelperOptions) {
  return type === value
    ? options.inverse(this)
    : options.fn(this);
}
 
export function properCase(value: string) {
  if (typeof value !== "string") {
    throw new Error("Can only proper case strings.");
  }
  let cased = "";
  for (let i = 0; i < value.length; i++) {
    if (i === 0) {
      cased += value[i].toUpperCase()
    } else {
      cased += value[i];
    }
  }
  return cased;
}
 
Handlebars.registerHelper({
  ne,
  eq,
  union,
  prefix,
  properCase,
  stringUnion,
  buildIndexType,
  concatIndexType,
  filterIndexType,
});
 
export default Handlebars;