@ninetailed/experience.js-utils 3.0.1-beta.4 → 3.0.2-beta.0

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,9 +1,12 @@
1
1
  import { z } from 'zod';
2
2
  export declare const Variant: z.ZodObject<{
3
3
  id: z.ZodString;
4
- }, "passthrough", z.ZodTypeAny, {
4
+ }, "strip", z.ZodUnknown, {
5
+ [x: string]: unknown;
5
6
  id: string;
6
7
  }, {
8
+ [x: string]: unknown;
7
9
  id: string;
8
10
  }>;
11
+ export declare type VariantLike = z.input<typeof Variant>;
9
12
  export declare type Variant = z.infer<typeof Variant>;
@@ -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
- }));
package/lib/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { ExperienceMapper } from './ExperienceMapper';
package/types/index.d.ts DELETED
@@ -1,5 +0,0 @@
1
- export { Experience } from './Experience';
2
- export { Experiment } from './Experiment';
3
- export { Audience } from './Audience';
4
- export { Variant } from './Variant';
5
- export { Config } from './Config';