math_compound_store.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.addCharacterRules = exports.baseStores = exports.subStores = void 0;
  4. exports.changeLocale = changeLocale;
  5. exports.setSiPrefixes = setSiPrefixes;
  6. exports.defineRules = defineRules;
  7. exports.defineRule = defineRule;
  8. exports.addSymbolRules = addSymbolRules;
  9. exports.addFunctionRules = addFunctionRules;
  10. exports.addUnitRules = addUnitRules;
  11. exports.lookupRule = lookupRule;
  12. exports.lookupCategory = lookupCategory;
  13. exports.lookupString = lookupString;
  14. exports.enumerate = enumerate;
  15. exports.reset = reset;
  16. const engine_js_1 = require("../common/engine.js");
  17. const l10n_js_1 = require("../l10n/l10n.js");
  18. const semantic_attr_js_1 = require("../semantic_tree/semantic_attr.js");
  19. const math_simple_store_js_1 = require("./math_simple_store.js");
  20. const dynamic_cstr_js_1 = require("./dynamic_cstr.js");
  21. let locale = dynamic_cstr_js_1.DynamicCstr.DEFAULT_VALUES[dynamic_cstr_js_1.Axis.LOCALE];
  22. let modality = dynamic_cstr_js_1.DynamicCstr.DEFAULT_VALUES[dynamic_cstr_js_1.Axis.MODALITY];
  23. function changeLocale(json) {
  24. if (!json['locale'] && !json['modality']) {
  25. return false;
  26. }
  27. locale = json['locale'] || locale;
  28. modality = json['modality'] || modality;
  29. return true;
  30. }
  31. let siPrefixes = {};
  32. function setSiPrefixes(prefixes) {
  33. siPrefixes = prefixes;
  34. }
  35. exports.subStores = new Map();
  36. exports.baseStores = new Map();
  37. function getSubStore(base, key) {
  38. let store = exports.subStores.get(key);
  39. if (store) {
  40. return store;
  41. }
  42. store = new math_simple_store_js_1.MathSimpleStore();
  43. store.base = exports.baseStores.get(base);
  44. exports.subStores.set(key, store);
  45. return store;
  46. }
  47. function completeWithBase(json) {
  48. const base = exports.baseStores.get(json.key);
  49. if (!base) {
  50. return;
  51. }
  52. const names = json.names;
  53. Object.assign(json, base);
  54. if (names && base.names) {
  55. json.names = json.names.concat(names);
  56. }
  57. }
  58. function defineRules(base, str, mappings) {
  59. const store = getSubStore(base, str);
  60. store.defineRulesFromMappings(locale, modality, mappings);
  61. }
  62. function defineRule(domain, style, str, content) {
  63. const store = getSubStore(str, str);
  64. store.defineRuleFromStrings(locale, modality, domain, style, content);
  65. }
  66. function addSymbolRules(json) {
  67. for (const rule of json) {
  68. if (changeLocale(rule)) {
  69. continue;
  70. }
  71. const key = math_simple_store_js_1.MathSimpleStore.parseUnicode(rule['key']);
  72. if (locale === 'base') {
  73. exports.baseStores.set(key, rule);
  74. continue;
  75. }
  76. defineRules(key, key, rule['mappings']);
  77. }
  78. }
  79. function addCharacterRule(json) {
  80. if (changeLocale(json)) {
  81. return;
  82. }
  83. for (const [key, value] of Object.entries(json)) {
  84. defineRule('default', 'default', key, value);
  85. }
  86. }
  87. const addCharacterRules = (json) => json.forEach(addCharacterRule);
  88. exports.addCharacterRules = addCharacterRules;
  89. function addFunctionRule(json) {
  90. for (let j = 0, name; (name = json.names[j]); j++) {
  91. defineRules(json.key, name, json.mappings);
  92. }
  93. }
  94. function addFunctionRules(json) {
  95. for (const rule of json) {
  96. if (changeLocale(rule)) {
  97. continue;
  98. }
  99. (0, semantic_attr_js_1.addFunctionSemantic)(rule.key, rule.names || []);
  100. if (locale === 'base') {
  101. exports.baseStores.set(rule.key, rule);
  102. continue;
  103. }
  104. completeWithBase(rule);
  105. addFunctionRule(rule);
  106. }
  107. }
  108. function addUnitRules(json) {
  109. for (const rule of json) {
  110. if (changeLocale(rule)) {
  111. continue;
  112. }
  113. rule.key += ':unit';
  114. if (locale === 'base') {
  115. exports.baseStores.set(rule.key, rule);
  116. continue;
  117. }
  118. completeWithBase(rule);
  119. if (rule.names) {
  120. rule.names = rule.names.map(function (name) {
  121. return name + ':unit';
  122. });
  123. }
  124. if (rule.si) {
  125. addSiUnitRule(rule);
  126. }
  127. addFunctionRule(rule);
  128. }
  129. }
  130. function addSiUnitRule(json) {
  131. for (const key of Object.keys(siPrefixes)) {
  132. const newJson = Object.assign({}, json);
  133. newJson.mappings = {};
  134. const prefix = siPrefixes[key];
  135. newJson['names'] = newJson['names'].map(function (name) {
  136. return key + name;
  137. });
  138. for (const domain of Object.keys(json['mappings'])) {
  139. newJson.mappings[domain] = {};
  140. for (const style of Object.keys(json['mappings'][domain])) {
  141. newJson['mappings'][domain][style] = l10n_js_1.locales[locale]().FUNCTIONS.si(prefix, json['mappings'][domain][style]);
  142. }
  143. }
  144. addFunctionRule(newJson);
  145. }
  146. }
  147. function lookupRule(node, dynamic) {
  148. const store = exports.subStores.get(node);
  149. return store ? store.lookupRule(null, dynamic) : null;
  150. }
  151. function lookupCategory(character) {
  152. const store = exports.subStores.get(character);
  153. return (store === null || store === void 0 ? void 0 : store.base) ? store.base.category : '';
  154. }
  155. function lookupString(text, dynamic) {
  156. const rule = lookupRule(text, dynamic);
  157. if (!rule) {
  158. return null;
  159. }
  160. return rule.action;
  161. }
  162. engine_js_1.Engine.getInstance().evaluator = lookupString;
  163. function enumerate(info = {}) {
  164. for (const store of exports.subStores.values()) {
  165. for (const [, rules] of store.rules.entries()) {
  166. for (const { cstr: dynamic } of rules) {
  167. info = enumerate_(dynamic.getValues(), info);
  168. }
  169. }
  170. }
  171. return info;
  172. }
  173. function enumerate_(dynamic, info) {
  174. info = info || {};
  175. if (!dynamic.length) {
  176. return info;
  177. }
  178. info[dynamic[0]] = enumerate_(dynamic.slice(1), info[dynamic[0]]);
  179. return info;
  180. }
  181. function reset() {
  182. locale = dynamic_cstr_js_1.DynamicCstr.DEFAULT_VALUES[dynamic_cstr_js_1.Axis.LOCALE];
  183. modality = dynamic_cstr_js_1.DynamicCstr.DEFAULT_VALUES[dynamic_cstr_js_1.Axis.MODALITY];
  184. }