'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"use strict";
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
 
var _messageformatParser = require("messageformat-parser");
 
var _utils = require("./utils");
 
/** @private */
class Compiler {
  /** Creates a new message compiler. Called internally from {@link MessageFormat#compile}.
   *
   * @private
   * @param {MessageFormat} mf - A MessageFormat instance
   * @property {object} locales - The locale identifiers that are used by the compiled functions
   * @property {object} runtime - Names of the core runtime functions that are used by the compiled functions
   * @property {object} formatters - The formatter functions that are used by the compiled functions
   */
  constructor(mf) {
    this.mf = mf;
    this.lc = null;
    this.locales = {};
    this.runtime = {};
    this.formatters = {};
  }
  /** Recursively compile a string or a tree of strings to JavaScript function sources
   *
   *  If `src` is an object with a key that is also present in `plurals`, the key
   *  in question will be used as the locale identifier for its value. To disable
   *  the compile-time checks for plural & selectordinal keys while maintaining
   *  multi-locale support, use falsy values in `plurals`.
   *
   * @private
   * @param {string|object} src - the source for which the JS code should be generated
   * @param {string} lc - the default locale
   * @param {object} plurals - a map of pluralization keys for all available locales
   */
 
 
  compile(src, lc, plurals) {
    if (typeof src != 'object') {
      this.lc = lc;
      const pc = plurals[lc] || {
        cardinal: [],
        ordinal: []
      };
      pc.strict = !!this.mf.options.strictNumberSign;
      const r = (0, _messageformatParser.parse)(src, pc).map(token => this.token(token));
      return `function(d) { return ${r.join(' + ') || '""'}; }`;
    } else {
      const result = {};
 
      for (var key in src) {
        // eslint-disable-next-line no-prototype-builtins
        var lcKey = plurals.hasOwnProperty(key) ? key : lc;
        result[key] = this.compile(src[key], lcKey, plurals);
      }
 
      return result;
    }
  }
  /** @private */
 
 
  cases(token, plural) {
    let needOther = token.type === 'select' || !this.mf.hasCustomPluralFuncs;
    const r = token.cases.map(({
      key,
      tokens
    }) => {
      if (key === 'other') needOther = false;
      const s = tokens.map(tok => this.token(tok, plural));
      return (0, _utils.propname)(key) + ': ' + (s.join(' + ') || '""');
    });
    if (needOther) throw new Error("No 'other' form found in " + JSON.stringify(token));
    return `{ ${r.join(', ')} }`;
  }
  /** @private */
 
 
  token(token, plural) {
    if (typeof token == 'string') return JSON.stringify(token);
    let fn;
    let args = [(0, _utils.propname)(token.arg, 'd')];
 
    switch (token.type) {
      case 'argument':
        return this.mf.options.biDiSupport ? (0, _utils.biDiMarkText)(args[0], this.lc) : args[0];
 
      case 'select':
        fn = 'select';
        if (plural && this.mf.options.strictNumberSign) plural = null;
        args.push(this.cases(token, plural));
        this.runtime.select = true;
        break;
 
      case 'selectordinal':
        fn = 'plural';
        args.push(0, (0, _utils.funcname)(this.lc), this.cases(token, token), 1);
        this.locales[this.lc] = true;
        this.runtime.plural = true;
        break;
 
      case 'plural':
        fn = 'plural';
        args.push(token.offset || 0, (0, _utils.funcname)(this.lc), this.cases(token, token));
        this.locales[this.lc] = true;
        this.runtime.plural = true;
        break;
 
      case 'function':
        if (!(token.key in this.mf.fmt) && token.key in this.mf.constructor.formatters) {
          const fmt = this.mf.constructor.formatters[token.key];
          this.mf.fmt[token.key] = fmt(this.mf);
        }
 
        if (!this.mf.fmt[token.key]) throw new Error(`Formatting function ${JSON.stringify(token.key)} not found!`);
        args.push(JSON.stringify(this.lc));
 
        if (token.param) {
          if (plural && this.mf.options.strictNumberSign) plural = null;
          const s = token.param.tokens.map(tok => this.token(tok, plural));
          args.push('(' + (s.join(' + ') || '""') + ').trim()');
        }
 
        fn = (0, _utils.propname)(token.key, 'fmt');
        this.formatters[token.key] = true;
        break;
 
      case 'octothorpe':
        if (!plural) return '"#"';
        fn = 'number';
        args = [(0, _utils.propname)(plural.arg, 'd'), JSON.stringify(plural.arg)];
        if (plural.offset) args.push(plural.offset);
        this.runtime.number = true;
        break;
    }
 
    if (!fn) throw new Error('Parser error for token ' + JSON.stringify(token));
    return `${fn}(${args.join(', ')})`;
  }
 
}
 
exports.default = Compiler;