{"id":19746755,"url":"https://github.com/nikaple/abtest-util","last_synced_at":"2026-06-11T08:31:02.314Z","repository":{"id":57172179,"uuid":"130849187","full_name":"Nikaple/abtest-util","owner":"Nikaple","description":"A tiny ABTest utility library","archived":false,"fork":false,"pushed_at":"2019-12-05T13:56:02.000Z","size":105,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-10T21:25:51.305Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Nikaple.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-24T12:16:03.000Z","updated_at":"2018-04-24T13:46:35.000Z","dependencies_parsed_at":"2022-08-24T13:30:54.634Z","dependency_job_id":null,"html_url":"https://github.com/Nikaple/abtest-util","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikaple%2Fabtest-util","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikaple%2Fabtest-util/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikaple%2Fabtest-util/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nikaple%2Fabtest-util/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nikaple","download_url":"https://codeload.github.com/Nikaple/abtest-util/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241078399,"owners_count":19905850,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-12T02:15:47.929Z","updated_at":"2026-06-11T08:31:02.308Z","avatar_url":"https://github.com/Nikaple.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# abtest-util\n\n[![CircleCI](https://circleci.com/gh/Nikaple/abtest-util.svg?style=shield)](https://circleci.com/gh/Nikaple/abtest-util)\n[![codecov](https://codecov.io/gh/Nikaple/abtest-util/branch/master/graph/badge.svg)](https://codecov.io/gh/Nikaple/abtest-util)\n[![version](https://badge.fury.io/js/abtest-util.svg)](https://badge.fury.io/js/abtest-util)\n[![license](https://img.shields.io/npm/l/abtest-util.svg)](https://www.npmjs.com/package/abtest-util)\n\nA simple and extensible utility library for abtest\n\n## Usage\n\nFirst, create your test with `new ABTest(config)`:\n\n```javascript\nconst appId = require('./some.state')\nconst appIdToRunABTest = '1731035743'\nconst test = new ABTest({\n    // current user\n    user: \"7ae4d9c516\",\n    // method to classify user into groups\n    classify(user) {\n      \treturn parseInt(user, 16) % 2 === 0\n        \t? this.groups.A\n        \t: this.groups.B\n    },\n    // assign handler(s) to each group\n    handlers: {\n        A: () =\u003e 'Running default function of group A',\n        B: {\n            foo: () =\u003e 'Running foo of group B',\n            bar: ABTest.noop, // do nothing\n        },\n    }\n    // optional: if you need 3 or more groups, assign them to\n    // `groups`, and they can be accessed as this.groups.C\n    groups: ['A', 'B', 'C'],\n    // optional: conditionally run the test\n    shouldRunTest(user) {\n    \treturn appId === appIdToRunABTest\n\t}\n})\n\nmodule.exports = test\n```\n\nWhen you want to run the test:\n\n```javascript\nconst test = require(\"./abtest\");\n// if user is in group A:\nconst resultA = test.run();\n// if user is in group B:\nconst resultB = test.run(\"foo\");\n// NOTE: user can't be both group A and group B!\n// so one of (resultA, resultB) will throw an Error!\n```\n\n## API\n\n**new ABTest(config)**: create a new ABTest.\n\n| config               | description                                    | type                                              |\n| -------------------- | ---------------------------------------------- | ------------------------------------------------- |\n| config.user          | required. Current user                         | string\\|number                                    |\n| config.classify      | required. Method to divide user into groups    | (string\\|number) =\u003e string                        |\n| config.handlers      | required. Individual handlers for groups       | see example                                       |\n| config.shouldRunTest | optional. Method to decide whether test or not | (...param: any[]) =\u003e boolean. default: () =\u003e true |\n| config.groups        | optional. All groups of current test           | string[]. default: ['A', 'B']                     |\n\n```javascript\nconst test = new ABTest({\n  user: \"7ae4d9c516\",\n  classify(user) {\n    return parseInt(user, 16) % 2 === 0 ? this.groups.A : this.groups.B;\n  },\n  handlers: {\n    A: () =\u003e \"Running default function of group A\",\n    B: {\n      foo: () =\u003e \"Running foo of group B\",\n      bar: () =\u003e \"Running bar of group B\"\n    }\n  }\n});\n```\n\n**ABTest.prototype.setUser(user)**\n\nSet current user.\n\n**ABTest.prototype.getUser()**\n\nGet current user.\n\n```javascript\ntest.setUser(\"7ae4d9c517\");\ntest.getUser(); // 7ae4d9c517\n```\n\n**ABTest.prototype.getGroupId()**\n\nGet current group id.\n\n```javascript\ntest.getGroupId(); // B\ntest.setUser(\"7ae4d9c517\");\ntest.getGroupId(); // A\n```\n\n**ABTest.prototype.addHandler(name, handler[, groupId])**\n\nAdds the `handler` of `name` on group `groupId`(default: current groupId).\n\n```javascript\ntest.getGroupId(); // B\ntest.addHandler(\"baz\", () =\u003e \"Running baz of group B\");\ntest.run(\"baz\"); // Running foo of group B\ntest.setUser(\"7ae4d9c517\");\ntest.getGroupId(); // A\ntest.addHandler(\"foo\", () =\u003e \"Running foo of group A\");\ntest.run(\"foo\"); // Running foo of group\ntest.run(); // Running default function of group A\n```\n\n**ABTest.prototype.run([name])**\n\nRun `name` function in handler groups. When no name was provided, test will try to run default handler.\n\n```javascript\n// if user is in group A:\nconst resultA = test.run(); // Running default function of group A\n// if user is in group B:\nconst resultB = test.run(\"foo\"); // Running foo of group B\n// NOTE: user can't be both group A and group B!\n// so one of (resultA, resultB) will throw an Error!\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikaple%2Fabtest-util","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnikaple%2Fabtest-util","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnikaple%2Fabtest-util/lists"}