-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
185 lines (162 loc) · 4.89 KB
/
index.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* === Types === */
type ValueType = string | boolean | number | bigint;
type GetVariablesHelper<
Text extends string,
Prefix extends string,
Suffix extends string,
Accumulator,
> = string extends Text
? string
: Prefix extends ""
? Suffix extends ""
? // Prefix === "", Suffix === ""
Text extends `${infer Letter}${infer Rest}`
? GetVariablesHelper<Rest, Prefix, Suffix, Letter | Accumulator>
: Accumulator
: // Prefix === "", Suffix !== ""
Text extends `${infer Variable}${Suffix}${infer Rest}`
? GetVariablesHelper<Rest, Prefix, Suffix, Variable | Accumulator>
: Accumulator
: Suffix extends ""
? // Prefix !== "", Suffix === ""
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Text extends `${infer _Start}${Prefix}${infer Variable}`
? Variable extends `${infer _Variable}${Prefix}${infer Rest}`
? GetVariablesHelper<
`${Prefix}${Rest}`,
Prefix,
Suffix,
_Variable | Accumulator
>
: Variable | Accumulator
: Accumulator
: // Prefix !== "", Suffix !== ""
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Text extends `${infer _Start}${Prefix}${infer Variable}${Suffix}${infer Rest}`
? Variable extends `${infer _Start}${Prefix}${infer _Variable}`
? GetVariablesHelper<
`${_Start}${Prefix}${_Variable}${Suffix}${Rest}`,
Prefix,
Suffix,
Accumulator
>
: GetVariablesHelper<Rest, Prefix, Suffix, Variable | Accumulator>
: Accumulator;
type GetVariables<
Text extends string,
Prefix extends string,
Suffix extends string,
// The helper's usage is inspired by: https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/#tailrec-conditional
> = GetVariablesHelper<Text, Prefix, Suffix, never>;
interface InterpolationOptions<Prefix extends string, Suffix extends string> {
prefix: Prefix;
suffix: Suffix;
}
type DefaultPrefix = "{";
type DefaultSuffix = "}";
interface HydrateText {
<
Text extends string,
Prefix extends string = DefaultPrefix,
Suffix extends string = DefaultSuffix,
>(
text: Text,
variables?: Record<GetVariables<Text, Prefix, Suffix>, ValueType>,
interpolationOptions?: InterpolationOptions<Prefix, Suffix>,
): string;
}
interface ConfigureHydrateText {
<
_Prefix extends string = DefaultPrefix,
_Suffix extends string = DefaultSuffix,
>(
interpolationOptions: InterpolationOptions<_Prefix, _Suffix>,
): <
Text extends string,
Prefix extends string = _Prefix,
Suffix extends string = _Suffix,
>(
text: Text,
variables?: Record<GetVariables<Text, Prefix, Suffix>, ValueType>,
interpolationOptions?: InterpolationOptions<Prefix, Suffix>,
) => string;
}
/* === Constants === */
const DEFAULT_INTERPOLATION_OPTIONS: InterpolationOptions<
DefaultPrefix,
DefaultSuffix
> = {
prefix: "{",
suffix: "}",
};
/* === Utility functions === */
/*
Used to match `RegExp`.
Syntax characters: http://ecma-international.org/ecma-262/7.0/#sec-patterns.
*/
const reRegExpChar = /[\\^$.*+?()[\]{}|]/g;
const reHasRegExpChar = RegExp(reRegExpChar.source);
/*
Source: https://github.com/lodash/lodash/blob/master/escapeRegExp.js
Escapes the `RegExp` special characters "^", "$", "\", ".", "*", "+", "?",
"(", ")", "[", "]", "{", "}", and "|" in `value`.
*/
const escapeRegExp = (value: string): string => {
if (!value || !reHasRegExpChar.test(value)) {
return value;
}
return value.replace(reRegExpChar, "\\$&");
};
/*
Source: https://github.com/lodash/lodash/blob/master/isUndefined.js
Checks if `value` is `undefined`.
*/
const isUndefined = (value: unknown): value is undefined => {
return value === undefined;
};
/* === Main functions === */
const hydrateText: HydrateText = (text, variables, interpolationOptions) => {
if (isUndefined(variables)) {
return text;
}
const { prefix, suffix } = isUndefined(interpolationOptions)
? DEFAULT_INTERPOLATION_OPTIONS
: interpolationOptions;
const resultText = Object.entries(variables).reduce<string>(
(resultText, [name, value]) => {
const regExpSource = escapeRegExp(`${prefix}${name}${suffix}`);
const regExp = new RegExp(regExpSource, "g");
return resultText.replace(regExp, String(value));
},
text,
);
return resultText;
};
const configureHydrateText: ConfigureHydrateText =
(interpolationOptionsFromConfigurer) =>
(text, variables, interpolationOptionsFromInnerFunction) => {
const interpolationOptions = isUndefined(
interpolationOptionsFromInnerFunction,
)
? interpolationOptionsFromConfigurer
: interpolationOptionsFromInnerFunction;
return hydrateText(
text,
variables,
/*
`interpolationOptions` inherits `prefix` and `suffix` types from
`interpolationOptionsFromConfigurer`, so it is not necessary to define
the union.
*/
interpolationOptions as typeof interpolationOptionsFromInnerFunction,
);
};
export { configureHydrateText, hydrateText };
export type {
ConfigureHydrateText,
DefaultPrefix,
DefaultSuffix,
HydrateText,
InterpolationOptions,
ValueType,
};