UNPKG

vuex-pathify

Version:
2 lines (1 loc) 10.7 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("vue"),require("vue-class-component")):"function"==typeof define&&define.amd?define(["exports","vue","vue-class-component"],e):e(t.VuexPathify={},t.vue,t.vueClassComponent)}(this,function(t,e,n){"use strict";function r(t){return!!t&&"object"==typeof t}function o(t){return"number"==typeof t||/^\d+$/.test(t)}function i(t,e){return r(t)&&e in t}function a(t){return t?Array.isArray(t)?t.map(function(t){return String(t)}):"object"==typeof t?Object.keys(t):"string"==typeof t&&t.match(/[-$\w]+/g)||[]:[]}function u(t,e){var n=t;return a(e).every(function(t){var e=r(n)&&n.hasOwnProperty(t);return n=e?n[t]:void 0,e}),n}var c,s={mapping:"standard",strict:!0,cache:!0,deep:1},f={camel:function(){for(var t=[],e=arguments.length;e--;)t[e]=arguments[e];return t.shift()+t.map(function(t){return t.replace(/\w/,function(t){return t.toUpperCase()})}).join("")},snake:function(){for(var t,e=[],n=arguments.length;n--;)e[n]=arguments[n];return(t=this).camel.apply(t,e).replace(/([a-z])([A-Z])/g,function(t,e,n){return e+"_"+n}).toLowerCase()},const:function(){for(var t,e=[],n=arguments.length;n--;)e[n]=arguments[n];return(t=this).snake.apply(t,e).toUpperCase()}},p={state:"state",getters:"getters",actions:"_actions",mutations:"_mutations"},l={standard:function(t,e,n){switch(t){case"mutations":return n.const("set",e);case"actions":return n.camel("set",e)}return e},simple:function(t,e,n){return"actions"===t?n.camel("set",e):e}};function d(t,e){if(e.match(/!$/))return e.substr(0,e.length-1);var n=c;if(!n){if("function"==typeof s.mapping)n=s.mapping;else if(!(n=l[s.mapping]))throw new Error("[Vuex Pathify] Unknown mapping '"+s.mapping+"' in options\n - Choose one of '"+Object.keys(l).join("', '")+"'\n - Or, supply a custom function\n");c=n}return c(t,e,f)}function h(t,e){var n,o,u=e.replace(/[/@!]+/g,"."),c=e.split("@"),s=c[0],f=c[1];if(s.indexOf("/")>-1){var l=s.split("/");o=l.pop(),n=l.join("/")}else o=s;if(n&&!t._modulesNamespaceMap[n+"/"])throw new Error("[Vuex Pathify] Unknown module '"+n+"' via path '"+e+"'");return{absPath:u,module:n,target:s,name:o.replace("!",""),isDynamic:e.indexOf(":")>-1,get:function(e){var u=t[p[e]],c=d(e,o),s=n?n+"/"+c:c;return{exists:"state"===e?function(t,e){var n=a(e);if(r(t)){for(;n.length;){var o=n.shift();if(!i(t,o))return!1;t=t[o]}return!0}return!1}(u,s):!!u&&s in u,member:u,trgPath:s,trgName:c,objPath:f}}}}function m(t,e,n,r,o,i){var a="[Vuex Pathify] Unable to map path '"+t+"':";t.indexOf("!")>-1?a+="\n - Did not find "+n+" or "+o+" named '"+e.name+"' on "+(e.module?"module '"+e.module+"'":"root store"):a+="\n - Did not find "+(r?n+" '"+r.trgName+"' or ":"")+(o+" '"+i.trgName+"'")+" on "+(e.module?"module '"+e.module+"'":"store")+"\n - Use direct syntax '"+e.target.replace(/(@|$)/,"!$1")+"' (if member exists) to target directly";return a}var g=function(t,e,n){this.expr=t,this.path=e,this.value=n};function y(t,e){var n=h(t,e),r=n.get("actions");if(r.exists)return function(n){var o=r.objPath?new g(e,r.objPath,n):n;return t.dispatch(r.trgPath,o)};var o=n.get("mutations");return o.exists||n.isDynamic?function(r){if(n.isDynamic){var i=x(e,this);o=h(t,i).get("mutations")}var a=o.objPath?new g(e,o.objPath,r):r;return t.commit(o.trgPath,a)}:("production"!==process.env.NODE_ENV&&console.error(m(e,n,"action",r,"mutation",o)),function(t){})}function v(t,e,n){var r,o=h(t,e);if(!n&&(r=o.get("getters")).exists)return function(){var t=r.member[r.trgPath];return r.objPath?b(e,t,r.objPath):t};var i=o.get("state");return i.exists||o.isDynamic?function(){var n=o.isDynamic?x(o.absPath,this):o.absPath;return b(e,t.state,n)}:("production"!==process.env.NODE_ENV&&console.error(m(e,o,"getter",r,"state",i)),function(){})}function b(t,e,n){if(s.deep||!(t.indexOf("@")>-1))return u(e,n);console.error("[Vuex Pathify] Unable to access sub-property for path '"+t+"':\n - Set option 'deep' to 1 to allow it")}function x(t,e){return t.replace(/:(\w+)/g,function(t,n){return n in e||console.error('Error resolving dynamic store path: The property "'+n+'" does not exist on the scope',e),e[n]})}g.prototype.update=function(t){return s.deep?function(t,e,n,i){void 0===i&&(i=!1);var u=a(e);return u.reduce(function(t,e,a){if(!t)return!1;if(Array.isArray(t)&&o(e)&&(e=parseInt(e)),a===u.length-1)return t[e]=n,!0;if(!(r(t[e])&&e in t)){if(!i)return!1;t[e]=o(u[a+1])?[]:{}}return t[e]},t)}(t,this.path,this.value,s.deep>1)||"production"===process.env.NODE_ENV?Array.isArray(t)?[].concat(t):Object.assign({},t):(console.error("[Vuex Pathify] Unable to create sub-property for path '"+this.expr+"':\n - Set option 'deep' to 2 to allow it"),t):(console.error("[Vuex Pathify] Unable to access sub-property for path '"+this.expr+"':\n - Set option 'deep' to 1 to allow it"),t)},g.isSerialized=function(t){return function(t){return r(t)&&!Array.isArray(t)}(t)&&"expr"in t&&"path"in t&&"value"in t};var P={store:{state:null,commit:function(){"production"!==process.env.NODE_ENV&&console.error("[Vuex Pathify] Plugin not initialized!")},dispatch:function(){"production"!==process.env.NODE_ENV&&console.error("[Vuex Pathify] Plugin not initialized!")}}};function w(){return P.store}function O(t){P.store=t,t.set=function(e,n){var r=y(t,e);if(void 0!==r)return r(n)},t.get=function(e){for(var n=[],r=arguments.length-1;r-- >0;)n[r]=arguments[r+1];var o=v(t,e);if(void 0!==o){var i=o();return"function"==typeof i?i.apply(void 0,n):i}},t.copy=function(e){for(var n=[],o=arguments.length-1;o-- >0;)n[o]=arguments[o+1];var i,a=t.get.apply(t,[e].concat(n));return r(a)?(i=a,JSON.parse(JSON.stringify(i))):a}}function j(t){return d(t,"value")}var $={options:s,plugin:function(t){O(t)},debug:function(){console.log("\n [Vuex Pathify] Options:\n\n Mapping ("+("function"==typeof s.mapping?"custom":s.mapping)+")\n-------------------------------\n path : value\n state : "+j("state")+"\n getters : "+j("getters")+"\n actions : "+j("actions")+"\n mutations : "+j("mutations")+"\n\n Settings\n-------------------------------\n strict : "+s.strict+"\n cache : "+s.cache+"\n deep : "+s.deep+"\n\n")}};function E(t){return a("function"==typeof t?t():t)}var V={getters:function(t){return E(t).reduce(function(t,e){return t[d("getters",e)]=function(t){return t[e]},t},{})},mutations:function(t){return E(t).reduce(function(t,e){return t[d("mutations",e)]=function(t,n){n instanceof g?n=n.update(t[e]):g.isSerialized(n)&&(n=g.prototype.update.call(n,t[e])),t[e]=n},t},{})},actions:function(t){return E(t).reduce(function(t,e){var n=d("actions",e),r=d("mutations",e);return t[n]=function(t,e){(0,t.commit)(r,e)},t},{})}};function N(t,e){var n=t.match(/([^/@\.]+)$/)[1],r=t.substring(0,t.length-n.length),o=r.replace(/\W+$/,"").split(/[/@.]/),i=r?u(e,o):e;if(!i)return console.error("[Vuex Pathify] Unable to expand wildcard path '"+t+"':\n - It looks like '"+r.replace(/\W+$/,"")+"' does not resolve to an existing state property"),[];var a=new RegExp("^"+n.replace(/\*/g,"\\w+")+"$");return Object.keys(i).filter(function(t){return a.test(t)}).map(function(t){return r+t})}function D(t,e){var n=new RegExp("^"+t.replace(/\*/g,"\\w+")+"$");return Object.keys(e).filter(function(t){return n.test(t)})}function k(t,e){return t.indexOf("*")>-1&&/\*.*[/@.]/.test(t)?(console.error("[Vuex Pathify] Invalid wildcard placement for path '"+t+"':\n - Wildcards may only be used in the last segment of a path"),!1):!!e||(console.error("[Vuex Pathify] Unable to expand wildcard path '"+t+"':\n - The usual reason for this is that the router was set up before the store\n - Make sure the store is imported before the router, then reload"),!1)}function A(t,e){return void 0===e&&(e=""),((t=t.replace(/\/+$/,"")).indexOf("@")>-1?t+"."+e:t+"/"+e).replace(/^\/|[.@/]+$/,"").replace(/\/@/,"@").replace(/@\./,"@")}function _(t){return t.reduce(function(t,e){return t[e.match(/\w+$/)]=e,t},{})}function U(t,e,n){void 0===n&&(n=!0),2===arguments.length&&!1===e&&(e=null,n=!1);var r=w();return M(t,e,I,function(t){return function(t,e,n){return k(t,e)?N(t,e).concat(D(t,n)):""}(t,r.state,r.getters)},n)}function S(t,e,n){void 0===n&&(n=!0),2===arguments.length&&!1===e&&(e=null,n=!1);var r=w();return M(t,e,z,function(t){return function(t,e){return k(t,e)?N(t,e):""}(t,r.state)},n)}function C(t,e){var n=w();return M(t,e,q,function(t){return function(t,e){return k(t,e)?D(t,e):""}(t,n._actions)})}function M(t,n,o,i,a){void 0===a&&(a=!1);var u=function(t,e,n){return"string"==typeof t&&t.indexOf("*")>-1?_(n(t)):Array.isArray(t)?_(t):(r(t)&&(e=t,t=""),Array.isArray(e)?_(e.map(function(e){return A(t,e)})):r(e)?Object.keys(e).reduce(function(n,r){return n[r]=A(t,e[r]),n},{}):t)}(t,n,i);return"string"==typeof u?a?e.computed(o(u)):o(u):(Object.keys(u).forEach(function(t){u[t]=a?e.computed(o(u[t])):o(u[t])}),u)}function z(t){var e=t.split("|"),n=e[0],r=e[1];return r&&(r=n.replace(/\w+!?$/,r.replace("!","")+"!")),n&&r?{get:I(n,!0),set:W(r)}:{get:I(n,!0),set:W(n)}}function I(t,e){var n,r;return function(){for(var o=[],i=arguments.length;i--;)o[i]=arguments[i];return n&&r||(r=w(),n=v(r,t,e)),n.call.apply(n,[this].concat(o))}}function W(t){var e,n;return function(r){return e&&n||(n=w(),e=y(n,t)),e.call(this,r)}}function q(t){var e=w();return function(n){return e.dispatch(t,n)}}t.default=$,t.make=V,t.Payload=g,t.get=U,t.sync=S,t.call=C,t.Get=function(t){if("string"!=typeof t||arguments.length>1)throw new Error("Property decorators can only be used for single property access");return n.createDecorator(function(e,n){e.computed||(e.computed={}),e.computed[n]=U(t)})},t.Sync=function(t){if("string"!=typeof t||arguments.length>1)throw new Error("Property decorators can only be used for single property access");return n.createDecorator(function(e,n){e.computed||(e.computed={}),e.computed[n]=S(t)})},t.Call=function(t){if("string"!=typeof t||arguments.length>1)throw new Error("Property decorators can only be used for single property access");return n.createDecorator(function(e,n){e.methods||(e.methods={}),e.methods[n]=C(t)})},t.commit=function(){for(var t,e=[],n=arguments.length;n--;)e[n]=arguments[n];(t=P.store).commit.apply(t,e)},t.dispatch=function(){for(var t,e=[],n=arguments.length;n--;)e[n]=arguments[n];return(t=P.store).dispatch.apply(t,e)},t.registerModule=function(t,e,n,r){return{beforeCreate:function(){this.$store.registerModule(t,e,r);var o=n();this.$options.computed=Object.assign(this.$options.computed||{},o.computed||{}),this.$options.methods=Object.assign(this.$options.methods||{},o.methods||{})},destroyed:function(){this.$store.unregisterModule(t)}}},Object.defineProperty(t,"__esModule",{value:!0})});