@ninetailed/experience.js-utils 3.0.0-beta.14 → 3.0.0-beta.19

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.
@@ -43,48 +43,38 @@ const Variant = z.object({
43
43
  * const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
44
44
  * console.log(binaryArray) // ['0', '1', '0']
45
45
  */
46
-
47
46
  function zodArrayIgnoreUnknown(zodType) {
48
47
  const isKnownItem = item => zodType.safeParse(item).success;
49
-
50
48
  return z.preprocess(val => toSafeArray(val).filter(isKnownItem), z.array(zodType));
51
49
  }
52
-
53
50
  function toSafeArray(item) {
54
51
  if (isArray(item)) {
55
52
  return item;
56
53
  }
57
-
58
54
  return [item];
59
55
  }
60
-
61
56
  function isArray(item) {
62
57
  return Array.isArray(item);
63
58
  }
64
59
 
65
60
  const Experience = z.object({
66
61
  id: z.string(),
67
-
68
62
  /**
69
63
  * The name of the experience (Short Text)
70
64
  */
71
65
  name: z.string(),
72
-
73
66
  /**
74
67
  * The type if the experience (nt_experiment | nt_personalization)
75
68
  */
76
69
  type: z.union([z.literal('nt_experiment'), z.literal('nt_personalization')]),
77
-
78
70
  /**
79
71
  * The config of the experience (JSON)
80
72
  */
81
73
  config: Config.default({}),
82
-
83
74
  /**
84
75
  * The audience of the experience (Audience)
85
76
  */
86
77
  audience: Audience.optional().nullable(),
87
-
88
78
  /**
89
79
  * All used variants of the experience (References to other Content Types)
90
80
  */
@@ -99,15 +89,12 @@ class ExperienceMapper {
99
89
  static isExperienceEntry(experience) {
100
90
  return Experience.safeParse(experience).success;
101
91
  }
102
-
103
92
  static mapExperience(experience) {
104
93
  const parsedExperience = Experience.safeParse(experience);
105
-
106
94
  if (!parsedExperience.success) {
107
95
  logger.warn('[Ninetailed ExperienceMapper]', 'Error parsing experience', parsedExperience.error.format());
108
96
  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)}`);
109
97
  }
110
-
111
98
  const {
112
99
  id,
113
100
  type,
@@ -134,28 +121,22 @@ class ExperienceMapper {
134
121
  if (variant.hidden) {
135
122
  return variant;
136
123
  }
137
-
138
124
  const matchingVariant = variants.find(variantReference => variantReference.id === variant.id);
139
-
140
125
  if (!matchingVariant) {
141
126
  return null;
142
127
  }
143
-
144
128
  return matchingVariant;
145
129
  }).filter(variant => variant !== null)
146
130
  };
147
131
  })
148
132
  });
149
133
  }
150
-
151
134
  static isExperimentEntry(experiment) {
152
135
  return Experiment.safeParse(experiment).success;
153
136
  }
154
-
155
137
  static mapExperiment(experiment) {
156
138
  return ExperienceMapper.mapExperience(experiment);
157
139
  }
158
-
159
140
  }
160
141
 
161
142
  export { Audience, Config, Experience, ExperienceMapper, Experiment, Variant };
package/package.json CHANGED
@@ -1,20 +1,14 @@
1
1
  {
2
2
  "name": "@ninetailed/experience.js-utils",
3
- "version": "3.0.0-beta.14",
4
- "main": "./index.umd.js",
5
- "module": "./index.esm.js",
6
- "typings": "./index.d.ts",
3
+ "version": "3.0.0-beta.19",
4
+ "module": "./index.js",
5
+ "main": "./index.js",
6
+ "type": "module",
7
+ "types": "./index.d.ts",
7
8
  "dependencies": {
8
- "@ninetailed/experience.js": "3.0.0-beta.14",
9
- "@ninetailed/experience.js-shared": "3.0.0-beta.14",
10
- "ts-toolbelt": "^9.6.0",
11
- "diary": "^0.3.1",
12
- "zod": "^3.18.0",
13
- "locale-enum": "^1.1.1",
14
- "i18n-iso-countries": "^7.3.0",
15
- "analytics": "^0.8.0",
16
- "lodash": "^4.17.21",
17
- "murmurhash-js": "^1.0.0"
9
+ "@ninetailed/experience.js": "3.0.0-beta.19",
10
+ "@ninetailed/experience.js-shared": "3.0.0-beta.19",
11
+ "zod": "3.19.1"
18
12
  },
19
13
  "peerDependencies": {}
20
14
  }
@@ -96,9 +96,7 @@ export declare const Experience: z.ZodObject<{
96
96
  id: string;
97
97
  }>, "many">, {
98
98
  id: string;
99
- }[], {
100
- id: string;
101
- }[]>>;
99
+ }[], unknown>>;
102
100
  }, "strip", z.ZodTypeAny, {
103
101
  audience?: {
104
102
  id: string;
@@ -123,9 +121,7 @@ export declare const Experience: z.ZodObject<{
123
121
  }[];
124
122
  };
125
123
  }, {
126
- variants?: {
127
- id: string;
128
- }[] | undefined;
124
+ variants?: unknown;
129
125
  config?: {
130
126
  distribution?: number[] | undefined;
131
127
  traffic?: number | undefined;
@@ -81,9 +81,7 @@ export declare const Experiment: z.ZodObject<z.extendShape<{
81
81
  id: string;
82
82
  }>, "many">, {
83
83
  id: string;
84
- }[], {
85
- id: string;
86
- }[]>>;
84
+ }[], unknown>>;
87
85
  }, {
88
86
  type: z.ZodLiteral<"nt_experiment">;
89
87
  }>, "strip", z.ZodTypeAny, {
@@ -110,9 +108,7 @@ export declare const Experiment: z.ZodObject<z.extendShape<{
110
108
  }[];
111
109
  };
112
110
  }, {
113
- variants?: {
114
- id: string;
115
- }[] | undefined;
111
+ variants?: unknown;
116
112
  config?: {
117
113
  distribution?: number[] | undefined;
118
114
  traffic?: number | undefined;
@@ -11,4 +11,4 @@ import { z, ZodTypeAny } from 'zod';
11
11
  * const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
12
12
  * console.log(binaryArray) // ['0', '1', '0']
13
13
  */
14
- export declare function zodArrayIgnoreUnknown<T extends ZodTypeAny>(zodType: T): z.ZodEffects<z.ZodArray<T, "many">, T["_output"][], T["_input"][]>;
14
+ export declare function zodArrayIgnoreUnknown<T extends ZodTypeAny>(zodType: T): z.ZodEffects<z.ZodArray<T, "many">, T["_output"][], unknown>;
package/index.umd.js DELETED
@@ -1,217 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@ninetailed/experience.js-shared'), require('zod')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@ninetailed/experience.js-shared', 'zod'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.UtilsJavascript = {}, global.experience_jsShared, global.zod));
5
- })(this, (function (exports, experience_jsShared, 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
- /**
64
- * Zod helper for parsing arrays and ignore items not specified in the schema
65
- *
66
- * @param zodUnion - union of known types
67
- *
68
- * @example
69
- * const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')]))
70
- * type BinaryArray = z.TypeOf<typeof binaryArraySchema>
71
- *
72
- * const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0'])
73
- * console.log(binaryArray) // ['0', '1', '0']
74
- */
75
-
76
- function zodArrayIgnoreUnknown(zodType) {
77
- var isKnownItem = function (item) {
78
- return zodType.safeParse(item).success;
79
- };
80
-
81
- return zod.z.preprocess(function (val) {
82
- return toSafeArray(val).filter(isKnownItem);
83
- }, zod.z.array(zodType));
84
- }
85
-
86
- function toSafeArray(item) {
87
- if (isArray(item)) {
88
- return item;
89
- }
90
-
91
- return [item];
92
- }
93
-
94
- function isArray(item) {
95
- return Array.isArray(item);
96
- }
97
-
98
- var Experience = zod.z.object({
99
- id: zod.z.string(),
100
-
101
- /**
102
- * The name of the experience (Short Text)
103
- */
104
- name: zod.z.string(),
105
-
106
- /**
107
- * The type if the experience (nt_experiment | nt_personalization)
108
- */
109
- type: zod.z.union([zod.z.literal('nt_experiment'), zod.z.literal('nt_personalization')]),
110
-
111
- /**
112
- * The config of the experience (JSON)
113
- */
114
- config: Config.default({}),
115
-
116
- /**
117
- * The audience of the experience (Audience)
118
- */
119
- audience: Audience.optional().nullable(),
120
-
121
- /**
122
- * All used variants of the experience (References to other Content Types)
123
- */
124
- variants: zodArrayIgnoreUnknown(Variant).default([])
125
- });
126
-
127
- var Experiment = Experience.extend({
128
- type: zod.z.literal('nt_experiment')
129
- });
130
-
131
- var ExperienceMapper =
132
- /** @class */
133
- function () {
134
- function ExperienceMapper() {}
135
-
136
- ExperienceMapper.isExperienceEntry = function (experience) {
137
- return Experience.safeParse(experience).success;
138
- };
139
-
140
- ExperienceMapper.mapExperience = function (experience) {
141
- var parsedExperience = Experience.safeParse(experience);
142
-
143
- if (!parsedExperience.success) {
144
- experience_jsShared.logger.warn('[Ninetailed ExperienceMapper]', 'Error parsing experience', parsedExperience.error.format());
145
- 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)));
146
- }
147
-
148
- var _a = parsedExperience.data,
149
- id = _a.id,
150
- type = _a.type,
151
- audience = _a.audience,
152
- config = _a.config,
153
- variants = _a.variants;
154
- return __assign(__assign({
155
- id: id,
156
- type: type
157
- }, audience ? {
158
- audience: audience
159
- } : {}), {
160
- trafficAllocation: config.traffic,
161
- distribution: config.distribution.map(function (percentage, index) {
162
- return {
163
- index: index,
164
- start: config.distribution.slice(0, index).reduce(function (a, b) {
165
- return a + b;
166
- }, 0),
167
- end: config.distribution.slice(0, index + 1).reduce(function (a, b) {
168
- return a + b;
169
- }, 0)
170
- };
171
- }),
172
- components: config.components.map(function (component) {
173
- return {
174
- baseline: component.baseline,
175
- variants: component.variants.map(function (variant) {
176
- if (variant.hidden) {
177
- return variant;
178
- }
179
-
180
- var matchingVariant = variants.find(function (variantReference) {
181
- return variantReference.id === variant.id;
182
- });
183
-
184
- if (!matchingVariant) {
185
- return null;
186
- }
187
-
188
- return matchingVariant;
189
- }).filter(function (variant) {
190
- return variant !== null;
191
- })
192
- };
193
- })
194
- });
195
- };
196
-
197
- ExperienceMapper.isExperimentEntry = function (experiment) {
198
- return Experiment.safeParse(experiment).success;
199
- };
200
-
201
- ExperienceMapper.mapExperiment = function (experiment) {
202
- return ExperienceMapper.mapExperience(experiment);
203
- };
204
-
205
- return ExperienceMapper;
206
- }();
207
-
208
- exports.Audience = Audience;
209
- exports.Config = Config;
210
- exports.Experience = Experience;
211
- exports.ExperienceMapper = ExperienceMapper;
212
- exports.Experiment = Experiment;
213
- exports.Variant = Variant;
214
-
215
- Object.defineProperty(exports, '__esModule', { value: true });
216
-
217
- }));