Skip to content

Commit 9b8aeb9

Browse files
committed
refactor: extract sanitizeString into @monkeytype/util/strings
1 parent fe0904b commit 9b8aeb9

7 files changed

Lines changed: 73 additions & 62 deletions

File tree

backend/__tests__/utils/misc.spec.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -197,43 +197,6 @@ describe("Misc Utils", () => {
197197
);
198198
});
199199

200-
it("sanitizeString", () => {
201-
const testCases = [
202-
{
203-
input: "h̶̼͔̭͈̏́̀́͋͜ͅe̵̺̞̦̫̫͔̋́̅̅̃̀͝͝ļ̶̬̯͚͇̺͍̞̫̟͖͋̓͛̆̒̓͜ĺ̴̗̘͇̬̆͂͌̈͊͝͝ỡ̴̡̦̩̠̞̐̃͆̚͠͝",
204-
expected: "hello",
205-
},
206-
{
207-
input: "hello",
208-
expected: "hello",
209-
},
210-
{
211-
input: "hel lo",
212-
expected: "hel lo",
213-
},
214-
{
215-
input: " hel lo ",
216-
expected: "hel lo",
217-
},
218-
{
219-
input: "",
220-
expected: "",
221-
},
222-
{
223-
input: " \n\n\n",
224-
expected: "",
225-
},
226-
{
227-
input: undefined,
228-
expected: undefined,
229-
},
230-
];
231-
232-
testCases.forEach(({ input, expected }) => {
233-
expect(Misc.sanitizeString(input)).toEqual(expected);
234-
});
235-
});
236-
237200
it("getOrdinalNumberString", () => {
238201
const testCases = [
239202
{

backend/src/utils/misc.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MILLISECONDS_IN_DAY } from "@monkeytype/util/date-and-time";
22
import { roundTo2 } from "@monkeytype/util/numbers";
3+
export { sanitizeString } from "@monkeytype/util/strings";
34
import uaparser from "ua-parser-js";
45
import { MonkeyRequest } from "../api/types";
56
import { ObjectId } from "mongodb";
@@ -115,18 +116,6 @@ export function flattenObjectDeep(
115116
return result;
116117
}
117118

118-
export function sanitizeString(str: string | undefined): string | undefined {
119-
if (str === undefined || str === "") {
120-
return str;
121-
}
122-
123-
return str
124-
.replace(/[\u0300-\u036F]/g, "")
125-
.trim()
126-
.replace(/\n{3,}/g, "\n\n")
127-
.replace(/[^\S\r\n]{3,}/g, " ");
128-
}
129-
130119
const suffixes = ["th", "st", "nd", "rd"];
131120

132121
export function getOrdinalNumberString(number: number): string {

packages/schemas/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"lint": "oxlint . --type-aware --type-check",
2323
"lint-fast": "oxlint ."
2424
},
25+
"dependencies": {
26+
"@monkeytype/util": "workspace:*"
27+
},
2528
"devDependencies": {
2629
"@monkeytype/tsup-config": "workspace:*",
2730
"@monkeytype/typescript-config": "workspace:*",

packages/schemas/src/validation/validation.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { replaceHomoglyphs } from "./homoglyphs";
2+
import { sanitizeString } from "@monkeytype/util/strings";
23
import { ZodEffects, ZodString } from "zod";
34

45
// Sorry for the bad words
@@ -391,18 +392,6 @@ const disallowedWords = [
391392
"zabourah",
392393
];
393394

394-
function sanitizeString(str: string | undefined): string | undefined {
395-
if (str === undefined || str === "") {
396-
return str;
397-
}
398-
399-
return str
400-
.replace(/[\u0300-\u036F]/g, "")
401-
.trim()
402-
.replace(/\n{3,}/g, "\n\n")
403-
.replace(/[^\S\r\n]{3,}/g, " ");
404-
}
405-
406395
function containsDisallowedWords(
407396
text: string,
408397
mode: "word" | "substring",

packages/util/__test__/strings.spec.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { kebabToCamelCase } from "../src/strings";
2+
import { kebabToCamelCase, sanitizeString } from "../src/strings";
33

44
describe("strings", () => {
55
describe("kebabToCamelCase", () => {
@@ -11,4 +11,56 @@ describe("strings", () => {
1111
).toEqual("oneTwoThreeFourFiveSixSevenEightNineTen");
1212
});
1313
});
14+
15+
describe("sanitizeString", () => {
16+
const testCases = [
17+
{
18+
input: "h̶̼͔̭͈̏́̀́͋͜ͅe̵̺̞̦̫̫͔̋́̅̅̃̀͝͝ļ̶̬̯͚͇̺͍̞̫̟͖͋̓͛̆̒̓͜ĺ̴̗̘͇̬̆͂͌̈͊͝͝ỡ̴̡̦̩̠̞̐̃͆̚͠͝",
19+
expected: "hello",
20+
},
21+
{
22+
input: "hello",
23+
expected: "hello",
24+
},
25+
{
26+
input: "hel lo",
27+
expected: "hel lo",
28+
},
29+
{
30+
input: " hel lo ",
31+
expected: "hel lo",
32+
},
33+
{
34+
input: "",
35+
expected: "",
36+
},
37+
{
38+
input: " \n\n\n",
39+
expected: "",
40+
},
41+
{
42+
input: undefined,
43+
expected: undefined,
44+
},
45+
{
46+
input: "hello\r\n\r\nworld",
47+
expected: "hello\r\n\r\nworld",
48+
},
49+
{
50+
input: "hello\n\nworld",
51+
expected: "hello\n\nworld",
52+
},
53+
{
54+
input: "test \r\n test",
55+
expected: "test \r\n test",
56+
},
57+
];
58+
59+
it.each(testCases)(
60+
"sanitizeString with input '$input' expects '$expected'",
61+
({ input, expected }) => {
62+
expect(sanitizeString(input)).toEqual(expected);
63+
},
64+
);
65+
});
1466
});

packages/util/src/strings.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
11
export function kebabToCamelCase(kebab: string): string {
22
return kebab.replace(/-([a-z])/g, (_, char: string) => char.toUpperCase());
33
}
4+
5+
export function sanitizeString(str: string | undefined): string | undefined {
6+
if (str === undefined || str === "") {
7+
return str;
8+
}
9+
10+
return str
11+
.replace(/[\u0300-\u036F]/g, "")
12+
.trim()
13+
.replace(/\n{3,}/g, "\n\n")
14+
.replace(/[^\S\r\n]{3,}/g, " ");
15+
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)