Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export type MessageFormatFunc =
(args: MessageArg[], options: string[]) => string;
export type MessageFormatFuncMap = { [name: string]: MessageFormatFunc };
const get = (key: number | string, args: MessageArgs): MessageArg => {
const res: MessageArg = args.named[key];
return res !== undefined ? res : (typeof key === 'number' ? args.positional[key] : undefined);
};
// Save a bit of processing of common exact matches
const DECIMAL_EXACT: { [n: string]: Decimal } = {
0: DecimalConstants.ZERO,
1: DecimalConstants.ONE,
2: DecimalConstants.TWO
};
/**
* Evaluates a message format against a set of arguments, producing a string.
*/
export class MessageEngine {
private buf: string = '';
constructor(
private plurals: PluralRules,
private formatters: MessageFormatFuncMap,
private code: MessageCode) { }
evaluate(positional: MessageArg[], named: MessageNamedArgs = {}): string {
return this._evaluate(this.code, { positional, named });
const iszero = n.compare(DecimalConstants.ZERO) === 0;
let res = '';
if (iszero) {
if (options.alwaysNow || !options.numericOnly) {
res = format.current.get(bundle, field);
}
} else if (!options.numericOnly) {
switch (field) {
case 'hour':
case 'minute':
case 'second':
break;
default:
if (n.compare(DecimalConstants.TWO) === 0) {
const p = negative ? format.previous2.get(bundle, field) : format.next2.get(bundle, field);
if (p !== '') {
res = p;
}
// Fall through
} else if (n.compare(DecimalConstants.ONE) === 0) {
res = negative ? format.previous.get(bundle, field) : format.next.get(bundle, field);
}
break;
}
}
// If we output anything above, return it
if (res) {
if (options.context) {
res = this.internals.general.contextTransform(res, transform,