From b9649d7f15500ab525dc4c5c1baa3bfab62803e9 Mon Sep 17 00:00:00 2001 From: kiranjd Date: Mon, 29 Mar 2021 01:35:02 +0530 Subject: [PATCH 1/2] feat: add exception when array is passed for 2nd arg - For cases when two or more key values pairs are passed as arguements to multiSet. resolves issue#519 --- src/AsyncStorage.native.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/AsyncStorage.native.js b/src/AsyncStorage.native.js index 89bb49b6..7f196ffe 100644 --- a/src/AsyncStorage.native.js +++ b/src/AsyncStorage.native.js @@ -69,6 +69,14 @@ function checkValidInput(usedKey: string, value: any) { } } +function checkValidArgs(args: Array>) { + if (Array.isArray(args[1])) { + throw new Error( + '[AsyncStorage] Did you mean to pass an array of key value pairs instead? The second arguement to multiSet cannot be an array\n', + ); + } +} + /** * `AsyncStorage` is a simple, unencrypted, asynchronous, persistent, key-value * storage system that is global to the app. It should be used instead of @@ -322,6 +330,7 @@ const AsyncStorage = { callback?: ?(errors: ?$ReadOnlyArray) => void, ): Promise { return new Promise((resolve, reject) => { + checkValidArgs(arguments); keyValuePairs.forEach(([key, value]) => { checkValidInput(key, value); }); From 1a694cce6650c35adbc00f5d0e87dd63e606404e Mon Sep 17 00:00:00 2001 From: kiranjd Date: Tue, 30 Mar 2021 22:31:45 +0530 Subject: [PATCH 2/2] fix: add erros for both arguements of multiSet --- src/AsyncStorage.native.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/AsyncStorage.native.js b/src/AsyncStorage.native.js index 7f196ffe..a195e386 100644 --- a/src/AsyncStorage.native.js +++ b/src/AsyncStorage.native.js @@ -69,10 +69,26 @@ function checkValidInput(usedKey: string, value: any) { } } -function checkValidArgs(args: Array>) { - if (Array.isArray(args[1])) { +function checkValidArgs(keyValuePairs, callback) { + if ( + !Array.isArray(keyValuePairs) || + keyValuePairs.length === 0 || + !Array.isArray(keyValuePairs[0]) + ) { throw new Error( - '[AsyncStorage] Did you mean to pass an array of key value pairs instead? The second arguement to multiSet cannot be an array\n', + '[AsyncStorage] Expected array of key-value pairs as first argument to multiSet', + ); + } + + if (callback && typeof callback !== 'function') { + if (Array.isArray(callback)) { + throw new Error( + '[AsyncStorage] Expected function as second argument to multiSet. Did you forget to wrap key-value pairs in an array for the first argument?', + ); + } + + throw new Error( + '[AsyncStorage] Expected function as second argument to multiSet', ); } } @@ -329,8 +345,8 @@ const AsyncStorage = { keyValuePairs: Array>, callback?: ?(errors: ?$ReadOnlyArray) => void, ): Promise { + checkValidArgs(keyValuePairs, callback); return new Promise((resolve, reject) => { - checkValidArgs(arguments); keyValuePairs.forEach(([key, value]) => { checkValidInput(key, value); });