Skip to content

Commit 431ce84

Browse files
committed
camelCase module
1 parent c3ba76c commit 431ce84

File tree

4 files changed

+182
-43
lines changed

4 files changed

+182
-43
lines changed

LEARN.md

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,48 @@ If you're new to **String-Methods**, here are some steps to get you started:
2323

2424
## Project Documentation
2525

26-
- [Capitalize Module](string-methods/docs/capitalize.md)
27-
- [CaseFolder Module](string-methods/docs/caseFolder.md)
28-
- [Center Module](string-methods/docs/center.md)
29-
- [Count Module](string-methods/docs/count.md)
30-
- [Encode Module](string-methods/docs/encode.md)
31-
- [EndsWith Module](string-methods/docs/endsWith.md)
32-
- [ExpandTabs Module](string-methods/docs/expandTabs.md)
33-
- [Find Module](string-methods/docs/find.md)
34-
- [Format Module](string-methods/docs/format.md)
35-
- [IsAlnum Module](string-methods/docs/isAlnum.md)
36-
- [IsAlpha Module](string-methods/docs/isAlpha.md)
37-
- [IsAscii Module](string-methods/docs/isAscii.md)
38-
- [IsDecimal Module](string-methods/docs/isDecimal.md)
39-
- [IsDigit Module](string-methods/docs/isDigit.md)
40-
- [IsIdentifier Module](string-methods/docs/isIdentifier.md)
41-
- [IsLower Module](string-methods/docs/isLower.md)
42-
- [IsNumeric Module](string-methods/docs/isNumeric.md)
43-
- [IsSpace Module](string-methods/docs/isSpace.md)
44-
- [IsTitle Module](string-methods/docs/isTitle.md)
45-
- [IsUpper Module](string-methods/docs/isUpper.md)
46-
- [Join Module](string-methods/docs/join.md)
47-
- [LeftJustify Module](string-methods/docs/leftJustify.md)
48-
- [LeftStrip Module](string-methods/docs/leftStrip.md)
49-
- [Lower Module](string-methods/docs/lower.md)
50-
- [MakeTrans Module](string-methods/docs/makeTrans.md)
51-
- [Partition Module](string-methods/docs/partition.md)
52-
- [Replace Module](string-methods/docs/replace.md)
53-
- [RightFind Module](string-methods/docs/rightFind.md)
54-
- [RightIndex Module](string-methods/docs/rightIndex.md)
55-
- [RightJustify Module](string-methods/docs/rightJustify.md)
56-
- [RightPartition Module](string-methods/docs/rightPartition.md)
57-
- [RightSplit Module](string-methods/docs/rightSplit.md)
58-
- [RightStrip Module](string-methods/docs/rightStrip.md)
59-
- [Split Module](string-methods/docs/split.md)
60-
- [SplitLines Module](string-methods/docs/splitLines.md)
61-
- [StartsWith Module](string-methods/docs/startsWith.md)
62-
- [Strip Module](string-methods/docs/strip.md)
63-
- [SwapCase Module](string-methods/docs/swapCase.md)
64-
- [Title Module](string-methods/docs/title.md)
65-
- [Upper Module](string-methods/docs/upper.md)
66-
- [ZeroFill Module](string-methods/docs/zeroFill.md)
67-
26+
- [Capitalize Module](string-methods/docs/capitalize.md)
27+
- [CaseFolder Module](string-methods/docs/caseFolder.md)
28+
- [Center Module](string-methods/docs/center.md)
29+
- [Count Module](string-methods/docs/count.md)
30+
- [Encode Module](string-methods/docs/encode.md)
31+
- [EndsWith Module](string-methods/docs/endsWith.md)
32+
- [ExpandTabs Module](string-methods/docs/expandTabs.md)
33+
- [Find Module](string-methods/docs/find.md)
34+
- [Format Module](string-methods/docs/format.md)
35+
- [IsAlnum Module](string-methods/docs/isAlnum.md)
36+
- [IsAlpha Module](string-methods/docs/isAlpha.md)
37+
- [IsAscii Module](string-methods/docs/isAscii.md)
38+
- [IsDecimal Module](string-methods/docs/isDecimal.md)
39+
- [IsDigit Module](string-methods/docs/isDigit.md)
40+
- [IsIdentifier Module](string-methods/docs/isIdentifier.md)
41+
- [IsLower Module](string-methods/docs/isLower.md)
42+
- [IsNumeric Module](string-methods/docs/isNumeric.md)
43+
- [IsSpace Module](string-methods/docs/isSpace.md)
44+
- [IsTitle Module](string-methods/docs/isTitle.md)
45+
- [IsUpper Module](string-methods/docs/isUpper.md)
46+
- [Join Module](string-methods/docs/join.md)
47+
- [LeftJustify Module](string-methods/docs/leftJustify.md)
48+
- [LeftStrip Module](string-methods/docs/leftStrip.md)
49+
- [Lower Module](string-methods/docs/lower.md)
50+
- [MakeTrans Module](string-methods/docs/makeTrans.md)
51+
- [Partition Module](string-methods/docs/partition.md)
52+
- [Replace Module](string-methods/docs/replace.md)
53+
- [RightFind Module](string-methods/docs/rightFind.md)
54+
- [RightIndex Module](string-methods/docs/rightIndex.md)
55+
- [RightJustify Module](string-methods/docs/rightJustify.md)
56+
- [RightPartition Module](string-methods/docs/rightPartition.md)
57+
- [RightSplit Module](string-methods/docs/rightSplit.md)
58+
- [RightStrip Module](string-methods/docs/rightStrip.md)
59+
- [Split Module](string-methods/docs/split.md)
60+
- [SplitLines Module](string-methods/docs/splitLines.md)
61+
- [StartsWith Module](string-methods/docs/startsWith.md)
62+
- [Strip Module](string-methods/docs/strip.md)
63+
- [SwapCase Module](string-methods/docs/swapCase.md)
64+
- [Title Module](string-methods/docs/title.md)
65+
- [Upper Module](string-methods/docs/upper.md)
66+
- [ZeroFill Module](string-methods/docs/zeroFill.md)
67+
- [CamelCase Module](string-methods/docs/camelCase.md)
6868

