plural-forms-parser
v1.0.1
Published
Safe parser for gettext Plural forms expression
Downloads
5
Maintainers
Readme
plural-forms-parser
Why?
This library can parse Plural Forms header without violation of unsafe-eval rule from Content Security Policy
How?
It is using Shunting yard algorithm instead of eval()
or new Function()
Example
const { parse } = require('plural-froms-parser');
const expression = 'n > 1 ? 2 : 3';
const rpnExpression = parse(expression);
// {
// postfix: [ 'n', 1, '>', 2, 3, '?:' ],
// evaluate: [Function: evaluate]
// }
rpnExpression.evaluate(1) // 2
rpnExpression.evaluate(2) // 3