pickpick-targeting-compiler
compiles targeting expressions to javascript code for pickpick engine
- uses jsep to parse expressions and turn them into a javascript function that evaluates a conditiongiven a certain input.
- uses the
vm
module to have isolated context - in addition to normal javascript operators, there are some special operators (see below for example):
in
is exactlyArray.includes
match
regexp teststartsWith
andendsWith
is exactlyString.startsWith
andString.endsWith
respectively
- exposes a bunch of useful
is*
functions fromutil
module - open for extension via user defined envinronment accessible using
$
example
npm i pickpick-targeting-compiler
const assert = const compile = // intentionally using var here // all normal js operators applyvar isMatch features = var isMatch features = // can create compound logical statementsvar isMatch features = // in operator - same as Array.includes(value)var isMatch features = // operators apply to context data as wellvar isMatch features = // startsWith operator - same as String.startsWith(string)var isMatch features = // endsWith operator - same as String.endsWith(string)var isMatch features = // match operator for literal regular expressions, same as /regex/g.test('value')var isMatch features = // deeplyEquals operator performs a deep equal comparison (uses [lodash.isEqual](https://lodash.com/docs/4.17.10#isEqual))var isMatch features = // exposes a bunch of isSomething from util:// isNullOrUndefined,// isNull,// isUndefined,// isString,// isNumber,// isArray,// isObject,// isBoolean,// isDate,// isNull,// isPrimitivevar isMatch features = // provide user defined functions and dataconst userEnvironment = x: 1 2 3 arg var isMatch features =
api
Table of Contents
compile
Compile an expression into a resuable javascript function
Parameters
expression
String any expression that can be parsed by jsepoptions
Object (optional, default{}
)options.matcherProperty
String return the match function using a different property name in the compound return value, e.g: (optional, default'isMatch'
)options.userEnvironment
Object allows the user to inject additional functionality that will be exposed to the expression. e.g: (optional, default{}
)options.userEnvNamespace
String change the name used to access user environment in an expression (optional, default$
)options.inputNamespace
String change the name used to access input in the expression (optional, default_
)
Examples
// options.matcherProperty // normally this would be isMatch instead of 'foo' let foo =
// options.userEnvironment let userEnvironment = geos: 'MX' 'US' 'IL' value let isMatch =
Returns Function a function that accepts input and returns a boolean value
license
MIT © ironSource LTD