Skip to content

add SnakeToCamelCase conversion #1700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Conversions/SnakeToCamelCase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Converts a string from snake_case to camelCase.
*
* @param {string} str - The input string in snake_case format.
* @throws {Error} Will throw an error if the input is not a string.
* @returns {string} The converted string in camelCase format.
*
* @example
*
* snakeToCamelCase("hello_world"); // Returns "helloWorld"
* snakeToCamelCase("snake_case_example"); // Returns "snakeCaseExample"
* snakeToCamelCase("_leading_underscore"); // Returns "leadingUnderscore"
* snakeToCamelCase("trailing_underscore_"); // Returns "trailingUnderscore"
* snakeToCamelCase("__multiple__underscores__"); // Returns "multipleUnderscores"
* snakeToCamelCase("snake_case@example"); // Returns "snakeCaseExample"
* snakeToCamelCase("_leading_underscore_#"); // Returns "leadingUnderscore"
* snakeToCamelCase("trailing_underscore_&"); // Returns "trailingUnderscore"
* snakeToCamelCase(""); // Returns ""
*
* @throws {Error} If the input is not a string.
*/
function snakeToCamelCase(str) {
// Will throw an error if the input is not a string.
if (typeof str !== 'string') {
throw new Error(`Expected string as input, found ${typeof str}`)
}

if (str.trim() === '') return '' // Handle empty string

// Remove special characters (excluding underscores)
const cleanedStr = str.replace(/[^a-zA-Z0-9_]/g, '')

return cleanedStr
.split('_')
.filter(Boolean)
.map((value, index) => {
return index === 0
? value
: value.charAt(0).toUpperCase() + value.slice(1)
})
.join('')
}

export { snakeToCamelCase }
21 changes: 21 additions & 0 deletions Conversions/test/SnakeToCamelCase.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { snakeToCamelCase } from '../SnakeToCamelCase'

describe('snakeToCamelCase', () => {
it.each([
['hello_world', 'helloWorld'],
['snake_case_example', 'snakeCaseExample'],
['_leading_underscore', 'leadingUnderscore'],
['trailing_underscore_', 'trailingUnderscore'],
['__multiple__underscores__', 'multipleUnderscores'],
['snake_case@example', 'snakeCaseexample'],
['_leading_underscore_#', 'leadingUnderscore'],
['trailing_underscore_&', 'trailingUnderscore'],
['', '']
])('converts %s to snake_case %s', (input, expected) => {
expect(snakeToCamelCase(input)).toBe(expected)
})

it('throws an error when the input is not a string', () => {
expect(() => snakeToCamelCase(123)).toThrow('Expected string as input')
})
})
4 changes: 4 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
* [ROT13](Ciphers/ROT13.js)
* [VigenereCipher](Ciphers/VigenereCipher.js)
* [XORCipher](Ciphers/XORCipher.js)
* **Compression**
* [RLE](Compression/RLE.js)
* **Conversions**
* [ArbitraryBase](Conversions/ArbitraryBase.js)
* [ArrayBufferToBase64](Conversions/ArrayBufferToBase64.js)
Expand Down Expand Up @@ -62,6 +64,7 @@
* [RgbHsvConversion](Conversions/RgbHsvConversion.js)
* [RGBToHex](Conversions/RGBToHex.js)
* [RomanToDecimal](Conversions/RomanToDecimal.js)
* [SnakeToCamelCase](Conversions/SnakeToCamelCase.js)
* [TemperatureConversion](Conversions/TemperatureConversion.js)
* [TitleCaseConversion](Conversions/TitleCaseConversion.js)
* [UpperCaseConversion](Conversions/UpperCaseConversion.js)
Expand Down Expand Up @@ -285,6 +288,7 @@
* [Problem016](Project-Euler/Problem016.js)
* [Problem017](Project-Euler/Problem017.js)
* [Problem018](Project-Euler/Problem018.js)
* [Problem019](Project-Euler/Problem019.js)
* [Problem020](Project-Euler/Problem020.js)
* [Problem021](Project-Euler/Problem021.js)
* [Problem023](Project-Euler/Problem023.js)
Expand Down