-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
176 lines (152 loc) · 4.02 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* eslint-disable import/no-unused-modules */
import {
getAll,
getItem,
setItem,
removeItem,
type SetCookieItemOptions
} from '@rambler-tech/cookie-storage'
const DEFAULT_OPTIONS: SplitsOptions = {
path: '/',
expires: 182,
domain: `.${window.location.hostname}`,
samesite: 'none',
secure: true
}
/** Split test declaration */
export interface SplitTest {
/** Test name */
name: string
/** Custom cookie name */
cookie?: string
/** Test variations */
variations: SplitTestVariation[]
}
/** Split test variation declaration */
export interface SplitTestVariation {
/** Variation name */
name: string
/** Variation probability */
probability: number[]
}
/** Split options */
export interface SplitsOptions extends Omit<SetCookieItemOptions, 'raw'> {
/** Cookie prefix */
prefix?: string
}
/** Split calculation result */
export interface SplitsVariations {
[key: string]: string
}
/**
* Splits calculator
*
* ```ts
* const tests = [
* {
* name: 'loginType',
* variations: [
* {
* name: 'compact',
* probability: [0, 0.5]
* },
* {
* name: 'full',
* probability: [0.5, 1]
* }
* ]
* }
* ]
*
* const splits = new Splits(tests, {prefix: 'x_'})
* const variations = splits.getVariations()
*
* // {loginType: 'compact'}
* ```
*/
export class Splits {
private tests: SplitTest[]
private options: SplitsOptions
/** Splits constructor */
public constructor(tests: SplitTest[], options: SplitsOptions = {}) {
if (!Array.isArray(tests) || !tests.length) {
throw new Error('expected `settings.tests` is not empty array')
}
tests.forEach(({name, variations}) => {
if (!/^[A-Za-z0-9_-]+$/.test(name)) {
throw new Error(
`expected \`${name}\` contains \`A-Za-z0-9_-\` symbols only`
)
}
if (!Array.isArray(variations) || !variations.length) {
throw new Error(
`expected \`variations\` of \`${name}\` is not empty array`
)
}
variations.forEach(({name: varName, probability}) => {
if (
!Array.isArray(probability) ||
typeof probability[0] !== 'number' ||
typeof probability[1] !== 'number'
) {
throw new Error(
`expected \`probability\` of \`${name}.${varName}\` is array of numbers`
)
}
})
})
this.tests = tests
this.options = {...DEFAULT_OPTIONS, ...options}
}
/** Get calculated variations */
public getVariations(): SplitsVariations {
const variations = this.tests.reduce<SplitsVariations>(
(accumulator, {name, cookie, variations}) => {
accumulator[name] = this.chooseVariation(
variations,
this.getProbability(cookie || name, this.options)
)
return accumulator
},
{}
)
this.dropIrrelevantProbabilities()
return variations
}
private dropIrrelevantProbabilities(): void {
const items = getAll()
const {
tests,
options: {prefix = '', ...options}
} = this
if (items) {
const removal = Object.keys(items).filter(
(cookieName) =>
cookieName.indexOf(prefix) === 0 &&
!tests.filter(
({name, cookie}) => cookieName === `${prefix}${cookie || name}`
).length
)
removal.forEach((cookieName) => removeItem(cookieName, options))
}
}
private getProbability(name: string, {prefix = '', ...options}): number {
const cookieName = `${prefix}${name}`
let probability = getItem<number>(cookieName)
if (!probability || typeof probability !== 'number') {
probability = Math.random()
}
setItem<number>(cookieName, probability, options)
return probability
}
private chooseVariation(
variations: SplitTestVariation[],
probability: number
): string {
return variations.reduce<string>(
(accumulator, {name, probability: [start, end]}) =>
probability >= start && probability < end ? name : accumulator,
'default'
)
}
}