dynamic_cstr.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.DefaultComparator = exports.DynamicCstrParser = exports.DynamicCstr = exports.DynamicProperties = exports.Axis = void 0;
  4. var Axis;
  5. (function (Axis) {
  6. Axis["DOMAIN"] = "domain";
  7. Axis["STYLE"] = "style";
  8. Axis["LOCALE"] = "locale";
  9. Axis["TOPIC"] = "topic";
  10. Axis["MODALITY"] = "modality";
  11. })(Axis || (exports.Axis = Axis = {}));
  12. class DynamicProperties {
  13. static createProp(...cstrList) {
  14. const axes = DynamicCstr.DEFAULT_ORDER;
  15. const dynamicCstr = {};
  16. for (let i = 0, l = cstrList.length, k = axes.length; i < l && i < k; i++) {
  17. dynamicCstr[axes[i]] = cstrList[i];
  18. }
  19. return new DynamicProperties(dynamicCstr);
  20. }
  21. constructor(properties, order = Object.keys(properties)) {
  22. this.properties = properties;
  23. this.order = order;
  24. }
  25. getProperties() {
  26. return this.properties;
  27. }
  28. getOrder() {
  29. return this.order;
  30. }
  31. getAxes() {
  32. return this.order;
  33. }
  34. getProperty(key) {
  35. return this.properties[key];
  36. }
  37. updateProperties(props) {
  38. this.properties = props;
  39. }
  40. allProperties() {
  41. const propLists = [];
  42. this.order.forEach((key) => propLists.push(this.getProperty(key).slice()));
  43. return propLists;
  44. }
  45. toString() {
  46. const cstrStrings = [];
  47. this.order.forEach((key) => cstrStrings.push(key + ': ' + this.getProperty(key).toString()));
  48. return cstrStrings.join('\n');
  49. }
  50. }
  51. exports.DynamicProperties = DynamicProperties;
  52. class DynamicCstr extends DynamicProperties {
  53. static createCstr(...cstrList) {
  54. const axes = DynamicCstr.DEFAULT_ORDER;
  55. const dynamicCstr = {};
  56. for (let i = 0, l = cstrList.length, k = axes.length; i < l && i < k; i++) {
  57. dynamicCstr[axes[i]] = cstrList[i];
  58. }
  59. return new DynamicCstr(dynamicCstr);
  60. }
  61. static defaultCstr() {
  62. return DynamicCstr.createCstr.apply(null, DynamicCstr.DEFAULT_ORDER.map(function (x) {
  63. return DynamicCstr.DEFAULT_VALUES[x];
  64. }));
  65. }
  66. static validOrder(order) {
  67. const axes = DynamicCstr.DEFAULT_ORDER.slice();
  68. return order.every((x) => {
  69. const index = axes.indexOf(x);
  70. return index !== -1 && axes.splice(index, 1);
  71. });
  72. }
  73. constructor(components_, order) {
  74. const properties = {};
  75. for (const [key, value] of Object.entries(components_)) {
  76. properties[key] = [value];
  77. }
  78. super(properties, order);
  79. this.components = components_;
  80. }
  81. getComponents() {
  82. return this.components;
  83. }
  84. getValue(key) {
  85. return this.components[key];
  86. }
  87. getValues() {
  88. return this.order.map((key) => this.getValue(key));
  89. }
  90. allProperties() {
  91. const propLists = super.allProperties();
  92. for (let i = 0, props, key; (props = propLists[i]), (key = this.order[i]); i++) {
  93. const value = this.getValue(key);
  94. if (props.indexOf(value) === -1) {
  95. props.unshift(value);
  96. }
  97. }
  98. return propLists;
  99. }
  100. toString() {
  101. return this.getValues().join('.');
  102. }
  103. equal(cstr) {
  104. const keys1 = cstr.getAxes();
  105. if (this.order.length !== keys1.length) {
  106. return false;
  107. }
  108. for (let j = 0, key; (key = keys1[j]); j++) {
  109. const comp2 = this.getValue(key);
  110. if (!comp2 || cstr.getValue(key) !== comp2) {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. }
  117. exports.DynamicCstr = DynamicCstr;
  118. DynamicCstr.DEFAULT_ORDER = [
  119. Axis.LOCALE,
  120. Axis.MODALITY,
  121. Axis.DOMAIN,
  122. Axis.STYLE,
  123. Axis.TOPIC
  124. ];
  125. DynamicCstr.BASE_LOCALE = 'base';
  126. DynamicCstr.DEFAULT_VALUE = 'default';
  127. DynamicCstr.DEFAULT_VALUES = {
  128. [Axis.LOCALE]: 'en',
  129. [Axis.DOMAIN]: DynamicCstr.DEFAULT_VALUE,
  130. [Axis.STYLE]: DynamicCstr.DEFAULT_VALUE,
  131. [Axis.TOPIC]: DynamicCstr.DEFAULT_VALUE,
  132. [Axis.MODALITY]: 'speech'
  133. };
  134. class DynamicCstrParser {
  135. constructor(order) {
  136. this.order = order;
  137. }
  138. parse(str) {
  139. const order = str.split('.');
  140. const cstr = {};
  141. if (order.length > this.order.length) {
  142. throw new Error('Invalid dynamic constraint: ' + cstr);
  143. }
  144. let j = 0;
  145. for (let i = 0, key; (key = this.order[i]), order.length; i++, j++) {
  146. const value = order.shift();
  147. cstr[key] = value;
  148. }
  149. return new DynamicCstr(cstr, this.order.slice(0, j));
  150. }
  151. }
  152. exports.DynamicCstrParser = DynamicCstrParser;
  153. class DefaultComparator {
  154. constructor(reference, fallback = new DynamicProperties(reference.getProperties(), reference.getOrder())) {
  155. this.reference = reference;
  156. this.fallback = fallback;
  157. this.order = this.reference.getOrder();
  158. }
  159. getReference() {
  160. return this.reference;
  161. }
  162. setReference(cstr, props) {
  163. this.reference = cstr;
  164. this.fallback =
  165. props || new DynamicProperties(cstr.getProperties(), cstr.getOrder());
  166. this.order = this.reference.getOrder();
  167. }
  168. match(cstr) {
  169. const keys1 = cstr.getAxes();
  170. return (keys1.length === this.reference.getAxes().length &&
  171. keys1.every((key) => {
  172. const value = cstr.getValue(key);
  173. return (value === this.reference.getValue(key) ||
  174. this.fallback.getProperty(key).indexOf(value) !== -1);
  175. }));
  176. }
  177. compare(cstr1, cstr2) {
  178. let ignore = false;
  179. for (let i = 0, key; (key = this.order[i]); i++) {
  180. const value1 = cstr1.getValue(key);
  181. const value2 = cstr2.getValue(key);
  182. if (!ignore) {
  183. const ref = this.reference.getValue(key);
  184. if (ref === value1 && ref !== value2) {
  185. return -1;
  186. }
  187. if (ref === value2 && ref !== value1) {
  188. return 1;
  189. }
  190. if (ref === value1 && ref === value2) {
  191. continue;
  192. }
  193. if (ref !== value1 && ref !== value2) {
  194. ignore = true;
  195. }
  196. }
  197. const prop = this.fallback.getProperty(key);
  198. const index1 = prop.indexOf(value1);
  199. const index2 = prop.indexOf(value2);
  200. if (index1 < index2) {
  201. return -1;
  202. }
  203. if (index2 < index1) {
  204. return 1;
  205. }
  206. }
  207. return 0;
  208. }
  209. toString() {
  210. return this.reference.toString() + '\n' + this.fallback.toString();
  211. }
  212. }
  213. exports.DefaultComparator = DefaultComparator;