@ninetailed/experience.js-utils
Advanced tools
Comparing version 2.2.9-beta.0 to 3.0.1-beta.3
@@ -1,2 +0,2 @@ | ||
import { logger } from '@ninetailed/experience.js'; | ||
import { logger } from '@ninetailed/experience.js-shared'; | ||
import { z } from 'zod'; | ||
@@ -34,2 +34,33 @@ | ||
/** | ||
* Zod helper for parsing arrays and ignore items not specified in the schema | ||
* | ||
* @param zodUnion - union of known types | ||
* | ||
* @example | ||
* const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')])) | ||
* type BinaryArray = z.TypeOf<typeof binaryArraySchema> | ||
* | ||
* const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0']) | ||
* console.log(binaryArray) // ['0', '1', '0'] | ||
*/ | ||
function zodArrayIgnoreUnknown(zodType) { | ||
const isKnownItem = item => zodType.safeParse(item).success; | ||
return z.preprocess(val => toSafeArray(val).filter(isKnownItem), z.array(zodType)); | ||
} | ||
function toSafeArray(item) { | ||
if (isArray(item)) { | ||
return item; | ||
} | ||
return [item]; | ||
} | ||
function isArray(item) { | ||
return Array.isArray(item); | ||
} | ||
const Experience = z.object({ | ||
@@ -61,3 +92,3 @@ id: z.string(), | ||
*/ | ||
variants: z.array(Variant).default([]) | ||
variants: zodArrayIgnoreUnknown(Variant).default([]) | ||
}); | ||
@@ -64,0 +95,0 @@ |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@ninetailed/experience.js'), require('zod')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@ninetailed/experience.js', 'zod'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.UtilsJavascript = {}, global.experience_js, global.zod)); | ||
})(this, (function (exports, experience_js, zod) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@ninetailed/experience.js-shared'), require('zod')) : | ||
typeof define === 'function' && define.amd ? define(['exports', '@ninetailed/experience.js-shared', 'zod'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.UtilsJavascript = {}, global.experience_jsShared, global.zod)); | ||
})(this, (function (exports, experience_jsShared, zod) { 'use strict'; | ||
@@ -63,2 +63,37 @@ /*! ***************************************************************************** | ||
/** | ||
* Zod helper for parsing arrays and ignore items not specified in the schema | ||
* | ||
* @param zodUnion - union of known types | ||
* | ||
* @example | ||
* const binaryArraySchema = arrayIgnoreUnknown(z.union([z.literal('0'), z.literal('1')])) | ||
* type BinaryArray = z.TypeOf<typeof binaryArraySchema> | ||
* | ||
* const binaryArray: BinaryArray = binaryArraySchema.parse(['0', '1', '2', '0']) | ||
* console.log(binaryArray) // ['0', '1', '0'] | ||
*/ | ||
function zodArrayIgnoreUnknown(zodType) { | ||
var isKnownItem = function (item) { | ||
return zodType.safeParse(item).success; | ||
}; | ||
return zod.z.preprocess(function (val) { | ||
return toSafeArray(val).filter(isKnownItem); | ||
}, zod.z.array(zodType)); | ||
} | ||
function toSafeArray(item) { | ||
if (isArray(item)) { | ||
return item; | ||
} | ||
return [item]; | ||
} | ||
function isArray(item) { | ||
return Array.isArray(item); | ||
} | ||
var Experience = zod.z.object({ | ||
@@ -90,3 +125,3 @@ id: zod.z.string(), | ||
*/ | ||
variants: zod.z.array(Variant).default([]) | ||
variants: zodArrayIgnoreUnknown(Variant).default([]) | ||
}); | ||
@@ -111,3 +146,3 @@ | ||
if (!parsedExperience.success) { | ||
experience_js.logger.warn('[Ninetailed ExperienceMapper]', 'Error parsing experience', parsedExperience.error.format()); | ||
experience_jsShared.logger.warn('[Ninetailed ExperienceMapper]', 'Error parsing experience', parsedExperience.error.format()); | ||
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))); | ||
@@ -114,0 +149,0 @@ } |
{ | ||
"name": "@ninetailed/experience.js-utils", | ||
"version": "2.2.9-beta.0", | ||
"version": "3.0.1-beta.3", | ||
"main": "./index.umd.js", | ||
@@ -8,15 +8,14 @@ "module": "./index.esm.js", | ||
"dependencies": { | ||
"@ninetailed/experience.js": "2.2.9-beta.0", | ||
"analytics": "^0.8.0", | ||
"@ninetailed/experience.js-shared": "2.2.9-beta.0", | ||
"uuid": "^8.3.2", | ||
"@ninetailed/experience.js": "3.0.1-beta.3", | ||
"@ninetailed/experience.js-shared": "3.0.1-beta.3", | ||
"ts-toolbelt": "^9.6.0", | ||
"diary": "^0.3.1", | ||
"zod": "^3.18.0", | ||
"locale-enum": "^1.1.1", | ||
"i18n-iso-countries": "^7.3.0", | ||
"analytics": "^0.8.0", | ||
"lodash": "^4.17.21", | ||
"murmurhash-js": "^1.0.0", | ||
"diary": "^0.3.1", | ||
"zod": "^3.18.0" | ||
"murmurhash-js": "^1.0.0" | ||
}, | ||
"peerDependencies": {} | ||
} |
@@ -25,5 +25,3 @@ import { z } from 'zod'; | ||
id?: string | undefined; | ||
}>; /** | ||
* The type if the experience (nt_experiment | nt_personalization) | ||
*/ | ||
}>; | ||
variants: z.ZodArray<z.ZodObject<{ | ||
@@ -38,5 +36,3 @@ id: z.ZodDefault<z.ZodString>; | ||
hidden?: boolean | undefined; | ||
}>, "many">; /** | ||
* The config of the experience (JSON) | ||
*/ | ||
}>, "many">; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -97,3 +93,3 @@ baseline: { | ||
*/ | ||
variants: z.ZodDefault<z.ZodArray<z.ZodObject<{ | ||
variants: z.ZodDefault<z.ZodEffects<z.ZodArray<z.ZodObject<{ | ||
id: z.ZodString; | ||
@@ -104,3 +100,7 @@ }, "passthrough", z.ZodTypeAny, { | ||
id: string; | ||
}>, "many">>; | ||
}>, "many">, { | ||
id: string; | ||
}[], { | ||
id: string; | ||
}[]>>; | ||
}, "strip", z.ZodTypeAny, { | ||
@@ -107,0 +107,0 @@ audience?: { |
@@ -76,3 +76,3 @@ import { z } from 'zod'; | ||
}>>>; | ||
variants: z.ZodDefault<z.ZodArray<z.ZodObject<{ | ||
variants: z.ZodDefault<z.ZodEffects<z.ZodArray<z.ZodObject<{ | ||
id: z.ZodString; | ||
@@ -83,3 +83,7 @@ }, "passthrough", z.ZodTypeAny, { | ||
id: string; | ||
}>, "many">>; | ||
}>, "many">, { | ||
id: string; | ||
}[], { | ||
id: string; | ||
}[]>>; | ||
}, { | ||
@@ -86,0 +90,0 @@ type: z.ZodLiteral<"nt_experiment">; |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
24150
10
14
719
1
+ Added@ninetailed/experience.js@3.0.1-beta.3(transitive)
+ Added@ninetailed/experience.js-shared@3.0.1-beta.3(transitive)
- Removeduuid@^8.3.2
- Removed@ninetailed/experience.js@2.2.9-beta.0(transitive)
- Removed@ninetailed/experience.js-shared@2.2.9-beta.0(transitive)
- Removeduuid@8.3.2(transitive)