-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring-utils.ts
290 lines (268 loc) · 7.39 KB
/
string-utils.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import {
CamelCase,
CamelCasedProperties,
DelimiterCasedProperties,
EatWhitespace,
Interpolate,
KebabCase,
PascalCase,
Replace,
SnakeCase,
Split,
VarRecord,
} from "./deps.ts";
import { extname } from "./path.ts";
export * from "https://git.quack.id/types/string.ts";
export type {
CamelCase,
EatWhitespace,
Interpolate,
KebabCase,
PascalCase,
Replace,
SnakeCase,
Split,
VarRecord,
};
const wordSeparators =
/[\s\u2000-\u206F\u2E00-\u2E7F\\'!"#$%&()*+,\-.\/:;<=>?@\[\]^_`{|}~]+/;
const capital_plus_lower = /[A-ZÀ-Ý\u00C0-\u00D6\u00D9-\u00DD][a-zà-ÿ]/g;
const capitals = /[A-ZÀ-Ý\u00C0-\u00D6\u00D9-\u00DD]+/g;
const spacesRegex = / /g;
const basicCamelRegEx =
/^[a-z\u00E0-\u00FCA-Z\u00C0-\u00DC][\d|a-z\u00E0-\u00FCA-Z\u00C0-\u00DC]*$/;
const fourOrMoreConsecutiveCapsRegEx = /([A-Z\u00C0-\u00DC]{4,})/g;
const allCapsRegEx = /^[A-Z\u00C0-\u00DC]+$/;
export function isUppercase(char = "") {
return char.toUpperCase() === char;
}
/**
* Type safe interpolation function. See also {@link replace}
*
* @example
*
* const res = interpolate(`My Name Is {{name}}, I'm {{wow_look_at_me}} years old`, {
* name: "curtis",
* wow_look_at_me: "22",
* });
*
* assertEquals(res, "My name is curtis, I'm 22 years old")
*/
export function interpolate<T extends string, Obj extends VarRecord<T>>(
str: T,
vars: Obj,
) {
return Object.entries(vars).reduce((acc, [key, val]) => {
acc = acc.replace("{{" + key + "}}", val + "");
return acc;
}, str as string) as Interpolate<T, Obj>;
}
export function upperFirst<T extends string>(str: T) {
if (!str) {
return "";
}
return str[0].toUpperCase() + str.slice(1);
}
export function lowerFirst<T extends string>(str: T) {
if (!str) {
return "";
}
return str[0].toLocaleLowerCase() + str.slice(1);
}
export function eatWhitespace<T extends string>(str: T) {
return str.replace(spacesRegex, "") as EatWhitespace<T>;
}
export function pascalCase<T extends string>(str: T) {
const words = str.split(wordSeparators);
const len = words.length;
const mappedWords = new Array(len);
for (let i = 0; i < len; i++) {
const word = words[i];
if (word === "") {
continue;
}
mappedWords[i] = word[0].toUpperCase() + word.slice(1);
}
return mappedWords.join("") as PascalCase<T>;
}
export function camelCase<Str extends string>(str: Str) {
const words = str.split(wordSeparators);
const len = words.length;
const mappedWords = new Array(len);
for (let i = 0; i < len; i++) {
let word = words[i];
if (word === "") {
continue;
}
const isCamelCase = basicCamelRegEx.test(word) && !allCapsRegEx.test(word);
if (isCamelCase) {
word = word.replace(
fourOrMoreConsecutiveCapsRegEx,
function (match, _, offset) {
return deCap(match, word.length - offset - match.length == 0);
},
);
}
let firstLetter = word[0];
firstLetter = i > 0 ? firstLetter.toUpperCase() : firstLetter.toLowerCase();
mappedWords[i] = firstLetter +
(!isCamelCase ? word.slice(1).toLowerCase() : word.slice(1));
}
return mappedWords.join("") as CamelCase<Str>;
}
export function camelCaseProperties<
S extends string,
T extends Record<S, unknown>,
>(obj: T) {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [camelCase(k), v]),
) as CamelCasedProperties<T>;
}
function deCap(match: string, endOfWord: boolean) {
const arr = match.split("");
const first = arr.shift()!.toUpperCase();
const last = endOfWord ? arr.pop()!.toLowerCase() : arr.pop();
return first + arr.join("").toLowerCase() + last;
}
export function snakeCase<T extends string>(str: T) {
// replace capitals with space + lower case equivalent for later parsing
const strBuilder = str.replace(capitals, function (match) {
return " " + (match.toLowerCase() || match);
});
return strBuilder.trim().split(wordSeparators).join("_") as SnakeCase<T>;
}
export function snakeCaseProperties<
S extends string,
T extends Record<S, unknown>,
>(obj: T) {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [snakeCase(k), v]),
) as DelimiterCasedProperties<T, "_">;
}
export function kebabCase<T extends string>(str: T) {
// replace word starts with space + lower case equivalent for later parsing
// 1) treat cap + lower as start of new word
let strBuilder = str.replace(capital_plus_lower, function (match) {
// match is one caps followed by one non-cap
return " " + (match[0].toLowerCase() || match[0]) + match[1];
});
// 2) treat all remaining capitals as words
strBuilder = strBuilder.replace(capitals, function (match) {
// match is a series of caps
return " " + match.toLowerCase();
});
return strBuilder.trim().split(wordSeparators).join("-").replace(/^-/, "")
.replace(/-\s*$/, "") as KebabCase<T>;
}
export function kebabCasePropertes<
S extends string,
T extends Record<S, unknown>,
>(obj: T) {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [kebabCase(k), v]),
) as DelimiterCasedProperties<T, "-">;
}
/**
* A type safe line split.
*/
export function splitLines<S extends string, SP extends Split<S, "\n">>(
str: S,
) {
return str.split("\n") as SP;
}
/**
* A type safe string split.
*/
export function split<
S extends string,
Delim extends string,
SP extends Split<S, Delim>,
>(str: S, delim: Delim) {
return str.split(delim) as SP;
}
/**
* A type safe string replace function
*
* @example
* const target = "aaabbcccbbdd";
* const searchVal = "bb";
* const replaceVal = "curtis";
* // "aaacurtisccccurtisdd"
* const ret = replace(target, searchVal, replaceVal, true);
*/
export function replace<
TargetInput extends string,
SearchValue extends string,
ReplaceValue extends string,
All extends true | false,
>(
target: TargetInput,
searchValue: SearchValue,
replaceValue: ReplaceValue,
all: All,
) {
if (all === true) {
return target.replaceAll(searchValue, replaceValue) as Replace<
TargetInput,
SearchValue,
ReplaceValue,
{ all: All }
>;
} else {
return target.replace(searchValue, replaceValue) as Replace<
TargetInput,
SearchValue,
ReplaceValue,
{ all: All }
>;
}
}
/**
* Strip the prefix value from the target value
*/
export function stripPrefix<
Prefix extends string,
Target extends `${Prefix}${string}`,
>(
target: Target,
prefix: Prefix,
) {
return replace(target, prefix, "", true);
}
/**
* Strip the given prefix from the properties of the given object.
*/
export function stripPrefixProperties<
Obj extends Record<string, string>,
Prefix extends string,
Key extends keyof Obj & `${Prefix}${string}`,
>(obj: Obj, prefix: Prefix) {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => {
return [stripPrefix(k as Key, prefix), v] as const;
}),
) as Record<Replace<Key, Prefix, "", { all: true }>, string>;
}
/**
* Ensure the given path ends in the given extension
*/
export function enforceExtension<
Path extends string,
Extension extends `.${string}`,
>(
path: Path,
extension: Extension,
): `${string}${Extension}` {
const ext = extname(path);
if (ext === extension) {
return path as `${string}${Extension}`;
} else {
return `${path}${extension}` as const;
}
}
// export function endsWith<Path extends string, Ext extends `.${string}`>(
// path: Path,
// ext: Ext,
// ): `${string}${Ext}` {
// return path.endsWith(ext)
// }