https://github.com/nikaple/abtest-util
A tiny ABTest utility library
https://github.com/nikaple/abtest-util
Last synced: 4 days ago
JSON representation
A tiny ABTest utility library
- Host: GitHub
- URL: https://github.com/nikaple/abtest-util
- Owner: Nikaple
- License: mit
- Created: 2018-04-24T12:16:03.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-12-05T13:56:02.000Z (over 6 years ago)
- Last Synced: 2025-02-10T21:25:51.305Z (over 1 year ago)
- Language: TypeScript
- Size: 103 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# abtest-util
[](https://circleci.com/gh/Nikaple/abtest-util)
[](https://codecov.io/gh/Nikaple/abtest-util)
[](https://badge.fury.io/js/abtest-util)
[](https://www.npmjs.com/package/abtest-util)
A simple and extensible utility library for abtest
## Usage
First, create your test with `new ABTest(config)`:
```javascript
const appId = require('./some.state')
const appIdToRunABTest = '1731035743'
const test = new ABTest({
// current user
user: "7ae4d9c516",
// method to classify user into groups
classify(user) {
return parseInt(user, 16) % 2 === 0
? this.groups.A
: this.groups.B
},
// assign handler(s) to each group
handlers: {
A: () => 'Running default function of group A',
B: {
foo: () => 'Running foo of group B',
bar: ABTest.noop, // 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
shouldRunTest(user) {
return appId === appIdToRunABTest
}
})
module.exports = test
```
When you want to run the test:
```javascript
const test = require("./abtest");
// if user is in group A:
const resultA = test.run();
// if user is in group B:
const resultB = test.run("foo");
// NOTE: user can't be both group A and group B!
// so one of (resultA, resultB) will throw an Error!
```
## API
**new ABTest(config)**: create a new ABTest.
| 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'] |
```javascript
const test = new ABTest({
user: "7ae4d9c516",
classify(user) {
return parseInt(user, 16) % 2 === 0 ? this.groups.A : this.groups.B;
},
handlers: {
A: () => "Running default function of group A",
B: {
foo: () => "Running foo of group B",
bar: () => "Running bar of group B"
}
}
});
```
**ABTest.prototype.setUser(user)**
Set current user.
**ABTest.prototype.getUser()**
Get current user.
```javascript
test.setUser("7ae4d9c517");
test.getUser(); // 7ae4d9c517
```
**ABTest.prototype.getGroupId()**
Get current group id.
```javascript
test.getGroupId(); // B
test.setUser("7ae4d9c517");
test.getGroupId(); // A
```
**ABTest.prototype.addHandler(name, handler[, groupId])**
Adds the `handler` of `name` on group `groupId`(default: current groupId).
```javascript
test.getGroupId(); // B
test.addHandler("baz", () => "Running baz of group B");
test.run("baz"); // Running foo of group B
test.setUser("7ae4d9c517");
test.getGroupId(); // A
test.addHandler("foo", () => "Running foo of group A");
test.run("foo"); // Running foo of group
test.run(); // Running default function of group A
```
**ABTest.prototype.run([name])**
Run `name` function in handler groups. When no name was provided, test will try to run default handler.
```javascript
// if user is in group A:
const resultA = test.run(); // Running default function of group A
// if user is in group B:
const resultB = test.run("foo"); // Running foo of group B
// NOTE: user can't be both group A and group B!
// so one of (resultA, resultB) will throw an Error!
```