All files / src/types/kmx/kmx-plus-builder build-vars.ts

40% Statements 26/65
100% Branches 0/0
0% Functions 0/1
40% Lines 26/65

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 661x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                
import { constants } from "@keymanapp/ldml-keyboard-constants";
import { KMXPlus } from "@keymanapp/common-types";
import { build_strs_index, BUILDER_STR_REF, BUILDER_STRS } from "./build-strs.js";
import { BUILDER_SECTION } from "./builder-section.js";
import { build_list_index, BUILDER_LIST, BUILDER_LIST_REF } from "./build-list.js";
import { build_elem_index, BUILDER_ELEM, BUILDER_ELEM_REF } from "./build-elem.js";
 
import KMXPlusData = KMXPlus.KMXPlusData;
 
interface BUILDER_VARS_ITEM {
  type: number;
  id: BUILDER_STR_REF; // str
  value: BUILDER_STR_REF; // str
  elem?: BUILDER_ELEM_REF; // elem
};
 
export interface BUILDER_VARS extends BUILDER_SECTION {
  markers: BUILDER_LIST_REF;
  varCount: number;
  varEntries: BUILDER_VARS_ITEM[];
};
 
/**
* Builder for the 'vars' section
*/
export function build_vars(kmxplus: KMXPlusData, sect_strs: BUILDER_STRS, sect_elem: BUILDER_ELEM, sect_list: BUILDER_LIST) : BUILDER_VARS {
  if(!kmxplus.vars) {
    return null;
  }

  const stringVars = kmxplus.vars.strings.map(v => <BUILDER_VARS_ITEM>{
    type: constants.vars_entry_type_string,
    id: build_strs_index(sect_strs, v.id),
    value: build_strs_index(sect_strs, v.value),
  });
  const setVars = kmxplus.vars.sets.map(v => <BUILDER_VARS_ITEM>{
    type: constants.vars_entry_type_set,
    id: build_strs_index(sect_strs, v.id),
    value: build_strs_index(sect_strs, v.value),
    elem: build_elem_index(sect_elem, v.items),
  });
  const uniSetVars = kmxplus.vars.usets.map(v => <BUILDER_VARS_ITEM>{
    type: constants.vars_entry_type_unicodeSet,
    id: build_strs_index(sect_strs, v.id),
    value: build_strs_index(sect_strs, v.value),
  });

  const vars: BUILDER_VARS = {
    ident: constants.hex_section_id(constants.section.vars),
    size: constants.length_vars +
      (constants.length_vars_item * kmxplus.vars.totalCount()),
    _offset: 0,
    markers: build_list_index(sect_list, kmxplus.vars.markers),
    varCount: kmxplus.vars.totalCount(),
    varEntries: [
      ...stringVars,
      ...setVars,
      ...uniSetVars,
    ],
  };
  // sort IDs by binary order
  vars.varEntries.sort((a,b) => a.id - b.id);

  return vars;
}