APIのレスポンスをキャメルケースに変換したい
Nuxtアプリなどを作成してて、なにかしらのAPIのレスポンスがスネークケースだったりすることがままある が、jsの世界ではキャメルケースで書きたい(lint先生に怒られるし)
そんなときに camelcase-keys を使う
install
npm install camelcase-keys
使い方
READMEのとおりだけど、基本的に関数読み込んで変換したいオブジェクトを引数にいれるだけ
const camelcaseKeys = require('camelcase-keys'); // Convert an object camelcaseKeys({'foo-bar': true}); //=> {fooBar: true} // Convert an array of objects camelcaseKeys([{'foo-bar': true}, {'bar-foo': false}]); //=> [{fooBar: true}, {barFoo: false}] camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true}); //=> {fooBar: true, nested: {unicornRainbow: true}} camelcaseKeys({a_b: 1, a_c: {c_d: 1, c_e: {e_f: 1}}}, {deep: true, stopPaths: ['a_c.c_e']}), //=> {aB: 1, aC: {cD: 1, cE: {e_f: 1}}} // Convert object keys to pascal case camelcaseKeys({'foo-bar': true, nested: {unicorn_rainbow: true}}, {deep: true, pascalCase: true}); //=> {FooBar: true, Nested: {UnicornRainbow: true}}