@ninetailed/experience.js-utils 7.5.3 → 7.6.0-beta.2

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.cjs.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
@@ -30,7 +30,8 @@ const Config = zod.z.object({
30
30
  id: '',
31
31
  hidden: false
32
32
  }]
33
- }])
33
+ }]),
34
+ sticky: zod.z.boolean().optional().default(false)
34
35
  });
35
36
 
36
37
  const Variant = zod.z.object({
@@ -158,7 +159,8 @@ class ExperienceMapper {
158
159
  } = parsedExperience.data;
159
160
  const {
160
161
  components,
161
- traffic
162
+ traffic,
163
+ sticky
162
164
  } = config;
163
165
  return Object.assign(Object.assign(Object.assign({
164
166
  id,
@@ -175,6 +177,7 @@ class ExperienceMapper {
175
177
  start: config.distribution.slice(0, index).reduce((a, b) => a + b, 0),
176
178
  end: config.distribution.slice(0, index + 1).reduce((a, b) => a + b, 0)
177
179
  })),
180
+ sticky,
178
181
  components: components.map(component => ({
179
182
  baseline: component.baseline,
180
183
  variants: component.variants.map(variantRef => {
@@ -26,24 +26,25 @@ const Config = z.object({
26
26
  id: '',
27
27
  hidden: false
28
28
  }]
29
- }])
29
+ }]),
30
+ sticky: z.boolean().optional().default(false)
30
31
  });
31
32
 
32
33
  const Variant = z.object({
33
34
  id: z.string()
34
35
  }).catchall(z.unknown());
35
36
 
36
- /**
37
- * Zod helper for parsing arrays and ignore items not specified in the schema
38
- *
39
- * @param zodUnion - union of known types
40
- *
41
- * @example
42
- * const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')]))
43
- * type BinaryArray = z.TypeOf<typeof binaryArraySchema>
44
- *
45
- * const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
46
- * console.log(binaryArray) // ['0', '1', '0']
37
+ /**
38
+ * Zod helper for parsing arrays and ignore items not specified in the schema
39
+ *
40
+ * @param zodUnion - union of known types
41
+ *
42
+ * @example
43
+ * const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')]))
44
+ * type BinaryArray = z.TypeOf<typeof binaryArraySchema>
45
+ *
46
+ * const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
47
+ * console.log(binaryArray) // ['0', '1', '0']
47
48
  */
48
49
  function zodArrayIgnoreUnknown(zodType) {
49
50
  const isKnownItem = item => zodType.safeParse(item).success;
@@ -61,34 +62,34 @@ function isArray(item) {
61
62
 
62
63
  const ExperienceSchema = z.object({
63
64
  id: z.string(),
64
- /**
65
- * The name of the experience (Short Text)
65
+ /**
66
+ * The name of the experience (Short Text)
66
67
  */
67
68
  name: z.string(),
68
- /**
69
- * The description of the experience (Short Text)
69
+ /**
70
+ * The description of the experience (Short Text)
70
71
  */
71
72
  description: z.string().optional(),
72
- /**
73
- * The type if the experience (nt_experiment | nt_personalization)
73
+ /**
74
+ * The type if the experience (nt_experiment | nt_personalization)
74
75
  */
75
76
  type: z.union([z.string().regex(/^nt_experiment$/g), z.string().regex(/^nt_personalization$/g)]),
76
- /**
77
- * The config of the experience (JSON)
77
+ /**
78
+ * The config of the experience (JSON)
78
79
  */
79
80
  config: Config.default({}),
80
- /**
81
- * The audience of the experience (Audience)
81
+ /**
82
+ * The audience of the experience (Audience)
82
83
  */
83
84
  audience: Audience.optional().nullable(),
84
- /**
85
- * All used variants of the experience (References to other Content Types)
85
+ /**
86
+ * All used variants of the experience (References to other Content Types)
86
87
  */
87
88
  variants: zodArrayIgnoreUnknown(Variant).default([])
88
89
  });
89
90
  const parse$1 = input => {
90
91
  const output = ExperienceSchema.parse(input);
91
- return Object.assign(Object.assign({}, output), {
92
+ return Object.assign({}, output, {
92
93
  variants: input.variants
93
94
  });
94
95
  };
@@ -97,13 +98,13 @@ const safeParse$1 = input => {
97
98
  if (!output.success) {
98
99
  return output;
99
100
  }
100
- return Object.assign(Object.assign({}, output), {
101
- data: Object.assign(Object.assign({}, output.data), {
101
+ return Object.assign({}, output, {
102
+ data: Object.assign({}, output.data, {
102
103
  variants: input.variants
103
104
  })
104
105
  });
105
106
  };
106
- const Experience = Object.assign(Object.assign({}, ExperienceSchema), {
107
+ const Experience = Object.assign({}, ExperienceSchema, {
107
108
  parse: parse$1,
108
109
  safeParse: safeParse$1
109
110
  });
@@ -113,7 +114,7 @@ const ExperimentSchema = ExperienceSchema.extend({
113
114
  });
114
115
  const parse = input => {
115
116
  const output = ExperimentSchema.parse(input);
116
- return Object.assign(Object.assign({}, output), {
117
+ return Object.assign({}, output, {
117
118
  variants: input.variants
118
119
  });
119
120
  };
@@ -122,13 +123,13 @@ const safeParse = input => {
122
123
  if (!output.success) {
123
124
  return output;
124
125
  }
125
- return Object.assign(Object.assign({}, output), {
126
- data: Object.assign(Object.assign({}, output.data), {
126
+ return Object.assign({}, output, {
127
+ data: Object.assign({}, output.data, {
127
128
  variants: input.variants
128
129
  })
129
130
  });
130
131
  };
131
- const Experiment = Object.assign(Object.assign({}, ExperimentSchema), {
132
+ const Experiment = Object.assign({}, ExperimentSchema, {
132
133
  parse,
133
134
  safeParse
134
135
  });
@@ -154,23 +155,25 @@ class ExperienceMapper {
154
155
  } = parsedExperience.data;
155
156
  const {
156
157
  components,
157
- traffic
158
+ traffic,
159
+ sticky
158
160
  } = config;
159
- return Object.assign(Object.assign(Object.assign({
161
+ return Object.assign({
160
162
  id,
161
163
  type: type,
162
164
  name
163
165
  }, description ? {
164
166
  description
165
- } : {}), audience ? {
167
+ } : {}, audience ? {
166
168
  audience
167
- } : {}), {
169
+ } : {}, {
168
170
  trafficAllocation: traffic,
169
171
  distribution: config.distribution.map((percentage, index) => ({
170
172
  index,
171
173
  start: config.distribution.slice(0, index).reduce((a, b) => a + b, 0),
172
174
  end: config.distribution.slice(0, index + 1).reduce((a, b) => a + b, 0)
173
175
  })),
176
+ sticky,
174
177
  components: components.map(component => ({
175
178
  baseline: component.baseline,
176
179
  variants: component.variants.map(variantRef => {
@@ -178,7 +181,7 @@ class ExperienceMapper {
178
181
  return variantRef;
179
182
  }
180
183
  const matchingVariant = variants.find(variant => variant.id === variantRef.id);
181
- return matchingVariant !== null && matchingVariant !== void 0 ? matchingVariant : null;
184
+ return matchingVariant != null ? matchingVariant : null;
182
185
  }).filter(variant => variant !== null)
183
186
  }))
184
187
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-utils",
3
- "version": "7.5.3",
3
+ "version": "7.6.0-beta.2",
4
4
  "description": "Ninetailed Experience.js Utils",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -8,14 +8,11 @@
8
8
  "url": "https://github.com/ninetailed-inc/experience.js.git",
9
9
  "directory": "packages/utils/javascript"
10
10
  },
11
- "module": "./index.js",
12
- "main": "./index.cjs",
13
- "type": "module",
14
- "types": "./src/index.d.ts",
15
11
  "dependencies": {
16
- "@ninetailed/experience.js": "7.5.3",
17
- "@ninetailed/experience.js-shared": "7.5.3",
12
+ "@ninetailed/experience.js": "*",
13
+ "@ninetailed/experience.js-shared": "*",
18
14
  "zod": "3.21.4"
19
15
  },
20
- "peerDependencies": {}
16
+ "module": "./index.esm.js",
17
+ "main": "./index.cjs.js"
21
18
  }
@@ -38,6 +38,7 @@ export declare const Config: z.ZodObject<{
38
38
  hidden?: boolean | undefined;
39
39
  }[];
40
40
  }>, "many">>>;
41
+ sticky: z.ZodDefault<z.ZodOptional<z.ZodBoolean>>;
41
42
  }, "strip", z.ZodTypeAny, {
42
43
  distribution: number[];
43
44
  traffic: number;
@@ -50,6 +51,7 @@ export declare const Config: z.ZodObject<{
50
51
  hidden: boolean;
51
52
  }[];
52
53
  }[];
54
+ sticky: boolean;
53
55
  }, {
54
56
  distribution?: number[] | undefined;
55
57
  traffic?: number | undefined;
@@ -62,6 +64,7 @@ export declare const Config: z.ZodObject<{
62
64
  hidden?: boolean | undefined;
63
65
  }[];
64
66
  }[] | undefined;
67
+ sticky?: boolean | undefined;
65
68
  }>;
66
69
  export type ConfigLike = z.input<typeof Config>;
67
70
  export type Config = z.infer<typeof Config>;