Skip to content

Commit e933dfb

Browse files
WHeirstrateWouter Heirstrate
andauthored
feat(pascal-case): add pascalCase as alias to camelCase and fix camelCase helper split logic (#450)
Co-authored-by: Wouter Heirstrate <[email protected]>
1 parent 3fd56de commit e933dfb

File tree

10 files changed

+110
-8
lines changed

10 files changed

+110
-8
lines changed

apps/docs/src/app/pages/docs/javascript/utils/introduction/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Converts a camelCase string to a human-readable sentence.
3030

3131
Replace all special characters to their normal counterparts.
3232

33+
### `*pascalCaseToSentencePage`
34+
35+
Converts a PascalCase string to a human-readable sentence.
36+
3337
### `*replaceHtmlWhitespacePage`
3438

3539
Returns the filtered html as a string that has replaced the non-breakable whitespaces with regular spaces.

apps/docs/src/app/pages/docs/javascript/utils/string/camelCaseToSentence/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ The camelCaseToSentence helper converts a camelCase string to a human-readable s
99
```typescript
1010
import { camelCaseToSentence } from '@studiohyperdrive/utils';
1111

12-
const result = camelCaseToSentence('SubscribeForMore');
12+
const result = camelCaseToSentence('subscribeForMore');
1313

1414
// result = 'Subscribe for more'
1515
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
keyword: pascalCaseToSentencePage
3+
---
4+
5+
The pascalCaseToSentence helper converts a PascalCase string to a human-readable sentence.
6+
7+
## How to use
8+
9+
```typescript
10+
import { pascalCaseToSentence } from '@studiohyperdrive/utils';
11+
12+
const result = pascalCaseToSentence('SubscribeForMore');
13+
14+
// result = 'Subscribe for more'
15+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NgDocPage } from '@ng-doc/core';
2+
import { StringCategory } from '../../../../../../categories/javascript';
3+
4+
const pascalCaseToSentencePage: NgDocPage = {
5+
title: `pascalCaseToSentence`,
6+
mdFile: './index.md',
7+
category: StringCategory,
8+
order: 0,
9+
};
10+
11+
export default pascalCaseToSentencePage;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# camelCaseToSentence
22

3-
The camelCaseToSentence helper converts a camelCase string to a human-readable sentence.
3+
The camelCaseToSentence helper converts a camelCase string to a human-readable sentence. The first letter will be uppercase, while the rest of the string will be lowercase.
44

55
## How to use
66

77
```typescript
88
import { camelCaseToSentence } from '@studiohyperdrive/utils';
99

10-
const result = camelCaseToSentence('SubscribeForMore');
10+
const result = camelCaseToSentence('subscribeForMore');
1111

1212
// result = 'Subscribe for more'
1313
```

libs/javascript/utils/src/lib/string/camel-case-to-sentence/camel-case-to-sentence.util.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ import { camelCaseToSentence } from './camel-case-to-sentence.util';
33
/* eslint-disable @typescript-eslint/no-explicit-any */
44
describe('camelCaseToSentence', () => {
55
it('should convert a camelCase string to a sentence', () => {
6+
expect(camelCaseToSentence('subscribeForMore')).toEqual('Subscribe for more');
7+
expect(camelCaseToSentence('string')).toEqual('String');
8+
expect(camelCaseToSentence('someExtraLongDescription')).toEqual(
9+
'Some extra long description'
10+
);
11+
});
12+
13+
it('should also convert a PascalCase string to a sentence', () => {
614
expect(camelCaseToSentence('SubscribeForMore')).toEqual('Subscribe for more');
715
});
816

libs/javascript/utils/src/lib/string/camel-case-to-sentence/camel-case-to-sentence.util.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
/**
2-
* Converts a camelCase string to a human-readable sentence
2+
* Converts a camelCase string to a human-readable sentence.
33
*
44
* @param value - The provided value
5+
*
6+
* @example
7+
* camelCaseToSentence('awesomeUtils') // 'Awesome utils'
8+
*
9+
* @returns A human-readable sentence derived from the camelCase string. The first
10+
* letter will be uppercase, while the rest of the string will be lowercase.
511
*/
6-
712
export const camelCaseToSentence = (value: string): string => {
813
// Iben: Early exit if no (string) value was provided
914
if (!value || typeof value !== 'string') {
@@ -13,10 +18,12 @@ export const camelCaseToSentence = (value: string): string => {
1318
return (
1419
value
1520
// Iben: Split by uppercase
16-
.match(/[A-Z][a-z]+/g)
17-
// Iben: Lowercase all but the first word
21+
.split(/(?=[A-Z])/)
22+
// Iben: Lowercase all but the first letter of the first word
1823
.map((value, index) => {
19-
return index === 0 ? value : value.toLowerCase();
24+
return index === 0
25+
? value[0].toUpperCase() + value.slice(1).toLowerCase()
26+
: value.toLowerCase();
2027
})
2128
// Iben: Join back to a string
2229
.join(' ')
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# camelCaseToSentence
2+
3+
The pascalCaseToSentence helper converts a PascalCase string to a human-readable sentence. The first letter will be uppercase, while the rest of the string will be lowercase.
4+
5+
## How to use
6+
7+
```typescript
8+
import { pascalCaseToSentence } from '@studiohyperdrive/utils';
9+
10+
const result = pascalCaseToSentence('SubscribeForMore');
11+
12+
// result = 'Subscribe for more'
13+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { pascalCaseToSentence } from './pascal-case-to-sentence.util';
2+
3+
/* eslint-disable @typescript-eslint/no-explicit-any */
4+
describe('pascalCaseToSentence', () => {
5+
it('should convert a PascalCase string to a sentence', () => {
6+
expect(pascalCaseToSentence('SubscribeForMore')).toEqual('Subscribe for more');
7+
expect(pascalCaseToSentence('String')).toEqual('String');
8+
expect(pascalCaseToSentence('SomeExtraLongDescription')).toEqual(
9+
'Some extra long description'
10+
);
11+
});
12+
13+
it('should also convert a camelCase string to a sentence', () => {
14+
expect(pascalCaseToSentence('subscribeForMore')).toEqual('Subscribe for more');
15+
});
16+
17+
it('should return an empty string if no value was provided', () => {
18+
expect(pascalCaseToSentence(null)).toEqual('');
19+
expect(pascalCaseToSentence(undefined)).toEqual('');
20+
});
21+
22+
it('should return an empty string if no string value was provided', () => {
23+
expect(pascalCaseToSentence(true as any)).toEqual('');
24+
expect(pascalCaseToSentence({ hello: 'world' } as any)).toEqual('');
25+
expect(pascalCaseToSentence(0 as any)).toEqual('');
26+
expect(pascalCaseToSentence([1] as any)).toEqual('');
27+
});
28+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { camelCaseToSentence } from '../camel-case-to-sentence/camel-case-to-sentence.util';
2+
3+
/**
4+
* Converts a PascalCase string to a human-readable sentence.
5+
*
6+
* *This is an alias for {@link camelCaseToSentence}.*
7+
*
8+
* @param value - The provided value
9+
*
10+
* @example
11+
* pascalCaseToSentence('AwesomeUtils') // 'Awesome utils'
12+
*
13+
* @returns A human-readable sentence derived from the PascalCase string. The first
14+
* letter will be uppercase, while the rest of the string will be lowercase.
15+
*/
16+
export const pascalCaseToSentence = camelCaseToSentence;

0 commit comments

Comments
 (0)