auditory_description.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.AuditoryDescription = exports.AuditoryList = exports.AuditoryItem = void 0;
  4. const grammar_js_1 = require("../rule_engine/grammar.js");
  5. const span_js_1 = require("./span.js");
  6. class AuditoryItem {
  7. constructor(data = null) {
  8. this.data = data;
  9. this.prev = null;
  10. this.next = null;
  11. }
  12. }
  13. exports.AuditoryItem = AuditoryItem;
  14. class AuditoryList extends Set {
  15. constructor(descrs) {
  16. super();
  17. this.annotations = [];
  18. this.anchor = new AuditoryItem();
  19. this.anchor.next = this.anchor;
  20. this.anchor.prev = this.anchor;
  21. descrs.forEach((d) => {
  22. const item = new AuditoryItem(d);
  23. if (d.annotation) {
  24. this.annotations.push(item);
  25. }
  26. this.push(item);
  27. });
  28. }
  29. first() {
  30. return this.empty ? null : this.anchor.next;
  31. }
  32. last() {
  33. return this.empty ? null : this.anchor.prev;
  34. }
  35. push(item) {
  36. item.next = this.anchor;
  37. item.prev = this.anchor.prev;
  38. item.prev.next = item;
  39. this.anchor.prev = item;
  40. super.add(item);
  41. }
  42. pop() {
  43. const item = this.last();
  44. if (!item) {
  45. return null;
  46. }
  47. this.delete(item);
  48. return item;
  49. }
  50. delete(item) {
  51. if (!this.has(item)) {
  52. return false;
  53. }
  54. super.delete(item);
  55. item.prev.next = item.next;
  56. item.next = item.prev;
  57. return true;
  58. }
  59. insertAfter(descr, item) {
  60. this.insertBefore(descr, item.next);
  61. }
  62. insertBefore(descr, item) {
  63. const nitem = new AuditoryItem(descr);
  64. if (!item || !this.has(item)) {
  65. this.push(nitem);
  66. return;
  67. }
  68. item.prev.next = nitem;
  69. nitem.prev = item.prev;
  70. nitem.next = item;
  71. item.prev = nitem;
  72. }
  73. prevText(item) {
  74. do {
  75. item = item.prev;
  76. } while (item !== this.anchor && !item.data.text);
  77. return item === this.anchor ? null : item;
  78. }
  79. *[Symbol.iterator]() {
  80. let current = this.anchor.next;
  81. while (current !== this.anchor) {
  82. yield current;
  83. current = current.next;
  84. }
  85. }
  86. nextText(item) {
  87. while (item !== this.anchor && !item.data.text) {
  88. item = item.next;
  89. }
  90. return item;
  91. }
  92. clear() {
  93. this.anchor.next = this.anchor;
  94. this.anchor.prev = this.anchor;
  95. super.clear();
  96. }
  97. empty() {
  98. return this.anchor.prev === this.anchor && this.anchor === this.anchor.next;
  99. }
  100. toList() {
  101. const result = [];
  102. let item = this.anchor.next;
  103. while (item !== this.anchor) {
  104. result.push(item.data);
  105. item = item.next;
  106. }
  107. return result;
  108. }
  109. }
  110. exports.AuditoryList = AuditoryList;
  111. class AuditoryDescription {
  112. static create(args, flags = {}) {
  113. args.text = grammar_js_1.Grammar.getInstance().apply(args.text, flags);
  114. return new AuditoryDescription(args);
  115. }
  116. constructor({ context, text, userValue, annotation, attributes, personality, layout }) {
  117. this.context = context || '';
  118. this.text = text || '';
  119. this.userValue = userValue || '';
  120. this.annotation = annotation || '';
  121. this.attributes = attributes || {};
  122. this.personality = personality || {};
  123. this.layout = layout || '';
  124. }
  125. isEmpty() {
  126. return (this.context.length === 0 &&
  127. this.text.length === 0 &&
  128. this.userValue.length === 0 &&
  129. this.annotation.length === 0);
  130. }
  131. clone() {
  132. let personality;
  133. if (this.personality) {
  134. personality = {};
  135. for (const [key, val] of Object.entries(this.personality)) {
  136. personality[key] = val;
  137. }
  138. }
  139. let attributes;
  140. if (this.attributes) {
  141. attributes = {};
  142. for (const [key, val] of Object.entries(this.attributes)) {
  143. attributes[key] = val;
  144. }
  145. }
  146. return new AuditoryDescription({
  147. context: this.context,
  148. text: this.text,
  149. userValue: this.userValue,
  150. annotation: this.annotation,
  151. personality: personality,
  152. attributes: attributes,
  153. layout: this.layout
  154. });
  155. }
  156. toString() {
  157. return ('AuditoryDescription(context="' +
  158. this.context +
  159. '" ' +
  160. ' text="' +
  161. this.text +
  162. '" ' +
  163. ' userValue="' +
  164. this.userValue +
  165. '" ' +
  166. ' annotation="' +
  167. this.annotation +
  168. '")');
  169. }
  170. descriptionString() {
  171. return this.context && this.text
  172. ? this.context + ' ' + this.text
  173. : this.context || this.text;
  174. }
  175. descriptionSpan() {
  176. return span_js_1.Span.stringAttr(this.descriptionString(), this.attributes);
  177. }
  178. equals(that) {
  179. return (this.context === that.context &&
  180. this.text === that.text &&
  181. this.userValue === that.userValue &&
  182. this.annotation === that.annotation);
  183. }
  184. }
  185. exports.AuditoryDescription = AuditoryDescription;