-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathutils.ts
More file actions
46 lines (40 loc) · 1.56 KB
/
Copy pathutils.ts
File metadata and controls
46 lines (40 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { PayloadActionCreator } from './create-action.js';
/**
* Internal helper utilities for constructing and inspecting Redux-style
* lifecycle action types (`::request`, `::success`, `::failure`).
*/
/**
* Separates an action's base type from its lifecycle suffix.
*/
export const actionSuffixDivider = '::';
/**
* Creates a matcher for action types ending with the requested suffix.
*
* @param suffix - The action suffix to match.
* @returns A regular expression that matches the suffix at the end of an action type.
*/
export const matchActionSuffix = (suffix: string): RegExp =>
new RegExp(`${actionSuffixDivider}${suffix}$`);
type BaseType<T extends string> = T extends `${infer A}${typeof actionSuffixDivider}${infer _R}`
? A
: never;
/**
* Extracts the base type from an action type string.
*
* @template T - The action type string.
* @param type - The action type string to extract the base type from.
* @returns The base type of the action type string, or `never` if the input string does not match the expected format.
*/
export function getBaseType<T extends string>(type: T): BaseType<T> {
return type.replace(matchActionSuffix('\\w+$'), '') as BaseType<T>;
}
/**
* Returns the action type of an action creator generated by `createAction`.
*
* @template T - The action type string.
* @param actionCreator - The action creator whose action type to get.
* @returns The action type used by the action creator.
*/
export function getType<T extends string>(actionCreator: PayloadActionCreator<unknown, T>): T {
return `${actionCreator}` as T;
}