UNPKG

@zensen/form-service

Version:
3 lines (2 loc) 17.6 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).FormService=e()}(this,function(){"use strict";function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function e(t,e,r){return e=s(e),function(t,e){if(e&&("object"==typeof e||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}(t,u()?Reflect.construct(e,r||[],s(t).constructor):e.apply(t,r))}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function n(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,v(n.key),n)}}function i(t,e,r){return e&&n(t.prototype,e),r&&n(t,r),Object.defineProperty(t,"prototype",{writable:!1}),t}function o(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=m(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0,i=function(){};return{s:i,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return a=t.done,t},e:function(t){s=!0,o=t},f:function(){try{a||null==r.return||r.return()}finally{if(s)throw o}}}}function a(t,e,r){return(e=v(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function s(t){return s=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},s(t)}function c(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),Object.defineProperty(t,"prototype",{writable:!1}),e&&h(t,e)}function u(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(u=function(){return!!t})()}function f(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter(function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable})),r.push.apply(r,n)}return r}function l(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?f(Object(r),!0).forEach(function(e){a(t,e,r[e])}):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):f(Object(r)).forEach(function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))})}return t}function h(t,e){return h=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},h(t,e)}function _(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,i,o,a,s=[],c=!0,u=!1;try{if(o=(r=r.call(t)).next,0===e){if(Object(r)!==r)return;c=!1}else for(;!(c=(n=o.call(r)).done)&&(s.push(n.value),s.length!==e);c=!0);}catch(t){u=!0,i=t}finally{try{if(!c&&null!=r.return&&(a=r.return(),Object(a)!==a))return}finally{if(u)throw i}}return s}}(t,e)||m(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function y(e){return function(e){if(Array.isArray(e))return t(e)}(e)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(e)||m(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function v(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var n=r.call(t,e||"default");if("object"!=typeof n)return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}function p(t){return p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},p(t)}function m(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}function d(t){var e="function"==typeof Map?new Map:void 0;return d=function(t){if(null===t||!function(t){try{return-1!==Function.toString.call(t).indexOf("[native code]")}catch(e){return"function"==typeof t}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,r)}function r(){return function(t,e,r){if(u())return Reflect.construct.apply(null,arguments);var n=[null];n.push.apply(n,e);var i=new(t.bind.apply(t,n));return r&&h(i,r.prototype),i}(t,arguments,s(this).constructor)}return r.prototype=Object.create(t.prototype,{constructor:{value:r,enumerable:!1,writable:!0,configurable:!0}}),h(r,t)},d(t)}function b(t){return"object"===p(t)?JSON.stringify(t,"",2):t}var g=function(t){function n(t){return r(this,n),e(this,n,[t])}return c(n,t),i(n)}(d(Error)),S=function(t){function n(t){var i;return r(this,n),(i=e(this,n,[t])).name="VerificationError",i}return c(n,t),i(n)}(d(Error)),j=function(t){function n(t){var i;return r(this,n),(i=e(this,n,["Selector (".concat(t.join("."),") cannot have pristine state")])).name="PristineError",i}return c(n,t),i(n)}(d(Error)),w=function(t){function n(t,i,o){var a;return r(this,n),(a=e(this,n,["Reshaping on mutation not allowed at path: ".concat(t.join("."),"\nOld Value: ").concat(b(i),"\nNew Value: ").concat(b(o))])).name="MutationError",a}return c(n,t),i(n)}(d(Error)),O=function(t){function n(t){var i;return r(this,n),(i=e(this,n,["No key found in state for path: ".concat(t.join("."))])).name="PathError",i}return c(n,t),i(n)}(d(Error));function A(t,e){var r=[""],n=function(i){Object.entries(i).forEach(function(i){var o=_(i,2),a=o[0],s=o[1];r[r.length-1]=a;var c=s instanceof Date,u=!1===e([].concat(r),s),f=T(t,r),l=null!==f&&"object"===p(f);u||c||!l||(r.push(""),n(f),r.pop())})};arguments.length>2&&void 0!==arguments[2]&&arguments[2]&&e([],t),n(t)}function k(t,e){var r=Array.isArray(t)?[]:{};return A(t,function(t,n){n instanceof Date||null===n||"object"!==p(n)?E(r,t,e(t,n)):E(r,t,Array.isArray(n)?[]:{})}),r}function I(t,e){if(t===e)return!0;if(t&&e&&"object"==p(t)&&"object"==p(e)){if(t.constructor!==e.constructor)return!1;var r,n,i;if(Array.isArray(t)){if((r=t.length)!=e.length)return!1;for(n=r;0!==n--;)if(!I(t[n],e[n]))return!1;return!0}if(t instanceof Map&&e instanceof Map){if(t.size!==e.size)return!1;var a,s=o(t.entries());try{for(s.s();!(a=s.n()).done;)if(n=a.value,!e.has(n[0]))return!1}catch(t){s.e(t)}finally{s.f()}var c,u=o(t.entries());try{for(u.s();!(c=u.n()).done;)if(!I((n=c.value)[1],e.get(n[0])))return!1}catch(t){u.e(t)}finally{u.f()}return!0}if(t instanceof Set&&e instanceof Set){if(t.size!==e.size)return!1;var f,l=o(t.entries());try{for(l.s();!(f=l.n()).done;)if(n=f.value,!e.has(n[0]))return!1}catch(t){l.e(t)}finally{l.f()}return!0}if(ArrayBuffer.isView(t)&&ArrayBuffer.isView(e)){if((r=t.length)!=e.length)return!1;for(n=r;0!==n--;)if(t[n]!==e[n])return!1;return!0}if(t.constructor===RegExp)return t.source===e.source&&t.flags===e.flags;if(t.valueOf!==Object.prototype.valueOf)return t.valueOf()===e.valueOf();if(t.toString!==Object.prototype.toString)return t.toString()===e.toString();if((r=(i=Object.keys(t)).length)!==Object.keys(e).length)return!1;for(n=r;0!==n--;)if(!Object.prototype.hasOwnProperty.call(e,i[n]))return!1;for(n=r;0!==n--;){var h=i[n];if(!I(t[h],e[h]))return!1}return!0}return t!=t&&e!=e}function P(t){return k(t,function(t,e){return e})}function E(t,e,r){e.reduce(function(t,n,i){if(i!==e.length-1)return t[n];t[n]=r},t)}function T(t,e){return e.reduce(function(t,e){return void 0!==t?t[e]:void 0},t)}function V(t){var e=[];return A(t,function(t){return e.push(t)}),e}var D=function(t){return Array.isArray(t)||t.validators},x=function(t){return t.clipPristine};function M(t){var e="".concat(t);return e?e.split("."):[]}var R=function(){return i(function t(e,n,i){r(this,t),this.__state={},this.__errors={},this.__pristine={},this.__selectors=n,this.__onChange=i,this.refresh(e),this.__verifySelectors()},[{key:"isDirty",get:function(){return!I(this.__state,this.__initialState)}},{key:"isPristine",get:function(){var t=function(e){return!Object.values(e).filter(function(e){return"object"===p(e)?t(e):e}).length};return"object"===p(this.__pristine)?t(this.__pristine):this.__pristine}},{key:"hasErrors",get:function(){var t=function(e){return Object.values(e).filter(function(e){return"object"===p(e)?t(e):e}).length>0};return"object"===p(this.__errors)?t(this.__errors):Boolean(this.__errors)}},{key:"state",get:function(){return this.__state}},{key:"errors",get:function(){return this.__errors}},{key:"refresh",value:function(t){this.__state=P(t),this.__state=this.convert(t,"format"),this.__initialState=P(this.__state),this.__refreshErrors(),this.__refreshPristine(),this.__change()}},{key:"reset",value:function(){this.__state=P(this.__initialState),this.__refreshErrors(),this.__refreshPristine(),this.__change()}},{key:"apply",value:function(t,e){var r=M(t);if(t&&e===this.__state)throw new w(r,e,this.__state);if("object"===p(T(this.__pristine,r)))throw new j(r);this.__verifyValue(r,e),E(this.__state,r,e),this.validateKey(r),E(this.__pristine,r,!1),this.__spreadSchema("__state",r),this.__modify(r)}},{key:"addItem",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,r=M(t),n=T(this.__state,r),i=-1!==e?e:n.length,o=this.getSelector(r),a=this.convert(this.__state,"unformat"),s=o.createItem(r,i,a,this),c=this.__convertItem(s,r);n.splice(i,0,c),this.__spreadSchema("__state",[].concat(y(r),[i])),this.__addItemToSchema("__errors",r,i,c,"",D),this.__addItemToSchema("__pristine",r,i,c,!0,x),this.__modifyPristineItem([].concat(y(r),[i])),this.__modify(r),this.__change()}},{key:"removeItem",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:-1,r=M(t),n=T(this.__state,r),i=-1===e?n.length-1:e;n.splice(i,1),this.__spreadSchema("__state",[].concat(y(r),["".concat(i)])),this.__removeItemFromSchema("__errors",r,i),this.__removeItemFromSchema("__pristine",r,i),this.__modify(r),this.__change()}},{key:"moveItem",value:function(t,e,r){var n=M(t);this.__moveItemInSchema("__state",n,e,r),this.__moveItemInSchema("__errors",n,e,r),this.__moveItemInSchema("__pristine",n,e,r),this.__change()}},{key:"swapItems",value:function(t,e,r){var n=M(t);this.__swapItemsInSchema("__state",n,e,r),this.__swapItemsInSchema("__errors",n,e,r),this.__swapItemsInSchema("__pristine",n,e,r),E(this.__pristine,[].concat(y(n),[e]),!1),E(this.__pristine,[].concat(y(n),[r]),!1),this.__change()}},{key:"convert",value:function(t,e){var r=this,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:[],i=this.getSelector([],!0),o=i&&i[e],a="object"===p(t)?P(t):t,s=o?o(a,n,t):a;return A(s,function(i,o){var a=[].concat(y(n),y(i)),c=r.getSelector(a,!0);if(c&&c[e]){var u=c[e](o,a,t);if(null!==u&&"object"===p(u)){var f=u instanceof Date?new Date(u.getTime()):P(u);E(s,i,f)}else E(s,i,u)}}),s}},{key:"build",value:function(){return this.convert(this.__state,"unformat")}},{key:"validate",value:function(){var t=this;return this.__pristine=k(this.__pristine,function(){return!1}),A(this.__state,function(e){var r=T(t.__pristine,e);void 0!==r&&"object"!==p(r)&&t.validateKey(e,!0)}),!this.hasErrors}},{key:"validateKey",value:function(t){var e=this,r=arguments.length>1&&void 0!==arguments[1]&&arguments[1],n=t.findIndex(function(r,n){var i=t.slice(0,n);return e.getValidators(i)}),i=-1!==n?n:t.length,o=t.slice(0,i),a=this.getValidators(o),s=T(this.__pristine,t),c=this.__errors;if(a&&(!s||r)){var u=this.getSelector(o),f=u.validateRaw||!1;u.validateManually&&!r||this.__processValidator(o,a,f)}c!==this.__errors&&this.__change()}},{key:"unsetPristine",value:function(t){if("boolean"!=typeof T(this.__pristine,t))throw new TypeError("Invalid path: ".concat(t.join(".")));E(this.__pristine,t,!1)}},{key:"getSelectorPath",value:function(t){var e=this;if(!(arguments.length>1&&void 0!==arguments[1]&&arguments[1])&&void 0===T(this.__state,t))throw new O(t);var r=t.length?["children"]:[];return t.reduce(function(r,n,i){var o=t.slice(0,i),a=T(e.__state,o),s=Array.isArray(a)?"$":n;return i<t.length-1?[].concat(y(r),[s,"children"]):[].concat(y(r),[s])},r)}},{key:"getSelector",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]&&arguments[1],r=this.getSelectorPath(t,e);return T(this.__selectors,r)}},{key:"getValidators",value:function(t){var e=this.getSelector(t);return e?Array.isArray(e)?e:e.validators:null}},{key:"__change",value:function(){this.__onChange(this.isDirty,this.__state,this.__errors,this.__pristine)}},{key:"__verifySelectors",value:function(){var t=this;A(this.__state,function(e,r){if(t.getValidators(e)){var n=e.slice(0,e.length-1);n.forEach(function(r,i){var o=n.slice(0,i+1);if(t.getValidators(o))throw new S("Selector (".concat(e.join("."),") has ancestor selector with validators: ").concat(o.join(".")))})}if("object"===p(r)){var i=t.getSelector(e);if(i&&i.ignorePristine&&!i.clipPristine){var o="ignorePristine set object-type key for path: ".concat(e.join("."),". Perhaps you meant to use clipPristine?");throw new S(o)}}})}},{key:"__verifyValue",value:function(t,e){var r=this.getSelector(t,!0),n=T(this.__state,t);if(!r||!r.unsafe){if(void 0===n)throw new TypeError("Invalid path: ".concat(t.join(".")));if(null!==n&&null!==e&&"object"===p(n)){var i=V(n);if("object"!==p(e))throw new w(t,n,e);if(!I(i,V(e)))throw new w(t,n,e)}}}},{key:"__buildSchema",value:function(t,e,r){var n=this,i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:[],o=Array.isArray(t)?[]:{},a=this.getSelector(i);return a&&r(a)?e:(A(t,function(t,a){if(a instanceof Date||null===a||"object"!==p(a))E(o,t,e);else{var s=[].concat(y(i),y(t)),c=n.getSelector(s);if(c&&r(c))return E(o,t,e),!1;E(o,t,Array.isArray(a)?[]:{})}},!0),o)}},{key:"__convertItem",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],r="object"===p(t)?P(t):t;return this.convert([r],"format",e)[0]}},{key:"__spreadSchema",value:function(t,e){var r=this,n=this[t];this[t]=Array.isArray(n)?y(n):l({},n),e.length>1&&e.slice(0,e.length-1).forEach(function(n,i){var o=e.slice(0,i+1),a=T(r[t],o),s=Array.isArray(a)?y(a):l({},a);E(r[t],o,s)})}},{key:"__addItemToSchema",value:function(t,e,r,n,i,o){var a=T(this[t],e);if("object"===p(a)){var s=this.getSelector(e),c=s&&s.children&&s.children.$&&o(s.children.$)||"object"!==p(n)?i:this.__buildSchema(n,i,o,[].concat(y(e),["".concat(r)]));a.splice(r,0,c),this.__spreadSchema(t,[].concat(y(e),["".concat(r)]))}}},{key:"__removeItemFromSchema",value:function(t,e,r){var n=T(this[t],e);Array.isArray(n)&&(n.splice(r,1),this.__spreadSchema(t,[].concat(y(e),["".concat(r)])))}},{key:"__moveItemInSchema",value:function(t,e,r,n){var i=T(this[t],e);if(Array.isArray(i)){var o=function(t,e,r){var n=t[e],i=y(t);return i.splice(e,1),i.splice(r,0,n),i}(i,r,n).map(function(t){return"object"===p(t)?Array.isArray(t)?y(t):l({},t):t});e.length?E(this[t],e,o):this[t]=o,this.__spreadSchema(t,e)}}},{key:"__swapItemsInSchema",value:function(t,e,r,n){var i=T(this[t],e);if(Array.isArray(i)){var o=function(t,e,r){var n=y(t);return n.splice(e,1),n.splice(e,0,t[r]),n.splice(r,1),n.splice(r,0,t[e]),n}(i,r,n);e.length?E(this[t],e,o):this[t]=o,this.__spreadSchema(t,[].concat(y(e),[r])),this.__spreadSchema(t,[].concat(y(e),[n]))}}},{key:"__modify",value:function(t){var e=T(this.__pristine,t);e&&"object"!==p(e)&&E(this.__pristine,t,!1),this.__change()}},{key:"__processValidator",value:function(t,e,r){var n=this,i=r?this.convert(this.__state,"unformat"):this.__state,o=T(i,t);try{e.forEach(function(e){if(!e.validate(o,t,i,n))throw new g(e.error)}),this.__setError(t,"")}catch(e){if(!(e instanceof g))throw e;this.__setError(t,e.message)}}},{key:"__refreshErrors",value:function(){this.__errors=this.__buildSchema(this.__state,"",function(t){return Array.isArray(t)||t.validators})}},{key:"__setError",value:function(t,e){t.length?(E(this.__errors,t,e),this.__spreadSchema("__errors",t)):this.__errors=e}},{key:"__refreshPristine",value:function(){var t=this;this.__pristine=this.__buildSchema(this.__state,!0,function(t){return t.clipPristine}),A(this.__pristine,function(e){var r=t.getSelector(e);r&&r.ignorePristine&&E(t.__pristine,e,!1)})}},{key:"__modifyPristineItem",value:function(t){var e=this,r=T(this.__pristine,t);if("object"===p(r))A(r,function(r){var n=[].concat(y(t),y(r)),i=e.getSelector(n);i&&i.ignorePristine&&E(e.__pristine,n,!1)});else{var n=this.getSelector(t);n&&n.ignorePristine&&E(this.__pristine,t,!1)}}}])}();return R}); //# sourceMappingURL=form-service.umd.min.js.map