Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

abtest-util

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

abtest-util

A tiny ABTest utility library

latest
Source
npmnpm
Version
0.0.4
Version published
Maintainers
1
Created
Source

abtest-util

CircleCI Codecov version license

A simple and extensible utility library for abtest

Usage

First, create your test with new ABTest(config):

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:

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 group A and group B!
// so one of (resultA, resultB) will throw an Error!

API

new ABTest(config)

configdescriptiontype
config.userrequired. Current userstring|number
config.classifyrequired. Method to divide user into groups(string|number) => string
config.handlersrequired. Individual handlers for groupssee example
config.shouldRunTestoptional. Method to decide whether test or not(...param: any[]) => boolean. default: () => true
config.groupsoptional. All groups of current teststring[]. default: ['A', 'B']
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.

test.setUser("7ae4d9c517");
test.getUser(); // 7ae4d9c517

ABTest.prototype.getGroupId()

Get current group id.

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).

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', () => )

ABTest.prototype.run

test.run(); // Error: Name is required for object type handlers.
test.run(); // 'Running default function of group A'
test.run("foo"); // 'Running foo of group B'
test.run("bar"); // 'Running bar of group B'

Keywords

abtest

FAQs

Package last updated on 24 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts