speech_rule_functions.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. class FunctionsStore {
  2. constructor(prefix, store) {
  3. this.prefix = prefix;
  4. this.store = store;
  5. }
  6. add(name, func) {
  7. if (this.checkCustomFunctionSyntax_(name)) {
  8. this.store[name] = func;
  9. }
  10. }
  11. addStore(store) {
  12. const keys = Object.keys(store.store);
  13. for (let i = 0, key; (key = keys[i]); i++) {
  14. this.add(key, store.store[key]);
  15. }
  16. }
  17. lookup(name) {
  18. return this.store[name];
  19. }
  20. checkCustomFunctionSyntax_(name) {
  21. const reg = new RegExp('^' + this.prefix);
  22. if (!name.match(reg)) {
  23. console.error('FunctionError: Invalid function name. Expected prefix ' + this.prefix);
  24. return false;
  25. }
  26. return true;
  27. }
  28. }
  29. export class CustomQueries extends FunctionsStore {
  30. constructor() {
  31. const store = {};
  32. super('CQF', store);
  33. }
  34. }
  35. export class CustomStrings extends FunctionsStore {
  36. constructor() {
  37. const store = {};
  38. super('CSF', store);
  39. }
  40. }
  41. export class ContextFunctions extends FunctionsStore {
  42. constructor() {
  43. const store = {};
  44. super('CTF', store);
  45. }
  46. }
  47. export class CustomGenerators extends FunctionsStore {
  48. constructor() {
  49. const store = {};
  50. super('CGF', store);
  51. }
  52. }