Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active October 31, 2022 02:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codeBelt/0dbdc2e0e53c1ca35e161c946795c37e to your computer and use it in GitHub Desktop.
Save codeBelt/0dbdc2e0e53c1ca35e161c946795c37e to your computer and use it in GitHub Desktop.
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));
}
}
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