From 2bf0ac5c4e7faa91d39d69133027e01b3eabf9c5 Mon Sep 17 00:00:00 2001 From: Acathur Date: Tue, 26 May 2020 20:58:58 +0800 Subject: [PATCH] Add support for multi keys `delete` --- mod.ts | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/mod.ts b/mod.ts index 3d8c07e..712fefb 100644 --- a/mod.ts +++ b/mod.ts @@ -116,7 +116,10 @@ export class Store { if (dataChanged) { await this.save() + return true } + + return false } async has(key: string) { @@ -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() {