-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored toApiFormat.js to use json-pointer
Showing
4 changed files
with
74 additions
and
35 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,59 @@ | ||
'use strict'; | ||
var _ = require('lodash'); | ||
var _ = require('lodash') | ||
, pointer = require('json-pointer'); | ||
|
||
var excludeList = [ | ||
'/substitution_data', | ||
'/tags', | ||
'/metadata', | ||
'/attributes', | ||
'/headers', | ||
'/content/email_rfc822' | ||
]; | ||
|
||
function snakedKeyClone(source) { | ||
|
||
if (!_.isObject(source)) { | ||
return source; | ||
} | ||
|
||
var target = {}; | ||
|
||
if (Array.isArray(source)) { | ||
target = []; | ||
for (var i = 0; i < source.length; i++) { | ||
target.push(snakedKeyClone(source[i])); | ||
} | ||
return target; | ||
} | ||
|
||
Object.keys(source).forEach(function(key) { | ||
target[_.snakeCase(key)] = snakedKeyClone(source[key]); | ||
}); | ||
|
||
return target; | ||
} | ||
|
||
module.exports = function toApiFormat(source) { | ||
|
||
var dest = {}; | ||
// List of property names which we do not want to modify the sub-property names | ||
var excludeList = ['substitution_data', 'tags', 'metadata', 'attributes', 'headers']; | ||
|
||
try{ | ||
// Handle arrays (Only need to handle arrays of objects for our use case.) | ||
if(Array.isArray(source) && _.isPlainObject(source[0])) { | ||
dest = []; | ||
for(var i = 0; i < source.length; i++) { | ||
dest.push(toApiFormat(source[i])); | ||
} | ||
return dest; | ||
var excludedObjects = {}; | ||
|
||
// Look in the source object for the excluded pointers and take a copy of the | ||
// objects pointed to by those keys. Then remove them from the source object. | ||
excludeList.forEach(function(exclusionPointer) { | ||
if (pointer.has(source, exclusionPointer)) { | ||
pointer.set(excludedObjects, exclusionPointer, pointer.get(source, exclusionPointer)); | ||
pointer.remove(source, exclusionPointer); | ||
} | ||
}); | ||
|
||
// Handle objects | ||
Object.keys(source).forEach(function(key) { | ||
|
||
// Cache snake_cased keys | ||
var snakedKey = _.snakeCase(key); | ||
|
||
// Exclude appropriately | ||
if( -1 === excludeList.indexOf(key)) { | ||
dest[snakedKey] = source[key]; | ||
if( _.isObject (source[key] ) ) { | ||
dest[snakedKey] = toApiFormat(source[key]); | ||
} | ||
} else { | ||
dest[snakedKey] = source[key]; | ||
} | ||
}); | ||
} catch(e) { | ||
// Errors | ||
return e; | ||
} | ||
// No errors return results | ||
return dest; | ||
// Make a clone of the remaining source object but with snaked case keys | ||
var target = snakedKeyClone(source); | ||
|
||
// Reinstated the un-modified objects into the target | ||
pointer.walk(excludedObjects, function(val, ptr) { | ||
pointer.set(target, ptr, val); | ||
}); | ||
|
||
return target; | ||
}; |
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
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
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