From 3ea60737d4a8764c56d50345552a37cce3be7e78 Mon Sep 17 00:00:00 2001 From: Mayank Date: Wed, 18 Dec 2024 17:01:37 +0530 Subject: [PATCH] Update doc comments --- lib/src/index.ts | 19 +++++++++++-------- lib/src/utils.ts | 5 +++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/lib/src/index.ts b/lib/src/index.ts index 336c35f7..17b45242 100644 --- a/lib/src/index.ts +++ b/lib/src/index.ts @@ -6,19 +6,21 @@ import type { RGS, SetStateAction, ValueType } from "./utils"; export type { SetterArgType, SetStateAction, Plugin } from "./utils"; /** - * Use this hook similar to `useState` hook. - * The difference is that you need to pass a - * unique key - unique across the app to make - * this state accessible to all client components. + * A React hook for managing shared global state, similar to the `useState` hook. + * This hook requires a unique key, which identifies the global store and allows state sharing across all client components. * * @example * ```tsx * const [state, setState] = useRGS("counter", 1); * ``` * - * @param key - Unique key to identify the store. - * @param value - Initial value of the store. - * @returns - A tuple (Ordered sequance of values) containing the state and a function to set the state. + * @param key - A unique key to identify the global store. + * @param value - The initial value of the global state. Can be a value or a function returning a value. + * @param includeRegExp - (Optional) A regular expression to specify which fields trigger updates. + * @param excludeRegExp - (Optional) A regular expression to specify which fields should be excluded from updates. + * @returns A tuple containing the current state and a function to update the state. + * + * @see [Learn More](https://r18gs.vercel.app/) */ const useRGS = ( key: string, @@ -27,7 +29,7 @@ const useRGS = ( excludeRegExp?: RegExp, ): [T, SetStateAction] => { /** Initialize the named store when invoked for the first time. */ - if (!globalRGS[key]) + if (!globalRGS[key]) { globalRGS[key] = { v: value instanceof Function ? value() : value, l: [], @@ -38,6 +40,7 @@ const useRGS = ( triggerListeners(rgs, oldV, rgs.v); }, }; + } return createHook(key, includeRegExp, excludeRegExp); }; diff --git a/lib/src/utils.ts b/lib/src/utils.ts index 34501527..84a583c4 100644 --- a/lib/src/utils.ts +++ b/lib/src/utils.ts @@ -158,6 +158,11 @@ export const useRGSWithPlugins = ( return createHook(key, includeRegExp, excludeRegExp); }; +/** + * Converts a list of selectors into a regular expression. + * @param list - An array of strings representing the fields to match. + * @returns A regular expression that matches any field from the provided list. + */ export const listToRegExp = (list: string[]) => { const escapedList = list.map(s => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")); return new RegExp(`^(${escapedList.join("|")})$`);