6969
## Contributing
7070

@@ -74,6 +74,6 @@ Interested in contributing to **String-Methods**? Check out the [Contribution Gu
7474

7575
Join our community to connect with other developers, ask questions, and share your experiences:
7676

77-
- [GitHub Discussions](../../discussions)
77+
- [GitHub Discussions](../../discussions)
7878

7979
We hope you find this learning guide helpful in your journey with **String-Methods**. If you have any questions or need assistance, feel free to reach out to our community. Happy learning and coding!

string-methods/docs/camelCase.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## CamelCase
2+
3+
This module offers a method for converting strings into camelCase, where the first word is in lowercase and subsequent words are capitalized without spaces.
4+
5+
### Installation
6+
7+
To use this module, you should have the "node-string-methods" npm package installed. You can install it using npm or yarn:
8+
9+
```bash
10+
npm install node-string-methods
11+
```
12+
13+
### Usage
14+
15+
```javascript
16+
const camelCase = require('node-string-methods/camelCase');
17+
18+
// Create an instance of the camelCase class with a string as input
19+
const str = 'FIRST name LAST';
20+
const camelCaser = new camelCase(str);
21+
22+
// Execute the capitalization operation
23+
const result = camelCaser.execute();
24+
25+
console.log(result); // Output: 'firstNameLast'
26+
```
27+
28+
### Constructor
29+
30+
#### `new CamelCase(str)`
31+
32+
- `str` (String): The input string that you want to camelcase.
33+
34+
Creates a new instance of the `Camelcase` class with the provided string.
35+
36+
### Methods
37+
38+
#### `execute()`
39+
40+
It will convert a given string into its camelCase form
41+
42+
- Returns: The camelcase string.
43+
44+
### Example
45+
46+
```javascript
47+
const CamelCase = require('node-string-methods/camelCase');
48+
49+
const str = 'first_name_last';
50+
const camelCase = new CamelCase(str);
51+
52+
const result = camelCase.execute();
53+
54+
console.log(result); // Output: 'firstNameLast'
55+
```
56+
57+
### Errors
58+
59+
- Throws an `Error` if the input provided to the constructor is not a string.

string-methods/src/camelCase.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class CamelCase {
2+
constructor(str) {
3+
if (typeof str !== 'string') {
4+
throw new Error('Input must be a string');
5+
}
6+
this.str = str;
7+
}
8+
9+
execute() {
10+
return this.str
11+
.trim()
12+
.split(/[\W_]+/)
13+
.map((word, index) => {
14+
if (index === 0) {
15+
return word.toLowerCase();
16+
}
17+
return (
18+
word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
19+
);
20+
})
21+
.join('');
22+
}
23+
}
24+
25+
module.exports = CamelCase;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const CamelCase = require('../src/camelCase');
2+
3+
describe('camelCase', () => {
4+
it('should convert the rest of the word to lowercase', () => {
5+
const camelCaseInstance = new CamelCase('WORLD');
6+
expect(camelCaseInstance.execute()).toBe('world');
7+
});
8+
9+
it('should convert "hello world" to "helloWorld"', () => {
10+
const camelCaseInstance = new CamelCase('hello world');
11+
expect(camelCaseInstance.execute()).toBe('helloWorld');
12+
});
13+
14+
it('should convert "FIRST name LAST" to "firstNameLast"', () => {
15+
const camelCaseInstance = new CamelCase('FIRST name LAST');
16+
expect(camelCaseInstance.execute()).toBe('firstNameLast');
17+
});
18+
19+
it('should convert " mixed INPUT data " to "mixedInputData"', () => {
20+
const camelCaseInstance = new CamelCase(' mixed INPUT data ');
21+
expect(camelCaseInstance.execute()).toBe('mixedInputData');
22+
});
23+
24+
it('should handle single word strings', () => {
25+
const camelCaseInstance = new CamelCase('TESTING');
26+
expect(camelCaseInstance.execute()).toBe('testing');
27+
});
28+
29+
it('should handle strings with underscores', () => {
30+
const camelCaseInstance = new CamelCase('first_name_last');
31+
expect(camelCaseInstance.execute()).toBe('firstNameLast');
32+
});
33+
34+
it('should handle strings with dashes', () => {
35+
const camelCaseInstance = new CamelCase('first-name-last');
36+
expect(camelCaseInstance.execute()).toBe('firstNameLast');
37+
});
38+
39+
it('should handle strings with mixed special characters', () => {
40+
const camelCaseInstance = new CamelCase('first_name-last');
41+
expect(camelCaseInstance.execute()).toBe('firstNameLast');
42+
});
43+
44+
it('should handle empty strings', () => {
45+
const camelCaseInstance = new CamelCase('');
46+
expect(camelCaseInstance.execute()).toBe('');
47+
});
48+
49+
it('should handle non-string inputs', () => {
50+
expect(() => new CamelCase(123)).toThrowError('Input must be a string');
51+
expect(() => new CamelCase(null)).toThrowError(
52+
'Input must be a string'
53+
);
54+
});
55+
});

0 commit comments

Comments
 (0)