UNPKG

vuex-pathify

Version:
2 lines (1 loc) 10.4 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.VuexPathify={})}(this,function(t){"use strict";var e={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 n(t){return!!t&&"object"==typeof t}function r(t,e){return n(t)&&e in t}function o(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 i(t,e){var r=t;return o(e).every(function(t){var e=n(r)&&r.hasOwnProperty(t);return r=e?r[t]:void 0,e}),r}var a,u={mapping:"standard",strict:!0,cache:!0,deep:1},s={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()}},c={state:"state",getters:"getters",actions:"_actions",mutations:"_mutations"},f={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 p(t,e){if(e.match(/!$/))return e.substr(0,e.length-1);var n=a;if(!n){if("function"==typeof u.mapping)n=u.mapping;else if(!(n=f[u.mapping]))throw new Error("[Vuex Pathify] Unknown mapping '"+u.mapping+"' in options\n - Choose one of '"+Object.keys(f).join("', '")+"'\n - Or, supply a custom function\n");a=n}return a(t,e,s)}function l(t,e){var i,a,u=e.replace(/[/@!]+/g,"."),s=e.split("@"),f=s[0],l=s[1];if(f.indexOf("/")>-1){var h=f.split("/");a=h.pop(),i=h.join("/")}else a=f;if(i&&!t._modulesNamespaceMap[i+"/"])throw new Error("[Vuex Pathify] Unknown module '"+i+"' via path '"+e+"'");return{absPath:u,module:i,target:f,name:a.replace("!",""),isDynamic:e.indexOf(":")>-1,get:function(e){var u=t[c[e]],s=p(e,a),f=i?i+"/"+s:s;return{exists:"state"===e?function(t,e){var i=o(e);if(n(t)){for(;i.length;){var a=i.shift();if(!r(t,a))return!1;t=t[a]}return!0}return!1}(u,f):f in u,member:u,trgPath:f,trgName:s,objPath:l}}}}function h(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 d=function(t,e,n){this.expr=t,this.path=e,this.value=n};function m(t,e){var n=l(t,e),r=n.get("actions");if(r.exists)return function(n){var o=r.objPath?new d(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=v(e,this);o=l(t,i).get("mutations")}var a=o.objPath?new d(e,o.objPath,r):r;return t.commit(o.trgPath,a)}:("production"!==process.env.NODE_ENV&&console.error(h(e,n,"action",r,"mutation",o)),function(t){})}function g(t,e,n){var r,o=l(t,e);if(!n&&(r=o.get("getters")).exists)return function(){var t=r.member[r.trgPath];return r.objPath?y(e,t,r.objPath):t};var i=o.get("state");return i.exists||o.isDynamic?function(){var n=o.isDynamic?v(o.absPath,this):o.absPath;return y(e,t.state,n)}:("production"!==process.env.NODE_ENV&&console.error(h(e,o,"getter",r,"state",i)),function(){})}function y(t,e,n){if(u.deep||!(t.indexOf("@")>-1))return i(e,n);console.error("[Vuex Pathify] Unable to access sub-property for path '"+t+"':\n - Set option 'deep' to 1 to allow it")}function v(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]})}function b(t){t.set=function(e,n){var r=m(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=g(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 r=[],o=arguments.length-1;o-- >0;)r[o]=arguments[o+1];var i,a=t.get.apply(t,[e].concat(r));return n(a)?(i=a,JSON.parse(JSON.stringify(i))):a}}function x(t){return p(t,"value")}d.prototype.update=function(t){return u.deep?function(t,e,r,i){void 0===i&&(i=!1);var a=o(e);return a.reduce(function(t,e,o){var u=/^\d+$/.test(e);if(u&&(e=parseInt(e)),!t)return!1;if(o===a.length-1)return t[e]=r,!0;if(!(n(t[e])&&e in t)){if(!i)return!1;t[e]=u?[]:{}}return t[e]},t)}(t,this.path,this.value,u.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)};var w={options:u,plugin:function(t){e.store=t,b(t)},debug:function(){console.log("\n [Vuex Pathify] Options:\n\n Mapping ("+("function"==typeof u.mapping?"custom":u.mapping)+")\n-------------------------------\n path : value\n state : "+x("state")+"\n getters : "+x("getters")+"\n actions : "+x("actions")+"\n mutations : "+x("mutations")+"\n\n Settings\n-------------------------------\n strict : "+u.strict+"\n cache : "+u.cache+"\n deep : "+u.deep+"\n\n")}};function P(t){return o("function"==typeof t?t():t)}var $={getters:function(t){return P(t).reduce(function(t,e){return t[p("getters",e)]=function(t){return t[e]},t},{})},mutations:function(t){return P(t).reduce(function(t,e){return t[p("mutations",e)]=function(t,n){t[e]=n instanceof d?n.update(t[e]):n},t},{})},actions:function(t){return P(t).reduce(function(t,e){var n=p("actions",e),r=p("mutations",e);return t[n]=function(t,e){(0,t.commit)(r,e)},t},{})}};function O(t,e){var n=t.match(/([^/@\.]+)$/)[1],r=t.substring(0,t.length-n.length),o=r.replace(/\W+$/,"").split(/[/@.]/),a=r?i(e,o):e;if(!a)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 u=new RegExp("^"+n.replace(/\*/g,"\\w+")+"$");return Object.keys(a).filter(function(t){return u.test(t)}).map(function(t){return r+t})}function j(t,e){var n=new RegExp("^"+t.replace(/\*/g,"\\w+")+"$");return Object.keys(e).filter(function(t){return n.test(t)})}function E(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 V(t,e){return void 0===e&&(e=""),((t=t.replace(/\/+$/,"")).indexOf("@")>-1?t+"."+e:t+"/"+e).replace(/^\/|[.@/]+$/,"").replace(/\/@/,"@").replace(/@\./,"@")}function k(t){return t.reduce(function(t,e){return t[e.match(/\w+$/)]=e,t},{})}function N(t,n){return _(t,n,S,function(t){return function(t,e,n){return E(t,e)?O(t,e).concat(j(t,n)):""}(t,e.store.state,e.store.getters)})}function D(t,n){return _(t,n,A,function(t){return function(t,e){return E(t,e)?O(t,e):""}(t,e.store.state)})}function U(t,n){return _(t,n,C,function(t){return function(t,e){return E(t,e)?j(t,e):""}(t,e.store._actions)})}function _(t,e,r,o){var i=function(t,e,r){return"string"==typeof t&&t.indexOf("*")>-1?k(r(t)):Array.isArray(t)?k(t):(n(t)&&(e=t,t=""),Array.isArray(e)?k(e.map(function(e){return V(t,e)})):n(e)?Object.keys(e).reduce(function(n,r){return n[r]=V(t,e[r]),n},{}):t)}(t,e,o);return"string"==typeof i?r(i):(Object.keys(i).forEach(function(t){i[t]=r(i[t])}),i)}function A(t){var e=t.split("|"),n=e[0],r=e[1];return r&&(r=n.replace(/\w+!?$/,r.replace("!","")+"!")),n&&r?{get:S(n,!0),set:M(r)}:{get:S(n,!0),set:M(n)}}function S(t,e){var n,r;return function(){for(var o=[],i=arguments.length;i--;)o[i]=arguments[i];if(!this.$store)throw new Error("[Vuex Pathify] Unexpected condition: this.$store is undefined.\n\nThis is a known edge case with some setups and will cause future lookups to fail");return n&&r===this.$store||(r=this.$store,n=g(r,t,e)),n.call.apply(n,[this].concat(o))}}function M(t){var e,n;return function(r){var o=this;return e&&n===this.$store||(n=this.$store,e=m(n,t)),this.$nextTick(function(){return o.$emit("sync",t,r)}),e.call(this,r)}}function C(t){return function(e){return this.$store.dispatch(t,e)}}var T=require("vue-class-component").createDecorator;t.default=w,t.make=$,t.Payload=d,t.get=N,t.sync=D,t.call=U,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 T(function(e,n){e.computed||(e.computed={}),e.computed[n]=N(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 T(function(e,n){e.computed||(e.computed={}),e.computed[n]=D(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 T(function(e,n){e.methods||(e.methods={}),e.methods[n]=U(t)})},t.commit=function(){for(var t,n=[],r=arguments.length;r--;)n[r]=arguments[r];(t=e.store).commit.apply(t,n)},t.dispatch=function(){for(var t,n=[],r=arguments.length;r--;)n[r]=arguments[r];return(t=e.store).dispatch.apply(t,n)},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||{})},destoyed:function(){this.$store.unregisterModule(t)}}},Object.defineProperty(t,"__esModule",{value:!0})});