app-state-utils
Version:
A set of utilities for state management in web apps, integrates well with front-end state management libraries like redux and vuex, and can be used on server side as well. It defines methods for mutating keys and values in a conventional javascript object
1 lines • 6.86 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["app-state-utils"]=t():e["app-state-utils"]=t()}(self,(function(){return(()=>{"use strict";var e={d:(t,n)=>{for(var r in n)e.o(n,r)&&!e.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:n[r]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}e.r(t),e.d(t,{default:()=>r});const r=function(e){this.createEntry=function(e,t){return function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t,l=JSON.parse(JSON.stringify(e)),o=r.name,i=r.value,h=o,g=r.child,f=e?Array.from(e):null,u=[];if("object"!==n(e)||e[e.length-1]===f.pop()&&void 0!==f.pop())return e;if("object"===n(g)&&0===g.length&&void 0===g[g.length-1]&&(g=null),!g&&l[h])return i===l[h]||n(i)!==n(l[h])?e:(Object.assign(l,e),l[h]=i,l);if(!g&&!l[h])return!l.hasOwnProperty(h)||i!==l[h]&&n(i)===n(l[h])?(Object.assign(l,e),l[h]=i,l):e;if(!g.length||i===l[h])return Object.assign(l,e),l;for(var s=0;s<g.length;s++){if(u.length)s===g.length-1&&(u[u.length-1][g[s]]=i,u.push(u[u.length-1][g[s]])),void 0!==u[u.length-1][g[s]]?s!==g.length-1&&u.push(u[u.length-1][g[s]]):s!==g.length-1&&(u[u.length-1][g[s]]={},u.push(u[u.length-1][g[s]]));else if(void 0===l[h])s===g.length-1?(l[h]={},l[h][g[s]]=i,u.push(l[h][g[s]])):(l[h]={},l[h][g[s]]={},u.push(l[h][g[s]]));else if(void 0===l[h][g[s]])s===g.length-1?(l[h][g[s]]=i,u.push(l[h][g[s]])):(l[h][g[s]]={},u.push(l[h][g[s]]));else if(s===g.length-1){if(n(l[h][g[s]])!==n(i))return e;l[h][g[s]]=i,u.push(l[h][g[s]])}else u.push(l[h][g[s]]);if(s===g.length-1)for(var a=u.length-1;a>=0;a--)if(a!==u.length-1&&(u[a][g[a+1]]=u[a+1]),0===a)return Object.assign(l,e),l[h][g[a]]=u[a],l}}}(e),this.updateValue=function(e,t){var r=!(arguments.length>2&&void 0!==arguments[2])||arguments[2];return function(){var l=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t,o=arguments.length>1&&void 0!==arguments[1]?arguments[1]:r,i=JSON.parse(JSON.stringify(e)),h=l.name,g=l.newValue,f=h,u=l.child,s=e?Array.from(e):null,a=[],p=!!o;if("object"!==n(e)||e[e.length-1]===s.pop()&&void 0!==s.pop())return e;if("object"===n(u)&&0===u.length&&void 0===u[u.length-1]&&(u=null),!u&&!i.hasOwnProperty(f))return e;if(!u&&g===i[f])return e;if(!u&&g!==i[f])return n(g)!==n(i[f])&&p?e:(Object.assign(i,e),i[f]=g,i);if(!u.length||g===i[f])return Object.assign(i,e),i;for(var c=0;c<u.length;c++){if(a.length){if(void 0===a[a.length-1][u[c]])return e;a.push(a[a.length-1][u[c]])}else{if(void 0===i[f][u[c]])return e;a.push(i[f][u[c]])}if(c===u.length-1){if(g===a[a.length-1])return e;if(n(g)!==n(a[a.length-1])&&p)return e;for(var y=a.length-1;y>=0;y--)if(y===a.length-1?a[y]=g:a[y][u[y+1]]=a[y+1],0===y)return Object.assign(i,e),i[f][u[y]]=a[y],i}}}}(e),this.clearValue=function(e,t){return function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t,l=JSON.parse(JSON.stringify(e)),o=r.name,i=r.child,h=e?Array.from(e):null,g=[];if("object"!==n(e)||e[e.length-1]===h.pop()&&void 0!==h.pop())return e;if("object"===n(i)&&0===i.length&&void 0===i[i.length-1]&&(i=null),!i&&l.hasOwnProperty(o))if(Object.assign(l,e),"boolean"==typeof l[o]){if(l[o])return l[o]=!1,l}else if("number"==typeof l[o]){if(0!==l[o])return l[o]=0,l}else if("object"===n(l[o])){if(l[o])return"number"==typeof l.length&&void 0!==l[l.length-1]?(l[o]=[],l):(l[o]={},l)}else{if("string"!=typeof l[o]&&"symbol"!==n(l[o]))return l[o]="",l;if(l[o].length)return l[o]="",l}else{if(!i&&!l.hasOwnProperty(o))return e;if(i.length)for(var f=0;f<i.length;f++){if(g.length){if(void 0===g[g.length-1][i[f]])return e;g.push(g[g.length-1][i[f]])}else{if(void 0===l[o][i[f]])return e;g.push(l[o][i[f]])}if(f===i.length-1)for(var u=g.length-1;u>=0;u--)if(u===g.length-1?null!==g[g.length-1]&&("boolean"==typeof g[g.length-1]?g[g.length-1]&&(g[g.length-1]=!1):"number"==typeof g[g.length-1]?0!==g[g.length-1]&&(g[g.length-1]=0):"object"===n(g[g.length-1])?g[g.length-1]&&("number"==typeof g[g.length-1].length&&void 0!==g[g.length-1].length-1?g[g.length-1]=[]:g[g.length-1]={}):"string"==typeof g[g.length-1]?g[g.length-1].length&&(g[g.length-1]=""):(n(g[g.length-1]),g[g.length-1]="")):g[u][i[u+1]]=g[u+1],0===u)return Object.assign(l,e),l[o][i[u]]=g[u],l}}}}(e),this.deleteEntry=function(e,t){return function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t,l=JSON.parse(JSON.stringify(e)),o=null,i=r.name,h=r.child,g=e?Array.from(e):null,f=[];if("object"!==n(e)||e[e.length-1]===g.pop()&&void 0!==g.pop())return e;if("object"===n(h)&&0===h.length&&void 0===h[h.length-1]&&(h=null),!h&&!l.hasOwnProperty(i))return e;if(!h&&l.hasOwnProperty(i)){if(o={},Object.keys(l).length>1){for(var u=0;u<Object.keys(l).length;u++)Object.keys(l)[u]!==i&&(o[Object.keys(l)[u]]=Object.values(l)[u]);return o}return o}if(!h.length||!l.hasOwnProperty(i))return e;for(var s=0;s<h.length;s++)if(f.length){if(s===h.length-1){for(var a={},p=0;p<Object.keys(f[f.length-1]).length;p++){if(Object.keys(f[f.length-1])[p]!==h[h.length-1])if(void 0!==f[f.length-1][h[s]])a[Object.keys(f[f.length-1])[p]]=f[f.length-1][Object.keys(f[f.length-1])[p]];else if(void 0===f[f.length-1][h[s]])return e;p===Object.keys(f[f.length-1]).length-1&&(f[f.length-1]=a)}for(var c=f.length-1;c>=0;c--)if(c<f.length-1&&(f[c][h[c+1]]=f[c+1]),0===c)return Object.assign(l,e),l[i][h[c]]=f[c],l}else if("object"===n(f[f.length-1][h[s]])&&void 0!==f[f.length-1][h[s]])f.push(f[f.length-1][h[s]]);else if(void 0===f[f.length-1][h[s]])return e}else if(s!==h.length-1)f.push(l[i][h[s]]);else for(var y={},b=0;b<Object.keys(l[i]).length;b++)if(Object.keys(l[i])[b]!==h[h.length-1]&&(y[Object.keys(l[i])[b]]=l[i][Object.keys(l[i])[b]]),b===Object.keys(l[i]).length-1)return Object.assign(l,e),l[i]=y,l}}(e),this.logState=function(e,t){return function(){var r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:t||{name:null,child:[]},l=JSON.parse(JSON.stringify(e)),o=r.name,i=r.child,h=e?Array.from(e):null,g=[];if(new Date,"state.".concat(o),"object"!==n(e)||e[e.length-1]===h.pop()&&void 0!==h.pop())return e;if("object"===n(i)&&0===i.length&&void 0===i[i.length-1]&&(i=null),!r.name)return e;if(!i&&!l.hasOwnProperty(o))return e;if(!i&&l.hasOwnProperty(o))return e;if(i.length){for(var f=0;f<i.length;f++)g.length?(".".concat(i[f]),i.length,g.push(g[g.length-1][i[f]])):(".".concat(i[f]),f===i.length-1||g.push(e[o][i[f]]));return e}}}(e)};return t})()}));