@ninetailed/experience.js-utils 2.2.3 → 2.2.5

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.
@@ -1,2 +1,2 @@
1
- export { ExperienceMapper } from './lib';
2
- export { Experience, Experiment, Audience, Config, Variant } from './types';
1
+ export { ExperienceMapper } from './lib';
2
+ export { Experience, Experiment, Audience, Config, Variant } from './types';
package/index.esm.js ADDED
@@ -0,0 +1,130 @@
1
+ import { logger } from '@ninetailed/experience.js';
2
+ import { z } from 'zod';
3
+
4
+ const Audience = z.object({
5
+ id: z.string()
6
+ });
7
+
8
+ const Config = z.object({
9
+ distribution: z.array(z.number()).default([0.5, 0.5]),
10
+ traffic: z.number().default(0),
11
+ components: z.array(z.object({
12
+ baseline: z.object({
13
+ id: z.string().default('')
14
+ }),
15
+ variants: z.array(z.object({
16
+ id: z.string().default(''),
17
+ hidden: z.boolean().default(false)
18
+ }))
19
+ })).default([{
20
+ baseline: {
21
+ id: ''
22
+ },
23
+ variants: [{
24
+ id: '',
25
+ hidden: false
26
+ }]
27
+ }])
28
+ });
29
+
30
+ const Variant = z.object({
31
+ id: z.string()
32
+ }).passthrough();
33
+
34
+ const Experience = z.object({
35
+ id: z.string(),
36
+
37
+ /**
38
+ * The name of the experience (Short Text)
39
+ */
40
+ name: z.string(),
41
+
42
+ /**
43
+ * The type if the experience (nt_experiment | nt_personalization)
44
+ */
45
+ type: z.union([z.literal('nt_experiment'), z.literal('nt_personalization')]),
46
+
47
+ /**
48
+ * The config of the experience (JSON)
49
+ */
50
+ config: Config.default({}),
51
+
52
+ /**
53
+ * The audience of the experience (Audience)
54
+ */
55
+ audience: Audience.optional().nullable(),
56
+
57
+ /**
58
+ * All used variants of the experience (References to other Content Types)
59
+ */
60
+ variants: z.array(Variant).default([])
61
+ });
62
+
63
+ const Experiment = Experience.extend({
64
+ type: z.literal('nt_experiment')
65
+ });
66
+
67
+ class ExperienceMapper {
68
+ static isExperienceEntry(experience) {
69
+ return Experience.safeParse(experience).success;
70
+ }
71
+
72
+ static mapExperience(experience) {
73
+ const parsedExperience = Experience.safeParse(experience);
74
+
75
+ if (!parsedExperience.success) {
76
+ logger.warn('[Ninetailed ExperienceMapper]', 'Error parsing experience', parsedExperience.error.format());
77
+ throw new Error(`[Ninetailed ExperienceMapper] The Experience Input is not valid. Please filter data first with "ExperienceMapper.isExperienceEntry".\n${JSON.stringify(parsedExperience.error.format(), null, 2)}`);
78
+ }
79
+
80
+ const {
81
+ id,
82
+ type,
83
+ audience,
84
+ config,
85
+ variants
86
+ } = parsedExperience.data;
87
+ return Object.assign(Object.assign({
88
+ id,
89
+ type
90
+ }, audience ? {
91
+ audience
92
+ } : {}), {
93
+ trafficAllocation: config.traffic,
94
+ distribution: config.distribution.map((percentage, index) => ({
95
+ index,
96
+ start: config.distribution.slice(0, index).reduce((a, b) => a + b, 0),
97
+ end: config.distribution.slice(0, index + 1).reduce((a, b) => a + b, 0)
98
+ })),
99
+ components: config.components.map(component => {
100
+ return {
101
+ baseline: component.baseline,
102
+ variants: component.variants.map(variant => {
103
+ if (variant.hidden) {
104
+ return variant;
105
+ }
106
+
107
+ const matchingVariant = variants.find(variantReference => variantReference.id === variant.id);
108
+
109
+ if (!matchingVariant) {
110
+ return null;
111
+ }
112
+
113
+ return matchingVariant;
114
+ }).filter(variant => variant !== null)
115
+ };
116
+ })
117
+ });
118
+ }
119
+
120
+ static isExperimentEntry(experiment) {
121
+ return Experiment.safeParse(experiment).success;
122
+ }
123
+
124
+ static mapExperiment(experiment) {
125
+ return ExperienceMapper.mapExperience(experiment);
126
+ }
127
+
128
+ }
129
+
130
+ export { Audience, Config, Experience, ExperienceMapper, Experiment, Variant };
package/index.umd.js ADDED
@@ -0,0 +1,182 @@
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@ninetailed/experience.js'), require('zod')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@ninetailed/experience.js', 'zod'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.UtilsJavascript = {}, global.experience_js, global.zod));
5
+ })(this, (function (exports, experience_js, zod) { 'use strict';
6
+
7
+ /*! *****************************************************************************
8
+ Copyright (c) Microsoft Corporation.
9
+
10
+ Permission to use, copy, modify, and/or distribute this software for any
11
+ purpose with or without fee is hereby granted.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
14
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
16
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
18
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19
+ PERFORMANCE OF THIS SOFTWARE.
20
+ ***************************************************************************** */
21
+
22
+ var __assign = function() {
23
+ __assign = Object.assign || function __assign(t) {
24
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
25
+ s = arguments[i];
26
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
27
+ }
28
+ return t;
29
+ };
30
+ return __assign.apply(this, arguments);
31
+ };
32
+
33
+ var Audience = zod.z.object({
34
+ id: zod.z.string()
35
+ });
36
+
37
+ var Config = zod.z.object({
38
+ distribution: zod.z.array(zod.z.number()).default([0.5, 0.5]),
39
+ traffic: zod.z.number().default(0),
40
+ components: zod.z.array(zod.z.object({
41
+ baseline: zod.z.object({
42
+ id: zod.z.string().default('')
43
+ }),
44
+ variants: zod.z.array(zod.z.object({
45
+ id: zod.z.string().default(''),
46
+ hidden: zod.z.boolean().default(false)
47
+ }))
48
+ })).default([{
49
+ baseline: {
50
+ id: ''
51
+ },
52
+ variants: [{
53
+ id: '',
54
+ hidden: false
55
+ }]
56
+ }])
57
+ });
58
+
59
+ var Variant = zod.z.object({
60
+ id: zod.z.string()
61
+ }).passthrough();
62
+
63
+ var Experience = zod.z.object({
64
+ id: zod.z.string(),
65
+
66
+ /**
67
+ * The name of the experience (Short Text)
68
+ */
69
+ name: zod.z.string(),
70
+
71
+ /**
72
+ * The type if the experience (nt_experiment | nt_personalization)
73
+ */
74
+ type: zod.z.union([zod.z.literal('nt_experiment'), zod.z.literal('nt_personalization')]),
75
+
76
+ /**
77
+ * The config of the experience (JSON)
78
+ */
79
+ config: Config.default({}),
80
+
81
+ /**
82
+ * The audience of the experience (Audience)
83
+ */
84
+ audience: Audience.optional().nullable(),
85
+
86
+ /**
87
+ * All used variants of the experience (References to other Content Types)
88
+ */
89
+ variants: zod.z.array(Variant).default([])
90
+ });
91
+
92
+ var Experiment = Experience.extend({
93
+ type: zod.z.literal('nt_experiment')
94
+ });
95
+
96
+ var ExperienceMapper =
97
+ /** @class */
98
+ function () {
99
+ function ExperienceMapper() {}
100
+
101
+ ExperienceMapper.isExperienceEntry = function (experience) {
102
+ return Experience.safeParse(experience).success;
103
+ };
104
+
105
+ ExperienceMapper.mapExperience = function (experience) {
106
+ var parsedExperience = Experience.safeParse(experience);
107
+
108
+ if (!parsedExperience.success) {
109
+ experience_js.logger.warn('[Ninetailed ExperienceMapper]', 'Error parsing experience', parsedExperience.error.format());
110
+ throw new Error("[Ninetailed ExperienceMapper] The Experience Input is not valid. Please filter data first with \"ExperienceMapper.isExperienceEntry\".\n".concat(JSON.stringify(parsedExperience.error.format(), null, 2)));
111
+ }
112
+
113
+ var _a = parsedExperience.data,
114
+ id = _a.id,
115
+ type = _a.type,
116
+ audience = _a.audience,
117
+ config = _a.config,
118
+ variants = _a.variants;
119
+ return __assign(__assign({
120
+ id: id,
121
+ type: type
122
+ }, audience ? {
123
+ audience: audience
124
+ } : {}), {
125
+ trafficAllocation: config.traffic,
126
+ distribution: config.distribution.map(function (percentage, index) {
127
+ return {
128
+ index: index,
129
+ start: config.distribution.slice(0, index).reduce(function (a, b) {
130
+ return a + b;
131
+ }, 0),
132
+ end: config.distribution.slice(0, index + 1).reduce(function (a, b) {
133
+ return a + b;
134
+ }, 0)
135
+ };
136
+ }),
137
+ components: config.components.map(function (component) {
138
+ return {
139
+ baseline: component.baseline,
140
+ variants: component.variants.map(function (variant) {
141
+ if (variant.hidden) {
142
+ return variant;
143
+ }
144
+
145
+ var matchingVariant = variants.find(function (variantReference) {
146
+ return variantReference.id === variant.id;
147
+ });
148
+
149
+ if (!matchingVariant) {
150
+ return null;
151
+ }
152
+
153
+ return matchingVariant;
154
+ }).filter(function (variant) {
155
+ return variant !== null;
156
+ })
157
+ };
158
+ })
159
+ });
160
+ };
161
+
162
+ ExperienceMapper.isExperimentEntry = function (experiment) {
163
+ return Experiment.safeParse(experiment).success;
164
+ };
165
+
166
+ ExperienceMapper.mapExperiment = function (experiment) {
167
+ return ExperienceMapper.mapExperience(experiment);
168
+ };
169
+
170
+ return ExperienceMapper;
171
+ }();
172
+
173
+ exports.Audience = Audience;
174
+ exports.Config = Config;
175
+ exports.Experience = Experience;
176
+ exports.ExperienceMapper = ExperienceMapper;
177
+ exports.Experiment = Experiment;
178
+ exports.Variant = Variant;
179
+
180
+ Object.defineProperty(exports, '__esModule', { value: true });
181
+
182
+ }));
@@ -1,12 +1,12 @@
1
- import { ExperienceConfiguration } from '@ninetailed/experience.js';
2
- import { z } from 'zod';
3
- import { Experience, Experiment } from '../types';
4
- declare type ExperienceEntry = z.input<typeof Experience>;
5
- declare type ExperimentEntry = z.input<typeof Experiment>;
6
- export declare class ExperienceMapper {
7
- static isExperienceEntry(experience: ExperienceEntry): experience is Experience;
8
- static mapExperience(experience: ExperienceEntry): ExperienceConfiguration;
9
- static isExperimentEntry(experiment: ExperimentEntry): experiment is Experiment;
10
- static mapExperiment(experiment: ExperimentEntry): ExperienceConfiguration;
11
- }
12
- export {};
1
+ import { ExperienceConfiguration } from '@ninetailed/experience.js';
2
+ import { z } from 'zod';
3
+ import { Experience, Experiment } from '../types';
4
+ declare type ExperienceEntry = z.input<typeof Experience>;
5
+ declare type ExperimentEntry = z.input<typeof Experiment>;
6
+ export declare class ExperienceMapper {
7
+ static isExperienceEntry(experience: ExperienceEntry): experience is Experience;
8
+ static mapExperience(experience: ExperienceEntry): ExperienceConfiguration;
9
+ static isExperimentEntry(experiment: ExperimentEntry): experiment is Experiment;
10
+ static mapExperiment(experiment: ExperimentEntry): ExperienceConfiguration;
11
+ }
12
+ export {};
@@ -1 +1 @@
1
- export { ExperienceMapper } from './ExperienceMapper';
1
+ export { ExperienceMapper } from './ExperienceMapper';
package/package.json CHANGED
@@ -1,7 +1,22 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-utils",
3
- "version": "2.2.3",
3
+ "version": "2.2.5",
4
4
  "type": "commonjs",
