Skip to content

Instantly share code, notes, and snippets.

@codeBelt
Last active October 31, 2022 02:01
Show Gist options
  • Save codeBelt/0dbdc2e0e53c1ca35e161c946795c37e to your computer and use it in GitHub Desktop.
Save codeBelt/0dbdc2e0e53c1ca35e161c946795c37e to your computer and use it in GitHub Desktop.

Revisions

  1. codeBelt revised this gist Jun 3, 2019. No changes.
  2. codeBelt revised this gist Mar 5, 2019. 1 changed file with 106 additions and 0 deletions.
    106 changes: 106 additions & 0 deletions JavaScript Case Converter Using Lodash.test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,106 @@
    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);
    });
    });
    });

    });
  3. codeBelt revised this gist Mar 5, 2019. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion JavaScript Case Converter Using Lodash.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    import {camelCase, upperCase, startCase, lowerCase, kebabCase, upperFirst, snakeCase} from 'lodash';
    import {camelCase, kebabCase, lowerCase, snakeCase, startCase, upperCase, upperFirst} from 'lodash';

    export default class StringUtility {

    static toCamelCase(str) {
    return camelCase(str);
    }
    @@ -40,4 +41,5 @@ export default class StringUtility {
    static toSentenceCase(str) {
    return upperFirst(lowerCase(str));
    }

    }
  4. codeBelt created this gist Mar 5, 2019.
    43 changes: 43 additions & 0 deletions JavaScript Case Converter Using Lodash.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    import {camelCase, upperCase, startCase, lowerCase, kebabCase, upperFirst, snakeCase} 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));
    }
    }