template.d.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. export interface TemplateExpansionContext {
  2. /**
  3. * The template's name.
  4. */
  5. readonly name: string;
  6. }
  7. export type TemplateExpander = (this: TemplateExpansionContext) => string;
  8. export interface TemplateAPI {
  9. /**
  10. * Returns the number of existing templates.
  11. * @since 2.29.0
  12. * @example
  13. * if (Template.size === 0) {
  14. * // No templates exist.
  15. * }
  16. */
  17. readonly size: number;
  18. /**
  19. * Add new template(s).
  20. * @param name Name, or array of names, of the template(s) to add. **NOTE**: Names must consist of characters from the basic Latin
  21. * alphabet and start with a letter, which may be optionally followed by any number of letters, numbers, the underscore, or the hyphen.
  22. * @param definition Definition of the template(s), which may be a: function, string, or an array of either. **NOTE**: Each time array
  23. * definitions are referenced, one of their member templates is randomly selected to be the output source.
  24. * @since 2.29.0
  25. * @example
  26. * // Basic usage
  27. * // Define a function template named ?yolo.
  28. * Template.add('yolo', function () {
  29. * return either('YOLO', 'You Only Live Once');
  30. * });
  31. *
  32. * // Define a string template named ?nolf.
  33. * Template.add('nolf', 'No One Lives Forever');
  34. *
  35. * // Define an array of string templates named ?alsoYolo.
  36. * Template.add('alsoYolo', ['YOLO', 'You Only Live Once']);
  37. *
  38. * // Define an array of mixed string and function templates named ?cmyk.
  39. * Template.add('cmyk', [
  40. * 'Cyan',
  41. * function () {
  42. * return either('Magenta', 'Yellow');
  43. * },
  44. * 'Black'
  45. * ]);
  46. *
  47. * // Using the context object (this)
  48. * // Define a function template with two names, ?color and ?Color, whose output changes based on its name.
  49. * Template.add(['color', 'Color'], function () {
  50. * var color = either('red', 'green', 'blue');
  51. * return this.name === 'Color' ? color.toUpperFirst() : color;
  52. * });
  53. */
  54. add(name: string | string[], definition: string | string[] | TemplateExpander | TemplateExpander[]): void;
  55. /**
  56. * Remove existing template(s).
  57. * @param name Name, or array of names, of the template(s) to remove.
  58. * @since 2.29.0
  59. * @example
  60. * // Deletes the template ?yolo.
  61. * Template.delete('yolo');
  62. *
  63. * // Deletes the templates ?yolo and ?nolf.
  64. * Template.delete(['yolo', 'nolf']);
  65. */
  66. delete(name: string | string[]): void;
  67. /**
  68. * Return the named template definition, or null on failure.
  69. * @param name Name of the template whose definition should be returned.
  70. * @since 2.29.0
  71. * @example
  72. * // Returns the template ?yolo, or null if it doesn't exist.
  73. * var yolo = Template.get('yolo');
  74. */
  75. get(name: string): string | string[] | TemplateExpander | TemplateExpander[] | null;
  76. /**
  77. * Returns whether the named template exists.
  78. * @param name Name of the template to search for.
  79. * @since 2.29.0
  80. * @example
  81. * if (Template.has('yolo')) {
  82. * // A ?yolo template exists.
  83. * }
  84. */
  85. has(name: string): boolean;
  86. }
  87. export {};