5
- "main": "./src/index.js",
6
- "typings": "./src/index.d.ts"
5
+ "main": "./index.umd.js",
6
+ "module": "./index.esm.js",
7
+ "typings": "./index.d.ts",
8
+ "dependencies": {
9
+ "@ninetailed/experience.js": "2.2.5",
10
+ "analytics": "^0.8.0",
11
+ "@ninetailed/experience.js-shared": "2.2.5",
12
+ "uuid": "^8.3.2",
13
+ "ts-toolbelt": "^9.6.0",
14
+ "locale-enum": "^1.1.1",
15
+ "i18n-iso-countries": "^7.3.0",
16
+ "lodash": "^4.17.21",
17
+ "murmurhash-js": "^1.0.0",
18
+ "diary": "^0.3.1",
19
+ "zod": "^3.18.0"
20
+ },
21
+ "peerDependencies": {}
7
22
  }
@@ -1,9 +1,9 @@
1
- import { z } from 'zod';
2
- export declare const Audience: z.ZodObject<{
3
- id: z.ZodString;
4
- }, "strip", z.ZodTypeAny, {
5
- id: string;
6
- }, {
7
- id: string;
8
- }>;
9
- export declare type Audience = z.infer<typeof Audience>;
1
+ import { z } from 'zod';
2
+ export declare const Audience: z.ZodObject<{
3
+ id: z.ZodString;
4
+ }, "strip", z.ZodTypeAny, {
5
+ id: string;
6
+ }, {
7
+ id: string;
8
+ }>;
9
+ export declare type Audience = z.infer<typeof Audience>;
@@ -1,65 +1,65 @@
1
- import { z } from 'zod';
2
- export declare const Config: z.ZodObject<{
3
- distribution: z.ZodDefault<z.ZodArray<z.ZodNumber, "many">>;
4
- traffic: z.ZodDefault<z.ZodNumber>;
5
- components: z.ZodDefault<z.ZodArray<z.ZodObject<{
6
- baseline: z.ZodObject<{
7
- id: z.ZodDefault<z.ZodString>;
8
- }, "strip", z.ZodTypeAny, {
9
- id: string;
10
- }, {
11
- id?: string | undefined;
12
- }>;
13
- variants: z.ZodArray<z.ZodObject<{
14
- id: z.ZodDefault<z.ZodString>;
15
- hidden: z.ZodDefault<z.ZodBoolean>;
16
- }, "strip", z.ZodTypeAny, {
17
- id: string;
18
- hidden: boolean;
19
- }, {
20
- id?: string | undefined;
21
- hidden?: boolean | undefined;
22
- }>, "many">;
23
- }, "strip", z.ZodTypeAny, {
24
- baseline: {
25
- id: string;
26
- };
27
- variants: {
28
- id: string;
29
- hidden: boolean;
30
- }[];
31
- }, {
32
- baseline: {
33
- id?: string | undefined;
34
- };
35
- variants: {
36
- id?: string | undefined;
37
- hidden?: boolean | undefined;
38
- }[];
39
- }>, "many">>;
40
- }, "strip", z.ZodTypeAny, {
41
- distribution: number[];
42
- traffic: number;
43
- components: {
44
- baseline: {
45
- id: string;
46
- };
47
- variants: {
48
- id: string;
49
- hidden: boolean;
50
- }[];
51
- }[];
52
- }, {
53
- distribution?: number[] | undefined;
54
- traffic?: number | undefined;
55
- components?: {
56
- baseline: {
57
- id?: string | undefined;
58
- };
59
- variants: {
60
- id?: string | undefined;
61
- hidden?: boolean | undefined;
62
- }[];
63
- }[] | undefined;
64
- }>;
65
- export declare type Config = z.infer<typeof Config>;
1
+ import { z } from 'zod';
2
+ export declare const Config: z.ZodObject<{
3
+ distribution: z.ZodDefault<z.ZodArray<z.ZodNumber, "many">>;
4
+ traffic: z.ZodDefault<z.ZodNumber>;
5
+ components: z.ZodDefault<z.ZodArray<z.ZodObject<{
6
+ baseline: z.ZodObject<{
7
+ id: z.ZodDefault<z.ZodString>;
8
+ }, "strip", z.ZodTypeAny, {
9
+ id: string;
10
+ }, {
11
+ id?: string | undefined;
12
+ }>;
13
+ variants: z.ZodArray<z.ZodObject<{
14
+ id: z.ZodDefault<z.ZodString>;
15
+ hidden: z.ZodDefault<z.ZodBoolean>;
16
+ }, "strip", z.ZodTypeAny, {
17
+ id: string;
18
+ hidden: boolean;
19
+ }, {
20
+ id?: string | undefined;
21
+ hidden?: boolean | undefined;
22
+ }>, "many">;
23
+ }, "strip", z.ZodTypeAny, {
24
+ baseline: {
25
+ id: string;
26
+ };
27
+ variants: {
28
+ id: string;
29
+ hidden: boolean;
30
+ }[];
31
+ }, {
32
+ baseline: {
33
+ id?: string | undefined;
34
+ };
35
+ variants: {
36
+ id?: string | undefined;
37
+ hidden?: boolean | undefined;
38
+ }[];
39
+ }>, "many">>;
40
+ }, "strip", z.ZodTypeAny, {
41
+ distribution: number[];
42
+ traffic: number;
43
+ components: {
44
+ baseline: {
45
+ id: string;
46
+ };
47
+ variants: {
48
+ id: string;
49
+ hidden: boolean;
50
+ }[];
51
+ }[];
52
+ }, {
53
+ distribution?: number[] | undefined;
54
+ traffic?: number | undefined;
55
+ components?: {
56
+ baseline: {
57
+ id?: string | undefined;
58
+ };
59
+ variants: {
60
+ id?: string | undefined;
61
+ hidden?: boolean | undefined;
62
+ }[];
63
+ }[] | undefined;
64
+ }>;
65
+ export declare type Config = z.infer<typeof Config>;