-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (71 loc) · 2.22 KB
/
index.js
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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Registry = exports.init = exports.interpolated = void 0;
/** Creates a function from given template string literal which takes
* a mapping of key-value pairs to interpolate into this template string.
*
* Example
* -------
* ```typescript
* const helloName = interpolated`Hello, ${'firstName'} ${'lastName'}, age ${'age'}!`
* helloName({
* firstName: 'John',
* lastName: 'Doe',
* age: 42,
* })
* // => 'Hello, John Doe, age 42!'
* ```
*
* **Note** that it is important the interpolated values are string literals
* for the type system to properly pick these names up.
*/
function interpolated(strings) {
var keys = [];
for (var _i = 1; _i < arguments.length; _i++) {
keys[_i - 1] = arguments[_i];
}
return function (values) {
var parts = [strings[0]];
keys.forEach(function (key, i) {
parts.push(values[key] + '');
parts.push(strings[i + 1]);
});
return parts.join('');
};
}
exports.interpolated = interpolated;
/** Initiate a new instance, binding a strings mapping type.
* Next, add locales with `addLocales` and set a default locale with `setLocale`.
*/
function init() {
return Registry.init();
}
exports.init = init;
var Registry = /** @class */ (function () {
function Registry(init, locale) {
if (locale === void 0) { locale = 'en'; }
var _this = this;
this.strings = function (locale) {
if (locale === void 0) { locale = _this._locale; }
return locale in _this._locales
? _this._locales[locale]
: _this._locales['en'];
};
this.addLocales = function (locales) {
return new Registry(Object.assign(_this._locales, locales), _this._locale);
};
this.setLocale = function (locale) {
_this._locale = locale;
return _this;
};
this.getLocale = function () { return _this._locale; };
this._locales = init;
this._locale = locale;
}
Registry.init = function () {
return new Registry({});
};
return Registry;
}());
exports.Registry = Registry;
//# sourceMappingURL=index.js.map