Created
September 5, 2018 21:27
-
-
Save andrewcsmith/2ed5ef42cb7a1d883aaf50698479bb0f to your computer and use it in GitHub Desktop.
camelCase the things
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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