@discord/intl 0.5.2 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/format.d.ts CHANGED
@@ -16,7 +16,7 @@
16
16
  * - adding extra context for hook and link functions to provide a `key`
17
17
  * parameter, important to have React treat the resulting elements nicely.
18
18
  */
19
- import { FormatJsNode } from './keyless-json';
19
+ import { AstNode } from '@discord/intl-ast';
20
20
  import { Formatters } from 'intl-messageformat';
21
21
  import type { FormatConfig } from './format-config';
22
22
  import type { RichTextTagNames } from './types';
@@ -27,5 +27,5 @@ export declare abstract class FormatBuilder<Result> {
27
27
  abstract finish(): Result[];
28
28
  }
29
29
  export type FormatBuilderConstructor<Result> = new () => FormatBuilder<Result>;
30
- export declare function bindFormatValuesWithBuilder<T, Builder extends FormatBuilder<T>>(builder: Builder, nodes: FormatJsNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number, originalMessage?: string): void;
31
- export declare function bindFormatValues<Result>(Builder: FormatBuilderConstructor<Result>, nodes: FormatJsNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number): Result[];
30
+ export declare function bindFormatValuesWithBuilder<T, Builder extends FormatBuilder<T>>(builder: Builder, nodes: AstNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number, originalMessage?: string): void;
31
+ export declare function bindFormatValues<Result>(Builder: FormatBuilderConstructor<Result>, nodes: AstNode[], locales: string | string[], formatters: Formatters, formatConfig: FormatConfig, values?: Record<string, string | object>, currentPluralValue?: number): Result[];
package/dist/format.js CHANGED
@@ -17,24 +17,13 @@
17
17
  * - adding extra context for hook and link functions to provide a `key`
18
18
  * parameter, important to have React treat the resulting elements nicely.
19
19
  */
20
- var __values = (this && this.__values) || function(o) {
21
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
22
- if (m) return m.call(o);
23
- if (o && typeof o.length === "number") return {
24
- next: function () {
25
- if (o && i >= o.length) o = void 0;
26
- return { value: o && o[i++], done: !o };
27
- }
28
- };
29
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
30
- };
31
20
  Object.defineProperty(exports, "__esModule", { value: true });
32
21
  exports.FormatBuilder = void 0;
33
22
  exports.bindFormatValuesWithBuilder = bindFormatValuesWithBuilder;
34
23
  exports.bindFormatValues = bindFormatValues;
35
- var icu_skeleton_parser_1 = require("@formatjs/icu-skeleton-parser");
36
- var keyless_json_1 = require("./keyless-json");
37
- var intl_messageformat_1 = require("intl-messageformat");
24
+ const icu_skeleton_parser_1 = require("@formatjs/icu-skeleton-parser");
25
+ const intl_ast_1 = require("@discord/intl-ast");
26
+ const intl_messageformat_1 = require("intl-messageformat");
38
27
  /**
39
28
  * Returns true if the tag name should be considered a rich text tag that
40
29
  * applies formatting to a message, rather than being a user-supplied value.
@@ -42,46 +31,41 @@ var intl_messageformat_1 = require("intl-messageformat");
42
31
  function isRichTextTag(name) {
43
32
  return name[0] === '$';
44
33
  }
45
- var FormatBuilder = /** @class */ (function () {
46
- function FormatBuilder() {
47
- }
48
- return FormatBuilder;
49
- }());
34
+ class FormatBuilder {
35
+ }
50
36
  exports.FormatBuilder = FormatBuilder;
