Skip to content

Commit

Permalink
feat: added pathStore
Browse files Browse the repository at this point in the history
  • Loading branch information
kouts committed May 6, 2021
1 parent 2f9c057 commit 92f9225
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/pathStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Vue from 'vue'
import { getByPath, isArray } from './utils'
import { setOne, setMany } from './set.js'
import { ARRAY_METHODS } from './constants.js'

const createPathStore = (state) => {
const store = Vue.observable(state)

store.set = (path, value) => {
setMany(store, path, value)
}

store.toggle = (path) => {
setOne(store, path, !getByPath(store, path))
}

store.get = (path) => {
return path ? getByPath(store, path) : store
}

ARRAY_METHODS.forEach((method) => {
store[method] = (...args) => {
const path = args.shift()
const arr = getByPath(store, path)
if (!isArray(arr)) {
throw Error('Argument must be an array.')
}
arr[method](...args)
}
})

return store
}

export { createPathStore }

0 comments on commit 92f9225

Please sign in to comment.