Skip to content

Commit 3ea6073

Browse files
committed
Update doc comments
1 parent dae7253 commit 3ea6073

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

lib/src/index.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@ import type { RGS, SetStateAction, ValueType } from "./utils";
66
export type { SetterArgType, SetStateAction, Plugin } from "./utils";
77

88
/**
9-
* Use this hook similar to `useState` hook.
10-
* The difference is that you need to pass a
11-
* unique key - unique across the app to make
12-
* this state accessible to all client components.
9+
* A React hook for managing shared global state, similar to the `useState` hook.
10+
* This hook requires a unique key, which identifies the global store and allows state sharing across all client components.
1311
*
1412
* @example
1513
* ```tsx
1614
* const [state, setState] = useRGS<number>("counter", 1);
1715
* ```
1816
*
19-
* @param key - Unique key to identify the store.
20-
* @param value - Initial value of the store.
21-
* @returns - A tuple (Ordered sequance of values) containing the state and a function to set the state.
17+
* @param key - A unique key to identify the global store.
18+
* @param value - The initial value of the global state. Can be a value or a function returning a value.
19+
* @param includeRegExp - (Optional) A regular expression to specify which fields trigger updates.
20+
* @param excludeRegExp - (Optional) A regular expression to specify which fields should be excluded from updates.
21+
* @returns A tuple containing the current state and a function to update the state.
22+
*
23+
* @see [Learn More](https://r18gs.vercel.app/)
2224
*/
2325
const useRGS = <T>(
2426
key: string,
@@ -27,7 +29,7 @@ const useRGS = <T>(
2729
excludeRegExp?: RegExp,
2830
): [T, SetStateAction<T>] => {
2931
/** Initialize the named store when invoked for the first time. */
30-
if (!globalRGS[key])
32+
if (!globalRGS[key]) {
3133
globalRGS[key] = {
3234
v: value instanceof Function ? value() : value,
3335
l: [],
@@ -38,6 +40,7 @@ const useRGS = <T>(
3840
triggerListeners(rgs, oldV, rgs.v);
3941
},
4042
};
43+
}
4144

4245
return createHook<T>(key, includeRegExp, excludeRegExp);
4346
};

lib/src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ export const useRGSWithPlugins = <T>(
158158
return createHook<T>(key, includeRegExp, excludeRegExp);
159159
};
160160

161+
/**
162+
* Converts a list of selectors into a regular expression.
163+
* @param list - An array of strings representing the fields to match.
164+
* @returns A regular expression that matches any field from the provided list.
165+
*/
161166
export const listToRegExp = (list: string[]) => {
162167
const escapedList = list.map(s => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
163168
return new RegExp(`^(${escapedList.join("|")})$`);

0 commit comments

Comments
 (0)