51
- function bindFormatValuesWithBuilder(builder, nodes, locales, formatters, formatConfig, values, currentPluralValue, originalMessage) {
52
- var e_1, _a;
53
- var _b, _c;
54
- if (values === void 0) { values = {}; }
37
+ function bindFormatValuesWithBuilder(builder, nodes, locales, formatters, formatConfig, values = {}, currentPluralValue, originalMessage) {
38
+ var _a, _b;
55
39
  // Hot path for static messages that are just parsed as a single string element.
56
- if (nodes.length === 1 && nodes[0].type === keyless_json_1.FormatJsNodeType.Literal) {
57
- builder.pushLiteralText(nodes[0].value);
40
+ if (nodes.length === 1 && nodes[0][intl_ast_1.AstNodeIndices.Type] === intl_ast_1.FormatJsNodeType.Literal) {
41
+ builder.pushLiteralText(nodes[0][intl_ast_1.AstNodeIndices.Value]);
58
42
  return;
59
43
  }
60
- var _loop_1 = function (node) {
61
- var e_2, _d;
62
- switch (node.type) {
63
- case keyless_json_1.FormatJsNodeType.Literal:
64
- builder.pushLiteralText(node.value);
65
- return "continue";
66
- case keyless_json_1.FormatJsNodeType.Pound:
44
+ for (const node of nodes) {
45
+ const nodeType = node[intl_ast_1.AstNodeIndices.Type];
46
+ switch (nodeType) {
47
+ case intl_ast_1.FormatJsNodeType.Literal:
48
+ builder.pushLiteralText(node[intl_ast_1.AstNodeIndices.Value]);
49
+ continue;
50
+ case intl_ast_1.FormatJsNodeType.Pound:
67
51
  // Replace `#` in plural rules with the actual numeric value. Only
68
52
  // numeric values are replaced, otherwise the value is completed ignored?
69
53
  // Behavior copied from FormatJS directly.
70
54
  if (typeof currentPluralValue === 'number') {
71
- var value_1 = formatters.getNumberFormat(locales).format(currentPluralValue);
72
- builder.pushLiteralText(value_1);
55
+ const value = formatters.getNumberFormat(locales).format(currentPluralValue);
56
+ builder.pushLiteralText(value);
73
57
  }
74
- return "continue";
58
+ continue;
75
59
  }
76
- var variableName = node.value;
60
+ const variableName = node[intl_ast_1.AstNodeIndices.Value];
77
61
  // Enforce that all required values are provided by the caller, even if the
78
62
  // actual value is falsy/undefined.
79
63
  if (!(variableName in values) && !isRichTextTag(variableName)) {
80
64
  throw new intl_messageformat_1.MissingValueError(variableName, originalMessage);
81
65
  }
82
- var value = values[variableName];
83
- switch (node.type) {
84
- case keyless_json_1.FormatJsNodeType.Argument:
66
+ const value = values[variableName];
67
+ switch (nodeType) {
68
+ case intl_ast_1.FormatJsNodeType.Argument:
85
69
  // Empty values don't need to be added at all, they are purely for AST representation.
86
70
  if (variableName == '$_')
87
71
  break;
@@ -94,125 +78,110 @@ function bindFormatValuesWithBuilder(builder, nodes, locales, formatters, format
94
78
  builder.pushLiteralText(String(value));
95
79
  }
96
80
  break;
97
- case keyless_json_1.FormatJsNodeType.Date: {
81
+ case intl_ast_1.FormatJsNodeType.Date: {
82
+ const nodeStyle = node[intl_ast_1.AstNodeIndices.Style];
98
83
  // Distinct from FormatJS: We don't currently parse the skeleton ahead of time in the AST,
99
84
  // so this manages parsing the skeleton as well before passing it onto the date formatter.
100
- var style = node.style in formatConfig.date
101
- ? formatConfig.date[node.style]
102
- : node.style != null
103
- ? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(node.style)
85
+ const style = nodeStyle in formatConfig.date
86
+ ? formatConfig.date[nodeStyle]
87
+ : nodeStyle != null
88
+ ? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(nodeStyle)
104
89
  : formatConfig.time.medium;
105
90
  // @ts-expect-error Cast string values to dates properly.
106
91
  builder.pushLiteralText(formatters.getDateTimeFormat(locales, style).format(value));
107
92
  break;
108
93
  }
109
- case keyless_json_1.FormatJsNodeType.Time: {
94
+ case intl_ast_1.FormatJsNodeType.Time: {
95
+ const nodeStyle = node[intl_ast_1.AstNodeIndices.Style];
110
96
  // Distinct from FormatJS: We don't currently parse the skeleton ahead of time in the AST,
111
97
  // so this manages parsing the skeleton as well before passing it onto the date formatter.
112
- var style = node.style in formatConfig.time
113
- ? formatConfig.time[node.style]
114
- : node.style != null
115
- ? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(node.style)
98
+ const style = nodeStyle in formatConfig.time
99
+ ? formatConfig.time[nodeStyle]
100
+ : nodeStyle != null
101
+ ? (0, icu_skeleton_parser_1.parseDateTimeSkeleton)(nodeStyle)
116
102
  : undefined; // TODO: parseSkeleton();
117
103
  builder.pushLiteralText(
118
104
  // @ts-expect-error Cast string values to dates properly.
119
105
  formatters.getDateTimeFormat(locales, style).format(value));
120
106
  break;
121
107
  }
122
- case keyless_json_1.FormatJsNodeType.Number: {
108
+ case intl_ast_1.FormatJsNodeType.Number: {
109
+ const nodeStyle = node[intl_ast_1.AstNodeIndices.Style];
123
110
  // Distinct from FormatJS: We don't currently parse the skeleton ahead of time in the AST,
124
111
  // so this manages parsing the skeleton as well before passing it onto the date formatter.
125
- var style = node.style in formatConfig.number
126
- ? formatConfig.number[node.style]
127
- : node.style != null
128
- ? (0, icu_skeleton_parser_1.parseNumberSkeleton)((0, icu_skeleton_parser_1.parseNumberSkeletonFromString)(node.style))
112
+ const style = nodeStyle in formatConfig.number
113
+ ? formatConfig.number[nodeStyle]
114
+ : nodeStyle != null
115
+ ? (0, icu_skeleton_parser_1.parseNumberSkeleton)((0, icu_skeleton_parser_1.parseNumberSkeletonFromString)(nodeStyle))
129
116
  : undefined;
130
- var scaledValue =
117
+ const scaledValue =
131
118
  // @ts-expect-error This is a weird cast that's not accurate, but works in the short term.
132
- typeof value !== 'number' ? value : value * ((_b = style === null || style === void 0 ? void 0 : style.scale) !== null && _b !== void 0 ? _b : 1);
119
+ typeof value !== 'number' ? value : value * ((_a = style === null || style === void 0 ? void 0 : style.scale) !== null && _a !== void 0 ? _a : 1);
133
120
  builder.pushLiteralText(formatters.getNumberFormat(locales, style).format(scaledValue));
134
121
  break;
135
122
  }
136
- case keyless_json_1.FormatJsNodeType.Tag: {
137
- var children = node.children;
138
- var appliedChildren = bindFormatValues(builder.constructor, children, locales, formatters, formatConfig, values, currentPluralValue);
123
+ case intl_ast_1.FormatJsNodeType.Tag: {
124
+ const children = node[intl_ast_1.AstNodeIndices.Children];
125
+ const appliedChildren = bindFormatValues(builder.constructor, children, locales, formatters, formatConfig, values, currentPluralValue);
139
126
  if (isRichTextTag(variableName)) {
140
127
  builder.pushRichTextTag(variableName, appliedChildren);
141
128
  }
142
129
  else {
143
130
  if (typeof value !== 'function') {
144
- throw "expected a function type for a Tag formatting value, ".concat(variableName, ". got ").concat(typeof value, ": ").concat(value);
131
+ throw `expected a function type for a Tag formatting value, ${variableName}. got ${typeof value}: ${value}`;
145
132
  }
146
- var chunks = value(appliedChildren);
133
+ let chunks = value(appliedChildren);
147
134
  chunks = Array.isArray(chunks) ? chunks : [chunks];
148
- try {
149
- for (var chunks_1 = (e_2 = void 0, __values(chunks)), chunks_1_1 = chunks_1.next(); !chunks_1_1.done; chunks_1_1 = chunks_1.next()) {
150
- var chunk = chunks_1_1.value;
151
- if (typeof chunk === 'string') {
152
- builder.pushLiteralText(chunk);
153
- }
154
- else {
155
- builder.pushObject(chunk);
156
- }
135
+ for (const chunk of chunks) {
136
+ if (typeof chunk === 'string') {
137
+ builder.pushLiteralText(chunk);
157
138
  }
158
- }
159
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
160
- finally {
161
- try {
162
- if (chunks_1_1 && !chunks_1_1.done && (_d = chunks_1.return)) _d.call(chunks_1);
139
+ else {
140
+ builder.pushObject(chunk);
163
141
  }
164
- finally { if (e_2) throw e_2.error; }
165
142
  }
166
143
  }
167
144
  break;
168
145
  }
169
- case keyless_json_1.FormatJsNodeType.Select: {
170
- var option = node.value in node.options ? node.options[node.value] : node.options.other;
146
+ case intl_ast_1.FormatJsNodeType.Select: {
147
+ const value = node[intl_ast_1.AstNodeIndices.Value];
148
+ const options = node[intl_ast_1.AstNodeIndices.Options];
149
+ const option = value in options ? options[value] : options.other;
171
150
  if (option == null) {
172
- throw "".concat(node.value, " is not a known option for select value ").concat(variableName, ". Valid options are ").concat(Object.keys(node.options).join(', '));
151
+ throw `${value} is not a known option for select value ${variableName}. Valid options are ${Object.keys(options).join(', ')}`;
173
152
  }
174
153
  bindFormatValuesWithBuilder(builder, option.value, locales, formatters, formatConfig, values);
175
154
  break;
176
155
  }
177
- case keyless_json_1.FormatJsNodeType.Plural: {
178
- var option = (function () {
179
- var _a, _b;
180
- var exactSelector = "=".concat(value);
181
- if (exactSelector in node.options)
182
- return node.options[exactSelector];
183
- var rule = formatters
184
- .getPluralRules(locales, { type: node.pluralType })
156
+ case intl_ast_1.FormatJsNodeType.Plural: {
157
+ const value = node[intl_ast_1.AstNodeIndices.Value];
158
+ const options = node[intl_ast_1.AstNodeIndices.Options];
159
+ const offset = node[intl_ast_1.AstNodeIndices.Offset];
160
+ const pluralType = node[intl_ast_1.AstNodeIndices.PluralType];
161
+ const option = (() => {
162
+ var _a;
163
+ const exactSelector = `=${value}`;
164
+ if (exactSelector in options)
165
+ return options[exactSelector];
166
+ const rule = formatters
167
+ .getPluralRules(locales, { type: pluralType })
185
168
  // @ts-expect-error Assert this `as number` properly.
186
- .select(value - ((_a = node.offset) !== null && _a !== void 0 ? _a : 0));
187
- return (_b = node.options[rule]) !== null && _b !== void 0 ? _b : node.options.other;
169
+ .select(value - (offset !== null && offset !== void 0 ? offset : 0));
170
+ return (_a = options[rule]) !== null && _a !== void 0 ? _a : options.other;
188
171
  })();
189
172
  if (option == null) {
190
- throw "".concat(node.value, " is not a known option for plural value ").concat(variableName, ". Valid options are ").concat(Object.keys(node.options).join(', '));
173
+ throw `${value} is not a known option for plural value ${variableName}. Valid options are ${Object.keys(options).join(', ')}`;
191
174
  }
192
175
  bindFormatValuesWithBuilder(builder, option.value, locales, formatters, formatConfig, values,
193
176
  // @ts-expect-error assert this `as number` properly.
194
- value - ((_c = node.offset) !== null && _c !== void 0 ? _c : 0));
177
+ value - ((_b = node.offset) !== null && _b !== void 0 ? _b : 0));
195
178
  break;
196
179
  }
197
180
  }
198
- };
199
- try {
200
- for (var nodes_1 = __values(nodes), nodes_1_1 = nodes_1.next(); !nodes_1_1.done; nodes_1_1 = nodes_1.next()) {
201
- var node = nodes_1_1.value;
202
- _loop_1(node);
203
- }
204
- }
205
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
206
- finally {
207
- try {
208
- if (nodes_1_1 && !nodes_1_1.done && (_a = nodes_1.return)) _a.call(nodes_1);
209
- }
210
- finally { if (e_1) throw e_1.error; }
211
181
  }
212
182
  }
213
- function bindFormatValues(Builder, nodes, locales, formatters, formatConfig, values, currentPluralValue) {
214
- if (values === void 0) { values = {}; }
215
- var builder = new Builder();
183
+ function bindFormatValues(Builder, nodes, locales, formatters, formatConfig, values = {}, currentPluralValue) {
184
+ const builder = new Builder();
216
185
  bindFormatValuesWithBuilder(builder, nodes, locales, formatters, formatConfig, values, currentPluralValue);
217
186
  return builder.finish();
218
187
  }
@@ -1,48 +1,8 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
2
  Object.defineProperty(exports, "__esModule", { value: true });
43
3
  exports.astFormatter = exports.RichTextNodeType = void 0;
44
4
  exports.formatToAst = formatToAst;
45
- var format_1 = require("../format");
5
+ const format_1 = require("../format");
46
6
  // This structure aims to match `simple-markdown`'s AST, but that type is very
47
7
  // loosely defined as just "type" and "arbitrary map of content". This is more
48
8
  // explicit and type-safe.
@@ -66,59 +26,52 @@ var RichTextNodeType;
66
26
  */
67
27
  RichTextNodeType["Object"] = "object";
68
28
  })(RichTextNodeType || (exports.RichTextNodeType = RichTextNodeType = {}));
69
- var AST_RICH_TEXT_ELEMENTS = {
70
- $_: function () { return ({ type: RichTextNodeType.Text, content: '' }); },
71
- $b: function (content) { return ({ type: RichTextNodeType.Strong, content: content }); },
72
- $i: function (content) { return ({ type: RichTextNodeType.Emphasis, content: content }); },
73
- $del: function (content) { return ({ type: RichTextNodeType.Strikethrough, content: content }); },
74
- $code: function (content) { return ({ type: RichTextNodeType.Code, content: content }); },
75
- $link: function (_a) {
76
- var _b = __read(_a), target = _b[0], content = _b.slice(1);
77
- return ({
78
- type: RichTextNodeType.Link,
79
- target: target.content,
80
- content: content,
81
- });
82
- },
83
- $p: function (content) { return ({ type: RichTextNodeType.Paragraph, content: content }); },
29
+ const AST_RICH_TEXT_ELEMENTS = {
30
+ $_: () => ({ type: RichTextNodeType.Text, content: '' }),
31
+ $b: (content) => ({ type: RichTextNodeType.Strong, content }),
32
+ $i: (content) => ({ type: RichTextNodeType.Emphasis, content }),
33
+ $del: (content) => ({ type: RichTextNodeType.Strikethrough, content }),
34
+ $code: (content) => ({ type: RichTextNodeType.Code, content }),
35
+ $link: ([target, ...content]) => ({
36
+ type: RichTextNodeType.Link,
37
+ target: target.content,
38
+ content,
39
+ }),
40
+ $p: (content) => ({ type: RichTextNodeType.Paragraph, content }),
84
41
  };
85
- var AstBuilder = /** @class */ (function (_super) {
86
- __extends(AstBuilder, _super);
87
- function AstBuilder() {
88
- var _this = _super.apply(this, __spreadArray([], __read(arguments), false)) || this;
89
- _this.result = [];
90
- return _this;
42
+ class AstBuilder extends format_1.FormatBuilder {
43
+ constructor() {
44
+ super(...arguments);
45
+ this.result = [];
91
46
  }
92
- AstBuilder.prototype.pushRichTextTag = function (tag, children) {
93
- var _a;
47
+ pushRichTextTag(tag, children) {
94
48
  if (!(tag in AST_RICH_TEXT_ELEMENTS)) {
95
- throw "".concat(tag, " is not a known rich text formatting tag");
49
+ throw `${tag} is not a known rich text formatting tag`;
96
50
  }
97
- var result = AST_RICH_TEXT_ELEMENTS[tag](children, '');
51
+ const result = AST_RICH_TEXT_ELEMENTS[tag](children, '');
98
52
  if (Array.isArray(result)) {
99
- (_a = this.result).push.apply(_a, __spreadArray([], __read(result), false));
53
+ this.result.push(...result);
100
54
  }
101
55
  else {
102
56
  this.result.push(result);
103
57
  }
104
- };
105
- AstBuilder.prototype.pushLiteralText = function (text) {
106
- var last = this.result[this.result.length - 1];
58
+ }
59
+ pushLiteralText(text) {
60
+ const last = this.result[this.result.length - 1];
107
61
  if (last != null && last.type === RichTextNodeType.Text) {
108
62
  last.content += text;
109
63
  }
110
64
  else {
111
65
  this.result.push({ type: RichTextNodeType.Text, content: text });
112
66
  }
113
- };
114
- AstBuilder.prototype.pushObject = function (value) {
67
+ }
68
+ pushObject(value) {
115
69
  this.result.push({ type: RichTextNodeType.Object, content: value });
116
- };
117
- AstBuilder.prototype.finish = function () {
70
+ }
71
+ finish() {
118
72
  return this.result;
119
- };
120
- return AstBuilder;
121
- }(format_1.FormatBuilder));
73
+ }
74
+ }
122
75
  /**
123
76
  * Format the given message in the current locale with the provided values.
124
77
  * The returned values is _always_ an Array of parts, even if the message is
@@ -1,77 +1,30 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
2
  Object.defineProperty(exports, "__esModule", { value: true });
43
3
  exports.markdownFormatter = void 0;
44
4
  exports.formatToMarkdownString = formatToMarkdownString;
45
- var string_1 = require("./string");
46
- var MARKDOWN_RICH_TEXT_ELEMENTS = {
47
- $_: function () { return ''; },
48
- $b: function (content) { return '**' + content.join('') + '**'; },
49
- $i: function (content) { return '*' + content.join('') + '*'; },
50
- $del: function (content) { return '~~' + content.join('') + '~~'; },
51
- $code: function (content) { return '`' + content.join('') + '`'; },
52
- $link: function (_a) {
53
- var _b = __read(_a), target = _b[0], content = _b.slice(1);
54
- return '[' + content.join('') + '](' + target + ')';
55
- },
56
- $p: function (content) { return content.join('') + '\n\n'; },
5
+ const string_1 = require("./string");
6
+ const MARKDOWN_RICH_TEXT_ELEMENTS = {
7
+ $_: () => '',
8
+ $b: (content) => '**' + content.join('') + '**',
9
+ $i: (content) => '*' + content.join('') + '*',
10
+ $del: (content) => '~~' + content.join('') + '~~',
11
+ $code: (content) => '`' + content.join('') + '`',
12
+ $link: ([target, ...content]) => '[' + content.join('') + '](' + target + ')',
13
+ $p: (content) => content.join('') + '\n\n',
57
14
  };
58
- var MarkdownBuilder = /** @class */ (function (_super) {
59
- __extends(MarkdownBuilder, _super);
60
- function MarkdownBuilder() {
61
- var _this = _super.apply(this, __spreadArray([], __read(arguments), false)) || this;
62
- _this.result = '';
63
- return _this;
15
+ class MarkdownBuilder extends string_1.StringBuilder {
16
+ constructor() {
17
+ super(...arguments);
18
+ this.result = '';
64
19
  }
65
- MarkdownBuilder.prototype.pushRichTextTag = function (tag, children) {
20
+ pushRichTextTag(tag, children) {
66
21
  this.result += MARKDOWN_RICH_TEXT_ELEMENTS[tag](children, '');
67
- };
68
- return MarkdownBuilder;
69
- }(string_1.StringBuilder));
70
- function formatToMarkdownString(message, values, Builder) {
71
- if (Builder === void 0) { Builder = MarkdownBuilder; }
22
+ }
23
+ }
24
+ function formatToMarkdownString(message, values, Builder = MarkdownBuilder) {
72
25
  if (typeof message === 'string')
73
26
  return message;
74
- var result = this.bindFormatValues(Builder, message, values);
27
+ const result = this.bindFormatValues(Builder, message, values);
75
28
  // MarkdownBuilder always creates a single-element array with the string value.
76
29
  return result[0];
77
30
  }