camelify-recursive
Advanced tools
Comparing version 2.0.3 to 2.0.4
102
index.js
@@ -0,61 +1,59 @@ | ||
const log = console.log.bind(console) | ||
// https://stackoverflow.com/a/35976812/123671 | ||
function snakeToCamelCase(str) { | ||
return str | ||
.split("_") | ||
.map(function(word, index) { | ||
log(`Testing ${word}`); | ||
const snakeToCamelCase = function(str) { | ||
return str | ||
.split('_') | ||
.map(function(word, index) { | ||
// The word 'ID' is always 'ID' | ||
// Prevents 'Id' in output which is weird | ||
if (word === 'id') { | ||
return 'ID' | ||
} | ||
// The word 'ID' is always 'ID' | ||
// Prevents 'Id' in output which is weird | ||
if (word === "id") { | ||
return "ID"; | ||
} | ||
// If it is the first word make sure to lowercase all the chars. | ||
if (index === 0) { | ||
return word.toLowerCase() | ||
} | ||
// If it is the first word make sure to lowercase all the chars. | ||
if (index === 0) { | ||
return word.toLowerCase(); | ||
} | ||
// It is not the first word | ||
// only upper case the first char and lowercase the rest. | ||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase() | ||
}) | ||
.join('') | ||
} | ||
// It is not the first word | ||
// only upper case the first char and lowercase the rest. | ||
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(); | ||
}) | ||
.join(""); | ||
const kind = function(object) { | ||
if (object && object.constructor && object.constructor.name) { | ||
return object.constructor.name | ||
} | ||
return null | ||
} | ||
var log = console.log.bind(log); | ||
var kind = function(object) { | ||
if (object && object.constructor && object.constructor.name) { | ||
return object.constructor.name; | ||
} | ||
return null; | ||
}; | ||
// Inspired from https://gist.github.com/Sneagan/8366247 | ||
var camelifyObject = function(obj) { | ||
var keys = Object.keys(obj); | ||
var convertKey = function(oldKeyName) { | ||
var oldValue = obj[oldKeyName]; | ||
var newKeyName = snakeToCamelCase(oldKeyName); | ||
delete obj[oldKeyName]; | ||
obj[newKeyName] = oldValue; | ||
}; | ||
keys.forEach(function(key, index) { | ||
var value = obj[key]; | ||
const camelifyObject = function(obj) { | ||
var keys = Object.keys(obj) | ||
var convertKey = function(oldKeyName) { | ||
var oldValue = obj[oldKeyName] | ||
var newKeyName = snakeToCamelCase(oldKeyName) | ||
delete obj[oldKeyName] | ||
obj[newKeyName] = oldValue | ||
} | ||
keys.forEach(function(key, index) { | ||
var value = obj[key] | ||
if (kind(value) === "Object") { | ||
obj[key] = camelifyObject(value); | ||
} | ||
if (kind(value) === "Array") { | ||
value.forEach(function(item, index) { | ||
value = camelifyObject(obj[key][index]); | ||
}); | ||
} | ||
// Now we've fixed any potential subkeys, convert the actual key | ||
convertKey(key); | ||
}); | ||
return obj; | ||
}; | ||
if (kind(value) === 'Object') { | ||
obj[key] = camelifyObject(value) | ||
} | ||
if (kind(value) === 'Array') { | ||
value.forEach(function(item, index) { | ||
value = camelifyObject(obj[key][index]) | ||
}) | ||
} | ||
// Now we've fixed any potential subkeys, convert the actual key | ||
convertKey(key) | ||
}) | ||
return obj | ||
} | ||
module.exports = camelifyObject; | ||
module.exports = camelifyObject |
{ | ||
"name": "camelify-recursive", | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"description": "Recursively convert an object's keys to camelCase", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
# Recursively convert keys to camelCase | ||
# Recursively convert API responses to camelCase | ||
@@ -3,0 +3,0 @@ Have an API response from someone that doesn't use JavaScript? |
@@ -1,38 +0,38 @@ | ||
const camelify = require("../index.js"), | ||
assert = require("assert"); | ||
const camelify = require('../index.js'), | ||
assert = require('assert') | ||
var log = console.log.bind(console); | ||
const log = console.log.bind(console) | ||
suite("Camelifies recursively", function() { | ||
test("Camelifies an API output correctly", function() { | ||
var original = { | ||
api_version: "0.3.2", | ||
results: { | ||
page: 1, | ||
per_page: 30, | ||
id: 123456, | ||
banana_id: 123, | ||
total_pages: 1, | ||
total_count: 0, | ||
filings: [] | ||
} | ||
}; | ||
suite('Camelifies recursively', function() { | ||
test('Camelifies an API output correctly', function() { | ||
var original = { | ||
api_version: '0.3.2', | ||
results: { | ||
page: 1, | ||
per_page: 30, | ||
id: 123456, | ||
banana_id: 123, | ||
total_pages: 1, | ||
total_count: 0, | ||
filings: [] | ||
} | ||
} | ||
var output = camelify(original); | ||
var output = camelify(original) | ||
var expected = { | ||
apiVersion: "0.3.2", | ||
results: { | ||
page: 1, | ||
perPage: 30, | ||
ID: 123456, | ||
bananaID: 123, | ||
totalPages: 1, | ||
totalCount: 0, | ||
filings: [] | ||
} | ||
}; | ||
var expected = { | ||
apiVersion: '0.3.2', | ||
results: { | ||
page: 1, | ||
perPage: 30, | ||
ID: 123456, | ||
bananaID: 123, | ||
totalPages: 1, | ||
totalCount: 0, | ||
filings: [] | ||
} | ||
} | ||
assert.deepEqual(output, expected); | ||
}); | ||
}); | ||
assert.deepEqual(output, expected) | ||
}) | ||
}) |
5703
85