Skip to content

Instantly share code, notes, and snippets.

@Blezzoh
Created December 15, 2020 20:41
Show Gist options
  • Save Blezzoh/a057312b160981e3a22ef6bd41821c8a to your computer and use it in GitHub Desktop.
Save Blezzoh/a057312b160981e3a22ef6bd41821c8a to your computer and use it in GitHub Desktop.
const isObject = function (o) {
return o === Object(o) && !isArray(o) && typeof o !== 'function';
};
const isArray = function (a) {
return Array.isArray(a);
};
const toCamel = (s) => {
return s.replace(/([-_][a-z])/ig, ($1) => {
return $1.toUpperCase()
.replace('-', '')
.replace('_', '');
});
};
const keysToCamel = function (o) {
if (isObject(o)) {
const n = {};
Object.keys(o)
.forEach((k) => {
n[toCamel(k)] = keysToCamel(o[k]);
});
return n;
} else if (isArray(o)) {
return o.map((i) => {
return keysToCamel(i);
});
}
return o;
};
const obj = {
"a_a":"value"
};
keysToCamel(obj)
@Blezzoh
Copy link
Author

Blezzoh commented Dec 15, 2020

dashed json object to camel case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment