
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
@woocommerce/expression-evaluation
Advanced tools
Evaluation of JavaScript-like expressions in an optional context.
Examples of simple expressions:
1 + 2
foo === 'bar'
foo ? 'bar' : 'baz'
Examples of complex expressions:
foo.bar.baz === 'qux'
foo.bar
  && ( foo.bar.baz === 'qux' || foo.baz === 'quux' )
foo.bar
	&& ( foo.baz === "qux" || foo.baz === "quux" )
	&& ( foo.quux > 1 && foo.quux <= 5 )
foo.bar
	  && ( foo.baz === "qux" || foo.baz === "quux" )
	  && ( foo.quux > 1 && foo.quux <= 5 )
	? "boo"
	: "baa"
foo
  + 5
  /* This is a comment */
  * ( bar ? baz : qux )
Evaluates an expression in an optional context.
import { evaluate } from '@woocommerce/expression-evaluation';
const result = evaluate( '1 + foo', { foo: 2 } );
console.log( result ); // 3
string: The expression to evaluate.Object: Optional. The context to evaluate the expression in. Variables in the expression will be looked up in this object.any: The result of the expression evaluation.The expression syntax is based on JavaScript. The formal grammar is defined in parser.ts.
An expression consists of a single statement.
Features like if statements, for loops, function calls, and variable assignments, are not supported.
The following types are supported:
nulltrue and falseValues in an expression can be written as literals.
null
true
false
1
5.23
-9
String literals can be written with single or double quotes. This can be helpful if the string contains a single or double quote.
'foo'
"foo"
'foo "bar"'
"foo 'bar'"
Quotes can be escaped with a backslash.
'foo \'bar\''
"foo \"bar\""
Variables can be used in an expression. The value of a variable is looked up in the context.
const result = evaluate( 'foo', { foo: 1 } );
console.log( result ); // 1
Nested properties can be accessed with the dot operator.
const result = evaluate( 'foo.bar', { foo: { bar: 1 } } );
console.log( result ); // 1
The following operators are supported.
==)Returns true if the operands are equal.
1 == 1
!=)Returns true if the operands are not equal.
1 != 2
===)Returns true if the operands are equal and of the same type.
1 === 1
!==)Returns true if the operands are not equal and/or not of the same type.
1 !== "1"
>)Returns true if the left operand is greater than the right operand.
2 > 1
>=)Returns true if the left operand is greater than or equal to the right operand.
2 >= 2
<)Returns true if the left operand is less than the right operand.
1 < 2
<=)Returns true if the left operand is less than or equal to the right operand.
2 <= 2
+)Returns the sum of two operands.
1 + 2
-)Returns the difference of two operands.
2 - 1
*)Returns the product of two operands.
2 * 3
/)Returns the quotient of two operands.
6 / 2
%)Returns the remainder of two operands.
5 % 2
-)Returns the negation of an operand.
-1
&&)Returns true if both operands are true.
true && true
||)Returns true if either operand is true.
true || false
!)Returns true if the operand is false.
!false
Returns the first value if the condition is true, otherwise it returns the second value.
true ? 1 : 2
Comments can be used to document an expression. Comments are treated as whitespace and are ignored by the parser.
/* This is a comment */
FAQs
Library for evaluating expressions.
We found that @woocommerce/expression-evaluation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 19 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.