Skip to content

Instantly share code, notes, and snippets.

@andrewcsmith
Created September 5, 2018 21:27
Show Gist options
  • Save andrewcsmith/2ed5ef42cb7a1d883aaf50698479bb0f to your computer and use it in GitHub Desktop.
Save andrewcsmith/2ed5ef42cb7a1d883aaf50698479bb0f to your computer and use it in GitHub Desktop.
camelCase the things
import _ from 'lodash'
// Transforms a function into a deep function
_.mixin({
deeply: function (map) {
var deeplyArray = function (obj, fn) {
return obj.map(function(x) {
return _.isPlainObject(x) ? _.deeply(map)(x, fn) : x;
})
}
return function (obj, fn) {
if (_.isArray(obj)) {
return deeplyArray(obj, fn);
}
return map(_.mapValues(obj, function (v) {
return _.isPlainObject(v) ? _.deeply(map)(v, fn) : _.isArray(v) ?
deeplyArray(v, fn) : v;
}), fn);
}
},
});
import _ from 'lodash'
import '@/tools/lodashMixins'
// response is a Promise
let response = await fetch( ... some stuff ... )
if (response.ok || response.status == 304) {
let pieces = _.deeply(_.mapKeys)
(await response.json(), (v, k) => _.camelCase(k))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment