abtest-util
A simple and extensible utility library for abtest
Usage
First, create your test with new ABTest(config)
:
const appId = const appIdToRunABTest = '1731035743'const test = // current user user: "7ae4d9c516" // method to classify user into groups { return % 2 === 0 ? thisgroupsA : thisgroupsB } // assign handler(s) to each group handlers: 'Running default function of group A' B: 'Running foo of group B' bar: ABTestnoop // do nothing // optional: if you need 3 or more groups, assign them to // `groups`, and they can be accessed as this.groups.C groups: 'A' 'B' 'C' // optional: conditionally run the test { return appId === appIdToRunABTest } moduleexports = test
When you want to run the test:
const test = ;// if user is in group A:const resultA = test;// if user is in group B:const resultB = test;// NOTE: user can't be group A and group B!// so one of (resultA, resultB) will throw an Error!
API
new ABTest(config)
config | description | type |
---|---|---|
config.user | required. Current user | string|number |
config.classify | required. Method to divide user into groups | (string|number) => string |
config.handlers | required. Individual handlers for groups | see example |
config.shouldRunTest | optional. Method to decide whether test or not | (...param: any[]) => boolean. default: () => true |
config.groups | optional. All groups of current test | string[]. default: ['A', 'B'] |
const test = user: "7ae4d9c516" { return % 2 === 0 ? thisgroupsA : thisgroupsB; } handlers: "Running default function of group A" B: "Running foo of group B" "Running bar of group B" ;
ABTest.prototype.setUser(user)
Set current user.
ABTest.prototype.getUser()
Get current user.
test;test; // 7ae4d9c517
ABTest.prototype.getGroupId()
Get current group id.
test; // Btest;test; // A
ABTest.prototype.addHandler(name, handler[, groupId])
Adds the handler
of name
on group groupId
(default: current groupId).
test // Btesttest // Running foo of group Btesttest // Atest
ABTest.prototype.run
test; // Error: Name is required for object type handlers.test; // 'Running default function of group A'test; // 'Running foo of group B'test; // 'Running bar of group B'