From c729bab6f88311322af0763c4c94ab95238dac00 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Fri, 16 Aug 2024 05:29:12 +0000 Subject: [PATCH 1/7] test(pascal): add camalCase case --- tests/string/pascal.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/string/pascal.test.ts b/tests/string/pascal.test.ts index 0a6af19b..e0692ff8 100644 --- a/tests/string/pascal.test.ts +++ b/tests/string/pascal.test.ts @@ -13,4 +13,8 @@ describe('pascal', () => { const result = _.pascal(null as any) expect(result).toBe('') }) + test('converts camelCase to PascalCase', () => { + const result = _.pascal('fooBar') + expect(result).toBe('FooBar') + }) }) From 74248c02aee0a95670d8dda0a9611d38ff2a4716 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Fri, 16 Aug 2024 05:30:25 +0000 Subject: [PATCH 2/7] fix(pascal): handle with camel-cased strings --- src/string/pascal.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/string/pascal.ts b/src/string/pascal.ts index a4e076b9..8fa86c36 100644 --- a/src/string/pascal.ts +++ b/src/string/pascal.ts @@ -9,9 +9,13 @@ * ``` */ export function pascal(str: string): string { - const parts = str?.split(/[\.\-\s_]/).map(x => x.toLowerCase()) ?? [] - if (parts.length === 0) { - return '' - } - return parts.map(str => str.charAt(0).toUpperCase() + str.slice(1)).join('') + if (!str) return ""; + + let a= str.replace(/(?:[^\w\d]|_|\s)+(\w)([A-Z]+)?/g, (_, a, b) => { + if (b) return a.toUpperCase() + b.toLowerCase() + + return a.toUpperCase() + }) + + return a[0].toUpperCase() + a.slice(1) } From 371f7dc068e9642a95d1a3f5e2b75797d138ea3c Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Fri, 16 Aug 2024 05:31:13 +0000 Subject: [PATCH 3/7] perf(pascal): add more test cases --- benchmarks/string/pascal.bench.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/benchmarks/string/pascal.bench.ts b/benchmarks/string/pascal.bench.ts index 52d1a599..db248717 100644 --- a/benchmarks/string/pascal.bench.ts +++ b/benchmarks/string/pascal.bench.ts @@ -5,4 +5,13 @@ describe('pascal', () => { bench('with valid input', () => { _.pascal('hello world') }) + bench("wiht camelCase input", () => { + _.pascal("fooBar") + }) + bench("with non alphanumerics", () => { + _.pascal("Exobase Starter_flash AND-go") + }) + bench("With null input", () => { + _.pascal(null as any) + }) }) From 1599482b376e3c606d5ae5930044597cfd390d56 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Fri, 16 Aug 2024 05:42:58 +0000 Subject: [PATCH 4/7] refactor(pascal): improve vars names --- src/string/pascal.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/string/pascal.ts b/src/string/pascal.ts index 8fa86c36..fe508716 100644 --- a/src/string/pascal.ts +++ b/src/string/pascal.ts @@ -11,11 +11,10 @@ export function pascal(str: string): string { if (!str) return ""; - let a= str.replace(/(?:[^\w\d]|_|\s)+(\w)([A-Z]+)?/g, (_, a, b) => { - if (b) return a.toUpperCase() + b.toLowerCase() - - return a.toUpperCase() + let result = str.replace(/(?:[^\w\d]|_|\s)+(\w)([A-Z]+)?/g, (_, firstCharacter, capitalizedLetters) => { + if (capitalizedLetters) return firstCharacter.toUpperCase() + capitalizedLetters.toLowerCase() + return firstCharacter.toUpperCase() }) - return a[0].toUpperCase() + a.slice(1) + return result[0].toUpperCase() + result.substring(1) } From 0ca2b5785bc825f307db05564310db9d25205fd2 Mon Sep 17 00:00:00 2001 From: marlonpassos Date: Fri, 16 Aug 2024 05:49:08 +0000 Subject: [PATCH 5/7] docs(pascal): add camalCase example --- docs/string/pascal.mdx | 1 + src/string/pascal.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/docs/string/pascal.mdx b/docs/string/pascal.mdx index 4e83ab42..0f2b2dcf 100644 --- a/docs/string/pascal.mdx +++ b/docs/string/pascal.mdx @@ -12,4 +12,5 @@ import * as _ from 'radashi' _.pascal('hello world') // => 'HelloWorld' _.pascal('va va boom') // => 'VaVaBoom' +_.pascal('helloWorld') // => 'HelloWorld' ``` diff --git a/src/string/pascal.ts b/src/string/pascal.ts index fe508716..fd6bfb0d 100644 --- a/src/string/pascal.ts +++ b/src/string/pascal.ts @@ -6,6 +6,7 @@ * ```ts * pascal('hello world') // => 'HelloWorld' * pascal('va va boom') // => 'VaVaBoom' + * pascal('helloWorld') // => 'HelloWorld' * ``` */ export function pascal(str: string): string { From 7c28f835e0c08ab07d934f8574773e3641550dc8 Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 16 Aug 2024 17:48:22 -0400 Subject: [PATCH 6/7] chore: format --- benchmarks/string/pascal.bench.ts | 10 +++++----- src/string/pascal.ts | 17 ++++++++++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/benchmarks/string/pascal.bench.ts b/benchmarks/string/pascal.bench.ts index db248717..40a12e22 100644 --- a/benchmarks/string/pascal.bench.ts +++ b/benchmarks/string/pascal.bench.ts @@ -5,13 +5,13 @@ describe('pascal', () => { bench('with valid input', () => { _.pascal('hello world') }) - bench("wiht camelCase input", () => { - _.pascal("fooBar") + bench('wiht camelCase input', () => { + _.pascal('fooBar') }) - bench("with non alphanumerics", () => { - _.pascal("Exobase Starter_flash AND-go") + bench('with non alphanumerics', () => { + _.pascal('Exobase Starter_flash AND-go') }) - bench("With null input", () => { + bench('With null input', () => { _.pascal(null as any) }) }) diff --git a/src/string/pascal.ts b/src/string/pascal.ts index fd6bfb0d..c0004c47 100644 --- a/src/string/pascal.ts +++ b/src/string/pascal.ts @@ -10,12 +10,19 @@ * ``` */ export function pascal(str: string): string { - if (!str) return ""; + if (!str) { + return '' + } - let result = str.replace(/(?:[^\w\d]|_|\s)+(\w)([A-Z]+)?/g, (_, firstCharacter, capitalizedLetters) => { - if (capitalizedLetters) return firstCharacter.toUpperCase() + capitalizedLetters.toLowerCase() + const result = str.replace( + /(?:[^\w\d]|_|\s)+(\w)([A-Z]+)?/g, + (_, firstCharacter, capitalizedLetters) => { + if (capitalizedLetters) { + return firstCharacter.toUpperCase() + capitalizedLetters.toLowerCase() + } return firstCharacter.toUpperCase() - }) + }, + ) - return result[0].toUpperCase() + result.substring(1) + return result[0].toUpperCase() + result.substring(1) } From e099186c06f052062d00c59dfca67e194dc053ac Mon Sep 17 00:00:00 2001 From: Alec Larson <1925840+aleclarson@users.noreply.github.com> Date: Fri, 16 Aug 2024 17:53:32 -0400 Subject: [PATCH 7/7] fix typos + remove one benchmark --- benchmarks/string/pascal.bench.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/benchmarks/string/pascal.bench.ts b/benchmarks/string/pascal.bench.ts index 40a12e22..8ac105ab 100644 --- a/benchmarks/string/pascal.bench.ts +++ b/benchmarks/string/pascal.bench.ts @@ -5,13 +5,10 @@ describe('pascal', () => { bench('with valid input', () => { _.pascal('hello world') }) - bench('wiht camelCase input', () => { + bench('with camelCase input', () => { _.pascal('fooBar') }) bench('with non alphanumerics', () => { _.pascal('Exobase Starter_flash AND-go') }) - bench('With null input', () => { - _.pascal(null as any) - }) })