Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/libs/words.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const reservedWords = [
"GitHub",
"BitBucket",
"GitLab",
"JSON",
"via",
"by",
"with",
"for",
"a",
"at",
];
19 changes: 17 additions & 2 deletions src/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { reservedWords } from "../libs/words";

const normalizeWords = (value: string): string => {
for (const word of reservedWords) {
value = value.replace(new RegExp(`\\b${word}\\b`, "i"), word);
}

return value;
};

export const titleCase = (title: string | undefined) => {
if (title === "" || title === undefined) {
return "";
}

return title
title = title
.replace(/([A-Z])/g, "$1")
.toLowerCase()
.replace(/(^|\s|-|_)\S/g, (match: string) => match.toUpperCase())
.replace(/[-_]/g, " ");
.replace(/[-_]/g, " ")
.trim();

const normalized = normalizeWords(title);

return normalized.charAt(0).toUpperCase() + normalized.slice(1);
};

export const removeImages = (content: string): string =>
Expand Down
141 changes: 141 additions & 0 deletions tests/unit/__snapshots__/strings.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing

exports[`words 1`] = `
[
"GitHub Bar",
"Foo GitHub Bar",
"Foo GitHub",
"GitHub Bar",
"Foo GitHub Bar",
"Foo GitHub",
"GitHub Bar",
"Foo GitHub Bar",
"Foo GitHub",
]
`;

exports[`words 2`] = `
[
"BitBucket Bar",
"Foo BitBucket Bar",
"Foo BitBucket",
"BitBucket Bar",
"Foo BitBucket Bar",
"Foo BitBucket",
"BitBucket Bar",
"Foo BitBucket Bar",
"Foo BitBucket",
]
`;

exports[`words 3`] = `
[
"GitLab Bar",
"Foo GitLab Bar",
"Foo GitLab",
"GitLab Bar",
"Foo GitLab Bar",
"Foo GitLab",
"GitLab Bar",
"Foo GitLab Bar",
"Foo GitLab",
]
`;

exports[`words 4`] = `
[
"JSON Bar",
"Foo JSON Bar",
"Foo JSON",
"JSON Bar",
"Foo JSON Bar",
"Foo JSON",
"JSON Bar",
"Foo JSON Bar",
"Foo JSON",
]
`;

exports[`words 5`] = `
[
"Via Bar",
"Foo via Bar",
"Foo via",
"Via Bar",
"Foo via Bar",
"Foo via",
"Via Bar",
"Foo via Bar",
"Foo via",
]
`;

exports[`words 6`] = `
[
"By Bar",
"Foo by Bar",
"Foo by",
"By Bar",
"Foo by Bar",
"Foo by",
"By Bar",
"Foo by Bar",
"Foo by",
]
`;

exports[`words 7`] = `
[
"With Bar",
"Foo with Bar",
"Foo with",
"With Bar",
"Foo with Bar",
"Foo with",
"With Bar",
"Foo with Bar",
"Foo with",
]
`;

exports[`words 8`] = `
[
"For Bar",
"Foo for Bar",
"Foo for",
"For Bar",
"Foo for Bar",
"Foo for",
"For Bar",
"Foo for Bar",
"Foo for",
]
`;

exports[`words 9`] = `
[
"A Bar",
"Foo a Bar",
"Foo a",
"A Bar",
"Foo a Bar",
"Foo a",
"A Bar",
"Foo a Bar",
"Foo a",
]
`;

exports[`words 10`] = `
[
"At Bar",
"Foo at Bar",
"Foo at",
"At Bar",
"Foo at Bar",
"Foo at",
"At Bar",
"Foo at Bar",
"Foo at",
]
`;
26 changes: 26 additions & 0 deletions tests/unit/strings.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { reservedWords } from "../../src/libs/words";
import { titleCase } from "../../src/utils/strings";

test("words", () => {
for (const word of reservedWords) {
const lower = word.toLowerCase();
const upper = word.toUpperCase();

const items = [
// Base
`${word}_bar`,
`foo ${word}-bar`,
`foo ${word}_`,
// Lower
`${lower}_bar`,
`foo ${lower}-bar`,
`foo ${lower}_`,
// Upper
`${upper}_bar`,
`foo ${upper}-bar`,
`foo ${upper}_`,
];

expect(items.map((item: string) => titleCase(item))).toMatchSnapshot();
}
});