math_simple_store.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.MathSimpleStore = void 0;
  4. const engine_js_1 = require("../common/engine.js");
  5. const dynamic_cstr_js_1 = require("./dynamic_cstr.js");
  6. class MathSimpleStore {
  7. constructor() {
  8. this.rules = new Map();
  9. }
  10. static parseUnicode(num) {
  11. const keyValue = parseInt(num, 16);
  12. return String.fromCodePoint(keyValue);
  13. }
  14. static testDynamicConstraints_(dynamic, rule) {
  15. if (engine_js_1.Engine.getInstance().strict) {
  16. return rule.cstr.equal(dynamic);
  17. }
  18. return engine_js_1.Engine.getInstance().comparator.match(rule.cstr);
  19. }
  20. defineRulesFromMappings(locale, modality, mapping) {
  21. for (const [domain, styles] of Object.entries(mapping)) {
  22. for (const [style, content] of Object.entries(styles)) {
  23. this.defineRuleFromStrings(locale, modality, domain, style, content);
  24. }
  25. }
  26. }
  27. getRules(key) {
  28. let store = this.rules.get(key);
  29. if (!store) {
  30. store = [];
  31. this.rules.set(key, store);
  32. }
  33. return store;
  34. }
  35. defineRuleFromStrings(locale, modality, domain, style, content) {
  36. let store = this.getRules(locale);
  37. const parser = engine_js_1.Engine.getInstance().parsers[domain] ||
  38. engine_js_1.Engine.getInstance().defaultParser;
  39. const comp = engine_js_1.Engine.getInstance().comparators[domain];
  40. const cstr = `${locale}.${modality}.${domain}.${style}`;
  41. const dynamic = parser.parse(cstr);
  42. const comparator = comp ? comp() : engine_js_1.Engine.getInstance().comparator;
  43. const oldCstr = comparator.getReference();
  44. comparator.setReference(dynamic);
  45. const rule = { cstr: dynamic, action: content };
  46. store = store.filter((r) => !dynamic.equal(r.cstr));
  47. store.push(rule);
  48. this.rules.set(locale, store);
  49. comparator.setReference(oldCstr);
  50. }
  51. lookupRule(_node, dynamic) {
  52. let rules = this.getRules(dynamic.getValue(dynamic_cstr_js_1.Axis.LOCALE));
  53. rules = rules.filter(function (rule) {
  54. return MathSimpleStore.testDynamicConstraints_(dynamic, rule);
  55. });
  56. if (rules.length === 1) {
  57. return rules[0];
  58. }
  59. return rules.length
  60. ? rules.sort((r1, r2) => engine_js_1.Engine.getInstance().comparator.compare(r1.cstr, r2.cstr))[0]
  61. : null;
  62. }
  63. }
  64. exports.MathSimpleStore = MathSimpleStore;