feat(Case Converter): add Language specific Title Case

Fix #1831
This commit is contained in:
ShareVB committed 2026-07-09 22:57:39 +02:00
1 parent bad27cfe39
commit 9302b8e0da
7 files changed
+248 -10

No files matched your search

+3
View File
@@ -680,6 +680,9 @@ tools:
label-titlecase: 'Titlecase:'
label-spongecase: 'Spongecase:'
label-swapcase: 'Swapcase:'
label-lang-title-case: 'Language TitleCase:'
label-language: 'Language:'
placeholder-your-language: Language Title casing language...
html-entities:
title: Escape HTML entities
description: >-
+2
View File
@@ -353,6 +353,7 @@
"sql-formatter": "^15.6.12",
"sshpk": "^1.18.0",
"staveless-futhark": "^0.6.1",
"stopword": "^3.1.5",
"stringify-object": "^6.0.0",
"svg2png-wasm": "^1.4.1",
"svgo": "^3.3.3",
@@ -437,6 +438,7 @@
"@types/sanitize-html": "^2.13.0",
"@types/spdx-satisfies": "^6.0.0",
"@types/sshpk": "^1.17.4",
"@types/stopword": "^2.0.3",
"@types/stringify-object": "^4.0.5",
"@types/text-encoding-utf-8": "^1.0.5",
"@types/turndown": "^5.0.6",
+16
View File
@@ -944,6 +944,9 @@ importers:
staveless-futhark:
specifier: ^0.6.1
version: 0.6.1
stopword:
specifier: ^3.1.5
version: 3.1.5
stringify-object:
specifier: ^6.0.0
version: 6.0.0
@@ -1191,6 +1194,9 @@ importers:
'@types/sshpk':
specifier: ^1.17.4
version: 1.17.5
'@types/stopword':
specifier: ^2.0.3
version: 2.0.3
'@types/stringify-object':
specifier: ^4.0.5
version: 4.0.5
@@ -4623,6 +4629,9 @@ packages:
'@types/sshpk@1.17.5':
resolution: {integrity: sha512-khJbOFAOnyLaqOoGIlDCF2ZghIb0jM1K1N0rRoKL0ObqUMwfkicu5zlJ5Wsp+Xn2EUduVwONXwsgbGk+COaCug==}
'@types/stopword@2.0.3':
resolution: {integrity: sha512-hioMj0lOvISM+EDevf7ijG8EMbU+J3pj4SstCyfQC1t39uPYpAe7beSfBdU6c1d9jeECTQQtR3UJWtVoUO8Weg==}
'@types/stringify-object@4.0.5':
resolution: {integrity: sha512-TzX5V+njkbJ8iJ0mrj+Vqveep/1JBH4SSA3J2wYrE1eUrOhdsjTBCb0kao4EquSQ8KgPpqY4zSVP2vCPWKBElg==}
@@ -10053,6 +10062,9 @@ packages:
resolution: {integrity: sha512-eLoXW/DHyl62zxY4SCaIgnRhuMr6ri4juEYARS8E6sCEqzKpOiE521Ucofdx+KnDZl5xmvGYaaKCk5FEOxJCoQ==}
engines: {node: '>= 0.4'}
stopword@3.1.5:
resolution: {integrity: sha512-OgLYGVFCNa430WOrj9tYZhQge5yg6vd6JsKredveAqEhdLVQkfrpnQIGjx0L9lLqzL4Kq4J8yNTcfQR/MpBwhg==}
stream-browserify@3.0.0:
resolution: {integrity: sha512-H73RAHsVBapbim0tU2JwwOiXUj+fikfiaoYAKHF3VJfA0pe2BCzkhAHBlLG6REzE+2WNZcxOXjK7lkso+9euLA==}
@@ -14913,6 +14925,8 @@ snapshots:
'@types/asn1': 0.2.4
'@types/node': 24.13.2
'@types/stopword@2.0.3': {}
'@types/stringify-object@4.0.5': {}
'@types/swagger-ui-dist@3.30.6': {}
@@ -21000,6 +21014,8 @@ snapshots:
es-errors: 1.3.0
internal-slot: 1.1.0
stopword@3.1.5: {}
stream-browserify@3.0.0:
dependencies:
inherits: 2.0.4
@@ -0,0 +1,120 @@
import { describe, it, expect } from 'vitest';
import { toLanguageTitleCase, getSupportedLanguages } from './case-converter.service';
describe('toLanguageTitleCase – core behavior', () => {
it('capitalizes normal words', () => {
expect(toLanguageTitleCase('the quick brown fox', 'eng')).toBe('The Quick Brown Fox');
});
it('keeps stopwords lowercase except boundaries', () => {
expect(toLanguageTitleCase('the lord of the rings', 'eng')).toBe('The Lord of the Rings');
});
it('handles empty input', () => {
expect(toLanguageTitleCase('', 'eng')).toBe('');
});
it('handles single word', () => {
expect(toLanguageTitleCase('hello', 'eng')).toBe('Hello');
});
});
describe('toLanguageTitleCase – punctuation handling', () => {
it('handles trailing punctuation', () => {
expect(toLanguageTitleCase('hello, world!', 'eng')).toBe('Hello, World!');
});
it('handles starting punctuation', () => {
expect(toLanguageTitleCase('(the lord of the rings)', 'eng')).toBe('(The Lord of the Rings)');
});
it('handles nested punctuation', () => {
expect(toLanguageTitleCase('((the lord of the rings))', 'eng')).toBe('((The Lord of the Rings))');
});
it('handles unicode punctuation', () => {
expect(toLanguageTitleCase('“the lord of the rings”', 'eng')).toBe('“The Lord of the Rings”');
});
it('handles mixed punctuation', () => {
expect(toLanguageTitleCase('...the lord of the rings?!', 'eng')).toBe('...the Lord of the Rings?!');
expect(toLanguageTitleCase('... the lord of the rings?!', 'eng')).toBe('... The Lord of the Rings?!');
});
it('handles punctuation inside sentence', () => {
expect(toLanguageTitleCase('hello: the lord of the rings', 'eng')).toBe('Hello: the Lord of the Rings');
});
});
describe('toLanguageTitleCase – French elisions', () => {
it('handles basic elision', () => {
expect(toLanguageTitleCase("l'homme et la mer", 'fra')).toBe('L’Homme et la Mer');
});
it('handles elision with punctuation', () => {
expect(toLanguageTitleCase("“l'homme”", 'fra')).toBe('“L’Homme”');
});
it('handles multiple elisions', () => {
expect(toLanguageTitleCase("d'amour et d'etoiles", 'fra')).toBe('D’Amour et d’Etoiles');
});
it('handles elisions with starting punctuation', () => {
expect(toLanguageTitleCase("(l'amour", 'fra')).toBe('(L’Amour');
});
it('handles elisions with trailing punctuation', () => {
expect(toLanguageTitleCase("l'homme!", 'fra')).toBe('L’Homme!');
});
it('handles elisions with unicode apostrophes', () => {
expect(toLanguageTitleCase('l’homme et l’amour', 'fra')).toBe('L’Homme et l’Amour');
});
});
describe('toLanguageTitleCase – advanced cases', () => {
it('handles numbers', () => {
expect(toLanguageTitleCase('the 7 wonders of the world', 'eng')).toBe('The 7 Wonders of the World');
});
it('handles acronyms', () => {
expect(toLanguageTitleCase('the nasa launch', 'eng')).toBe('The Nasa Launch');
});
it('handles hyphenated words', () => {
expect(toLanguageTitleCase('the state-of-the-art system', 'eng')).toBe('The State-of-the-art System');
});
it('handles mixed languages', () => {
expect(toLanguageTitleCase("l'homme et l'homme and the sea", 'fra')).toBe('L’Homme et l’Homme And The Sea');
});
it('handles multiple spaces', () => {
expect(toLanguageTitleCase('the lord of the rings', 'eng')).toBe('The Lord of the Rings');
});
it('handles tabs and newlines', () => {
expect(toLanguageTitleCase('the\tlord\nof the rings', 'eng')).toBe('The Lord of the Rings');
});
it('handles uppercase input', () => {
expect(toLanguageTitleCase('THE LORD OF THE RINGS', 'eng')).toBe('The Lord of the Rings');
});
it('handles mixed case input', () => {
expect(toLanguageTitleCase('tHe LoRd oF tHe RiNgS', 'eng')).toBe('The Lord of the Rings');
});
it('handles sentences input', () => {
expect(toLanguageTitleCase('tHe LoRd. oF tHe RiNgS', 'eng')).toBe('The Lord. Of the Rings');
});
});
describe('getSupportedLanguages', () => {
it('includes english stopwords', () => {
const langs = getSupportedLanguages();
const en = langs.find((l) => l.value === 'eng');
expect(en).toBeDefined();
expect(en!.label).toBe('English');
});
});
@@ -0,0 +1,68 @@
import { type LanguageCode } from 'stopword';
import * as stopwords from 'stopword';
import langs from 'langs';
const STARTING_PUNCT = /^[({[\“"']+/;
const TRAILING_PUNCT = /[.,;:!?'"”’)\]}]+$/;
const FRENCH_ELISION = /^(l|d|j|t|m|n|s|c|qu)['’](.+)$/iu;
/**
* Returns an array of { code, words } for all supported languages.
*/
export function getSupportedLanguages(): Array<{ value: string; label: string }> {
return Object.entries(stopwords).map(([code]) => ({
label: langs.where('3', code)?.name ?? code,
value: code,
}));
}
/**
* Title Case with stopwords, punctuation handling (start & end),
* and French elisions.
*/
export function toLanguageTitleCase(input: string, lang: string = 'eng'): string {
const stop = new Set(stopwords[lang as LanguageCode] ?? []);
const words = input
.trim()
.split(/\s+/)
.map((w) => w.toLowerCase());
let wasBoundary = true; // Start of string is a boundary
return words
.map((rawWord) => {
// Extract starting punctuation
const startMatch = rawWord.match(STARTING_PUNCT);
const startPunct = startMatch ? startMatch[0] : '';
const withoutStart = startPunct ? rawWord.slice(startPunct.length) : rawWord;
// Extract trailing punctuation
const endMatch = withoutStart.match(TRAILING_PUNCT);
const endPunct = endMatch ? endMatch[0] : '';
const core = endPunct ? withoutStart.slice(0, -endPunct.length) : withoutStart;
const isBoundary = wasBoundary;
const isStopword = stop.has(core);
wasBoundary = withoutStart.match(/[.!?]['"”’)\]}]*$/) !== null;
if (lang === 'fra') {
const elision = core.match(FRENCH_ELISION);
if (elision) {
const [, prefix, rest] = elision;
const capPrefix = isBoundary ? prefix.charAt(0).toUpperCase() + prefix.slice(1) : prefix;
const capRest = rest.charAt(0).toUpperCase() + rest.slice(1);
return `${startPunct}${capPrefix}’${capRest}${endPunct}`;
}
}
if (isStopword && !isBoundary) {
return `${startPunct}${core}${endPunct}`;
}
// Default capitalization
const cap = core.charAt(0).toUpperCase() + core.slice(1);
return `${startPunct}${cap}${endPunct}`;
})
.join(' ');
}
@@ -20,6 +20,7 @@ import { titleCase } from 'title-case';
import InputCopyable from '../../components/InputCopyable.vue';
import { useQueryParam, useQueryParamOrStorage } from '@/composable/queryParams';
import { useValidation } from '@/composable/validation';
import { getSupportedLanguages, toLanguageTitleCase } from './case-converter.service.ts';
const { t } = useI18n();
@@ -43,6 +44,7 @@ const cleaningRegexValidation = useValidation({
});
const input = useQueryParam({ tool: 'case-conv', name: 'text', defaultValue: 'lorem ipsum dolor sit amet' });
const language = useQueryParam({ tool: 'case-conv', name: 'lang', defaultValue: 'eng' });
const inputCleaned = computed(() => {
if (!cleaningRegexValidation.isValid) {
return input.value.split('\n');
@@ -51,6 +53,10 @@ const inputCleaned = computed(() => {
});
const formats = computed(() => [
{
label: t('tools.case-converter.texts.label-lang-title-case'),
value: inputCleaned.value.map((s) => toLanguageTitleCase(s, language.value)),
},
{
label: t('tools.case-converter.texts.label-lowercase'),
value: inputCleaned.value.map((s) => s.toLocaleLowerCase()),
@@ -154,6 +160,15 @@ const inputLabelAlignmentConfig = {
mb-1
/>
<c-select
v-model:value="language"
:options="getSupportedLanguages()"
:label="t('tools.case-converter.texts.label-language')"
:placeholder="t('tools.case-converter.texts.placeholder-your-language')"
v-bind="inputLabelAlignmentConfig"
mb-1
/>
<div my-16px divider />
<InputCopyable
+24 -10
View File
@@ -1,11 +1,25 @@
declare module 'langs' {
export function all(): Array<{
"name":string
"local": string
"1": string
"2": string
"2T":string
"2B":string
"3":string
}>
}
export function all(): Array<{
name: string;
local: string;
'1': string;
'2': string;
'2T': string;
'2B': string;
'3': string;
}>;
export function where(
type: string,
value: string,
):
| {
name: string;
local: string;
'1': string;
'2': string;
'2T': string;
'2B': string;
'3': string;
}
| undefined;
}