Skip to content

Commit

Permalink
Add contains none operator (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
tore-statsig authored Sep 8, 2021
1 parent a155fd2 commit f9fa7dc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 26 deletions.
47 changes: 22 additions & 25 deletions src/Evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
FETCH_FROM_SERVER,
} = require('./ConfigSpec');
const SpecStore = require('./SpecStore');
const UAParser = require('useragent')
const UAParser = require('useragent');

const TYPE_DYNAMIC_CONFIG = 'dynamic_config';
const CONDITION_SEGMENT_COUNT = 10 * 1000;
Expand Down Expand Up @@ -62,10 +62,10 @@ const Evaluator = {
const pass = this._evalPassPercent(user, rule, config);
return config.type.toLowerCase() === TYPE_DYNAMIC_CONFIG
? new DynamicConfig(
config.name,
pass ? rule.returnValue : config.defaultValue,
rule.id,
)
config.name,
pass ? rule.returnValue : config.defaultValue,
rule.id,
)
: { value: pass, rule_id: rule.id };
}
}
Expand Down Expand Up @@ -152,8 +152,8 @@ const Evaluator = {
if (value === FETCH_FROM_SERVER) {
return FETCH_FROM_SERVER;
}

switch (condition.operator?.toLowerCase()) {
const op = condition.operator?.toLowerCase();
switch (op) {
// numerical
case 'gt':
return numberCompare((a, b) => a > b)(value, target);
Expand Down Expand Up @@ -224,16 +224,13 @@ const Evaluator = {
return stringCompare((a, b) => a.endsWith(b))(value, target);
}
case 'str_contains_any':
if (Array.isArray(target)) {
for (let i = 0; i < target.length; i++) {
if (stringCompare((a, b) => a.includes(b))(value, target[i])) {
return true;
}
case 'str_contains_none':
for (let i = 0; i < target.length; i++) {
if (stringCompare((a, b) => a.includes(b))(value, target[i])) {
return op === 'str_contains_any';
}
return false;
} else {
return stringCompare((a, b) => a.includes(b))(value, target);
}
return op === 'str_contains_none';
case 'str_matches':
try {
return new RegExp(target).test(value);
Expand Down Expand Up @@ -316,20 +313,20 @@ function getFromUserAgent(user, field) {
}
const res = UAParser.parse(ua);
switch (field.toLowerCase()) {
case "os_name":
case "osname":
case 'os_name':
case 'osname':
return res.os.family ?? null;
case "os_version":
case "osversion":
case 'os_version':
case 'osversion':
return res.os.toVersion() ?? null;
case "browser_name":
case "browsername":
case 'browser_name':
case 'browsername':
return res.family ?? null;
case "browser_version":
case "browserversion":
return res.toVersion() ?? null
case 'browser_version':
case 'browserversion':
return res.toVersion() ?? null;
default:
return null
return null;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/RulesetsEvalConsistency.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ async function _validateServerSDKConsistency(api) {
const sdkValue = await statsig.checkGate(user, name);
if (sdkValue !== gates[name]) {
console.log(
`Test failed for gate ${name}. Server got ${gates[name]}, SDK got ${sdkValue}`,
`Test failed for gate ${name}. Server got ${
gates[name]
}, SDK got ${sdkValue} for ${JSON.stringify(user)}`,
);
}
expect(sdkValue).toEqual(gates[name]);
Expand Down

0 comments on commit f9fa7dc

Please sign in to comment.