Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward userEnvironment to pickpick-targeting-compiler #9

Merged
merged 2 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(Targeting)
- instantiate the compiler object with a userEnvironment
  • Loading branch information
Ohadbasan committed Jan 31, 2019
commit a6938cd996f3f68216a96b210bf576e6e3ff6ece
10 changes: 5 additions & 5 deletions lib/Targeting.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const debug = require('./debug')('Targeting')
const equal = require('deep-equal')
const { isNullOrUndefined, isString } = require('util')
const compile = require('pickpick-targeting-compiler')
const _expression = Symbol('_expression')
Expand All @@ -14,10 +13,11 @@ class Targeting {
* construct a new targeting instance
*
* @param {String} expression see [pickpick-targeting-compiler](https://github.com/ironSource/pickpick-targeting-compiler) for more details
* @param {Object} userEnvironment
*/
constructor(expression) {
constructor(expression, userEnvironment = {}) {
this[_expression] = expression
let { isMatch, features } = compile(expression)
let { isMatch, features } = compile(expression, userEnvironment)
this[_isMatch] = isMatch
this[_features] = features
}
Expand Down Expand Up @@ -70,8 +70,8 @@ class Targeting {
return `Targeting ( ${this[_expression]} )`
}

static create(experssion) {
return new Targeting(experssion)
static create(experssion, userEnvironment) {
return new Targeting(experssion, userEnvironment)
}

static default() {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"later": "^1.2.0",
"loadbalance": "^0.3.0",
"lodash": "^4.17.4",
"mocha": "^5.2.0",
"pickpick-targeting-compiler": "0.2.0"
},
"devDependencies": {
Expand Down
18 changes: 18 additions & 0 deletions test/Targeting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,23 @@ describe('Targeting is a logical expression designed to help target relevant exp
expect(targeting.match(true)).to.be.false
expect(targeting.match({ f1: 'f1' })).to.be.false
})
describe('custom userEnvironment', () => {
let userEnvironment = {
customFn: (str1,str2) => {
switch (str1) {
case 'ohad':
return true
case 1:
return false
case 0:
return false
}
}
}
it('match and test', () => {
let targeting = Targeting.create('$.customFn(_.str1, _.str2)', { userEnvironment })
expect(targeting.match({ str1: 'ohad' , str2:'1.2.4'})).to.be.true
})
})
})
})