Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 0 additions & 37 deletions backend/__tests__/utils/misc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,43 +197,6 @@ describe("Misc Utils", () => {
);
});

it("sanitizeString", () => {
const testCases = [
{
input: "h̶̼͔̭͈̏́̀́͋͜ͅe̵̺̞̦̫̫͔̋́̅̅̃̀͝͝ļ̶̬̯͚͇̺͍̞̫̟͖͋̓͛̆̒̓͜ĺ̴̗̘͇̬̆͂͌̈͊͝͝ỡ̴̡̦̩̠̞̐̃͆̚͠͝",
expected: "hello",
},
{
input: "hello",
expected: "hello",
},
{
input: "hel lo",
expected: "hel lo",
},
{
input: " hel lo ",
expected: "hel lo",
},
{
input: "",
expected: "",
},
{
input: " \n\n\n",
expected: "",
},
{
input: undefined,
expected: undefined,
},
];

testCases.forEach(({ input, expected }) => {
expect(Misc.sanitizeString(input)).toEqual(expected);
});
});

it("getOrdinalNumberString", () => {
const testCases = [
{
Expand Down
13 changes: 1 addition & 12 deletions backend/src/utils/misc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MILLISECONDS_IN_DAY } from "@monkeytype/util/date-and-time";
import { roundTo2 } from "@monkeytype/util/numbers";
export { sanitizeString } from "@monkeytype/util/strings";
import uaparser from "ua-parser-js";
import { MonkeyRequest } from "../api/types";
import { ObjectId } from "mongodb";
Expand Down Expand Up @@ -115,18 +116,6 @@ export function flattenObjectDeep(
return result;
}

export function sanitizeString(str: string | undefined): string | undefined {
if (str === undefined || str === "") {
return str;
}

return str
.replace(/[\u0300-\u036F]/g, "")
.trim()
.replace(/\n{3,}/g, "\n\n")
.replace(/\s{3,}/g, " ");
}

const suffixes = ["th", "st", "nd", "rd"];

export function getOrdinalNumberString(number: number): string {
Expand Down
3 changes: 3 additions & 0 deletions packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"lint": "oxlint . --type-aware --type-check",
"lint-fast": "oxlint ."
},
"dependencies": {
"@monkeytype/util": "workspace:*"
},
"devDependencies": {
"@monkeytype/tsup-config": "workspace:*",
"@monkeytype/typescript-config": "workspace:*",
Expand Down
13 changes: 1 addition & 12 deletions packages/schemas/src/validation/validation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { replaceHomoglyphs } from "./homoglyphs";
import { sanitizeString } from "@monkeytype/util/strings";
import { ZodEffects, ZodString } from "zod";

// Sorry for the bad words
Expand Down Expand Up @@ -391,18 +392,6 @@ const disallowedWords = [
"zabourah",
];

function sanitizeString(str: string | undefined): string | undefined {
if (str === undefined || str === "") {
return str;
}

return str
.replace(/[\u0300-\u036F]/g, "")
.trim()
.replace(/\n{3,}/g, "\n\n")
.replace(/\s{3,}/g, " ");
}

function containsDisallowedWords(
text: string,
mode: "word" | "substring",
Expand Down
54 changes: 53 additions & 1 deletion packages/util/__test__/strings.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { kebabToCamelCase } from "../src/strings";
import { kebabToCamelCase, sanitizeString } from "../src/strings";

describe("strings", () => {
describe("kebabToCamelCase", () => {
Expand All @@ -11,4 +11,56 @@ describe("strings", () => {
).toEqual("oneTwoThreeFourFiveSixSevenEightNineTen");
});
});

describe("sanitizeString", () => {
const testCases = [
{
input: "h̶̼͔̭͈̏́̀́͋͜ͅe̵̺̞̦̫̫͔̋́̅̅̃̀͝͝ļ̶̬̯͚͇̺͍̞̫̟͖͋̓͛̆̒̓͜ĺ̴̗̘͇̬̆͂͌̈͊͝͝ỡ̴̡̦̩̠̞̐̃͆̚͠͝",
expected: "hello",
},
{
input: "hello",
expected: "hello",
},
{
input: "hel lo",
expected: "hel lo",
},
{
input: " hel lo ",
expected: "hel lo",
},
{
input: "",
expected: "",
},
{
input: " \n\n\n",
expected: "",
},
{
input: undefined,
expected: undefined,
},
{
input: "hello\r\n\r\nworld",
expected: "hello\r\n\r\nworld",
},
{
input: "hello\n\nworld",
expected: "hello\n\nworld",
},
{
input: "test \r\n test",
expected: "test \r\n test",
},
];

it.each(testCases)(
"sanitizeString with input '$input' expects '$expected'",
({ input, expected }) => {
expect(sanitizeString(input)).toEqual(expected);
},
);
});
});
12 changes: 12 additions & 0 deletions packages/util/src/strings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
export function kebabToCamelCase(kebab: string): string {
return kebab.replace(/-([a-z])/g, (_, char: string) => char.toUpperCase());
}

export function sanitizeString(str: string | undefined): string | undefined {
if (str === undefined || str === "") {
return str;
}

return str
.replace(/[\u0300-\u036F]/g, "")
.trim()
.replace(/\n{3,}/g, "\n\n")
.replace(/[^\S\r\n]{3,}/g, " ");
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading