Skip to content

Stryzhevskyi/plural-forms-parser

Repository files navigation

plural-forms-parser

This library can parse Plural Forms header without violation of unsafe-eval rule from Content Security Policy.

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

There is also parseMemoized function, which caches results of evaluation.

References

About

Parse gettext Plural-Forms header without unsafe-eval

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published