Forked from codeBelt/JavaScript Case Converter Using Lodash.js
Created
October 31, 2022 02:01
-
-
Save 6220119/54b884675fc217615d02e196925eccef to your computer and use it in GitHub Desktop.
For Medium Article: https://medium.com/@robertsavian/javascript-case-converters-using-lodash-4f2f964091cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {camelCase, kebabCase, lowerCase, snakeCase, startCase, upperCase, upperFirst} from 'lodash'; | |
export default class StringUtility { | |
static toCamelCase(str) { | |
return camelCase(str); | |
} | |
static toTitleCase(str) { | |
return startCase(camelCase(str)); | |
} | |
static toPascalCase(str) { | |
return startCase(camelCase(str)).replace(/ /g, ''); | |
} | |
static toConstantCase(str) { | |
return upperCase(str).replace(/ /g, '_'); | |
} | |
static toDotCase(str) { | |
return lowerCase(str).replace(/ /g, '.'); | |
} | |
static toKebabCase(str) { | |
return kebabCase(str); | |
} | |
static toLowerCase(str) { | |
return lowerCase(str).replace(/ /g, ''); | |
} | |
static toPathCase(str) { | |
return lowerCase(str).replace(/ /g, '/'); | |
} | |
static toSnakeCase(str) { | |
return snakeCase(str); | |
} | |
static toSentenceCase(str) { | |
return upperFirst(lowerCase(str)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import StringUtility from './StringUtility'; | |
describe('StringUtility', () => { | |
const testStrings = ['lives-Down_by-the.River', 'Lives`Down,by~the RIVER', 'LivesDownBY the RIVER']; | |
describe('toCamelCase', () => { | |
it('returns camelCase string', () => { | |
const expectedResult = 'livesDownByTheRiver'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toCamelCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toTitleCase', () => { | |
it('returns Title Case string', () => { | |
const expectedResult = 'Lives Down By The River'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toTitleCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toPascalCase', () => { | |
it('returns PascalCase string', () => { | |
const expectedResult = 'LivesDownByTheRiver'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toPascalCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toConstantCase', () => { | |
it('returns CONSTANT_CASE string', () => { | |
const expectedResult = 'LIVES_DOWN_BY_THE_RIVER'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toConstantCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toDotCase', () => { | |
it('returns dot.case string', () => { | |
const expectedResult = 'lives.down.by.the.river'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toDotCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toKebabCase', () => { | |
it('returns kebab-case string', () => { | |
const expectedResult = 'lives-down-by-the-river'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toKebabCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toLowerCase', () => { | |
it('returns lowercase string', () => { | |
const expectedResult = 'livesdownbytheriver'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toLowerCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toPathCase', () => { | |
it('returns path/case string', () => { | |
const expectedResult = 'lives/down/by/the/river'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toPathCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toSnakeCase', () => { | |
it('returns snake_case string', () => { | |
const expectedResult = 'lives_down_by_the_river'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toSnakeCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
describe('toSentenceCase', () => { | |
it('returns Sentence case string', () => { | |
const expectedResult = 'Lives down by the river'; | |
testStrings.forEach((str) => { | |
expect(StringUtility.toSentenceCase(str)).toEqual(expectedResult); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment