bobril-g11n
Advanced tools
Comparing version 4.1.0 to 4.2.0
# CHANGELOG | ||
## 4.2.0 | ||
New feature relativepast how to format relative time. It is same as relative just it never display future time. | ||
## 4.1.0 | ||
@@ -4,0 +8,0 @@ |
{ | ||
"name": "bobril-g11n", | ||
"version": "4.1.0", | ||
"version": "4.2.0", | ||
"description": "Bobril globalization", | ||
@@ -16,2 +16,6 @@ "main": "index.js", | ||
}, | ||
"prettier": { | ||
"tabWidth": 4, | ||
"printWidth": 120 | ||
}, | ||
"bobril": { | ||
@@ -26,2 +30,3 @@ "compilerOptions": { | ||
"noFallthroughCasesInSwitch": true, | ||
"strict": true, | ||
"strictNullChecks": true | ||
@@ -28,0 +33,0 @@ } |
@@ -1,1 +0,1 @@ | ||
export declare function compile(locale: string, msgAst: any): (params: Object, hashArg?: string) => string; | ||
export declare function compile(locale: string, msgAst: any): (params?: Object, hashArg?: string) => string; |
@@ -10,3 +10,3 @@ "use strict"; | ||
function getFormatter(locale, format) { | ||
var key = locale + '|' + format; | ||
var key = locale + "|" + format; | ||
var res = numberFormatterCache[key]; | ||
@@ -19,39 +19,91 @@ if (res) | ||
} | ||
function noFuture(m) { | ||
if (m.toDate() > new Date()) | ||
return moment(new Date()); | ||
return m; | ||
} | ||
function AnyFormatter(locale, type, style, options) { | ||
switch (type) { | ||
case 'number': { | ||
if (style === 'custom' && 'format' in options) { | ||
case "number": { | ||
if (style === "custom" && "format" in options) { | ||
if (options.format === null) | ||
return function (val, opt) { return getFormatter(locale, opt.format)(val); }; | ||
return function (val, opt) { | ||
return getFormatter(locale, opt.format)(val); | ||
}; | ||
return getFormatter(locale, options.format); | ||
} | ||
if (style === 'default') { | ||
return getFormatter(locale, '0,0.[0000]'); | ||
if (style === "default") { | ||
return getFormatter(locale, "0,0.[0000]"); | ||
} | ||
if (style === 'percent') { | ||
return getFormatter(locale, '0%'); | ||
if (style === "percent") { | ||
return getFormatter(locale, "0%"); | ||
} | ||
if (style === 'bytes') { | ||
return getFormatter(locale, '0b'); | ||
if (style === "bytes") { | ||
return getFormatter(locale, "0b"); | ||
} | ||
break; | ||
} | ||
case 'date': | ||
case 'time': { | ||
if (style === 'relative') { | ||
if (options['noago'] === true) { | ||
return function (val, _opt) { return moment(val).locale(locale).fromNow(true); }; | ||
case "date": | ||
case "time": { | ||
if (style === "relative") { | ||
if (options["noago"] === true) { | ||
return function (val, _opt) { | ||
return moment(val) | ||
.locale(locale) | ||
.fromNow(true); | ||
}; | ||
} | ||
if (options['noago'] === null) { | ||
return function (val, opt) { return moment(val).locale(locale).fromNow(opt['noago']); }; | ||
if (options["noago"] === null) { | ||
return function (val, opt) { | ||
return moment(val) | ||
.locale(locale) | ||
.fromNow(opt["noago"]); | ||
}; | ||
} | ||
return function (val, _opt) { return moment(val).locale(locale).fromNow(false); }; | ||
return function (val, _opt) { | ||
return moment(val) | ||
.locale(locale) | ||
.fromNow(false); | ||
}; | ||
} | ||
if (style === 'calendar') { | ||
return function (val, _opt) { return moment(val).locale(locale).calendar(); }; | ||
if (style === "relativepast") { | ||
if (options["noago"] === true) { | ||
return function (val, _opt) { | ||
return noFuture(moment(val)) | ||
.locale(locale) | ||
.fromNow(true); | ||
}; | ||
} | ||
if (options["noago"] === null) { | ||
return function (val, opt) { | ||
return noFuture(moment(val)) | ||
.locale(locale) | ||
.fromNow(opt["noago"]); | ||
}; | ||
} | ||
return function (val, _opt) { | ||
return noFuture(moment(val)) | ||
.locale(locale) | ||
.fromNow(false); | ||
}; | ||
} | ||
if (style === 'custom' && 'format' in options) { | ||
return function (val, opt) { return moment(val).locale(locale).format(opt.format); }; | ||
if (style === "calendar") { | ||
return function (val, _opt) { | ||
return moment(val) | ||
.locale(locale) | ||
.calendar(); | ||
}; | ||
} | ||
return function (val, _opt) { return moment(val).locale(locale).format(style); }; | ||
if (style === "custom" && "format" in options) { | ||
return function (val, opt) { | ||
return moment(val) | ||
.locale(locale) | ||
.format(opt.format); | ||
}; | ||
} | ||
return function (val, _opt) { | ||
return moment(val) | ||
.locale(locale) | ||
.format(style); | ||
}; | ||
} | ||
@@ -62,3 +114,3 @@ } | ||
function compile(locale, msgAst) { | ||
if (typeof msgAst === 'string') { | ||
if (typeof msgAst === "string") { | ||
return function () { return msgAst; }; | ||
@@ -68,12 +120,12 @@ } | ||
if (msgAst.length === 0) | ||
return function () { return ''; }; | ||
return function () { return ""; }; | ||
var comp = new RuntimeFunctionGenerator_1.RuntimeFunctionGenerator(); | ||
var argParams = comp.addArg(0); | ||
var argHash = comp.addArg(1); | ||
comp.addBody('return '); | ||
comp.addBody("return "); | ||
for (var i = 0; i < msgAst.length; i++) { | ||
if (i > 0) | ||
comp.addBody('+'); | ||
comp.addBody("+"); | ||
var item = msgAst[i]; | ||
if (typeof item === 'string') { | ||
if (typeof item === "string") { | ||
comp.addBody(comp.addConstant(item)); | ||
@@ -85,15 +137,15 @@ } | ||
} | ||
comp.addBody(';'); | ||
comp.addBody(";"); | ||
return comp.build(); | ||
} | ||
switch (msgAst.type) { | ||
case 'arg': | ||
case "arg": | ||
return (function (name) { return function (params) { return params[name]; }; })(msgAst.id); | ||
case 'hash': | ||
case "hash": | ||
return function (_params, hashArg) { | ||
if (hashArg === undefined) | ||
return '#'; | ||
return "#"; | ||
return hashArg; | ||
}; | ||
case 'format': | ||
case "format": | ||
var comp = new RuntimeFunctionGenerator_1.RuntimeFunctionGenerator(); | ||
@@ -105,100 +157,97 @@ var argParams = comp.addArg(0); | ||
switch (type) { | ||
case 'plural': | ||
{ | ||
var localArgOffset = comp.addLocal(); | ||
comp.addBody("var " + localArgOffset + "=" + localArg + "-" + msgAst.format.offset + ";"); | ||
var options = msgAst.format.options; | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (typeof opt.selector !== 'number') | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("if (" + localArgOffset + "===" + opt.selector + ") return " + fn + "(" + argParams + ",''+" + localArgOffset + ");"); | ||
} | ||
var localCase = comp.addLocal(); | ||
var pluralFn = comp.addConstant(localeDataStorage.getRules(locale).pluralFn); | ||
comp.addBody("var " + localCase + "=" + pluralFn + "(" + localArgOffset + "," + (msgAst.format.ordinal ? 'true' : 'false') + ");"); | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (typeof opt.selector !== 'string') | ||
continue; | ||
if (opt.selector === 'other') | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("if (" + localCase + "===" + comp.addConstant(opt.selector) + ") return " + fn + "(" + argParams + ",''+" + localArgOffset + ");"); | ||
} | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (opt.selector !== 'other') | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("return " + fn + "(" + argParams + ",''+" + localArgOffset + ");"); | ||
} | ||
break; | ||
case "plural": { | ||
var localArgOffset = comp.addLocal(); | ||
comp.addBody("var " + localArgOffset + "=" + localArg + "-" + msgAst.format.offset + ";"); | ||
var options = msgAst.format.options; | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (typeof opt.selector !== "number") | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("if (" + localArgOffset + "===" + opt.selector + ") return " + fn + "(" + argParams + ",''+" + localArgOffset + ");"); | ||
} | ||
case 'select': | ||
{ | ||
var options = msgAst.format.options; | ||
var localCase = comp.addLocal(); | ||
var pluralFn = comp.addConstant(localeDataStorage.getRules(locale).pluralFn); | ||
comp.addBody("var " + localCase + "=" + pluralFn + "(" + localArgOffset + "," + (msgAst.format.ordinal ? "true" : "false") + ");"); | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (typeof opt.selector !== "string") | ||
continue; | ||
if (opt.selector === "other") | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("if (" + localCase + "===" + comp.addConstant(opt.selector) + ") return " + fn + "(" + argParams + ",''+" + localArgOffset + ");"); | ||
} | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (opt.selector !== "other") | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("return " + fn + "(" + argParams + ",''+" + localArgOffset + ");"); | ||
} | ||
break; | ||
} | ||
case "select": { | ||
var options = msgAst.format.options; | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (typeof opt.selector !== "string") | ||
continue; | ||
if (opt.selector === "other") | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("if (" + localArg + "===" + comp.addConstant(opt.selector) + ") return " + fn + "(" + argParams + "," + localArg + ");"); | ||
} | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (opt.selector !== "other") | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("return " + fn + "(" + argParams + "," + localArg + ");"); | ||
} | ||
break; | ||
} | ||
case "number": | ||
case "date": | ||
case "time": { | ||
var style = msgAst.format.style || "default"; | ||
var options = msgAst.format.options; | ||
if (options) { | ||
var opt = {}; | ||
var complex = false; | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (typeof opt.selector !== 'string') | ||
continue; | ||
if (opt.selector === 'other') | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("if (" + localArg + "===" + comp.addConstant(opt.selector) + ") return " + fn + "(" + argParams + "," + localArg + ");"); | ||
if (typeof options[i].value === "object") { | ||
complex = true; | ||
opt[options[i].key] = null; | ||
} | ||
else { | ||
var val = options[i].value; | ||
if (val === undefined) | ||
val = true; | ||
opt[options[i].key] = val; | ||
} | ||
} | ||
for (var i = 0; i < options.length; i++) { | ||
var opt = options[i]; | ||
if (opt.selector !== 'other') | ||
continue; | ||
var fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody("return " + fn + "(" + argParams + "," + localArg + ");"); | ||
} | ||
break; | ||
} | ||
case 'number': | ||
case 'date': | ||
case 'time': | ||
{ | ||
var style = msgAst.format.style || 'default'; | ||
var options = msgAst.format.options; | ||
if (options) { | ||
var opt = {}; | ||
var complex = false; | ||
var formatFn = comp.addConstant(AnyFormatter(locale, type, style, opt)); | ||
if (complex) { | ||
var optConst = comp.addConstant(opt); | ||
var optLocal = comp.addLocal(); | ||
var hashArg = comp.addArg(1); | ||
comp.addBody("var " + optLocal + "=" + optConst + ";"); | ||
for (var i = 0; i < options.length; i++) { | ||
if (typeof options[i].value === 'object') { | ||
complex = true; | ||
opt[options[i].key] = null; | ||
if (typeof options[i].value === "object") { | ||
var fnConst = comp.addConstant(compile(locale, options[i].value)); | ||
comp.addBody(optLocal + "[" + comp.addConstant(options[i].key) + "]=" + fnConst + "(" + argParams + "," + hashArg + ");"); | ||
} | ||
else { | ||
var val = options[i].value; | ||
if (val === undefined) | ||
val = true; | ||
opt[options[i].key] = val; | ||
} | ||
} | ||
var formatFn = comp.addConstant(AnyFormatter(locale, type, style, opt)); | ||
if (complex) { | ||
var optConst = comp.addConstant(opt); | ||
var optLocal = comp.addLocal(); | ||
var hashArg = comp.addArg(1); | ||
comp.addBody("var " + optLocal + "=" + optConst + ";"); | ||
for (var i = 0; i < options.length; i++) { | ||
if (typeof options[i].value === 'object') { | ||
var fnConst = comp.addConstant(compile(locale, options[i].value)); | ||
comp.addBody(optLocal + "[" + comp.addConstant(options[i].key) + "]=" + fnConst + "(" + argParams + "," + hashArg + ");"); | ||
} | ||
} | ||
comp.addBody("return " + formatFn + "(" + localArg + "," + optLocal + ");"); | ||
} | ||
else { | ||
comp.addBody("return " + formatFn + "(" + localArg + "," + comp.addConstant(opt) + ");"); | ||
} | ||
comp.addBody("return " + formatFn + "(" + localArg + "," + optLocal + ");"); | ||
} | ||
else { | ||
var formatFn = comp.addConstant(AnyFormatter(locale, type, style, {})); | ||
comp.addBody("return " + formatFn + "(" + localArg + ");"); | ||
comp.addBody("return " + formatFn + "(" + localArg + "," + comp.addConstant(opt) + ");"); | ||
} | ||
} | ||
else { | ||
var formatFn = comp.addConstant(AnyFormatter(locale, type, style, {})); | ||
comp.addBody("return " + formatFn + "(" + localArg + ");"); | ||
} | ||
} | ||
} | ||
@@ -205,0 +254,0 @@ return comp.build(); |
@@ -1,5 +0,5 @@ | ||
import * as moment from 'moment'; | ||
import { RuntimeFunctionGenerator } from './RuntimeFunctionGenerator'; | ||
import * as localeDataStorage from './localeDataStorage'; | ||
import * as numberFormatter from './numberFormatter'; | ||
import * as moment from "moment"; | ||
import { RuntimeFunctionGenerator } from "./RuntimeFunctionGenerator"; | ||
import * as localeDataStorage from "./localeDataStorage"; | ||
import * as numberFormatter from "./numberFormatter"; | ||
@@ -11,3 +11,3 @@ (<any>window).moment = moment; | ||
function getFormatter(locale: string, format: string): (val: number) => string { | ||
const key = locale + '|' + format; | ||
const key = locale + "|" + format; | ||
let res = numberFormatterCache[key]; | ||
@@ -20,39 +20,96 @@ if (res) return res; | ||
function AnyFormatter(locale: string, type: string, style: string, options: Object): (value: any, options: Object) => string { | ||
function noFuture(m: moment.Moment): moment.Moment { | ||
if (m.toDate() > new Date()) return moment(new Date()); | ||
return m; | ||
} | ||
function AnyFormatter( | ||
locale: string, | ||
type: string, | ||
style: string, | ||
options: Object | ||
): (value: any, options: Object) => string { | ||
switch (type) { | ||
case 'number': { | ||
if (style === 'custom' && 'format' in options) { | ||
case "number": { | ||
if (style === "custom" && "format" in options) { | ||
if ((options as any).format === null) | ||
return (val, opt) => { return getFormatter(locale, (opt as any).format)(val); }; | ||
return (val, opt) => { | ||
return getFormatter(locale, (opt as any).format)(val); | ||
}; | ||
return getFormatter(locale, (options as any).format); | ||
} | ||
if (style === 'default') { | ||
return getFormatter(locale, '0,0.[0000]'); | ||
if (style === "default") { | ||
return getFormatter(locale, "0,0.[0000]"); | ||
} | ||
if (style === 'percent') { | ||
return getFormatter(locale, '0%'); | ||
if (style === "percent") { | ||
return getFormatter(locale, "0%"); | ||
} | ||
if (style === 'bytes') { | ||
return getFormatter(locale, '0b'); | ||
if (style === "bytes") { | ||
return getFormatter(locale, "0b"); | ||
} | ||
break; | ||
} | ||
case 'date': | ||
case 'time': { | ||
if (style === 'relative') { | ||
if ((<any>options)['noago'] === true) { | ||
return (val, _opt) => { return moment(val).locale(locale).fromNow(true); }; | ||
case "date": | ||
case "time": { | ||
if (style === "relative") { | ||
if ((<any>options)["noago"] === true) { | ||
return (val, _opt) => { | ||
return moment(val) | ||
.locale(locale) | ||
.fromNow(true); | ||
}; | ||
} | ||
if ((<any>options)['noago'] === null) { | ||
return (val, opt) => { return moment(val).locale(locale).fromNow((<any>opt)['noago']); }; | ||
if ((<any>options)["noago"] === null) { | ||
return (val, opt) => { | ||
return moment(val) | ||
.locale(locale) | ||
.fromNow((<any>opt)["noago"]); | ||
}; | ||
} | ||
return (val, _opt) => { return moment(val).locale(locale).fromNow(false); }; | ||
return (val, _opt) => { | ||
return moment(val) | ||
.locale(locale) | ||
.fromNow(false); | ||
}; | ||
} | ||
if (style === 'calendar') { | ||
return (val, _opt) => { return moment(val).locale(locale).calendar(); }; | ||
if (style === "relativepast") { | ||
if ((<any>options)["noago"] === true) { | ||
return (val, _opt) => { | ||
return noFuture(moment(val)) | ||
.locale(locale) | ||
.fromNow(true); | ||
}; | ||
} | ||
if ((<any>options)["noago"] === null) { | ||
return (val, opt) => { | ||
return noFuture(moment(val)) | ||
.locale(locale) | ||
.fromNow((<any>opt)["noago"]); | ||
}; | ||
} | ||
return (val, _opt) => { | ||
return noFuture(moment(val)) | ||
.locale(locale) | ||
.fromNow(false); | ||
}; | ||
} | ||
if (style === 'custom' && 'format' in options) { | ||
return (val, opt) => { return moment(val).locale(locale).format((<any>opt).format); }; | ||
if (style === "calendar") { | ||
return (val, _opt) => { | ||
return moment(val) | ||
.locale(locale) | ||
.calendar(); | ||
}; | ||
} | ||
return (val, _opt) => { return moment(val).locale(locale).format(style); }; | ||
if (style === "custom" && "format" in options) { | ||
return (val, opt) => { | ||
return moment(val) | ||
.locale(locale) | ||
.format((<any>opt).format); | ||
}; | ||
} | ||
return (val, _opt) => { | ||
return moment(val) | ||
.locale(locale) | ||
.format(style); | ||
}; | ||
} | ||
@@ -63,16 +120,16 @@ } | ||
export function compile(locale: string, msgAst: any): (params: Object, hashArg?: string) => string { | ||
if (typeof msgAst === 'string') { | ||
export function compile(locale: string, msgAst: any): (params?: Object, hashArg?: string) => string { | ||
if (typeof msgAst === "string") { | ||
return () => msgAst; | ||
} | ||
if (Array.isArray(msgAst)) { | ||
if (msgAst.length === 0) return () => ''; | ||
if (msgAst.length === 0) return () => ""; | ||
let comp = new RuntimeFunctionGenerator(); | ||
let argParams = comp.addArg(0); | ||
let argHash = comp.addArg(1); | ||
comp.addBody('return '); | ||
comp.addBody("return "); | ||
for (let i = 0; i < msgAst.length; i++) { | ||
if (i > 0) comp.addBody('+'); | ||
if (i > 0) comp.addBody("+"); | ||
let item = msgAst[i]; | ||
if (typeof item === 'string') { | ||
if (typeof item === "string") { | ||
comp.addBody(comp.addConstant(item)); | ||
@@ -83,14 +140,14 @@ } else { | ||
} | ||
comp.addBody(';'); | ||
return <(params: Object, hashArg?: string) => string>comp.build(); | ||
comp.addBody(";"); | ||
return <(params?: Object, hashArg?: string) => string>comp.build(); | ||
} | ||
switch (msgAst.type) { | ||
case 'arg': | ||
return ((name: string) => (params: Object) => (<any>params)[name])(msgAst.id); | ||
case 'hash': | ||
case "arg": | ||
return ((name: string) => (params?: Object) => (<any>params)[name])(msgAst.id); | ||
case "hash": | ||
return (_params, hashArg) => { | ||
if (hashArg === undefined) return '#'; | ||
if (hashArg === undefined) return "#"; | ||
return hashArg; | ||
}; | ||
case 'format': | ||
case "format": | ||
let comp = new RuntimeFunctionGenerator(); | ||
@@ -102,93 +159,106 @@ let argParams = comp.addArg(0); | ||
switch (type) { | ||
case 'plural': | ||
{ | ||
let localArgOffset = comp.addLocal(); | ||
comp.addBody(`var ${localArgOffset}=${localArg}-${msgAst.format.offset};`); | ||
let options = msgAst.format.options; | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (typeof opt.selector !== 'number') continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody(`if (${localArgOffset}===${opt.selector}) return ${fn}(${argParams},''+${localArgOffset});`); | ||
} | ||
let localCase = comp.addLocal(); | ||
let pluralFn = comp.addConstant(localeDataStorage.getRules(locale).pluralFn); | ||
comp.addBody(`var ${localCase}=${pluralFn}(${localArgOffset},${msgAst.format.ordinal ? 'true' : 'false'});`); | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (typeof opt.selector !== 'string') continue; | ||
if (opt.selector === 'other') continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody(`if (${localCase}===${comp.addConstant(opt.selector)}) return ${fn}(${argParams},''+${localArgOffset});`); | ||
} | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (opt.selector !== 'other') continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody(`return ${fn}(${argParams},''+${localArgOffset});`); | ||
} | ||
break; | ||
case "plural": { | ||
let localArgOffset = comp.addLocal(); | ||
comp.addBody(`var ${localArgOffset}=${localArg}-${msgAst.format.offset};`); | ||
let options = msgAst.format.options; | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (typeof opt.selector !== "number") continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody( | ||
`if (${localArgOffset}===${opt.selector}) return ${fn}(${argParams},''+${localArgOffset});` | ||
); | ||
} | ||
case 'select': | ||
{ | ||
let options = msgAst.format.options; | ||
let localCase = comp.addLocal(); | ||
let pluralFn = comp.addConstant(localeDataStorage.getRules(locale).pluralFn); | ||
comp.addBody( | ||
`var ${localCase}=${pluralFn}(${localArgOffset},${msgAst.format.ordinal ? "true" : "false"});` | ||
); | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (typeof opt.selector !== "string") continue; | ||
if (opt.selector === "other") continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody( | ||
`if (${localCase}===${comp.addConstant( | ||
opt.selector | ||
)}) return ${fn}(${argParams},''+${localArgOffset});` | ||
); | ||
} | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (opt.selector !== "other") continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody(`return ${fn}(${argParams},''+${localArgOffset});`); | ||
} | ||
break; | ||
} | ||
case "select": { | ||
let options = msgAst.format.options; | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (typeof opt.selector !== "string") continue; | ||
if (opt.selector === "other") continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody( | ||
`if (${localArg}===${comp.addConstant( | ||
opt.selector | ||
)}) return ${fn}(${argParams},${localArg});` | ||
); | ||
} | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (opt.selector !== "other") continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody(`return ${fn}(${argParams},${localArg});`); | ||
} | ||
break; | ||
} | ||
case "number": | ||
case "date": | ||
case "time": { | ||
let style = msgAst.format.style || "default"; | ||
let options = msgAst.format.options; | ||
if (options) { | ||
let opt = {}; | ||
let complex = false; | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (typeof opt.selector !== 'string') continue; | ||
if (opt.selector === 'other') continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody(`if (${localArg}===${comp.addConstant(opt.selector)}) return ${fn}(${argParams},${localArg});`); | ||
if (typeof options[i].value === "object") { | ||
complex = true; | ||
(<any>opt)[options[i].key] = null; | ||
} else { | ||
let val = options[i].value; | ||
if (val === undefined) val = true; | ||
(<any>opt)[options[i].key] = val; | ||
} | ||
} | ||
for (let i = 0; i < options.length; i++) { | ||
let opt = options[i]; | ||
if (opt.selector !== 'other') continue; | ||
let fn = comp.addConstant(compile(locale, opt.value)); | ||
comp.addBody(`return ${fn}(${argParams},${localArg});`); | ||
} | ||
break; | ||
} | ||
case 'number': | ||
case 'date': | ||
case 'time': | ||
{ | ||
let style = msgAst.format.style || 'default'; | ||
let options = msgAst.format.options; | ||
if (options) { | ||
let opt = {}; | ||
let complex = false; | ||
let formatFn = comp.addConstant(AnyFormatter(locale, type, style, opt)); | ||
if (complex) { | ||
let optConst = comp.addConstant(opt); | ||
let optLocal = comp.addLocal(); | ||
let hashArg = comp.addArg(1); | ||
comp.addBody(`var ${optLocal}=${optConst};`); | ||
for (let i = 0; i < options.length; i++) { | ||
if (typeof options[i].value === 'object') { | ||
complex = true; | ||
(<any>opt)[options[i].key] = null; | ||
} else { | ||
let val = options[i].value; | ||
if (val === undefined) val = true; | ||
(<any>opt)[options[i].key] = val; | ||
if (typeof options[i].value === "object") { | ||
let fnConst = comp.addConstant(compile(locale, options[i].value)); | ||
comp.addBody( | ||
`${optLocal}[${comp.addConstant( | ||
options[i].key | ||
)}]=${fnConst}(${argParams},${hashArg});` | ||
); | ||
} | ||
} | ||
let formatFn = comp.addConstant(AnyFormatter(locale, type, style, opt)); | ||
if (complex) { | ||
let optConst = comp.addConstant(opt); | ||
let optLocal = comp.addLocal(); | ||
let hashArg = comp.addArg(1); | ||
comp.addBody(`var ${optLocal}=${optConst};`); | ||
for (let i = 0; i < options.length; i++) { | ||
if (typeof options[i].value === 'object') { | ||
let fnConst = comp.addConstant(compile(locale, options[i].value)); | ||
comp.addBody(`${optLocal}[${comp.addConstant(options[i].key)}]=${fnConst}(${argParams},${hashArg});`); | ||
} | ||
} | ||
comp.addBody(`return ${formatFn}(${localArg},${optLocal});`); | ||
} else { | ||
comp.addBody(`return ${formatFn}(${localArg},${comp.addConstant(opt)});`); | ||
} | ||
comp.addBody(`return ${formatFn}(${localArg},${optLocal});`); | ||
} else { | ||
let formatFn = comp.addConstant(AnyFormatter(locale, type, style, {})); | ||
comp.addBody(`return ${formatFn}(${localArg});`); | ||
comp.addBody(`return ${formatFn}(${localArg},${comp.addConstant(opt)});`); | ||
} | ||
} else { | ||
let formatFn = comp.addConstant(AnyFormatter(locale, type, style, {})); | ||
comp.addBody(`return ${formatFn}(${localArg});`); | ||
} | ||
} | ||
} | ||
return <(params: Object, hashArg?: string) => string>comp.build(); | ||
return <(params?: Object, hashArg?: string) => string>comp.build(); | ||
} | ||
throw new Error("invalid AST in compile"); | ||
} |
@@ -78,5 +78,3 @@ import * as moment from "moment"; | ||
if (ast.type === "error") { | ||
throw new Error( | ||
"message " + message + " in " + currentLocale + " has error: " + ast.msg | ||
); | ||
throw new Error("message " + message + " in " + currentLocale + " has error: " + ast.msg); | ||
} | ||
@@ -92,5 +90,3 @@ format = msgFormatter.compile(currentLocale, ast); | ||
if (ast.type === "error") { | ||
throw new Error( | ||
'message "' + message + '" has error: ' + ast.msg + " on position: " + ast.pos | ||
); | ||
throw new Error('message "' + message + '" has error: ' + ast.msg + " on position: " + ast.pos); | ||
} | ||
@@ -141,5 +137,3 @@ format = msgFormatter.compile(currentLocale, ast); | ||
if (locale != cfg.defaultLocale) | ||
return setLocale(cfg.defaultLocale!).then( | ||
() => Promise.reject(e) as Promise<void> | ||
); | ||
return setLocale(cfg.defaultLocale!).then(() => Promise.reject(e) as Promise<void>); | ||
return undefined; | ||
@@ -186,5 +180,3 @@ }); | ||
export function spyTranslation( | ||
spyFn?: ((text: string) => string) | null | ||
): ((text: string) => string) | undefined { | ||
export function spyTranslation(spyFn?: ((text: string) => string) | null): ((text: string) => string) | undefined { | ||
if (spyFn === undefined) return spyTranslationFunc; | ||
@@ -191,0 +183,0 @@ if (spyFn === null) { |
@@ -26,3 +26,4 @@ { | ||
"strictNullChecks": true, | ||
"types": [] | ||
"types": [], | ||
"strict": true | ||
}, | ||
@@ -29,0 +30,0 @@ "include": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91998
2213