@ninetailed/experience.js-utils 3.0.0-beta.33 → 3.0.0-beta.37
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/index.d.ts +11 -2
- package/index.js +68 -22
- package/lib/ExperienceMapper.d.ts +7 -10
- package/package.json +3 -3
- package/types/Audience.d.ts +1 -0
- package/types/Config.d.ts +6 -0
- package/types/Experience.d.ts +2255 -6
- package/types/Experiment.d.ts +1997 -7
- package/types/Variant.d.ts +4 -1
- package/lib/index.d.ts +0 -1
- package/types/index.d.ts +0 -5
package/index.d.ts
CHANGED
|
@@ -1,2 +1,11 @@
|
|
|
1
|
-
export { ExperienceMapper } from './lib';
|
|
2
|
-
export {
|
|
1
|
+
export { ExperienceMapper } from './lib/ExperienceMapper';
|
|
2
|
+
export { Audience } from './types/Audience';
|
|
3
|
+
export { Config } from './types/Config';
|
|
4
|
+
export { Experience } from './types/Experience';
|
|
5
|
+
export { Experiment } from './types/Experiment';
|
|
6
|
+
export { Variant } from './types/Variant';
|
|
7
|
+
export type { AudienceLike } from './types/Audience';
|
|
8
|
+
export type { ConfigLike } from './types/Config';
|
|
9
|
+
export type { ExperienceLike } from './types/Experience';
|
|
10
|
+
export type { ExperimentLike } from './types/Experiment';
|
|
11
|
+
export type { VariantLike } from './types/Variant';
|
package/index.js
CHANGED
|
@@ -26,10 +26,15 @@ const Config = z.object({
|
|
|
26
26
|
}]
|
|
27
27
|
}])
|
|
28
28
|
});
|
|
29
|
+
// export interface Config {
|
|
30
|
+
// distribution: number[];
|
|
31
|
+
// traffic: number;
|
|
32
|
+
// components: BaselineWithVariantRefs[];
|
|
33
|
+
// }
|
|
29
34
|
|
|
30
35
|
const Variant = z.object({
|
|
31
36
|
id: z.string()
|
|
32
|
-
}).
|
|
37
|
+
}).catchall(z.unknown());
|
|
33
38
|
|
|
34
39
|
/**
|
|
35
40
|
* Zod helper for parsing arrays and ignore items not specified in the schema
|
|
@@ -57,7 +62,7 @@ function isArray(item) {
|
|
|
57
62
|
return Array.isArray(item);
|
|
58
63
|
}
|
|
59
64
|
|
|
60
|
-
const
|
|
65
|
+
const ExperienceSchema = z.object({
|
|
61
66
|
id: z.string(),
|
|
62
67
|
/**
|
|
63
68
|
* The name of the experience (Short Text)
|
|
@@ -66,7 +71,7 @@ const Experience = z.object({
|
|
|
66
71
|
/**
|
|
67
72
|
* The type if the experience (nt_experiment | nt_personalization)
|
|
68
73
|
*/
|
|
69
|
-
type: z.union([z.
|
|
74
|
+
type: z.union([z.string().regex(/^nt_experiment$/g), z.string().regex(/^nt_personalization$/g)]),
|
|
70
75
|
/**
|
|
71
76
|
* The config of the experience (JSON)
|
|
72
77
|
*/
|
|
@@ -80,9 +85,51 @@ const Experience = z.object({
|
|
|
80
85
|
*/
|
|
81
86
|
variants: zodArrayIgnoreUnknown(Variant).default([])
|
|
82
87
|
});
|
|
88
|
+
const parse$1 = input => {
|
|
89
|
+
const output = ExperienceSchema.parse(input);
|
|
90
|
+
return Object.assign(Object.assign({}, output), {
|
|
91
|
+
variants: input.variants
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
const safeParse$1 = input => {
|
|
95
|
+
const output = ExperienceSchema.safeParse(input);
|
|
96
|
+
if (!output.success) {
|
|
97
|
+
return output;
|
|
98
|
+
}
|
|
99
|
+
return Object.assign(Object.assign({}, output), {
|
|
100
|
+
data: Object.assign(Object.assign({}, output.data), {
|
|
101
|
+
variants: input.variants
|
|
102
|
+
})
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
const Experience = Object.assign(Object.assign({}, ExperienceSchema), {
|
|
106
|
+
parse: parse$1,
|
|
107
|
+
safeParse: safeParse$1
|
|
108
|
+
});
|
|
83
109
|
|
|
84
|
-
const
|
|
85
|
-
type: z.
|
|
110
|
+
const ExperimentSchema = ExperienceSchema.extend({
|
|
111
|
+
type: z.string().regex(/^nt_experiment$/g)
|
|
112
|
+
});
|
|
113
|
+
const parse = input => {
|
|
114
|
+
const output = ExperimentSchema.parse(input);
|
|
115
|
+
return Object.assign(Object.assign({}, output), {
|
|
116
|
+
variants: input.variants
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
const safeParse = input => {
|
|
120
|
+
const output = ExperimentSchema.safeParse(input);
|
|
121
|
+
if (!output.success) {
|
|
122
|
+
return output;
|
|
123
|
+
}
|
|
124
|
+
return Object.assign(Object.assign({}, output), {
|
|
125
|
+
data: Object.assign(Object.assign({}, output.data), {
|
|
126
|
+
variants: input.variants
|
|
127
|
+
})
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
const Experiment = Object.assign(Object.assign({}, ExperimentSchema), {
|
|
131
|
+
parse,
|
|
132
|
+
safeParse
|
|
86
133
|
});
|
|
87
134
|
|
|
88
135
|
class ExperienceMapper {
|
|
@@ -102,33 +149,32 @@ class ExperienceMapper {
|
|
|
102
149
|
config,
|
|
103
150
|
variants
|
|
104
151
|
} = parsedExperience.data;
|
|
152
|
+
const {
|
|
153
|
+
components,
|
|
154
|
+
traffic
|
|
155
|
+
} = config;
|
|
105
156
|
return Object.assign(Object.assign({
|
|
106
157
|
id,
|
|
107
|
-
type
|
|
158
|
+
type: type
|
|
108
159
|
}, audience ? {
|
|
109
160
|
audience
|
|
110
161
|
} : {}), {
|
|
111
|
-
trafficAllocation:
|
|
162
|
+
trafficAllocation: traffic,
|
|
112
163
|
distribution: config.distribution.map((percentage, index) => ({
|
|
113
164
|
index,
|
|
114
165
|
start: config.distribution.slice(0, index).reduce((a, b) => a + b, 0),
|
|
115
166
|
end: config.distribution.slice(0, index + 1).reduce((a, b) => a + b, 0)
|
|
116
167
|
})),
|
|
117
|
-
components:
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
return matchingVariant;
|
|
129
|
-
}).filter(variant => variant !== null)
|
|
130
|
-
};
|
|
131
|
-
})
|
|
168
|
+
components: components.map(component => ({
|
|
169
|
+
baseline: component.baseline,
|
|
170
|
+
variants: component.variants.map(variantRef => {
|
|
171
|
+
if (variantRef.hidden) {
|
|
172
|
+
return variantRef;
|
|
173
|
+
}
|
|
174
|
+
const matchingVariant = variants.find(variant => variant.id === variantRef.id);
|
|
175
|
+
return matchingVariant !== null && matchingVariant !== void 0 ? matchingVariant : null;
|
|
176
|
+
}).filter(variant => variant !== null)
|
|
177
|
+
}))
|
|
132
178
|
});
|
|
133
179
|
}
|
|
134
180
|
static isExperimentEntry(experiment) {
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import { ExperienceConfiguration } from '@ninetailed/experience.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
declare type ExperienceEntry = z.input<typeof Experience>;
|
|
5
|
-
declare type ExperimentEntry = z.input<typeof Experiment>;
|
|
1
|
+
import { ExperienceConfiguration, Reference } from '@ninetailed/experience.js';
|
|
2
|
+
import { Experience, ExperienceLike } from '../types/Experience';
|
|
3
|
+
import { ExperimentLike } from '../types/Experiment';
|
|
6
4
|
export declare class ExperienceMapper {
|
|
7
|
-
static isExperienceEntry(experience:
|
|
8
|
-
static mapExperience(experience:
|
|
9
|
-
static isExperimentEntry(experiment:
|
|
10
|
-
static mapExperiment(experiment:
|
|
5
|
+
static isExperienceEntry<Variant extends Reference>(experience: ExperienceLike<Variant>): experience is Experience<Variant>;
|
|
6
|
+
static mapExperience<Variant extends Reference>(experience: ExperienceLike<Variant>): ExperienceConfiguration<Variant>;
|
|
7
|
+
static isExperimentEntry<Variant extends Reference>(experiment: ExperimentLike<Variant>): experiment is ExperimentLike<Variant>;
|
|
8
|
+
static mapExperiment<Variant extends Reference>(experiment: ExperimentLike<Variant>): ExperienceConfiguration<Variant>;
|
|
11
9
|
}
|
|
12
|
-
export {};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ninetailed/experience.js-utils",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.37",
|
|
4
4
|
"module": "./index.js",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"types": "./index.d.ts",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@ninetailed/experience.js": "3.0.0-beta.
|
|
10
|
-
"@ninetailed/experience.js-shared": "3.0.0-beta.
|
|
9
|
+
"@ninetailed/experience.js": "3.0.0-beta.37",
|
|
10
|
+
"@ninetailed/experience.js-shared": "3.0.0-beta.37",
|
|
11
11
|
"zod": "3.20.2"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {}
|
package/types/Audience.d.ts
CHANGED
package/types/Config.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Baseline, VariantRef } from '@ninetailed/experience.js';
|
|
1
2
|
import { z } from 'zod';
|
|
2
3
|
export declare const Config: z.ZodObject<{
|
|
3
4
|
distribution: z.ZodDefault<z.ZodArray<z.ZodNumber, "many">>;
|
|
@@ -62,4 +63,9 @@ export declare const Config: z.ZodObject<{
|
|
|
62
63
|
}[];
|
|
63
64
|
}[] | undefined;
|
|
64
65
|
}>;
|
|
66
|
+
export declare type ConfigLike = z.input<typeof Config>;
|
|
65
67
|
export declare type Config = z.infer<typeof Config>;
|
|
68
|
+
export interface BaselineWithVariantRefs {
|
|
69
|
+
baseline: Baseline;
|
|
70
|
+
variants: VariantRef[];
|
|
71
|
+
}
|