Skip to content

Commit

Permalink
Add support for multi keys delete
Browse files Browse the repository at this point in the history
  • Loading branch information
acathur committed May 26, 2020
1 parent 2346d72 commit 2bf0ac5
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ export class Store {

if (dataChanged) {
await this.save()
return true
}

return false
}

async has(key: string) {
Expand All @@ -125,19 +128,32 @@ export class Store {
return this.data.hasOwnProperty(key)
}

async delete(key: string) {
async delete(key: string | string[]) {
if (this.isNullOrEmptyData()) {
return false
}

await this.load()

if (this.has(key)) {
delete this.data[key]
let dataChanged = false

if (typeof key === 'string') {
key = [key]
}

for (const k of key) {
if (this.has(k)) {
delete this.data[k]
dataChanged = true
}
}

if (dataChanged) {
await this.save()
return true
}

return true
return false
}

async clear() {
Expand Down

0 comments on commit 2bf0ac5

Please sign in to comment.