vuex-dot
Version:
Simplifies two-way data binding and v-model usage on vuex state, providing full reactivity via generated setters/getters
1 lines • 3.42 kB
JavaScript
(function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports):"function"==typeof define&&define.amd?define(["exports"],b):b(a.VuexDot={})})(this,function(a){"use strict";function b(a,b,c){return"function"==typeof c.join?c.join(a):a[0]+b+a[1]}function c(a,b,c){return"function"==typeof c.split?c.split(a):a.split(b)}function d(a,b,c){return"function"!=typeof c.isValid||c.isValid(a,b)}function e(a){return f(a)||Array.isArray(a)||"function"==typeof a}var f=function(a){return null!=a&&"object"==typeof a&&!1===Array.isArray(a)},g=function(a,g,h){if(f(h)||(h={default:h}),!e(a))return"undefined"==typeof h.default?a:h.default;"number"==typeof g&&(g+="");const i=Array.isArray(g),j="string"==typeof g,k=h.separator||".",l=h.joinChar||("string"==typeof k?k:".");if(!j&&!i)return a;if(j&&g in a)return d(g,a,h)?a[g]:h.default;let m=i?g:c(g,k,h),o=m.length,p=0;do{let c=m[p];for("number"==typeof c&&(c+="");c&&"\\"===c.slice(-1);)c=b([c.slice(0,-1),m[++p]||""],l,h);if(c in a){if(!d(c,a,h))return h.default;a=a[c]}else{let e=!1,f=p+1;for(;f<o;)if(c=b([c,m[f++]],l,h),e=c in a){if(!d(c,a,h))return h.default;a=a[c],p=f-1;break}if(!e)return h.default}}while(++p<o&&e(a));return p===o?a:h.default},h=function(a,b,c){const d=!b||!!a.customPayload,e=()=>c?d?function(b){a.dispatcher.call(this,this.$store,b,f.call(this))}:function(c){a.dispatcher.call(this,this.$store,c,b,f.call(this))}:d?function(b){a.dispatcher.call(this,this.$store,b)}:function(c){a.dispatcher.call(this,this.$store,c,b)},f=function(){return g(this,a.path)},h=b?function(){return g(this,a.path+"."+b)}:f,i={get:a.getterGate?a.getterGate(b,h):h},j=a.action?"dispatch":a.mutation?"commit":null,k=a.action?a.action:a.mutation?a.mutation:null;return!j||a.hook(c?d?(a,b,c)=>a[j](k,{target:c,payload:b}):(a,b,c,d)=>a[j](k,{target:d,key:c,value:b}):d?(a,b)=>a[j](k,b):(a,b,c)=>a[j](k,{key:c,value:b})),a.dispatcher&&(i.set=a.gate?a.gate(b,e()):e()),i};var i=class{constructor(a,b){this.target=a,this.projection=b,this.sendTarget=!1}commit(a,b=!1){return this.target.commit(a),this.sendTarget=b||!1,this}dispatch(a,b=!1){return this.target.dispatch(a),this.sendTarget=b||!1,this}hook(a,b){return this.target.hook(a),this.sendTarget=b||!1,this}map(){const a={},{target:b,sendTarget:c}=this;return this.projection.forEach(d=>{let e=-1===d.indexOf(".")?d:d.replace(/\.(.)/g,(a,b)=>b.toUpperCase());a[e]=h(b,d,c)}),b.inject&&Object.assign(a,b.inject),a}use(a){return this.target.use(a),this}};var j=class{constructor(a){this.path=a}expose(a){return new i(this,a)}commit(a){return this.mutation=a,this}dispatch(a){return this.action=a,this}hook(a){return this.dispatcher=a,this}map(a){return a?Object.assign({[a]:h(this)},this.inject||{}):h(this)}use(a){return this.gate=(b=>b?(c,d)=>function(e){a.setter.call(this,c,e,b(c,d).bind(this))}:(b,c)=>function(d){a.setter.call(this,b,d,c.bind(this))})(this.gate),!a.getter||(this.getterGate=(b=>b?(c,d)=>function(){return a.getter.call(this,c,b(c,d).bind(this))}:(b,c)=>function(){return a.getter.call(this,b,c.bind(this))})(this.getterGate)),this.inject=Object.assign({},a.inject,this.inject),this.customPayload=this.customPayload||a.customPayload,this}};var k={take:a=>new j(a),takeState:(a,b)=>{b="undefined"==typeof b?a:`${a.replace("/",".")}.${b}`;const c=`$store.state.${b}`;return new j(c)}},l=k.take,m=k.takeState;a.default=k,a.take=l,a.takeState=m,Object.defineProperty(a,"__esModule",{value:!0})});