diff --git a/README.md b/README.md index 133f61b..a1969c7 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ The Vuex Easy Access plugin does two things: - [2. Automatically generate mutations for each state property](#2-automatically-generate-mutations-for-each-state-property) - [What's all this then?](#whats-all-this-then) - [Usage](#usage-1) + - [Ignore certain state props](#ignore-certain-state-props) - [Bonus: Array mutations!](#bonus-array-mutations) - [3. Advanced configuration](#3-advanced-configuration) - [Vuex Easy Firestore integration for Google firebase](#vuex-easy-firestore-integration-for-google-firebase) @@ -43,6 +44,7 @@ npm i --save vuex-easy-access ### What you can do You can access and set anything in your store through `get()` and `set()` methods: + - `get(path)` will automatically look for a getter, if none it will return the state - `set(path, val)` will automatically look for an action, if none it will make a mutation @@ -54,10 +56,11 @@ The `path` syntax is: `module/submodule/stateVal.subProp`.
In the example below we'll see why the `get()` and `set()` methods are helpful. You have an app where users have themes and can set colors per theme.
-The module path is: user/theme
-The state: `colors: {primary: 'blue', secondary: 'white'}` +The module path is: `user/theme`
+`state: {colors: {primary: 'blue', secondary: 'white'}}` Simple usage allows: + - Set primary color: `this.$store.set('user/theme/colors.primary', '#265EF6')` - Get primary color: `this.$store.get('user/theme/colors.primary')` @@ -79,6 +82,7 @@ The `get()` method of Vuex Easy Access first checks if a getter with the syntax #### Scenario 2 Now we want to make an api call to the server every time the user updates this value. We would want to set up an action in our same module: + ```js // in the user/theme module actions: { @@ -88,6 +92,7 @@ actions: { } } ``` + Now inside your entire app, whenever `set('user/theme/colors.primary')` is called, the action above will be triggered and the color is synced to the server. The `set()` method of Vuex Easy Access checks to see if an action with the syntax `setProp` exist. If it exists it will dispatch this action, if not it will just make a default mutation: `commit('user/theme/SET_COLORS.PRIMARY', newColor)`. (Make sure you set up a mutation with the correct syntax above. To auto-generate mutations see [chapter 2](#2-automatically-generate-mutations-for-each-state-property).) @@ -96,6 +101,7 @@ The `set()` method of Vuex Easy Access checks to see if an action with the synta In cases you want to sync your vuex store with Firebase's Firestore see [chapter 3](#3-advanced-configuration). ### Usage + ```js import createEasyAccess from 'vuex-easy-access' // do the magic 🧙🏻‍♂️ @@ -120,18 +126,21 @@ Vuex Easy Access creates one mutation for every single property in your store! A #### What really happens? In line with the examples above:
-The module path is: user/theme
-The state: `colors: {primary: 'blue', secondary: 'white'}` +The module path is: `user/theme`
+`state: {colors: {primary: 'blue', secondary: 'white'}}` Vuex Easy Access will then automatically generate these mutations: + ```js store.commit('user/theme/SET_COLORS', newValue) store.commit('user/theme/SET_COLORS.PRIMARY', newValue) store.commit('user/theme/SET_COLORS.SECONDARY', newValue) ``` + And Vuex Easy Access does all this in just 2 lines. Say goodbye to boilerplating. Don't let it be a roadblock in order to do best practices! ### Usage + ```js import { defaultMutations } from 'vuex-easy-access' // in the root or a module's mutations: @@ -142,11 +151,19 @@ mutations: { } ``` +### Ignore certain state props + +Vuex Easy Access will ignore any props starting with an underscore.
+ eg. `state: {_privateProp: '...', normalProp: '...'}` + +This will make sure that **only** the mutation for `normalProp` is generated: `mutate('SET_NORMALPROP')` + ### Bonus: Array mutations! Yes, yes, I know. The fun just doesn't end. We also have them array mutations set for you!
-state: `pokemon: []` +`state: {pokemon: []}` Will become: + ```js // you caught a new pokemon: store.commit('PUSH_POKEMON', newPokemon) @@ -161,6 +178,7 @@ All these mutations are set up for you, automatically. You only need to write yo ## 3. Advanced configuration When you create your easyAccess plugin, you can make some configuration through an object: + ```js import createEasyAccess from 'vuex-easy-access' const configuration = {/* your configuration */} @@ -171,6 +189,7 @@ store: { plugins: [easyAccess] } ``` + All possible values for `configuration` are explained here: ### Vuex Easy Firestore integration for Google firebase @@ -187,6 +206,7 @@ This will make sure that whenever you set a value in a module that's auto-synced ### get() set() function names If for some reason you want to change the default function names for `store.get()` and `store.set()`, you can do so by passing an object to `createEasyAccess()` like so: + ```js const easyAccess = createEasyAccess({ getter: 'getIt', @@ -194,6 +214,7 @@ const easyAccess = createEasyAccess({ }) // and include as shown above ``` + Now you will only be able to use `store.getIt()` and `store.setIt()` instead. ## Feedback diff --git a/dist/index.cjs.js b/dist/index.cjs.js index 52500d8..acad8a5 100644 --- a/dist/index.cjs.js +++ b/dist/index.cjs.js @@ -151,6 +151,7 @@ var _extends = Object.assign || function (target) { function makeMutationsForAllProps(propParent, path) { if (!isWhat.isObject(propParent)) return {}; return Object.keys(propParent).reduce(function (mutations, prop) { + if (prop[0] === '_') return mutations; var propPath = !path ? prop : path + '.' + prop; var name = 'SET_' + propPath.toUpperCase(); mutations[name] = function (state, newVal) { diff --git a/dist/index.cjs.js.map b/dist/index.cjs.js.map index 10bfb72..d6e4484 100644 --- a/dist/index.cjs.js.map +++ b/dist/index.cjs.js.map @@ -1 +1 @@ -{"version":3,"file":"index.cjs.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter"],"mappings":";;;;;;AAEA;;;;;;AAMA,SAASA,eAAT,CAA0BC,IAA1B,EAAgC;MAC1B,CAACA,IAAL,EAAW,OAAO,EAAP;SACJA,KAAKC,KAAL,CAAW,MAAX,CAAP;;;;;;;;;;;AAWF,SAASC,UAAT,GAAwC;MAAnBC,MAAmB,uEAAV,EAAU;MAANH,IAAM;;MAClCI,OAAOL,gBAAgBC,IAAhB,CAAX;MACI,CAACI,KAAKC,MAAV,EAAkB,OAAOF,MAAP;MACdG,MAAMH,MAAV;SACOG,OAAOF,KAAKC,MAAL,GAAc,CAA5B,EAA+B;UACvBC,IAAIF,KAAKG,KAAL,EAAJ,CAAN;;MAEEC,MAAMJ,KAAKG,KAAL,EAAV;MACID,OAAOA,IAAIG,cAAJ,CAAmBD,GAAnB,CAAX,EAAoC;WAC3BF,IAAIE,GAAJ,CAAP;;;;;;;;;;;;AAYJ,SAASE,YAAT,CAAuBP,MAAvB,EAA+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;;AC5IF,oBAAe;UACL,KADK;UAEL,KAFK;qBAGM;CAHrB;;ACDA;;;;;;;;AAYA,SAASiE,gBAAT,CAA2BC,UAA3B,EAAuC;MAC/BC,OAAOvC,OAAOwC,MAAP,CAAcC,aAAd,EAA6BH,UAA7B,CAAb;MACMtB,oBAAoBuB,KAAKvB,iBAA/B;SACO,iBAAS;UACRuB,KAAKG,MAAX,IAAqB,UAACtE,IAAD,EAAO0C,OAAP,EAAmB;aAASD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;KAA1C;UACMuB,KAAKI,MAAX,IAAqB,UAACvE,IAAD,EAAU;aAAS8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;KAAjC;GAFF;;;;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.cjs.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter"],"mappings":";;;;;;AAEA;;;;;;AAMA,SAASA,eAAT,CAA0BC,IAA1B,EAAgC;MAC1B,CAACA,IAAL,EAAW,OAAO,EAAP;SACJA,KAAKC,KAAL,CAAW,MAAX,CAAP;;;;;;;;;;;AAWF,SAASC,UAAT,GAAwC;MAAnBC,MAAmB,uEAAV,EAAU;MAANH,IAAM;;MAClCI,OAAOL,gBAAgBC,IAAhB,CAAX;MACI,CAACI,KAAKC,MAAV,EAAkB,OAAOF,MAAP;MACdG,MAAMH,MAAV;SACOG,OAAOF,KAAKC,MAAL,GAAc,CAA5B,EAA+B;UACvBC,IAAIF,KAAKG,KAAL,EAAJ,CAAN;;MAEEC,MAAMJ,KAAKG,KAAL,EAAV;MACID,OAAOA,IAAIG,cAAJ,CA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qB,OAAN,CAAcvD,cAAd,CAA6BT,IAA7B,CAArB;MACI+D,YAAJ,EAAkB,OAAOpB,MAAMqB,OAAN,CAAchE,IAAd,CAAP;SACXU,aAAaiC,MAAMR,KAAnB,EAA0BnC,IAA1B,CAAP;;;AC7IF,oBAAe;UACL,KADK;UAEL,KAFK;qBAGM;CAHrB;;ACDA;;;;;;;;AAYA,SAASiE,gBAAT,CAA2BC,UAA3B,EAAuC;MAC/BC,OAAOvC,OAAOwC,MAAP,CAAcC,aAAd,EAA6BH,UAA7B,CAAb;MACMtB,oBAAoBuB,KAAKvB,iBAA/B;SACO,iBAAS;UACRuB,KAAKG,MAAX,IAAqB,UAACtE,IAAD,EAAO0C,OAAP,EAAmB;aAASD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;KAA1C;UACMuB,KAAKI,MAAX,IAAqB,UAACvE,IAAD,EAAU;aAAS8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;KAAjC;GAFF;;;;;;;;;;;"} \ No newline at end of file diff --git a/dist/index.cjs.min.js b/dist/index.cjs.min.js index 09e4691..fa2ccaf 100644 --- a/dist/index.cjs.min.js +++ b/dist/index.cjs.min.js @@ -1,2 +1,2 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var isWhat=require("is-what");function getKeysFromPath(e){return e?e.match(/\w+/g):[]}function getDeepRef(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=getKeysFromPath(arguments[1]);if(!t.length)return e;for(var r=e;r&&t.length>1;)r=r[t.shift()];var a=t.shift();return r&&r.hasOwnProperty(a)?r[a]:void 0}function getDeepValue(e,t){return getDeepRef(e,t)}function setDeepValue(e,t,r){var a=getKeysFromPath(t),s=a.pop(),n=getDeepRef(e,a.join());return n&&n.hasOwnProperty(s)&&(n[s]=r),e}function popDeepValue(e,t){var r=getDeepRef(e,t);if(isWhat.isArray(r))return r.pop()}function pushDeepValue(e,t,r){var a=getDeepRef(e,t);if(isWhat.isArray(a))return a.push(r)}function spliceDeepValue(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,s=arguments[4],n=getDeepRef(e,t);if(isWhat.isArray(n))return n.splice(r,a,s)}var _extends=Object.assign||function(e){for(var t=1;t0&&void 0!==arguments[0]?arguments[0]:{},t=getKeysFromPath(arguments[1]);if(!t.length)return e;for(var r=e;r&&t.length>1;)r=r[t.shift()];var a=t.shift();return r&&r.hasOwnProperty(a)?r[a]:void 0}function getDeepValue(e,t){return getDeepRef(e,t)}function setDeepValue(e,t,r){var a=getKeysFromPath(t),s=a.pop(),n=getDeepRef(e,a.join());return n&&n.hasOwnProperty(s)&&(n[s]=r),e}function popDeepValue(e,t){var r=getDeepRef(e,t);if(isWhat.isArray(r))return r.pop()}function pushDeepValue(e,t,r){var a=getDeepRef(e,t);if(isWhat.isArray(a))return a.push(r)}function spliceDeepValue(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,s=arguments[4],n=getDeepRef(e,t);if(isWhat.isArray(n))return n.splice(r,a,s)}var _extends=Object.assign||function(e){for(var t=1;t 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter"],"mappings":"kGAQA,SAASA,gBAAiBC,UACnBA,EACEA,EAAKC,MAAM,WAWpB,SAASC,iBAAYC,4DACfC,EAAOL,kCACNK,EAAKC,OAAQ,OAAOF,UACrBG,EAAMH,EACHG,GAAOF,EAAKC,OAAS,KACpBC,EAAIF,EAAKG,aAEbC,EAAMJ,EAAKG,eACXD,GAAOA,EAAIG,eAAeD,GACrBF,EAAIE,UAYf,SAASE,aAAcP,EAAQH,UACtBE,WAAWC,EAAQH,GAY5B,SAASW,aAAcR,EAAQH,EAAMY,OAC7BR,EAAOL,gBAAgBC,GACvBa,EAAUT,EAAKU,MACfC,EAAUb,WAAWC,EAAQC,EAAKY,eACpCD,GAAWA,EAAQN,eAAeI,OAC5BA,GAAWD,GAEdT,EAWT,SAASc,aAAcd,EAAQH,OACvBe,EAAUb,WAAWC,EAAQH,MAC9BkB,eAAQH,UACNA,EAAQD,MAWjB,SAASK,cAAehB,EAAQH,EAAMY,OAC9BG,EAAUb,WAAWC,EAAQH,MAC9BkB,eAAQH,UACNA,EAAQK,KAAKR,GAatB,SAASS,gBAAiBlB,E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S,EAAMe,OAAOD,EAAcf,GAiBpC,SAASiB,cAAe1D,EAAM0C,UACPA,EAAMiB,QAAQlD,eAAeT,GACzB0C,EAAMiB,QAAQ3D,GAChCU,aAAagC,EAAMR,MAAOlC,GC5InC,0BACU,aACA,yBACW,GCQrB,SAAS4D,iBAAkBC,OACnBC,EAAOlC,OAAOmC,OAAOC,cAAeH,GACpClB,EAAoBmB,EAAKnB,yBACxB,cACCmB,EAAKG,QAAU,SAACjE,EAAMyC,UAAqBD,cAAcxC,EAAMyC,EAASC,EAAOC,MAC/EmB,EAAKI,QAAU,SAAClE,UAAkB0D,cAAc1D,EAAM0C"} \ No newline at end of file +{"version":3,"file":"index.cjs.min.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter"],"mappings":"kGAQA,SAASA,gBAAiBC,UACnBA,EACEA,EAAKC,MAAM,WAWpB,SAASC,iBAAYC,4DACfC,EAAOL,kCACNK,EAAKC,OAAQ,OAAOF,UACrBG,EAAMH,EACHG,GAAOF,EAAKC,OAAS,KACpBC,EAAIF,EAAKG,aAEbC,EAAMJ,EAAKG,eACXD,GAAOA,EAAIG,eAAeD,GACrBF,EAAIE,UAYf,SAASE,aAAcP,EAAQH,UACtBE,WAAWC,EAAQH,GAY5B,SAASW,aAAcR,EAAQH,EAAMY,OAC7BR,EAAOL,gBAAgBC,GACvBa,EAAUT,EAAKU,MACfC,EAAUb,WAAWC,EAAQC,EAAKY,eACpCD,GAAWA,EAAQN,eAAeI,OAC5BA,GAAWD,GAEdT,EAWT,SAASc,aAAcd,EAAQH,OACvBe,EAAUb,WAAWC,EAAQH,MAC9BkB,eAAQH,UACNA,EAAQD,MAWjB,SAASK,cAAehB,EAAQH,EAAMY,OAC9B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ECW,IAAcG,EAAWT,GAASL,GACTC,EAAMQ,SAASI,UAEpCZ,EAAMS,SAASG,EAAqBC,OAGzCC,EAAeT,EAAa,OAASD,EAAMb,qBAC1CS,EAAMe,OAAOD,EAAcf,GAiBpC,SAASiB,cAAe1D,EAAM0C,UACPA,EAAMiB,QAAQlD,eAAeT,GACzB0C,EAAMiB,QAAQ3D,GAChCU,aAAagC,EAAMR,MAAOlC,GC7InC,0BACU,aACA,yBACW,GCQrB,SAAS4D,iBAAkBC,OACnBC,EAAOlC,OAAOmC,OAAOC,cAAeH,GACpClB,EAAoBmB,EAAKnB,yBACxB,cACCmB,EAAKG,QAAU,SAACjE,EAAMyC,UAAqBD,cAAcxC,EAAMyC,EAASC,EAAOC,MAC/EmB,EAAKI,QAAU,SAAClE,UAAkB0D,cAAc1D,EAAM0C"} \ No newline at end of file diff --git a/dist/index.es.js b/dist/index.es.js index 3de8f67..9b0f995 100644 --- a/dist/index.es.js +++ b/dist/index.es.js @@ -147,6 +147,7 @@ var _extends = Object.assign || function (target) { function makeMutationsForAllProps(propParent, path) { if (!isObject(propParent)) return {}; return Object.keys(propParent).reduce(function (mutations, prop) { + if (prop[0] === '_') return mutations; var propPath = !path ? prop : path + '.' + prop; var name = 'SET_' + propPath.toUpperCase(); mutations[name] = function (state, newVal) { diff --git a/dist/index.es.js.map b/dist/index.es.js.map index b5f2f15..fd81cb6 100644 --- a/dist/index.es.js.map +++ b/dist/index.es.js.map @@ -1 +1 @@ -{"version":3,"file":"index.es.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter"],"mappings":";;AAEA;;;;;;AAMA,SAASA,eAAT,CAA0BC,IAA1B,EAAgC;MAC1B,CAACA,IAAL,E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,MAAMU,QAAN,CAAeI,mBAAf,CAA9B;QACIE,qBAAJ,EAA2B;aAClBhB,MAAMW,QAAN,CAAeG,mBAAf,EAAoCC,UAApC,CAAP;;;MAGEE,eAAeZ,aAAa,MAAb,GAAsBD,MAAMb,WAAN,EAA3C;SACOS,MAAMkB,MAAN,CAAaD,YAAb,EAA2BlB,OAA3B,CAAP;;;;;;;;;;;;;;;;;AAiBF,SAASoB,aAAT,CAAwB9D,IAAxB,EAA8B2C,KAA9B,EAAqC;MAC7BoB,eAAepB,MAAMqB,OAAN,CAAcvD,cAAd,CAA6BT,IAA7B,CAArB;MACI+D,YAAJ,EAAkB,OAAOpB,MAAMqB,OAAN,CAAchE,IAAd,CAAP;SACXU,aAAaiC,MAAMR,KAAnB,EAA0BnC,IAA1B,CAAP;;;AC5IF,oBAAe;UACL,KADK;UAEL,KAFK;qBAGM;CAHrB;;ACDA;;;;;;;;AAYA,SAASiE,gBAAT,CAA2BC,UAA3B,EAAuC;MAC/BC,OAAOvC,OAAOwC,MAAP,CAAcC,aAAd,EAA6BH,UAA7B,CAAb;MACMtB,oBAAoBuB,KAAKvB,iBAA/B;SACO,iBAAS;UACRuB,KAAKG,MAAX,IAAqB,UAACtE,IAAD,EAAO0C,OAAP,EAAmB;aAASD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;KAA1C;UACMuB,KAAKI,MAAX,IAAqB,UAACvE,IAAD,EAAU;aAAS8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;KAAjC;GAFF;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.es.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","createEasyAccess","userConfig","conf","assign","defaultConf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O,MADiB,GAExBgD,aAAa,KAFjB;QAGMU,aAAcH,YAAD,GACfb,OADe,GAEf,EAFJ;QAGI,CAACa,YAAL,EAAmBG,WAAWX,KAAX,IAAoBL,OAApB;QACbiB,wBAAwBhB,MAAMU,QAAN,CAAeI,mBAAf,CAA9B;QACIE,qBAAJ,EAA2B;aAClBhB,MAAMW,QAAN,CAAeG,mBAAf,EAAoCC,UAApC,CAAP;;;MAGEE,eAAeZ,aAAa,MAAb,GAAsBD,MAAMb,WAAN,EAA3C;SACOS,MAAMkB,MAAN,CAAaD,YAAb,EAA2BlB,OAA3B,CAAP;;;;;;;;;;;;;;;;;AAiBF,SAASoB,aAAT,CAAwB9D,IAAxB,EAA8B2C,KAA9B,EAAqC;MAC7BoB,eAAepB,MAAMqB,OAAN,CAAcvD,cAAd,CAA6BT,IAA7B,CAArB;MACI+D,YAAJ,EAAkB,OAAOpB,MAAMqB,OAAN,CAAchE,IAAd,CAAP;SACXU,aAAaiC,MAAMR,KAAnB,EAA0BnC,IAA1B,CAAP;;;AC7IF,oBAAe;UACL,KADK;UAEL,KAFK;qBAGM;CAHrB;;ACDA;;;;;;;;AAYA,SAASiE,gBAAT,CAA2BC,UAA3B,EAAuC;MAC/BC,OAAOvC,OAAOwC,MAAP,CAAcC,aAAd,EAA6BH,UAA7B,CAAb;MACMtB,oBAAoBuB,KAAKvB,iBAA/B;SACO,iBAAS;UACRuB,KAAKG,MAAX,IAAqB,UAACtE,IAAD,EAAO0C,OAAP,EAAmB;aAASD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;KAA1C;UACMuB,KAAKI,MAAX,IAAqB,UAACvE,IAAD,EAAU;aAAS8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;KAAjC;GAFF;;;;;;"} \ No newline at end of file diff --git a/dist/index.es.min.js b/dist/index.es.min.js index c5421fa..9f59b95 100644 --- a/dist/index.es.min.js +++ b/dist/index.es.min.js @@ -1,2 +1,2 @@ -import{isArray,isObject}from"is-what";function getKeysFromPath(e){return e?e.match(/\w+/g):[]}function getDeepRef(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=getKeysFromPath(arguments[1]);if(!t.length)return e;for(var r=e;r&&t.length>1;)r=r[t.shift()];var n=t.shift();return r&&r.hasOwnProperty(n)?r[n]:void 0}function getDeepValue(e,t){return getDeepRef(e,t)}function setDeepValue(e,t,r){var n=getKeysFromPath(t),a=n.pop(),s=getDeepRef(e,n.join());return s&&s.hasOwnProperty(a)&&(s[a]=r),e}function popDeepValue(e,t){var r=getDeepRef(e,t);if(isArray(r))return r.pop()}function pushDeepValue(e,t,r){var n=getDeepRef(e,t);if(isArray(n))return n.push(r)}function spliceDeepValue(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,a=arguments[4],s=getDeepRef(e,t);if(isArray(s))return s.splice(r,n,a)}var _extends=Object.assign||function(e){for(var t=1;t0&&void 0!==arguments[0]?arguments[0]:{},t=getKeysFromPath(arguments[1]);if(!t.length)return e;for(var r=e;r&&t.length>1;)r=r[t.shift()];var n=t.shift();return r&&r.hasOwnProperty(n)?r[n]:void 0}function getDeepValue(e,t){return getDeepRef(e,t)}function setDeepValue(e,t,r){var n=getKeysFromPath(t),a=n.pop(),s=getDeepRef(e,n.join());return s&&s.hasOwnProperty(a)&&(s[a]=r),e}function popDeepValue(e,t){var r=getDeepRef(e,t);if(isArray(r))return r.pop()}function pushDeepValue(e,t,r){var n=getDeepRef(e,t);if(isArray(n))return n.push(r)}function spliceDeepValue(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,a=arguments[4],s=getDeepRef(e,t);if(isArray(s))return s.splice(r,n,a)}var _extends=Object.assign||function(e){for(var t=1;t 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter"],"mappings":"sCAQA,SAASA,gBAAiBC,UACnBA,EACEA,EAAKC,MAAM,WAWpB,SAASC,iBAAYC,4DACfC,EAAOL,kCACNK,EAAKC,OAAQ,OAAOF,UACrBG,EAAMH,EACHG,GAAOF,EAAKC,OAAS,KACpBC,EAAIF,EAAKG,aAEbC,EAAMJ,EAAKG,eACXD,GAAOA,EAAIG,eAAeD,GACrBF,EAAIE,UAYf,SAASE,aAAcP,EAAQH,UACtBE,W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OACrBuC,EAAK5B,KAAK,KAAO,IACjB,GAIEgC,EAAaD,GAFA,MAAQD,EAAM,GAAGb,cAAgBa,EAAMG,UAAU,OAI/CP,EAAMQ,SAASF,UAE3BN,EAAMS,SAASH,EAAYP,MAEhCE,EAAmB,KAEfS,EAAeV,EAAMW,qBAAqBrD,EAAO,KACjDsD,EAAuBF,EACzBpD,EAAO,OACP+C,EAAa,MACXQ,EAAcH,EAChBX,QAECW,IAAcG,EAAWT,GAASL,GACTC,EAAMQ,SAASI,UAEpCZ,EAAMS,SAASG,EAAqBC,OAGzCC,EAAeT,EAAa,OAASD,EAAMb,qBAC1CS,EAAMe,OAAOD,EAAcf,GAiBpC,SAASiB,cAAe1D,EAAM0C,UACPA,EAAMiB,QAAQlD,eAAeT,GACzB0C,EAAMiB,QAAQ3D,GAChCU,aAAagC,EAAMR,MAAOlC,GC5InC,0BACU,aACA,yBACW,GCQrB,SAAS4D,iBAAkBC,OACnBC,EAAOlC,OAAOmC,OAAOC,cAAeH,GACpClB,EAAoBmB,EAAKnB,yBACxB,cACCmB,EAAKG,QAAU,SAACjE,EAAMyC,UAAqBD,cAAcxC,EAAMyC,EAASC,EAAOC,MAC/EmB,EAAKI,QAAU,SAAClE,UAAkB0D,cAAc1D,EAAM0C"} \ No newline at end of file +{"version":3,"file":"index.es.min.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter"],"mappings":"sCAQA,SAASA,gBAAiBC,UACnBA,EACEA,EAAKC,MAAM,WAWpB,SAASC,iBAAYC,4DACfC,EAAOL,kCACNK,EAAKC,OAAQ,OAAOF,UACrBG,EAAMH,EACHG,GAAOF,EAAKC,OAAS,KACpBC,EAAIF,EAAKG,aAEbC,EAAMJ,EAA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SAASC,cAAexC,EAAMyC,EAASC,EAAOC,OAEtCC,EAAO5C,EAAK6C,MAAM,KAElBC,EAAQF,EAAK9B,MAEbiC,EAAcH,EAAKvC,OACrBuC,EAAK5B,KAAK,KAAO,IACjB,GAIEgC,EAAaD,GAFA,MAAQD,EAAM,GAAGb,cAAgBa,EAAMG,UAAU,OAI/CP,EAAMQ,SAASF,UAE3BN,EAAMS,SAASH,EAAYP,MAEhCE,EAAmB,KAEfS,EAAeV,EAAMW,qBAAqBrD,EAAO,KACjDsD,EAAuBF,EACzBpD,EAAO,OACP+C,EAAa,MACXQ,EAAcH,EAChBX,QAECW,IAAcG,EAAWT,GAASL,GACTC,EAAMQ,SAASI,UAEpCZ,EAAMS,SAASG,EAAqBC,OAGzCC,EAAeT,EAAa,OAASD,EAAMb,qBAC1CS,EAAMe,OAAOD,EAAcf,GAiBpC,SAASiB,cAAe1D,EAAM0C,UACPA,EAAMiB,QAAQlD,eAAeT,GACzB0C,EAAMiB,QAAQ3D,GAChCU,aAAagC,EAAMR,MAAOlC,GC7InC,0BACU,aACA,yBACW,GCQrB,SAAS4D,iBAAkBC,OACnBC,EAAOlC,OAAOmC,OAAOC,cAAeH,GACpClB,EAAoBmB,EAAKnB,yBACxB,cACCmB,EAAKG,QAAU,SAACjE,EAAMyC,UAAqBD,cAAcxC,EAAMyC,EAASC,EAAOC,MAC/EmB,EAAKI,QAAU,SAAClE,UAAkB0D,cAAc1D,EAAM0C"} \ No newline at end of file diff --git a/dist/index.iife.js b/dist/index.iife.js index 545e149..2056029 100644 --- a/dist/index.iife.js +++ b/dist/index.iife.js @@ -148,6 +148,7 @@ var VuexEasyAccess = (function (exports,isWhat) { function makeMutationsForAllProps(propParent, path) { if (!isWhat.isObject(propParent)) return {}; return Object.keys(propParent).reduce(function (mutations, prop) { + if (prop[0] === '_') return mutations; var propPath = !path ? prop : path + '.' + prop; var name = 'SET_' + propPath.toUpperCase(); mutations[name] = function (state, newVal) { diff --git a/dist/index.iife.js.map b/dist/index.iife.js.map index 231460f..181ca72 100644 --- a/dist/index.iife.js.map +++ b/dist/index.iife.js.map @@ -1 +1 @@ -{"version":3,"file":"index.iife.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","setter","getter","createEasyAccess","userConfig","conf","assign","defaultConfig"],"mappings":";;;EAEA;;;;;;EAMA,SAASA,eAAT,CAA0BC,IAA1B,EAAgC;EAC9B,MAAI,CAACA,IAAL,EAAW,OAAO,EAAP;EACX,SAAOA,KAAKC,KAAL,CAAW,MAAX,CAAP;EACD;;EAED;;;;;;;;EAQA,SAASC,UAAT,GAAwC;EAAA,MAAnBC,MAAmB,uEAAV,EAAU;EAAA,MAANH,IAAM;;EACtC,MAAII,OAAOL,gBAAgBC,IAAhB,CAAX;EACA,MAAI,CAACI,KAAKC,MAAV,EAAkB,OAAOF,MAAP;EAClB,MAAIG,MAAMH,MAAV;EACA,SAAOG,OAAOF,KAAKC,MAAL,GAAc,CAA5B,EAA+B;EAC7BC,UAAMA,IAAIF,KAAKG,KAAL,EAAJ,CAAN;EACD;EACD,MAAIC,MAAMJ,KAAKG,KAAL,EAAV;EACA,MAAID,OAAOA,IAAIG,cAAJ,CAAmBD,GAAnB,CAAX,EAAoC;EAClC,WAAOF,IAAIE,GAAJ,CAAP;EACD;EACF;;EAED;;;;;;;;EAQA,SAASE,YAAT,CAAuBP,MAAvB,EAA+BH,IAA/B,EAAqC;EACnC,SAAOE,WAAWC,MAAX,EAAmBH,IAAnB,CAAP;EACD;;EAED;;;;;;;;;EASA,SAASW,YAAT,CAAuBR,MAAvB,EAA+BH,IAA/B,EAAqCY,KAArC,EAA4C;EAC1C,MAAMR,OAAOL,gBAAgBC,IAAhB,CAAb;EACA,MAAMa,UAAUT,KAAKU,GAAL,EAAhB;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;;;;;;;;;;EAcA,SAASoB,aAAT,CAAwB9D,IAAxB,EAA8B2C,KAA9B,EAAqC;EACnC,MAAMoB,eAAepB,MAAMqB,OAAN,CAAcvD,cAAd,CAA6BT,IAA7B,CAArB;EACA,MAAI+D,YAAJ,EAAkB,OAAOpB,MAAMqB,OAAN,CAAchE,IAAd,CAAP;EAClB,SAAOU,aAAaiC,MAAMR,KAAnB,EAA0BnC,IAA1B,CAAP;EACD;;AC7ID,sBAAe;EACbiE,UAAQ,KADK;EAEbC,UAAQ,KAFK;EAGbtB,qBAAmB;EAHN,CAAf;;ECDA;;;;;;;;EAYA,SAASuB,gBAAT,CAA2BC,UAA3B,EAAuC;EACrC,MAAMC,OAAOzC,OAAO0C,MAAP,CAAcC,aAAd,EAA6BH,UAA7B,CAAb;EACA,MAAMxB,oBAAoByB,KAAKzB,iBAA/B;EACA,SAAO,iBAAS;EACdD,UAAM0B,KAAKJ,MAAX,IAAqB,UAACjE,IAAD,EAAO0C,OAAP,EAAmB;EAAE,aAAOD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;EAA+D,KAAzG;EACAD,UAAM0B,KAAKH,MAAX,IAAqB,UAAClE,IAAD,EAAU;EAAE,aAAO8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;EAAmC,KAApE;EACD,GAHD;EAID;;;;;;;;;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.iife.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","setter","getter","createEasyAccess","userConfig","conf","assign","defaultConfig"],"mappings":";;;EAEA;;;;;;EAMA,SAASA,eAAT,CAA0BC,IAA1B,EAAgC;EAC9B,MAAI,CAACA,IAAL,EAAW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N,CAAf;;ECDA;;;;;;;;EAYA,SAASuB,gBAAT,CAA2BC,UAA3B,EAAuC;EACrC,MAAMC,OAAOzC,OAAO0C,MAAP,CAAcC,aAAd,EAA6BH,UAA7B,CAAb;EACA,MAAMxB,oBAAoByB,KAAKzB,iBAA/B;EACA,SAAO,iBAAS;EACdD,UAAM0B,KAAKJ,MAAX,IAAqB,UAACjE,IAAD,EAAO0C,OAAP,EAAmB;EAAE,aAAOD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;EAA+D,KAAzG;EACAD,UAAM0B,KAAKH,MAAX,IAAqB,UAAClE,IAAD,EAAU;EAAE,aAAO8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;EAAmC,KAApE;EACD,GAHD;EAID;;;;;;;;;;;;;;;;"} \ No newline at end of file diff --git a/dist/index.iife.min.js b/dist/index.iife.min.js index fa9a381..4b752b5 100644 --- a/dist/index.iife.min.js +++ b/dist/index.iife.min.js @@ -1,2 +1,2 @@ -var VuexEasyAccess=function(t,r){"use strict";function e(t){return t?t.match(/\w+/g):[]}function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e(arguments[1]);if(!r.length)return t;for(var n=t;n&&r.length>1;)n=n[r.shift()];var i=r.shift();return n&&n.hasOwnProperty(i)?n[i]:void 0}var i=Object.assign||function(t){for(var r=1;r2&&void 0!==arguments[2]?arguments[2]:0,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,s=arguments[4],u=n(t,e);if(r.isArray(u))return u.splice(i,a,s)}(t,c,e,i,a)});return u},{}):{}}function s(t,r,e,n){var i=t.split("/"),a=i.pop(),s=i.length?i.join("/")+"/":"",u=s+("set"+a[0].toUpperCase()+a.substring(1));if(e._actions[u])return e.dispatch(u,r);if(n){var o=e._modulesNamespaceMap[t+"/"],c=o?t+"/set":s+"set",f=o?r:{};if(o||(f[a]=r),e._actions[c])return e.dispatch(c,f)}var p=s+"SET_"+a.toUpperCase();return e.commit(p,r)}function u(t,r){return r.getters.hasOwnProperty(t)?r.getters[t]:function(t,r){return n(t,r)}(r.state,t)}var o={setter:"set",getter:"get",vuexEasyFirestore:!1};function c(t){var r=Object.assign(o,t),e=r.vuexEasyFirestore;return function(t){t[r.setter]=function(r,n){return s(r,n,t,e)},t[r.getter]=function(r){return u(r,t)}}}return t.default=c,t.createEasyAccess=c,t.defaultMutations=function(t){return a(t)},t.defaultSetter=s,t.defaultGetter=u,t.getDeepRef=n,t.getKeysFromPath=e,t}({},isWhat); +var VuexEasyAccess=function(t,r){"use strict";function e(t){return t?t.match(/\w+/g):[]}function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=e(arguments[1]);if(!r.length)return t;for(var n=t;n&&r.length>1;)n=n[r.shift()];var i=r.shift();return n&&n.hasOwnProperty(i)?n[i]:void 0}var i=Object.assign||function(t){for(var r=1;r2&&void 0!==arguments[2]?arguments[2]:0,a=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,s=arguments[4],u=n(t,e);if(r.isArray(u))return u.splice(i,a,s)}(t,c,e,i,a)});return u},{}):{}}function s(t,r,e,n){var i=t.split("/"),a=i.pop(),s=i.length?i.join("/")+"/":"",u=s+("set"+a[0].toUpperCase()+a.substring(1));if(e._actions[u])return e.dispatch(u,r);if(n){var o=e._modulesNamespaceMap[t+"/"],c=o?t+"/set":s+"set",f=o?r:{};if(o||(f[a]=r),e._actions[c])return e.dispatch(c,f)}var p=s+"SET_"+a.toUpperCase();return e.commit(p,r)}function u(t,r){return r.getters.hasOwnProperty(t)?r.getters[t]:function(t,r){return n(t,r)}(r.state,t)}var o={setter:"set",getter:"get",vuexEasyFirestore:!1};function c(t){var r=Object.assign(o,t),e=r.vuexEasyFirestore;return function(t){t[r.setter]=function(r,n){return s(r,n,t,e)},t[r.getter]=function(r){return u(r,t)}}}return t.default=c,t.createEasyAccess=c,t.defaultMutations=function(t){return a(t)},t.defaultSetter=s,t.defaultGetter=u,t.getDeepRef=n,t.getKeysFromPath=e,t}({},isWhat); //# sourceMappingURL=index.iife.min.js.map diff --git a/dist/index.iife.min.js.map b/dist/index.iife.min.js.map index e7573e0..5e5d265 100644 --- a/dist/index.iife.min.js.map +++ b/dist/index.iife.min.js.map @@ -1 +1 @@ -{"version":3,"file":"index.iife.min.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","value","lastKey","pop","deepRef","join","propValue","childrenMutations","isArray","popDeepValue","push","pushDeepValue","index","deleteCount","splice","spliceDeepValue","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","getDeepValue","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter","initialState"],"mappings":"8CAQA,SAASA,EAAiBC,UACnBA,EACEA,EAAKC,MAAM,WAWpB,SAASC,QAAYC,4DACfC,EAAOL,oBACNK,EAAKC,OAAQ,OAAOF,UACrBG,EAAMH,EACHG,GAAOF,EAAKC,OAAS,KACpBC,EAAIF,EAAKG,aAEbC,EAAMJ,EAAKG,eACXD,GAAOA,EAAIG,eAAeD,GACrBF,EAAIE,kLCXf,SAASE,EAAyBC,EAAYX,UACvCY,WAASD,GACPE,OAAOT,KAAKO,GAClBG,OAAO,SAACC,EAAWC,OACdC,EAAajB,EAEbA,EAAO,IAAMgB,EADbA,IAEO,OAASC,EAASC,eACX,SAACC,EAAOC,UD4BPjB,EC3BGgB,ED2BWE,EC3BMD,ED4BnChB,EAAOL,EC5BkBkB,GD6BzBK,EAAUlB,EAAKmB,OACfC,EAAUtB,EAAWC,EAAQC,EAAKqB,UACzBD,EAAQf,eAAea,OAC5BA,GAAWD,GAEdlB,EAPT,IAAuBA,EAAckB,EAC7BjB,EACAkB,EACAE,OC5BAE,EAAYf,EAAWK,MAEvBJ,WAASc,GAAY,KACnBC,EAAoBjB,EAAyBgB,EAAWT,UAC5CF,EAAcY,GAG5BC,UAAQF,OACA,OAAST,EAASC,eACX,SAACC,UDkCxB,SAAuBhB,EAAQH,OACvBwB,EAAUtB,EAAWC,EAAQH,MAC9B4B,UAAQJ,UACNA,EAAQD,MCpCFM,C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,EAAeH,GACpCnB,EAAoBoB,EAAKpB,yBACxB,cACCoB,EAAKG,QAAU,SAAC9D,EAAMqC,UAAqBD,EAAcpC,EAAMqC,EAASC,EAAOC,MAC/EoB,EAAKI,QAAU,SAAC/D,UAAkBsD,EAActD,EAAMsC,gEFmDhE,SAA2B0B,UAClBtD,EAAyBsD"} \ No newline at end of file +{"version":3,"file":"index.iife.min.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","value","lastKey","pop","deepRef","join","propValue","childrenMutations","isArray","popDeepValue","push","pushDeepValue","index","deleteCount","splice","spliceDeepValue","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","getDeepValue","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter","initialState"],"mappings":"8CAQA,SAASA,EAAiBC,UACnBA,EACEA,EAAKC,MAAM,WAWpB,SAASC,QAAYC,4DACfC,EAAOL,oBACNK,EAAKC,OAAQ,OAAOF,UACrBG,EAAMH,EACHG,GAAOF,EAAKC,OAAS,KACpBC,EAAIF,EAAKG,aAEbC,EAAMJ,EAAKG,eACXD,GAAOA,EAAIG,eAAeD,GACrBF,EAAIE,kLCXf,SAASE,EAAyBC,EAAYX,UACvCY,WAASD,GACPE,OAAOT,KAAKO,GAClBG,OAAO,SAACC,EAAWC,MACF,MAAZA,EAAK,GAAY,OAAOD,MACxBE,EAAajB,EAEbA,EAAO,IAAMgB,EADbA,IAEO,OAASC,EAASC,eACX,SAACC,EAAOC,UD2BPjB,EC1BGgB,ED0BWE,EC1BMD,ED2BnChB,EAAOL,EC3BkBkB,GD4BzBK,EAAUlB,EAAKmB,OACfC,EAAUtB,EAAWC,EAAQC,EAAKqB,UACzBD,EAAQf,eAAea,OAC5BA,GAAWD,GAEdlB,EAPT,IAAuBA,EAAckB,EAC7BjB,EACAkB,EACAE,OC3BAE,EAAYf,EAAWK,MAEvBJ,WAASc,GAAY,KACnBC,EAAoBjB,EAAyBgB,EAAWT,UAC5CF,EAAcY,GAG5BC,UAAQF,OACA,OAAST,EAASC,eACX,S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BwD,CAAalB,EAAMnB,MAAOnB,iBC5IzB,aACA,yBACW,GCQrB,SAASyD,EAAkBC,OACnBC,EAAO9C,OAAO+C,OAAOC,EAAeH,GACpCnB,EAAoBoB,EAAKpB,yBACxB,cACCoB,EAAKG,QAAU,SAAC9D,EAAMqC,UAAqBD,EAAcpC,EAAMqC,EAASC,EAAOC,MAC/EoB,EAAKI,QAAU,SAAC/D,UAAkBsD,EAActD,EAAMsC,gEFoDhE,SAA2B0B,UAClBtD,EAAyBsD"} \ No newline at end of file diff --git a/dist/index.umd.js b/dist/index.umd.js index 3906308..71adabd 100644 --- a/dist/index.umd.js +++ b/dist/index.umd.js @@ -151,6 +151,7 @@ function makeMutationsForAllProps(propParent, path) { if (!isWhat.isObject(propParent)) return {}; return Object.keys(propParent).reduce(function (mutations, prop) { + if (prop[0] === '_') return mutations; var propPath = !path ? prop : path + '.' + prop; var name = 'SET_' + propPath.toUpperCase(); mutations[name] = function (state, newVal) { diff --git a/dist/index.umd.js.map b/dist/index.umd.js.map index 1235f74..cd69f77 100644 --- a/dist/index.umd.js.map +++ b/dist/index.umd.js.map @@ -1 +1 @@ -{"version":3,"file":"index.umd.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","setter","getter","createEasyAccess","userConfig","conf","assign","defaultConfig"],"mappings":";;;;;;EAEA;;;;;;EAMA,SAASA,eAAT,CAA0BC,IAA1B,EAAgC;EAC9B,MAAI,CAACA,IAAL,EAAW,OAAO,EAAP;EACX,SAAOA,KAAKC,KAAL,CAAW,MAAX,CAAP;EACD;;EAED;;;;;;;;EAQA,SAASC,UAAT,GAAwC;EAAA,MAAnBC,MAAmB,uEAAV,EAAU;EAAA,MAANH,IAAM;;EACtC,MAAII,OAAOL,gBAAgBC,IAAhB,CA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,OAAP,EAAmB;EAAE,aAAOD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;EAA+D,KAAzG;EACAD,UAAM0B,KAAKH,MAAX,IAAqB,UAAClE,IAAD,EAAU;EAAE,aAAO8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;EAAmC,KAApE;EACD,GAHD;EAID;;;;;;;;;;;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"index.umd.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","getDeepValue","setDeepValue","value","lastKey","pop","deepRef","join","popDeepValue","isArray","pushDeepValue","push","spliceDeepValue","index","deleteCount","splice","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","name","toUpperCase","state","newVal","propValue","childrenMutations","defaultMutations","initialState","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionName","substring","actionPath","actionExists","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","firestoreActionExists","mutationPath","commit","defaultGetter","getterExists","getters","setter","getter","createEasyAccess","userConfig","conf","assign","defaultConfig"],"mappings":";;;;;;EAEA;;;;;;EAMA,SAASA,eAAT,CAA0BC,IAA1B,EAAgC;EAC9B,MAAI,CAACA,IAAL,EAAW,OAAO,EAAP;EACX,SAAOA,KAAKC,KAAL,CAAW,MAAX,CAAP;EACD;;EAED;;;;;;;;EAQA,SAASC,UAAT,GAAwC;EAAA,MAAnBC,MAAmB,uEAAV,EAAU;EAAA,MAANH,IAAM;;EACtC,MAAII,OAAOL,gBAAgBC,IAAhB,CAAX;EACA,MAAI,CAACI,KAAKC,MAAV,EAAkB,OAAOF,MAAP;EAClB,MAAIG,MAAMH,MAAV;EACA,SAAOG,OAAOF,KAAKC,MAAL,GAAc,CAA5B,EAA+B;EAC7BC,UAAMA,IAAIF,KAAKG,KAAL,EAAJ,CAAN;EACD;EACD,MAAIC,MAAMJ,KAAKG,KAAL,EAAV;EACA,MAAID,OAAOA,IAAIG,cAAJ,CAAmBD,GAAnB,CAAX,EAAoC;EAClC,WAAOF,IAAIE,GAAJ,CAAP;EACD;EACF;;EAED;;;;;;;;EAQA,SAASE,YAAT,CAAuBP,MAAvB,EAA+BH,IAA/B,EAAqC;EACn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wBhB,MAAMU,QAAN,CAAeI,mBAAf,CAA9B;EACA,QAAIE,qBAAJ,EAA2B;EACzB,aAAOhB,MAAMW,QAAN,CAAeG,mBAAf,EAAoCC,UAApC,CAAP;EACD;EACF;EACD,MAAME,eAAeZ,aAAa,MAAb,GAAsBD,MAAMb,WAAN,EAA3C;EACA,SAAOS,MAAMkB,MAAN,CAAaD,YAAb,EAA2BlB,OAA3B,CAAP;EACD;;EAED;;;;;;;;;;;;;;EAcA,SAASoB,aAAT,CAAwB9D,IAAxB,EAA8B2C,KAA9B,EAAqC;EACnC,MAAMoB,eAAepB,MAAMqB,OAAN,CAAcvD,cAAd,CAA6BT,IAA7B,CAArB;EACA,MAAI+D,YAAJ,EAAkB,OAAOpB,MAAMqB,OAAN,CAAchE,IAAd,CAAP;EAClB,SAAOU,aAAaiC,MAAMR,KAAnB,EAA0BnC,IAA1B,CAAP;EACD;;AC9ID,sBAAe;EACbiE,UAAQ,KADK;EAEbC,UAAQ,KAFK;EAGbtB,qBAAmB;EAHN,CAAf;;ECDA;;;;;;;;EAYA,SAASuB,gBAAT,CAA2BC,UAA3B,EAAuC;EACrC,MAAMC,OAAOzC,OAAO0C,MAAP,CAAcC,aAAd,EAA6BH,UAA7B,CAAb;EACA,MAAMxB,oBAAoByB,KAAKzB,iBAA/B;EACA,SAAO,iBAAS;EACdD,UAAM0B,KAAKJ,MAAX,IAAqB,UAACjE,IAAD,EAAO0C,OAAP,EAAmB;EAAE,aAAOD,cAAczC,IAAd,EAAoB0C,OAApB,EAA6BC,KAA7B,EAAoCC,iBAApC,CAAP;EAA+D,KAAzG;EACAD,UAAM0B,KAAKH,MAAX,IAAqB,UAAClE,IAAD,EAAU;EAAE,aAAO8D,cAAc9D,IAAd,EAAoB2C,KAApB,CAAP;EAAmC,KAApE;EACD,GAHD;EAID;;;;;;;;;;;;;;;;;;"} \ No newline at end of file diff --git a/dist/index.umd.min.js b/dist/index.umd.min.js index 4e93af4..8684ed9 100644 --- a/dist/index.umd.min.js +++ b/dist/index.umd.min.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("is-what")):"function"==typeof define&&define.amd?define(["exports","is-what"],e):e(t.VuexEasyAccess={},t.isWhat)}(this,function(t,e){"use strict";function r(t){return t?t.match(/\w+/g):[]}function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=r(arguments[1]);if(!e.length)return t;for(var n=t;n&&e.length>1;)n=n[e.shift()];var i=e.shift();return n&&n.hasOwnProperty(i)?n[i]:void 0}var i=Object.assign||function(t){for(var e=1;e2&&void 0!==arguments[2]?arguments[2]:0,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,s=arguments[4],u=n(t,r);if(e.isArray(u))return u.splice(i,o,s)}(t,f,r,i,o)});return u},{}):{}}function s(t,e,r,n){var i=t.split("/"),o=i.pop(),s=i.length?i.join("/")+"/":"",u=s+("set"+o[0].toUpperCase()+o.substring(1));if(r._actions[u])return r.dispatch(u,e);if(n){var a=r._modulesNamespaceMap[t+"/"],f=a?t+"/set":s+"set",c=a?e:{};if(a||(c[o]=e),r._actions[f])return r.dispatch(f,c)}var p=s+"SET_"+o.toUpperCase();return r.commit(p,e)}function u(t,e){return e.getters.hasOwnProperty(t)?e.getters[t]:function(t,e){return n(t,e)}(e.state,t)}var a={setter:"set",getter:"get",vuexEasyFirestore:!1};function f(t){var e=Object.assign(a,t),r=e.vuexEasyFirestore;return function(t){t[e.setter]=function(e,n){return s(e,n,t,r)},t[e.getter]=function(e){return u(e,t)}}}t.default=f,t.createEasyAccess=f,t.defaultMutations=function(t){return o(t)},t.defaultSetter=s,t.defaultGetter=u,t.getDeepRef=n,t.getKeysFromPath=r,Object.defineProperty(t,"__esModule",{value:!0})}); +!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("is-what")):"function"==typeof define&&define.amd?define(["exports","is-what"],e):e(t.VuexEasyAccess={},t.isWhat)}(this,function(t,e){"use strict";function r(t){return t?t.match(/\w+/g):[]}function n(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=r(arguments[1]);if(!e.length)return t;for(var n=t;n&&e.length>1;)n=n[e.shift()];var i=e.shift();return n&&n.hasOwnProperty(i)?n[i]:void 0}var i=Object.assign||function(t){for(var e=1;e2&&void 0!==arguments[2]?arguments[2]:0,o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,s=arguments[4],u=n(t,r);if(e.isArray(u))return u.splice(i,o,s)}(t,f,r,i,o)});return u},{}):{}}function s(t,e,r,n){var i=t.split("/"),o=i.pop(),s=i.length?i.join("/")+"/":"",u=s+("set"+o[0].toUpperCase()+o.substring(1));if(r._actions[u])return r.dispatch(u,e);if(n){var a=r._modulesNamespaceMap[t+"/"],f=a?t+"/set":s+"set",c=a?e:{};if(a||(c[o]=e),r._actions[f])return r.dispatch(f,c)}var p=s+"SET_"+o.toUpperCase();return r.commit(p,e)}function u(t,e){return e.getters.hasOwnProperty(t)?e.getters[t]:function(t,e){return n(t,e)}(e.state,t)}var a={setter:"set",getter:"get",vuexEasyFirestore:!1};function f(t){var e=Object.assign(a,t),r=e.vuexEasyFirestore;return function(t){t[e.setter]=function(e,n){return s(e,n,t,r)},t[e.getter]=function(e){return u(e,t)}}}t.default=f,t.createEasyAccess=f,t.defaultMutations=function(t){return o(t)},t.defaultSetter=s,t.defaultGetter=u,t.getDeepRef=n,t.getKeysFromPath=r,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=index.umd.min.js.map diff --git a/dist/index.umd.min.js.map b/dist/index.umd.min.js.map index 6296a37..47b143c 100644 --- a/dist/index.umd.min.js.map +++ b/dist/index.umd.min.js.map @@ -1 +1 @@ -{"version":3,"file":"index.umd.min.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","value","lastKey","pop","deepRef","join","propValue","childrenMutations","isArray","popDeepValue","push","pushDeepValue","index","deleteCount","splice","spliceDeepValue","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","getDeepValue","createEasyAccess","userConfig","conf","assign","defaultConfig","setter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ACNA,EAAQU,OAAOF,EAAOC,EAAaZ,GC1D7Bc,CAAgBhB,EAAOF,EAAUe,EAAOC,EAAaZ,YAGzDN,UAoCX,SAASqB,EAAepC,EAAMqC,EAASC,EAAOC,OAEtCC,EAAOxC,EAAKyC,MAAM,KAElBC,EAAQF,EAAKjB,MAEboB,EAAcH,EAAKnC,OACrBmC,EAAKf,KAAK,KAAO,IACjB,GAIEmB,EAAaD,GAFA,MAAQD,EAAM,GAAGxB,cAAgBwB,EAAMG,UAAU,OAI/CP,EAAMQ,SAASF,UAE3BN,EAAMS,SAASH,EAAYP,MAEhCE,EAAmB,KAEfS,EAAeV,EAAMW,qBAAqBjD,EAAO,KACjDkD,EAAuBF,EACzBhD,EAAO,OACP2C,EAAa,MACXQ,EAAcH,EAChBX,QAECW,IAAcG,EAAWT,GAASL,GACTC,EAAMQ,SAASI,UAEpCZ,EAAMS,SAASG,EAAqBC,OAGzCC,EAAeT,EAAa,OAASD,EAAMxB,qBAC1CoB,EAAMe,OAAOD,EAAcf,GAiBpC,SAASiB,EAAetD,EAAMsC,UACPA,EAAMiB,QAAQ9C,eAAeT,GACzBsC,EAAMiB,QAAQvD,GDlGzC,SAAuBG,EAAQH,UACtBE,EAAWC,EAAQH,GCkGnBwD,CAAalB,EAAMnB,MAAOnB,iBC3IzB,aACA,yBACW,GCQrB,SAASyD,EAAkBC,OACnBC,EAAO9C,OAAO+C,OAAOC,EAAeH,GACpCnB,EAAoBoB,EAAKpB,yBACxB,cACCoB,EAAKG,QAAU,SAAC9D,EAAMqC,UAAqBD,EAAcpC,EAAMqC,EAASC,EAAOC,MAC/EoB,EAAKI,QAAU,SAAC/D,UAAkBsD,EAActD,EAAMsC,yDFmDhE,SAA2B0B,UAClBtD,EAAyBsD"} \ No newline at end of file +{"version":3,"file":"index.umd.min.js","sources":["../src/objectDeepValueUtils.js","../src/storeAccess.js","../src/defaultConfig.js","../src/index.js"],"sourcesContent":["import { isArray } from 'is-what'\n\n/**\n * Returns the keys of a path\n *\n * @param {string} path a/path/like.this\n * @returns {array} with keys\n */\nfunction getKeysFromPath (path) {\n if (!path) return []\n return path.match(/\\w+/g)\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target an object to wherefrom to retrieve the deep reference of\n * @param {string} path 'path/to.prop'\n *\n * @returns {object} the property which was requested\n */\nfunction getDeepRef (target = {}, path) {\n let keys = getKeysFromPath(path)\n if (!keys.length) return target\n let obj = target\n while (obj && keys.length > 1) {\n obj = obj[keys.shift()]\n }\n let key = keys.shift()\n if (obj && obj.hasOwnProperty(key)) {\n return obj[key]\n }\n}\n\n/**\n * Gets a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to get the value of\n * @param {string} path 'path/to/prop.subprop'\n *\n * @returns {object} the property's value\n */\nfunction getDeepValue (target, path) {\n return getDeepRef(target, path)\n}\n\n/**\n * Sets a value to a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to/prop.subprop'\n * @param {*} value the value to set\n *\n * @returns {object} the original target object\n */\nfunction setDeepValue (target, path, value) {\n const keys = getKeysFromPath(path)\n const lastKey = keys.pop()\n const deepRef = getDeepRef(target, keys.join())\n if (deepRef && deepRef.hasOwnProperty(lastKey)) {\n deepRef[lastKey] = value\n }\n return target\n}\n\n/**\n * Pops a value of an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path.to.sub.prop'\n *\n * @returns {*} the popped value\n */\nfunction popDeepValue (target, path) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.pop()\n}\n/**\n * Pushes a value in an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to set\n *\n * @returns {number} the new length of the array\n */\nfunction pushDeepValue (target, path, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.push(value)\n}\n/**\n * Splice into an array which is a deep property in an object, based on a path to that property\n *\n * @param {object} target the Object to set the value on\n * @param {string} path 'path/to.sub.prop'\n * @param {*} value the value to splice in\n * @param {number} index the index to splice in the value, defaults to 0\n * @param {number} deleteCount the amount of items to delete, defaults to 0\n *\n * @returns {array} an array containing the deleted elements\n */\nfunction spliceDeepValue (target, path, index = 0, deleteCount = 0, value) {\n const deepRef = getDeepRef(target, path)\n if (!isArray(deepRef)) return\n return deepRef.splice(index, deleteCount, value)\n}\n\nexport { getDeepRef, getKeysFromPath, setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue }\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { setDeepValue, getDeepValue, popDeepValue, pushDeepValue, spliceDeepValue } from './objectDeepValueUtils'\nimport { isObject, isArray } from 'is-what'\n\n/**\n * Creates the mutations for each property of the object passed recursively\n *\n * @param {object} propParent an Object of which all props will get a mutation\n * @param {string} path the path taken until the current propParent instance\n *\n * @returns {object} all mutations for each property.\n */\nfunction makeMutationsForAllProps(propParent, path) {\n if (!isObject(propParent)) return {}\n return Object.keys(propParent)\n .reduce((mutations, prop) => {\n if (prop[0] === '_') return mutations\n let propPath = (!path)\n ? prop\n : path + '.' + prop\n let name = 'SET_' + propPath.toUpperCase()\n mutations[name] = (state, newVal) => {\n return setDeepValue(state, propPath, newVal)\n }\n let propValue = propParent[prop]\n // If the prop is an object, make the children setters as well\n if (isObject(propValue)) {\n let childrenMutations = makeMutationsForAllProps(propValue, propPath)\n mutations = {...mutations, ...childrenMutations}\n }\n // If the prop is an array, make array mutations as well\n if (isArray(propValue)) {\n let pop = 'POP_' + propPath.toUpperCase()\n mutations[pop] = (state) => {\n return popDeepValue(state, propPath)\n }\n let push = 'PUSH_' + propPath.toUpperCase()\n mutations[push] = (state, value) => {\n return pushDeepValue(state, propPath, value)\n }\n let splice = 'SPLICE_' + propPath.toUpperCase()\n mutations[splice] = (state, index, deleteCount, value) => {\n return spliceDeepValue(state, propPath, index, deleteCount, value)\n }\n }\n return mutations\n }, {})\n}\n\n/**\n * Creates all mutations for the state of a module.\n * Usage:\n * commit('module/path/SET_PATH.TO.PROP', newValue)\n * Import method:\n * mutations {\n * ...defaultMutations (initialState)\n * }\n *\n * @param {object} initialState the initial state of a module\n *\n * @returns {object} all mutations for the state\n */\nfunction defaultMutations (initialState) {\n return makeMutationsForAllProps(initialState)\n}\n\n/**\n * Creates a setter function in the store to set any state value\n * Usage:\n * `set('module/path/path.to.prop', newValue)`\n * it will check first for existence of: `dispatch('module/path/setPath.to.prop')`\n * if non existant it will execute: `commit('module/path/SET_PATH.TO.PROP', newValue)`\n * Import method:\n * `store.set = (path, payload) => { return defaultSetter(path, payload, store) }`\n *\n * @param {string} path the path of the prop to set eg. 'info/user/favColours.primary'\n * @param {*} payload the payload to set the prop to\n * @param {object} store the store to attach\n *\n * @returns {function} dispatch or commit\n */\nfunction defaultSetter (path, payload, store, vuexEasyFirestore) {\n // path = 'info/user/favColours.primary'\n const pArr = path.split('/')\n // ['info', 'user', 'favColours.primary']\n const props = pArr.pop()\n // 'favColours.primary'\n const modulePath = (pArr.length)\n ? pArr.join('/') + '/'\n : ''\n // 'info/user/'\n const actionName = 'set' + props[0].toUpperCase() + props.substring(1)\n // 'setFavColours.primary'\n const actionPath = modulePath + actionName\n // 'info/user/setFavColours.primary'\n const actionExists = store._actions[actionPath]\n if (actionExists) {\n return store.dispatch(actionPath, payload)\n }\n if (vuexEasyFirestore) {\n // 'info/user/set', {favColours: {primary: payload}}'\n const pathIsModule = store._modulesNamespaceMap[path + '/']\n const firestoreActionPath = (pathIsModule)\n ? path + '/set'\n : modulePath + 'set'\n const newPayload = (pathIsModule)\n ? payload\n : {}\n if (!pathIsModule) newPayload[props] = payload\n const firestoreActionExists = store._actions[firestoreActionPath]\n if (firestoreActionExists) {\n return store.dispatch(firestoreActionPath, newPayload)\n }\n }\n const mutationPath = modulePath + 'SET_' + props.toUpperCase()\n return store.commit(mutationPath, payload)\n}\n\n/**\n * Creates a getter function in the store to set any state value\n * Usage:\n * `get('module/path/path.to.prop')`\n * it will check first for existence of: `getters['module/path/path.to.prop']`\n * if non existant it will return: `state.module.path.path.to.prop`\n * Import method:\n * `store.get = (path) => { return defaultGetter(path, store) }`\n *\n * @param {string} path the path of the prop to get eg. 'info/user/favColours.primary'\n * @param {object} store the store to attach\n *\n * @returns {function} getter or state\n */\nfunction defaultGetter (path, store) {\n const getterExists = store.getters.hasOwnProperty(path)\n if (getterExists) return store.getters[path]\n return getDeepValue(store.state, path)\n}\n\nexport { defaultMutations, defaultSetter, defaultGetter }\n","\nexport default {\n setter: 'set',\n getter: 'get',\n vuexEasyFirestore: false\n}\n","/**\n * Vuex Easy Access plugin\n * Unified syntax with simple set() and get() store access + auto generate mutations!\n *\n * @author Luca Ban\n * @contact https://lucaban.com\n */\n\nimport { defaultMutations, defaultSetter, defaultGetter } from './storeAccess'\nimport defaultConfig from './defaultConfig'\nimport { getDeepRef, getKeysFromPath } from './objectDeepValueUtils'\n\nfunction createEasyAccess (userConfig) {\n const conf = Object.assign(defaultConfig, userConfig)\n const vuexEasyFirestore = conf.vuexEasyFirestore\n return store => {\n store[conf.setter] = (path, payload) => { return defaultSetter(path, payload, store, vuexEasyFirestore) }\n store[conf.getter] = (path) => { return defaultGetter(path, store) }\n }\n}\n\nexport default createEasyAccess\nexport { createEasyAccess, defaultMutations, defaultSetter, defaultGetter, getDeepRef, getKeysFromPath }\n"],"names":["getKeysFromPath","path","match","getDeepRef","target","keys","length","obj","shift","key","hasOwnProperty","makeMutationsForAllProps","propParent","isObject","Object","reduce","mutations","prop","propPath","toUpperCase","state","newVal","value","lastKey","pop","deepRef","join","propValue","childrenMutations","isArray","popDeepValue","push","pushDeepValue","index","deleteCount","splice","spliceDeepValue","defaultSetter","payload","store","vuexEasyFirestore","pArr","split","props","modulePath","actionPath","substring","_actions","dispatch","pathIsModule","_modulesNamespaceMap","firestoreActionPath","newPayload","mutationPath","commit","defaultGetter","getters","getDeepValue","createEasyAccess","userConfig","conf","assign","defaultConfig","setter","getter","initialState"],"mappings":"yOAQA,SAASA,EAAiBC,UACnBA,EACEA,EAAKC,MAAM,WAWpB,SAASC,QAAYC,4DACfC,EAAOL,oBACNK,EAAKC,OAAQ,OAAOF,UACrBG,EAAMH,EACHG,GAAOF,EAAKC,OAAS,KACpBC,EAAIF,EAAKG,aAEbC,EAAMJ,EAAKG,eACXD,GAAOA,EAAIG,eAAeD,GACrBF,EAAIE,kLCXf,SAASE,EAAyBC,EAAYX,UACvCY,WAASD,GACPE,OAAOT,KAAKO,GAClBG,OAAO,SAACC,EAAWC,MACF,MAAZA,EAAK,GAAY,OAAOD,MACxBE,EAAajB,EAEbA,EAAO,IAAMgB,EADbA,IAEO,OAASC,EAASC,eACX,SAACC,EAAOC,UD2BPjB,EC1BGgB,ED0BWE,EC1BMD,ED2BnChB,EAAOL,EC3BkBkB,GD4BzBK,EAAUlB,EAAKmB,OACfC,EAAUtB,EAAWC,EAAQC,EAAKqB,UACzBD,EAAQf,eAAea,OAC5BA,GAAWD,GAEdlB,EAPT,IAAuBA,EAAckB,EAC7BjB,EACAkB,EACAE,OC3BAE,EAAYf,EAAWK,MAEvBJ,WAASc,GAAY,KACnBC,EAAoBjB,EAAyBgB,EAAWT,UAC5CF,EAAcY,GAG5BC,UAAQF,OACA,OAAST,EAASC,eACX,SAACC,UDiCxB,SAAuBhB,EAAQH,OACvBwB,EAAUtB,EAAWC,EAAQH,MAC9B4B,UAAQJ,UACNA,EAAQD,MCnCFM,CAAaV,EAAOF,MAElB,QAAUA,EAASC,eACZ,SAACC,EAAOE,UD2ChC,SAAwBlB,EAAQH,EAAMqB,OAC9BG,EAAUtB,EAAWC,EAAQH,MAC9B4B,UAAQJ,UACNA,EAAQM,KAAKT,GC7CPU,CAAcZ,EAAOF,EAAUI,MAE3B,UAAYJ,EAASC,eACd,SAACC,EAAOa,EAAOC,EAAaZ,UDuDtD,SAA0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 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vuex-easy-access", - "version": "1.0.13", + "version": "1.1.0", "description": "Unified syntax with simple set() and get() store access + auto generate mutations", "main": "dist/index.cjs.min.js", "module": "dist/index.es.min.js", diff --git a/src/storeAccess.js b/src/storeAccess.js index 3a59264..c31d720 100644 --- a/src/storeAccess.js +++ b/src/storeAccess.js @@ -21,6 +21,7 @@ function makeMutationsForAllProps(propParent, path) { if (!isObject(propParent)) return {} return Object.keys(propParent) .reduce((mutations, prop) => { + if (prop[0] === '_') return mutations let propPath = (!path) ? prop : path + '.' + prop