Skip to content

Commit

Permalink
feat: added deleteOne, deleteMany methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kouts committed May 11, 2021
1 parent 56bfb69 commit e3408cd
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/vueSetPath.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from 'vue'
import { isNumeric, isArray, isObject, splitPath } from './utils.js'
import { isNumeric, isArray, isObject, splitPath, getByPath } from './utils.js'

export const setOne = (obj, pathStr, value) => {
const path = splitPath(pathStr)
Expand Down Expand Up @@ -52,3 +52,21 @@ export const setMany = (obj, path, value) => {
throw Error('Arguments must be either string or object.')
}
}

export const deleteOne = (obj, pathStr) => {
const path = splitPath(pathStr)
const prop = path.pop()
Vue.delete(getByPath(obj, path), prop)
}

export const deleteMany = (obj, path) => {
if (typeof path === 'string') {
deleteOne(obj, path)
} else if (isArray(path)) {
path.forEach((item) => {
deleteOne(obj, item)
})
} else {
throw Error('Arguments must be either string or array.')
}
}

0 comments on commit e3408cd

Please sign in to comment.