From a86b9493b97328313b374ab65dfdcbf53cadaff9 Mon Sep 17 00:00:00 2001 From: AQian <970506352@qq.com> Date: Thu, 20 Jun 2024 03:23:53 +0800 Subject: [PATCH] fix: make setItems and getItems work with prefixStorage --- src/utils.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 96534674..b18c381b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -5,8 +5,10 @@ type StorageKeys = Array; const storageKeyProperties: StorageKeys = [ "hasItem", "getItem", + "getItems", "getItemRaw", "setItem", + "setItems", "setItemRaw", "removeItem", "getMeta", @@ -27,11 +29,22 @@ export function prefixStorage( return storage; } const nsStorage: Storage = { ...storage }; + const keysPropertyRegexp = new RegExp(/(set|get)\w+s/); for (const property of storageKeyProperties) { - // @ts-ignore - nsStorage[property] = (key = "", ...args) => + if (keysPropertyRegexp.test(property)) { // @ts-ignore - storage[property](base + key, ...args); + nsStorage[property] = (keys = [], ...args) => + // @ts-ignore + storage[property]( + keys.map((key) => base + key), + ...args + ); + } else { + // @ts-ignore + nsStorage[property] = (key = "", ...args) => + // @ts-ignore + storage[property](base + key, ...args); + } } nsStorage.getKeys = (key = "", ...arguments_) => storage