@naturalcycles/abba 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/abba.js +2 -2
- package/dist/prisma-output/index-browser.js +0 -5
- package/dist/prisma-output/index.d.ts +0 -11
- package/dist/prisma-output/index.js +2 -7
- package/dist/prisma-output/schema.prisma +2 -2
- package/dist/util.d.ts +7 -14
- package/dist/util.js +18 -29
- package/package.json +1 -2
- package/readme.md +8 -8
- package/src/abba.ts +3 -3
- package/src/prisma-output/index-browser.js +0 -5
- package/src/prisma-output/index.d.ts +0 -11
- package/src/prisma-output/index.js +2 -7
- package/src/prisma-output/schema.prisma +2 -2
- package/src/util.ts +12 -20
package/src/util.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { satisfies } from 'semver'
|
|
2
|
-
import { Bucket
|
|
2
|
+
import { Bucket } from './prisma-output'
|
|
3
3
|
import { BucketInput, SegmentationData, SegmentationRule } from '.'
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -7,7 +7,7 @@ import { BucketInput, SegmentationData, SegmentationRule } from '.'
|
|
|
7
7
|
*
|
|
8
8
|
* @returns
|
|
9
9
|
*/
|
|
10
|
-
export
|
|
10
|
+
export const rollDie = (): number => {
|
|
11
11
|
return Math.random() * 100
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -18,10 +18,9 @@ export function rollDie(): number {
|
|
|
18
18
|
* @param buckets
|
|
19
19
|
* @returns
|
|
20
20
|
*/
|
|
21
|
-
export
|
|
21
|
+
export const determineAssignment = (sampling: number, buckets: Bucket[]): number | null => {
|
|
22
22
|
// Should this person be considered for the experiment?
|
|
23
|
-
|
|
24
|
-
if (!isIncludedInSample) {
|
|
23
|
+
if (rollDie() > sampling) {
|
|
25
24
|
return null
|
|
26
25
|
}
|
|
27
26
|
|
|
@@ -29,23 +28,13 @@ export function determineAssignment(sampling: number, buckets: Bucket[]): number
|
|
|
29
28
|
return determineBucket(buckets)
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
/**
|
|
33
|
-
* Determines whether a user will be considered for a bucket assignment based on the experiment sampling rate
|
|
34
|
-
*
|
|
35
|
-
* @param experiment
|
|
36
|
-
* @returns
|
|
37
|
-
*/
|
|
38
|
-
export function determineExperiment(experiment: Experiment): boolean {
|
|
39
|
-
return rollDie() <= experiment.sampling // between 0 and 100
|
|
40
|
-
}
|
|
41
|
-
|
|
42
31
|
/**
|
|
43
32
|
* Determines which bucket a user assignment will recieve
|
|
44
33
|
*
|
|
45
34
|
* @param buckets
|
|
46
35
|
* @returns
|
|
47
36
|
*/
|
|
48
|
-
export
|
|
37
|
+
export const determineBucket = (buckets: Bucket[]): number => {
|
|
49
38
|
const bucketRoll = rollDie()
|
|
50
39
|
let range: [number, number] | undefined
|
|
51
40
|
const bucket = buckets.find(b => {
|
|
@@ -73,7 +62,7 @@ export function determineBucket(buckets: Bucket[]): number {
|
|
|
73
62
|
* @param buckets
|
|
74
63
|
* @returns
|
|
75
64
|
*/
|
|
76
|
-
export
|
|
65
|
+
export const validateTotalBucketRatio = (buckets: BucketInput[]): void => {
|
|
77
66
|
const bucketSum = buckets.reduce((sum, current) => sum + current.ratio, 0)
|
|
78
67
|
if (bucketSum !== 100) {
|
|
79
68
|
throw new Error('Total bucket ratio must be 100 before you can activate an experiment')
|
|
@@ -87,10 +76,10 @@ export function validateBuckets(buckets: BucketInput[]): void {
|
|
|
87
76
|
* @param segmentationData
|
|
88
77
|
* @returns
|
|
89
78
|
*/
|
|
90
|
-
export
|
|
79
|
+
export const validateSegmentationRules = (
|
|
91
80
|
rules: SegmentationRule[],
|
|
92
81
|
segmentationData: SegmentationData,
|
|
93
|
-
): boolean {
|
|
82
|
+
): boolean => {
|
|
94
83
|
for (const rule of rules) {
|
|
95
84
|
if (!validateSegmentationRule(rule, segmentationData)) return false
|
|
96
85
|
}
|
|
@@ -104,7 +93,10 @@ export function validateSegmentationRules(
|
|
|
104
93
|
* @param segmentationData
|
|
105
94
|
* @returns
|
|
106
95
|
*/
|
|
107
|
-
export
|
|
96
|
+
export const validateSegmentationRule = (
|
|
97
|
+
rule: SegmentationRule,
|
|
98
|
+
data: SegmentationData,
|
|
99
|
+
): boolean => {
|
|
108
100
|
const { key, value, operator } = rule
|
|
109
101
|
if (operator === '==') {
|
|
110
102
|
return data[key] === value
|