Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
private checkIsPlural(src: string): boolean {
let tokens: any[];
try {
tokens = parse(src.replace(/\{\{/g, '{').replace(/\}\}/g, '}'),
{cardinal: [], ordinal: []});
} catch (e) {
console.warn(`Failed to parse source: ${src}`);
console.error(e);
}
const res = tokens.filter(
(value) => typeof value !== 'string' && value.type === 'plural'
);
return res.length > 0;
}
export default function compile(message: string): Function | string {
try {
return processTokens(parse(message))
} catch (e) {
console.error(`Message cannot be parsed due to syntax errors: ${message}`)
return message
}
}
export function compile(message: string) {
const arg = t.identifier("a")
let tokens
try {
tokens = parse(message)
} catch (e) {
throw new Error(
`Can't parse message. Please check correct syntax: "${message}"`
)
}
const ast = processTokens(tokens, arg)
if (isString(ast)) return t.stringLiteral(ast)
return t.functionExpression(
null,
[arg],
t.blockStatement([t.returnStatement(ast)])
)
}
const result = {};
for (const key of Object.keys(src)) {
const pl = plurals[key] || plural;
result[key] = this.compile(src[key], pl, plurals);
}
return result;
}
this.plural = plural;
const parserOptions = {
cardinal: plural.cardinals,
ordinal: plural.ordinals,
strict: this.options.strictNumberSign
};
this.arguments = [];
const r = parse(src, parserOptions).map(token => this.token(token));
let reqArgs = '';
if (this.options.requireAllArguments && this.arguments.length > 0) {
this.setRuntimeFn('reqArgs');
reqArgs = `reqArgs(${JSON.stringify(this.arguments)}, d); `;
}
return `function(d) { ${reqArgs}return ${this.concatenate(r, true)}; }`;
}
export const compileMessage = (message: string) => processTokens(parse(message))
'messageformat-parser': function () { return parse2(pattern) },
'format-message-parse': function () { return parse(pattern) }
module.exports = function (format) {
return icu.parse(format).map(stringify).join('')
}