diff --git a/docs/plugins/async-storage.md b/docs/plugins/async-storage.md index 43fe304d4..49aac214a 100644 --- a/docs/plugins/async-storage.md +++ b/docs/plugins/async-storage.md @@ -26,10 +26,12 @@ And you're done! Now you can see your AsyncStorage in Reactotron. ## Advanced Usage -`asyncStorage()` also accepts an object with an `ignore` key. The value is an array of strings you would like to prevent sending to Reactotron. +`asyncStorage()` also accepts an object with an `ignore` key to filter against. The value of `ignore` is an array of string or RegExp patterns that match against AsyncStorage keys you want to prevent from being sent to Reactotron. + +> String patterns match any key containing that text, while RegExp patterns use standard regex matching. ```js asyncStorage({ - ignore: ["secret"], + ignore: ["secret", /^debug_/, "temp"], }) ``` diff --git a/lib/reactotron-react-native/src/plugins/asyncStorage.ts b/lib/reactotron-react-native/src/plugins/asyncStorage.ts index 0a998e205..0369dc197 100644 --- a/lib/reactotron-react-native/src/plugins/asyncStorage.ts +++ b/lib/reactotron-react-native/src/plugins/asyncStorage.ts @@ -1,13 +1,25 @@ import type { ReactotronCore, Plugin } from "reactotron-core-client" import type { AsyncStorageStatic } from "@react-native-async-storage/async-storage" export interface AsyncStorageOptions { - ignore?: string[] + ignore?: (string | RegExp)[] } const PLUGIN_DEFAULTS: AsyncStorageOptions = { ignore: [], } +function shouldIgnore(key, ignore) { + for (const pattern of ignore) { + if (typeof pattern === "string") { + return key.includes(pattern) + } + if (pattern instanceof RegExp) { + return pattern.test(key) + } + } + return false +} + const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronCore) => { // setup configuration const config = Object.assign({}, PLUGIN_DEFAULTS, options || {}) @@ -28,7 +40,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC const setItem: AsyncStorageStatic["setItem"] = async (key, value, callback) => { try { - if (ignore.indexOf(key) < 0) { + if (!shouldIgnore(key, ignore)) { sendToReactotron("setItem", { key, value }) } } catch (e) {} @@ -37,7 +49,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC const removeItem: AsyncStorageStatic["removeItem"] = async (key, callback) => { try { - if (ignore.indexOf(key) < 0) { + if (!shouldIgnore(key, ignore)) { sendToReactotron("removeItem", { key }) } } catch (e) {} @@ -46,7 +58,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC const mergeItem: AsyncStorageStatic["mergeItem"] = async (key, value, callback) => { try { - if (ignore.indexOf(key) < 0) { + if (!shouldIgnore(key, ignore)) { sendToReactotron("mergeItem", { key, value }) } } catch (e) {} @@ -63,7 +75,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC const multiSet: AsyncStorageStatic["multiSet"] = async (pairs, callback) => { try { const shippablePairs = (pairs || []).filter( - (pair) => pair && pair[0] && ignore.indexOf(pair[0]) < 0 + (pair) => pair && pair[0] && !shouldIgnore(pair[0], ignore) ) if (shippablePairs.length > 0) { sendToReactotron("multiSet", { pairs: shippablePairs }) @@ -74,7 +86,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC const multiRemove: AsyncStorageStatic["multiRemove"] = async (keys, callback) => { try { - const shippableKeys = (keys || []).filter((key) => ignore.indexOf(key) < 0) + const shippableKeys = (keys || []).filter((key) => !shouldIgnore(key, ignore)) if (shippableKeys.length > 0) { sendToReactotron("multiRemove", { keys: shippableKeys }) } @@ -85,7 +97,7 @@ const asyncStorage = (options?: AsyncStorageOptions) => (reactotron: ReactotronC const multiMerge: AsyncStorageStatic["multiMerge"] = async (pairs, callback) => { try { const shippablePairs = (pairs || []).filter( - (pair) => pair && pair[0] && ignore.indexOf(pair[0]) < 0 + (pair) => pair && pair[0] && !shouldIgnore(pair[0], ignore) ) if (shippablePairs.length > 0) { sendToReactotron("multiMerge", { pairs: shippablePairs })