Skip to content

Instantly share code, notes, and snippets.

@HugoDF
Created June 8, 2018 10:56
Show Gist options
  • Select an option

  • Save HugoDF/bdd2a2475cd456c35bab8565e4e1b13e to your computer and use it in GitHub Desktop.

Select an option

Save HugoDF/bdd2a2475cd456c35bab8565e4e1b13e to your computer and use it in GitHub Desktop.

Revisions

  1. HugoDF created this gist Jun 8, 2018.
    16 changes: 16 additions & 0 deletions map-snake-to-camel.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    const delimiterRegEx = /(_\w)/g;
    export function convertSnakeToCamelCase (str) {
    return str.replace(delimiterRegEx, ([delimiter, firstChar]) =>
    firstChar.toUpperCase()
    );
    }

    export const convertKeysToCamelCase = obj =>
    Object.entries(obj).reduce((prev, [key, value]) => {
    if (value && typeof value === 'object') {
    prev[convertSnakeToCamelCase(key)] = convertKeysToCamelCase(value);
    } else {
    prev[convertSnakeToCamelCase(key)] = value;
    }
    return prev;
    }, {});