From 03a83abfbb805522b7cc2cd8a4f03e3d52963a4b Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Wed, 10 Dec 2025 11:26:04 -0800 Subject: [PATCH] [str] add capitalize and titlecase --- src/prelude/str.ts | 10 ++++++++++ src/prelude/tests/prelude.test.ts | 12 ++++++++++++ .../server/src/metadata/prelude-completions.ts | 10 ++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/prelude/str.ts b/src/prelude/str.ts index ec688ae..3c9f17c 100644 --- a/src/prelude/str.ts +++ b/src/prelude/str.ts @@ -28,6 +28,16 @@ export const str = { }, 'pad-start': (str: string, length: number, pad: string = ' ') => String(str ?? '').padStart(length, pad), 'pad-end': (str: string, length: number, pad: string = ' ') => String(str ?? '').padEnd(length, pad), + capitalize: (str: string) => { + const s = String(str ?? '') + return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase() + }, + titlecase: (s: string) => { + return String(s ?? '') + .split(' ') + .map(str.capitalize) + .join(' ') + }, lines: (str: string) => String(str ?? '').split('\n'), chars: (str: string) => String(str ?? '').split(''), diff --git a/src/prelude/tests/prelude.test.ts b/src/prelude/tests/prelude.test.ts index 3db92e6..b297b6a 100644 --- a/src/prelude/tests/prelude.test.ts +++ b/src/prelude/tests/prelude.test.ts @@ -16,6 +16,18 @@ describe('string operations', () => { await expect(`str.trim '\\n\\thello\\t\\n'`).toEvaluateTo('hello') }) + test('capitalize makes first char uppercase', async () => { + await expect(`str.capitalize 'hello'`).toEvaluateTo('Hello') + await expect(`str.capitalize 'HELLO'`).toEvaluateTo('Hello') + await expect(`str.capitalize 'hello world'`).toEvaluateTo('Hello world') + }) + + test('titlecase capitalizes each word', async () => { + await expect(`str.titlecase 'hello world'`).toEvaluateTo('Hello World') + await expect(`str.titlecase 'HELLO WORLD'`).toEvaluateTo('Hello World') + await expect(`str.titlecase 'the quick brown fox'`).toEvaluateTo('The Quick Brown Fox') + }) + test('split divides string by separator', async () => { await expect(`str.split 'a,b,c' ','`).toEvaluateTo(['a', 'b', 'c']) await expect(`str.split 'hello' ''`).toEvaluateTo(['h', 'e', 'l', 'l', 'o']) diff --git a/vscode-extension/server/src/metadata/prelude-completions.ts b/vscode-extension/server/src/metadata/prelude-completions.ts index 7985daa..621bfeb 100644 --- a/vscode-extension/server/src/metadata/prelude-completions.ts +++ b/vscode-extension/server/src/metadata/prelude-completions.ts @@ -739,6 +739,16 @@ export const completions = { "pad" ] }, + "capitalize": { + "params": [ + "str" + ] + }, + "titlecase": { + "params": [ + "s" + ] + }, "lines": { "params": [ "str"