1 | var Compiler = require('./compiler');
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | function Runtime(mf) {
|
12 | this.mf = mf;
|
13 | this.setStrictNumber(mf.strictNumberSign);
|
14 | }
|
15 |
|
16 | module.exports = Runtime;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function defaultNumber(value, name, offset) {
|
31 | if (!offset) return value;
|
32 | if (isNaN(value)) throw new Error('Can\'t apply offset:' + offset + ' to argument `' + name +
|
33 | '` with non-numerical value ' + JSON.stringify(value) + '.');
|
34 | return value - offset;
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 | function strictNumber(value, name, offset) {
|
40 | if (isNaN(value)) throw new Error('Argument `' + name + '` has non-numerical value ' + JSON.stringify(value) + '.');
|
41 | return value - (offset || 0);
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | Runtime.prototype.setStrictNumber = function(enable) {
|
58 | this.number = enable ? strictNumber : defaultNumber;
|
59 | }
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | Runtime.prototype.plural = function(value, offset, lcfunc, data, isOrdinal) {
|
72 | if ({}.hasOwnProperty.call(data, value)) return data[value];
|
73 | if (offset) value -= offset;
|
74 | var key = lcfunc(value, isOrdinal);
|
75 | if (key in data) return data[key];
|
76 | return data.other;
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | Runtime.prototype.select = function(value, data) {
|
87 | if ({}.hasOwnProperty.call(data, value)) return data[value];
|
88 | return data.other;
|
89 | }
|
90 |
|
91 |
|
92 |
|
93 | Runtime.prototype.toString = function(pluralFuncs, compiler) {
|
94 | function _stringify(o, level) {
|
95 | if (typeof o != 'object') {
|
96 | var funcStr = o.toString().replace(/^(function )\w*/, '$1');
|
97 | var indent = /([ \t]*)\S.*$/.exec(funcStr);
|
98 | return indent ? funcStr.replace(new RegExp('^' + indent[1], 'mg'), '') : funcStr;
|
99 | }
|
100 | var s = [];
|
101 | for (var i in o) {
|
102 | if (level == 0) s.push('var ' + i + ' = ' + _stringify(o[i], level + 1) + ';\n');
|
103 | else s.push(Compiler.propname(i) + ': ' + _stringify(o[i], level + 1));
|
104 | }
|
105 | if (level == 0) return s.join('');
|
106 | if (s.length == 0) return '{}';
|
107 | var indent = ' '; while (--level) indent += ' ';
|
108 | return '{\n' + s.join(',\n').replace(/^/gm, indent) + '\n}';
|
109 | }
|
110 |
|
111 | var obj = {};
|
112 | Object.keys(compiler.locales).forEach(function(lc) { obj[Compiler.funcname(lc)] = pluralFuncs[lc]; });
|
113 | Object.keys(compiler.runtime).forEach(function(fn) { obj[fn] = this[fn]; }, this);
|
114 | var fmtKeys = Object.keys(compiler.formatters);
|
115 | var fmt = this.mf.fmt;
|
116 | if (fmtKeys.length) obj.fmt = fmtKeys.reduce(function(o, key) { o[key] = fmt[key]; return o; }, {});
|
117 | return _stringify(obj, 0);
|
118 | }
|