Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const generate = (sample: string): string[] => {
if (sample.indexOf('~') === -1) {
return [sample];
}
const res: string[] = [];
const [_s, _e] = sample.split('~');
let s = new Decimal(_s);
const e = new Decimal(_e);
let incr = DecimalConstants.ONE;
if (s.scale() > 0) {
incr = incr.movePoint(-s.scale());
}
while (s.compare(e) !== 0) {
res.push(s.toString());
s = s.add(incr);
}
// console.log(`${s.toString()} ${e.toString()} incr ${incr.toString()}`;
return res;
};
} from '../parser';
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 {
}
} 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,
options.context, 'relative');
}
return res;
}
// Format a pluralized future / past.
format(bundle: Bundle, renderer: NumberRenderer, q: Quantity,
options: UnitFormatOptions, params: NumberParams): T {
const n = coerceDecimal(q.value);
const [num, plural] = this.internals.numbers.formatDecimal(bundle, renderer, n, options, params);
if (q.unit === undefined) {
return num;
}
// Compute plural category for the value '1'
const singular = bundle.plurals().cardinal(DecimalConstants.ONE);
// For default and "per" compound pattern, the {0} will use
// the plural category and {1} will be singular. Examples:
// 1 meter per second
// 10 meters per second
//
// For the 'times' compound pattern, the {0} will be singular,
// and the {1} will use the plural category. Examples:
// 1 newton-meter
// 10 newton-meters
const plural0 = q.times ? singular : plural;
const plural1 = q.times ? plural : singular;
const { general } = this.internals;
const info = this.getUnitInfo(options.length || '');