UNPKG

vicis

Version:

Presentation and transformation layer for data output in RESTful APIs.

3 lines (2 loc) 26 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).vicis={})}(this,(function(t){"use strict";var e;!function(t){t.BOOLEAN="boolean",t.FLAG="flag",t.INTEGER="integer",t.JSON="json",t.NUMERIC="numeric",t.STRING="string"}(e||(e={}));const r={BOOLEAN:e.BOOLEAN,FLAG:e.FLAG,INTEGER:e.INTEGER,JSON:e.JSON,NUMERIC:e.NUMERIC,STRING:e.STRING};function objectIsEmpty(t){return 0===Object.keys(t).length}function checkIsObjectLike(t){return t&&"object"==typeof t&&!Array.isArray(t)}function isString(t){return"string"==typeof t}function jsonStringify(t){return JSON.stringify(t)}function castConfig(t){if(!checkIsObjectLike(t))throw new TypeError("Cast should be an object");return objectIsEmpty(t)?{}:(Object.keys(t).forEach(r=>{if(!isString(t[r]))throw new TypeError(`'Cast' expect object values to be strings. Not a string at key: '${jsonStringify(t[r])}'.`);if(!Object.values(e).includes(t[r]))throw new TypeError(`'Cast' has unknown type in {${r}: "${t[r]}"}.`)}),t)}function castToString$1(t){if(null==t)return"";if("string"==typeof t)return t;const e=t.toString();return"0"===e&&Object.is(t,-0)?"-0":e}function jsonParse(t){return JSON.parse(t)}function isFunction(t){return"[object Function]"===Object.prototype.toString.call(t)&&!/^class\s/.test(Function.prototype.toString.call(t))}function objectToPlain(t){return function objectDeserialize(t){return isString(t)?jsonParse(t):t}(function objectSerialize(t){let e;const{toJSON:r,toObject:a}=t;return e=isFunction(a)?t.toObject():isFunction(r)?t.toJSON():t,isString(e)?e:jsonStringify(e)}(t))}function castData(t,r){return objectIsEmpty(t)||Object.keys(t).forEach(a=>{const i=t[a];if(!(a in r))throw new Error(`Field '${a}' suppose to be converted to ${i}.`);switch(i){case e.BOOLEAN:r[a]=Boolean(r[a]);break;case e.FLAG:r[a]=function convertToFlag(t,e=!1,r=!1){if(null==t)return e;if("boolean"==typeof t)return t;const a=t.toString().toLocaleLowerCase().trim();return 0===a.length?e:"true"===a||"1"===a||"false"!==a&&"0"!==a&&r}(r[a]);break;case e.NUMERIC:{const t=Number(r[a]);if(Number.isFinite(t))r[a]=t;else{const t=Number.parseFloat(castToString$1(r[a]).trim());Number.isFinite(t)?r[a]=t:r[a]=0}break}case e.INTEGER:{const t=Number(r[a]);if(Number.isFinite(t))r[a]=Math.trunc(t);else{const e=Number.parseFloat(castToString$1(r[a]).trim());Number.isFinite(e)?r[a]=Math.trunc(t):r[a]=0}break}case e.STRING:r[a]=castToString$1(r[a]);break;case e.JSON:r[a]=objectToPlain(r[a]);break;default:throw new Error("Unknown value convert error")}}),r}function defaultsConfig(t){if(!checkIsObjectLike(t))throw new TypeError("'Defaults' should be an object");return t}function defaultsData(t,e){return objectIsEmpty(t)||Object.keys(t).forEach(r=>{r in e&&void 0!==e[r]||(e[r]=t[r])}),e}function arrayIsEmpty(t){return 0===t.length}function arrayGetUnique(t,e=!0){if(t.length<2)return t;let r=[...new Set(t)];if(r.includes(0)){const e=t.filter(t=>0===t);e.length>1&&e.some(t=>1/t===Number.NEGATIVE_INFINITY)&&r.push(-0)}if(r.filter(t=>"string"==typeof t).length){const e=t.filter(t=>"string"==typeof t);if(e.length>1){const t=[...new Set(e.map(t=>t.normalize()))];t.forEach(t=>{delete r[r.indexOf(t)]});const a=[];for(let t=0;t<r.length;t++)t in r&&a.push(r[t]);r=a.concat(t)}}return e?r.sort():r}function definedConfig(t){if(!Array.isArray(t))throw new TypeError("'Defined' should be an array");return arrayIsEmpty(t)?[]:arrayGetUnique(t).map(t=>{if(!isString(t))throw new TypeError(`'Defined' expect array of strings. Value: '${jsonStringify(t)}'.`);return t})}function definedData(t,e){const r=definedConfig(t);return arrayIsEmpty(r)||r.forEach(t=>{if(!(t in e))throw new Error(`Field '${t}' must be defined.`);if(void 0===e[t])throw new Error(`Field '${t}' should have value.`)}),e}function isRegExp(t){return t instanceof RegExp}function objectGetKeys(t){return Object.keys(t).sort((t,e)=>t.localeCompare(e))}function excludeConfig(t){if(!Array.isArray(t))throw new TypeError("'Exclude' should be an array");return arrayIsEmpty(t)?[]:arrayGetUnique(t).map(t=>{if(!isString(t)&&!isRegExp(t))throw new TypeError(`'Exclude' expect array of strings or regular expressions. Value: '${jsonStringify(t)}'.`);return t})}function nullishConfig(t){if(!checkIsObjectLike(t))throw new TypeError("'Defaults' should be an object");return t}function nullishData(t,e){return objectIsEmpty(t)||Object.keys(t).forEach(r=>{r in e&&void 0!==e[r]&&null!==e[r]||(e[r]=t[r])}),e}function omitConfig(t){if(!Array.isArray(t))throw new TypeError("'Omit' should be an array");return arrayIsEmpty(t)?[]:arrayGetUnique(t).map(t=>{if(!isString(t))throw new TypeError(`'Omit' expect array of strings. Value: '${jsonStringify(t)}'.`);return t})}var a;function objectKeysOrder(t,e=[],r=!1){if(!Array.isArray(e)||0===e.length)return t;const a=e.filter(t=>"string"==typeof t);let i=new Set(Object.keys(t));const n={};return a.forEach(e=>{i.delete(e),e in t&&(n[e]=t[e])}),i=[...i],r&&(i=i.sort((t,e)=>t.localeCompare(e))),i.forEach(e=>n[e]=t[e]),n}function orderConfig(t){if(!Array.isArray(t))throw new TypeError("'Order' should be an array");return arrayIsEmpty(t)?[]:arrayGetUnique(t,!1).map(t=>{if(!isString(t))throw new TypeError(`'Order' expect array of strings. Value: '${jsonStringify(t)}'.`);return t})}function sortAsBoolean(t=a.Default){return"boolean"==typeof t?t:a.Yes===t}function pickConfig(t){if(!Array.isArray(t))throw new TypeError("'Pick' should be an array");return arrayIsEmpty(t)?[]:arrayGetUnique(t).map(t=>{if(!isString(t))throw new TypeError(`'Pick' expect array of strings. Value: '${jsonStringify(t)}'.`);return t})}function pickData(t,e){if(arrayIsEmpty(t))return e;const r={};return Object.keys(e).forEach(a=>{t.includes(a)&&(r[a]=e[a])}),r}function renameConfig(t){if(!checkIsObjectLike(t))throw new TypeError("'Rename' should be an object");if(objectIsEmpty(t))return{};Object.keys(t).forEach(t=>{if(!isString(t))throw new TypeError(`'Rename' expect object values to be strings. Not a string at key: '${t}'.`)});const e=Object.values(t),r=arrayGetUnique(e);if(e.length!==r.length)throw new TypeError(`'Rename' has similar values: '${jsonStringify(r)}'.`);return t}function clone$1(t){switch(typeOf(t)){case"arraybuffer":return function cloneArrayBuffer(t){const e=new t.constructor(t.byteLength);return new Uint8Array(e).set(new Uint8Array(t)),e}(t);case"array":return t.slice();case"object":return Object.assign({},t);case"date":return new t.constructor(Number(t));case"map":return new Map(t);case"set":return new Set(t);case"buffer":return function cloneBuffer(t){const e=t.length;let r;r=Buffer.allocUnsafe?Buffer.allocUnsafe(e):Buffer.from(e);return t.copy(r),r}(t);case"symbol":return function cloneSymbol(t){if(Symbol.prototype.valueOf)return Object(Symbol.prototype.valueOf.call(t));return{}}(t);case"float32array":case"float64array":case"int16array":case"int32array":case"int8array":case"uint16array":case"uint32array":case"uint8clampedarray":case"uint8array":return function cloneTypedArray(t){return new t.constructor(t.buffer,t.byteOffset,t.length)}(t);case"regexp":return function cloneRegExp(t){let e;e=void 0!==t.flags?t.flags:/\w+$/.exec(t)||void 0;const r=new t.constructor(t.source,e);return r.lastIndex=t.lastIndex,r}(t);case"error":return Object.create(t);default:return t}}function typeOf(t){if(void 0===t)return"undefined";if(null===t)return"null";if(!0===t||!1===t||t instanceof Boolean)return"boolean";if("string"==typeof t||t instanceof String)return"string";if("number"==typeof t||t instanceof Number)return"number";if("function"==typeof t||t instanceof Function)return"function";if(void 0!==Array.isArray&&Array.isArray(t))return"array";if(t instanceof RegExp)return"regexp";if(t instanceof Date)return"date";const e=Object.prototype.toString.call(t);return"[object RegExp]"===e?"regexp":"[object Date]"===e?"date":"[object Arguments]"===e?"arguments":"[object Error]"===e?"error":function isBuffer(t){return null!==t&&Boolean(t.constructor)&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}(t)?"buffer":"[object Set]"===e?"set":"[object WeakSet]"===e?"weakset":"[object Map]"===e?"map":"[object WeakMap]"===e?"weakmap":"[object Symbol]"===e?"symbol":"[object Int8Array]"===e?"int8array":"[object Uint8Array]"===e?"uint8array":"[object Uint8ClampedArray]"===e?"uint8clampedarray":"[object Int16Array]"===e?"int16array":"[object Uint16Array]"===e?"uint16array":"[object Int32Array]"===e?"int32array":"[object Uint32Array]"===e?"uint32array":"[object Float32Array]"===e?"float32array":"[object Float64Array]"===e?"float64array":"[object ArrayBuffer]"===e?"arraybuffer":"object"}function isObjectObject(t){return function isObject(t){return"object"==typeof t&&null!==t}(t)&&"[object Object]"===Object.prototype.toString.call(t)}function cloneObjectDeep(t,e){if("function"==typeof e)return e(t);if(e||function isPlainObject(t){if(!isObjectObject(t))return!1;const e=t.constructor;if("function"!=typeof e)return!1;const r=e.prototype;return!!isObjectObject(r)&&r.hasOwnProperty("isPrototypeOf")}(t)){const r=new t.constructor;for(const a in t)r[a]=cloneDeep(t[a],e);return r}return t}function cloneDeep(t,e){switch(typeOf(t)){case"object":return cloneObjectDeep(t,e);case"array":return function cloneArrayDeep(t,e){const r=new t.constructor(t.length);for(let a=0;a<t.length;a++)r[a]=cloneDeep(t[a],e);return r}(t,e);default:return clone$1(t)}}function renameData(t,e){if(objectIsEmpty(t))return e;const r=Object.keys(t).sort((t,e)=>t.localeCompare(e)),a={},i=cloneDeep(e);return r.forEach(e=>{if(!(e in i))throw new Error(`Field '${e}' suppose to be renamed.`);a[t[e]]=i[e]}),r.forEach(t=>{delete i[t]}),Object.assign(i,a),i}function replaceConfig(t){if(!checkIsObjectLike(t))throw new TypeError("'Replace' should be an object");return t}function replaceData(t,e){return objectIsEmpty(t)||Object.keys(t).forEach(r=>{e[r]=t[r]}),e}function requiredConfig(t=[]){if(!Array.isArray(t))throw new TypeError("'Required' should be an array");return arrayIsEmpty(t)?[]:arrayGetUnique(t).map(t=>{if(!isString(t))throw new TypeError(`'Required' expect array of strings. Value: '${jsonStringify(t)}'.`);return t})}function requiredData(t,e){return arrayIsEmpty(t)||t.forEach(t=>{if(!(t in e))throw new Error(`Field '${t}' is required.`)}),e}function transformConfig(t){if(!checkIsObjectLike(t))throw new TypeError("'Transform' should be an object");return objectIsEmpty(t)?{}:(Object.keys(t).forEach(e=>{if(!function checkIsCallable(t){return!!t&&(!!isFunction(t)||!!("toFunction"in t&&isFunction(t.toFunction)&&isFunction(t.toFunction())))}(t[e]))throw new TypeError(`'Transform' expect object values to be functions. Not a function at key: '${e}'.`)}),t)}function transformData(t,e){return objectIsEmpty(t)||Object.keys(t).forEach(r=>{if(!(r in e))throw new Error(`Field '${r}' suppose to be transformed.`);isFunction(t[r])?e[r]=t[r](e[r],r,e):e[r]=t[r].toFunction()(e[r],r,e)}),e}!function(t){t.Default="asc",t.No="no",t.Yes="asc"}(a||(a={}));const i=["cast","defaults","defined","exclude","nullish","omit","order","pick","rename","replace","required","sort","transform"];class AggregateError extends Error{constructor(t,e=""){super(e),this.name="AggregateError",this.errors=[],this.errors=t,this.message=e}}class ValidationError extends Error{constructor(t){super(t),this.name="ValidationError"}}function arrayBasicIntersect(t,e){if(!t.length||!e.length)return[];const r=new Set(e);return[...new Set(t)].filter(t=>r.has(t))}function arrayGetDifference(t,e){const r=new Set(e);return t.filter(t=>!r.has(t))}function arrayHasSame(t,e){if(!t.length||!e.length)return!1;const r=new Set(e);return Boolean([...new Set(t)].filter(t=>r.has(t)).length)}function castToJson(t,e=a.Default){return sortAsBoolean(e)?function objectKeysSort(t,e=!0){if(!t||"object"!=typeof t||Array.isArray(t))return t;const r=Object.keys(t);return r.length?r.reduce((r,a)=>(e&&t[a]&&"object"==typeof t[a]&&!Array.isArray(t[a])?r[a]=objectKeysSort(t[a],e):r[a]=t[a],r),Object.create(Object.getPrototypeOf(t))):t}(jsonParse(jsonStringify(t)),!0):jsonParse(jsonStringify(t))}function clone(t){return function checkIsPrimitive(t){return Object(t)!==t}(t)?t:cloneDeep(t)}function objectGetProperty(t,e,r){return e in t?t[e]:r}function objectCreateEmpty(){return new Object(null)}class VicisParameter{constructor(){this.__hasDefaults=!1,this.__hasNullish=!1,this.__hasValue=!1,this.__value=void 0}get boolean(){return this.__cast=e.BOOLEAN,this}get flag(){return this.__cast=e.FLAG,this}get integer(){return this.__cast=e.INTEGER,this}get numeric(){return this.__cast=e.NUMERIC,this}get string(){return this.__cast=e.STRING,this}get json(){return this.__cast=e.JSON,this}get defined(){return this.__defined=!0,this}get required(){return this.__required=!0,this}replace(t){return this.__value=t,this.__hasValue=!0,this}defaults(t){return this.__defaults=t,this.__hasDefaults=!0,this}nullish(t){return this.__nullish=t,this.__hasNullish=!0,this}transform(t){return this.__transform=t,this}toObject(){return{cast:this.__cast,defaults:this.__defaults,defined:this.__defined,hasDefaults:this.__hasDefaults,hasNullish:this.__hasNullish,hasValue:this.__hasValue,nullish:this.__nullish,required:this.__required,transform:this.__transform,value:this.__value}}}function convertFunctionToConfig(t){if(!isFunction(t))throw new TypeError("Callable must be a function");const e=t(new Proxy(new Object(null),{get:function(t,e){return e in t||(t[e]=new VicisParameter),t[e]}})),r=function createConfig(){return{cast:objectCreateEmpty(),defaults:objectCreateEmpty(),defined:[],exclude:[],nullish:objectCreateEmpty(),omit:[],order:[],pick:[],sort:a.Default,rename:objectCreateEmpty(),replace:objectCreateEmpty(),required:[],transform:objectCreateEmpty()}}();return Object.keys(e).forEach(t=>{const a=t;r.pick.push(a);const i={};if(e[a]instanceof VicisParameter)i[a]=e[a].toObject();else{const t=new VicisParameter;t.replace(e[a]),i[a]=t.toObject()}delete e[a],i[a].cast&&(r.cast[a]=i[a].cast),i[a].defined&&r.defined.push(a),i[a].required&&r.required.push(a),i[a].transform&&(r.transform[a]=i[a].transform),i[a].hasDefaults&&(r.defaults[a]=i[a].defaults),i[a].hasNullish&&(r.nullish[a]=i[a].nullish),i[a].hasValue&&(r.replace[a]=i[a].value)}),Object.keys(r.cast).length||delete r.cast,Object.keys(r.defaults).length||delete r.defaults,Object.keys(r.nullish).length||delete r.nullish,r.defined.length||delete r.defined,r.pick.length||delete r.pick,r.required.length||delete r.required,Object.keys(r.replace).length||delete r.replace,Object.keys(r.transform).length||delete r.transform,r}class Vicis{constructor(t={},e){this.__cast=objectCreateEmpty(),this.__defaults=objectCreateEmpty(),this.__nullish=objectCreateEmpty(),this.__defined=[],this.__exclude=[],this.__omit=[],this.__order=[],this.__pick=[],this.__rename=objectCreateEmpty(),this.__replace=objectCreateEmpty(),this.__required=[],this.__sort=a.Default,this.__transform=objectCreateEmpty(),this.__dataCache=objectCreateEmpty(),this.__dataOriginal=void 0,this.config(t),void 0!==e&&this.data(e)}validateConfig(){const t=objectGetKeys(this.__cast),e=objectGetKeys(this.__rename),r=objectGetKeys(this.__replace),a=objectGetKeys(this.__transform);if(arrayHasSame(this.__omit,t))throw new ValidationError(`'omit' has same keys as 'cast': ${arrayBasicIntersect(this.__omit,t)}.`);if(arrayHasSame(this.__omit,this.__defined))throw new ValidationError(`'omit' has same keys as 'defined': ${arrayBasicIntersect(this.__omit,this.__defined)}.`);if(arrayHasSame(this.__omit,this.__pick))throw new ValidationError(`'omit' has same keys as 'pick': ${arrayBasicIntersect(this.__omit,this.__pick)}.`);if(arrayHasSame(this.__omit,e))throw new ValidationError(`'omit' has same keys as 'rename': ${arrayBasicIntersect(this.__omit,e)}.`);if(arrayHasSame(this.__omit,r))throw new ValidationError(`'omit' has same keys as 'replace': ${arrayBasicIntersect(this.__omit,r)}.`);if(arrayHasSame(this.__omit,this.__required))throw new ValidationError(`'omit' has same keys as 'required': ${arrayBasicIntersect(this.__omit,this.__required)}.`);if(arrayHasSame(this.__omit,a))throw new ValidationError(`'omit' has same keys as 'transform': ${arrayBasicIntersect(this.__omit,a)}.`);if(arrayHasSame(t,r))throw new ValidationError(`'cast' has same keys as 'replace': ${arrayBasicIntersect(t,r)}.`);if(arrayHasSame(t,a))throw new ValidationError(`'cast' has same keys as 'transform': ${arrayBasicIntersect(t,a)}.`);if(arrayHasSame(r,a))throw new ValidationError(`'replace' has same keys as 'transform': ${arrayBasicIntersect(r,a)}.`);return this}validateData(){return void 0===this.__dataOriginal||("toObject"in this.__dataOriginal&&isFunction(this.__dataOriginal.toObject)?this.__dataCache=this.__dataOriginal.toObject():"toJSON"in this.__dataOriginal&&isFunction(this.__dataOriginal.toJSON)?this.__dataCache=this.__dataOriginal.toJSON():this.__dataCache=this.__dataOriginal,this.__dataCache=function omitData(t,e){if(arrayIsEmpty(t))return e;const r={};return Object.keys(e).forEach(a=>{t.includes(a)||(r[a]=e[a])}),r}(this.__omit,this.__dataCache),this.__dataCache=defaultsData(this.__defaults,this.__dataCache),this.__dataCache=nullishData(this.__nullish,this.__dataCache),this.__dataCache=requiredData(this.__required,this.__dataCache),this.__dataCache=definedData(this.__defined,this.__dataCache),this.__dataCache=castData(this.__cast,this.__dataCache),this.__dataCache=transformData(this.__transform,this.__dataCache),this.__dataCache=replaceData(this.__replace,this.__dataCache),this.__dataCache=renameData(this.__rename,this.__dataCache),this.__dataCache=pickData(this.__pick,this.__dataCache),this.__dataCache=function excludeData(t,e){if(arrayIsEmpty(t))return e;const r=t.filter(isString);if(r.length&&Object.keys(e).forEach(t=>{r.includes(t)&&delete e[t]}),0===objectGetKeys(e).length)return e;const a=t.filter(isRegExp);return a.length&&a.forEach(t=>{Object.keys(e).forEach(r=>{t.test(r)&&delete e[r]})}),e}(this.__exclude,this.__dataCache),this.__dataCache=castToJson(this.__dataCache,this.__sort),this.__dataCache=function orderData(t,e,r=a.Default){if(arrayIsEmpty(t)){if(sortAsBoolean(r)){return objectKeysOrder(e,Object.keys(e).sort((t,e)=>t.localeCompare(e)),!0)}return e}return objectKeysOrder(e,t,sortAsBoolean(r))}(this.__order,this.__dataCache,this.__sort)),this}static factory(t,e){return new Vicis(t,e)}static from(t,e){return Vicis.factory(e,t).getData()}static fromArray(t,e){const r=Vicis.factory(e);return Array.from(t).map(t=>r.data(t).getData())}static get BOOLEAN(){return e.BOOLEAN}static get FLAG(){return e.FLAG}static get NUMERIC(){return e.NUMERIC}static get INTEGER(){return e.INTEGER}static get STRING(){return e.STRING}static get JSON(){return e.JSON}getConfig(){return clone({cast:this.__cast,defaults:this.__defaults,defined:this.__defined,exclude:this.__exclude,nullish:this.__nullish,omit:this.__omit,order:this.__order,pick:this.__pick,sort:this.__sort,rename:this.__rename,replace:this.__replace,required:this.__required,transform:this.__transform})}resetConfig(){return this.__cast={},this.__defaults={},this.__defined=[],this.__exclude=[],this.__omit=[],this.__order=[],this.__pick=[],this.__sort=a.Default,this.__rename={},this.__replace={},this.__required=[],this.__transform={},this}static testConfig(t){let e;if(e=isFunction(t)?convertFunctionToConfig(t):t,!checkIsObjectLike(e))throw new AggregateError([new TypeError("Config should be an object")],"Configuration has errors");const r=arrayGetDifference(objectGetKeys(e),i);if(r.length)throw new AggregateError([new TypeError(`Config has unknown fields: '${r.join("', '")}'.`)],"Configuration has errors");const a=objectGetKeys(objectGetProperty(e,"cast",{})),n=objectGetKeys(objectGetProperty(e,"rename",{})),s=objectGetKeys(objectGetProperty(e,"replace",{})),o=objectGetKeys(objectGetProperty(e,"transform",{})),c=[];if("omit"in e&&arrayHasSame(e.omit,a)&&c.push(new ValidationError(`'omit' has same keys as 'cast': ${arrayBasicIntersect(e.omit,a)}.`)),"omit"in e&&"defined"in e&&arrayHasSame(e.omit,e.defined)&&c.push(new ValidationError(`'omit' has same keys as 'defined': ${arrayBasicIntersect(e.omit,e.defined)}.`)),"omit"in e&&"pick"in e&&arrayHasSame(e.omit,e.pick)&&c.push(new ValidationError(`'omit' has same keys as 'pick': ${arrayBasicIntersect(e.omit,e.pick)}.`)),"omit"in e&&arrayHasSame(e.omit,n)&&c.push(new ValidationError(`'omit' has same keys as 'rename': ${arrayBasicIntersect(e.omit,n)}.`)),"omit"in e&&arrayHasSame(e.omit,s)&&c.push(new ValidationError(`'omit' has same keys as 'replace': ${arrayBasicIntersect(e.omit,s)}.`)),"omit"in e&&"required"in e&&arrayHasSame(e.omit,e.required)&&c.push(new ValidationError(`'omit' has same keys as 'required': ${arrayBasicIntersect(e.omit,e.required)}.`)),"omit"in e&&arrayHasSame(e.omit,o)&&c.push(new ValidationError(`'omit' has same keys as 'transform': ${arrayBasicIntersect(e.omit,o)}.`)),arrayHasSame(a,s)&&c.push(new ValidationError(`'cast' has same keys as 'replace': ${arrayBasicIntersect(a,s)}.`)),arrayHasSame(a,o)&&c.push(new ValidationError(`'cast' has same keys as 'transform': ${arrayBasicIntersect(a,o)}.`)),arrayHasSame(s,o)&&c.push(new ValidationError(`'replace' has same keys as 'transform': ${arrayBasicIntersect(s,o)}.`)),c.length)throw new AggregateError(c,["Configuration has errors.",...c.map((t,e)=>`${e+1}). ${t.message}`)].join("\n"));return{...e}}config(t={}){let e;if(e=isFunction(t)?convertFunctionToConfig(t):t,!checkIsObjectLike(e))throw new TypeError("Config should be an object");const r=arrayGetDifference(objectGetKeys(e),i);if(r.length)throw new TypeError(`Config has unknown fields: '${r.join("', '")}'.`);return Vicis.testConfig(e),this.resetConfig(),this.sort(e.sort),this.omit(e.omit),this.defaults(e.defaults),this.nullish(e.nullish),this.cast(e.cast),this.defined(e.defined),this.pick(e.pick),this.rename(e.rename),this.replace(e.replace),this.required(e.required),this.transform(e.transform),this.exclude(e.exclude),this.order(e.order),this.validateConfig(),this.validateData(),this}cast(t={}){return this.__cast=castConfig(t),this.validateConfig(),this.validateData(),this}defaults(t={}){return this.__defaults=defaultsConfig(t),this.validateConfig(),this.validateData(),this}defined(t=[]){return this.__defined=definedConfig(t),this.validateConfig(),this.validateData(),this}exclude(t=[]){return this.__exclude=excludeConfig(t),this.validateConfig(),this.validateData(),this}nullish(t={}){return this.__nullish=nullishConfig(t),this.validateConfig(),this.validateData(),this}omit(t=[]){return this.__omit=omitConfig(t),this.validateConfig(),this.validateData(),this}order(t=[]){return this.__order=orderConfig(t),this.validateConfig(),this.validateData(),this}pick(t=[]){return this.__pick=pickConfig(t),this.validateConfig(),this.validateData(),this}rename(t={}){return this.__rename=renameConfig(t),this.validateConfig(),this.validateData(),this}replace(t={}){return this.__replace=replaceConfig(t),this.validateConfig(),this.validateData(),this}required(t=[]){return this.__required=requiredConfig(t),this.validateConfig(),this.validateData(),this}sort(t=a.Default){if("boolean"!=typeof t&&!Object.values(a).includes(t))throw new TypeError("'sort' should be a boolean");return sortAsBoolean(t)?this.__sort=a.Yes:this.__sort=a.No,this.validateData(),this}transform(t={}){return this.__transform=transformConfig(t),this.validateConfig(),this.validateData(),this}getData(){return clone(this.__dataCache)}data(t){if(!checkIsObjectLike(t))throw new TypeError("Data should be an object");return this.__dataOriginal=t,this.validateData(),this}clear(){return this.__dataCache=objectCreateEmpty(),this.__dataOriginal=void 0,this}toJSON(){return this.getData()}toString(){return jsonStringify(this.toJSON())}fromArray(t){return Array.from(t).map(t=>this.data(t).toJSON())}}function castToString(t){if(null==t)return"";if("string"==typeof t)return t;const e=String(t);return"0"===e&&Object.is(t,-0)?"-0":e}t.CAST_TYPE=r,t.Vicis=Vicis,t.cast=function cast(t,e){const r=castConfig(e);return objectIsEmpty(r)?t:castData(r,t)},t.defaults=function defaults(t,e={}){return objectIsEmpty(defaultsConfig(e))?t:defaultsData(e,t)},t.defined=function defined(t,e=[]){return arrayIsEmpty(e)?t:definedData(e,t)},t.exclude=function exclude(t,e=[]){const r=excludeConfig(e);if(arrayIsEmpty(r))return t;const a=r.filter(isString);if(a.length&&Object.keys(t).forEach(e=>{a.includes(e)&&delete t[e]}),0===objectGetKeys(t).length)return t;const i=r.filter(isRegExp);return i.length&&i.forEach(e=>{Object.keys(t).forEach(r=>{e.test(r)&&delete t[r]})}),t},t.nullish=function nullish(t,e={}){return objectIsEmpty(nullishConfig(e))?t:nullishData(e,t)},t.omit=function omit(t,e=[]){const r=omitConfig(e);if(arrayIsEmpty(r))return t;const a={};return Object.keys(t).forEach(e=>{r.includes(e)||(a[e]=t[e])}),a},t.order=function order(t,e=[],r=a.Default){return arrayIsEmpty(orderConfig(e))?t:objectKeysOrder(t,e,sortAsBoolean(r))},t.pick=function pick(t,e=[]){const r=pickConfig(e);return arrayIsEmpty(r)?t:pickData(r,t)},t.rename=function rename(t,e={}){const r=renameConfig(e);return objectIsEmpty(r)?t:renameData(r,t)},t.replace=function replace(t,e={}){const r=replaceConfig(e);return objectIsEmpty(r)?t:replaceData(r,t)},t.required=function required(t,e=[]){const r=requiredConfig(e);return arrayIsEmpty(r)?t:requiredData(r,t)},t.transform=function transform(t,e={}){const r=transformConfig(e);return objectIsEmpty(r)?t:transformData(r,t)},t.transformToStringIfNotNil=function transformToStringIfNotNil(t,e,r){return r&&e in r&&!function isNil(t){return null==t}(r[e])?castToString(t):t},t.transformToStringIfNotNull=function transformToStringIfNotNull(t,e,r){return r&&e in r&&null!==r[e]?castToString(t):null},t.transformToStringIfNotVoid=function transformToStringIfNotVoid(t,e,r){if(r&&e in r&&void 0!==r[e])return castToString(t)},Object.defineProperty(t,"__esModule",{value:!0})})); //# sourceMappingURL=index.js.map