Loopback JSON to Model
by nsatija
HTML
<pre id="output"></pre>
JavaScript
var obj = {
Called: '+14243635422',
ToState: 'CA',
DialCallStatus: 'completed',
CallerCountry: 'US',
Direction: 'inbound',
CallerState: 'CA',
ToZip: '90245',
DialCallSid: 'CAc81c1603abdc479476a3e15ec97bd952',
CallSid: 'CA6e810369433327abfe2bdeefab613976',
To: '+14243635422',
CallerZip: '',
ToCountry: 'US',
ApiVersion: '2010-04-01',
CalledZip: '90245',
CalledCity: 'EL SEGUNDO',
CallStatus: 'in-progress',
From: '+14245008090',
AccountSid: 'ACb83cf9f826495d46c77bd999a6a96c56',
DialCallDuration: '11',
CalledCountry: 'US',
CallerCity: '',
Caller: '+14245008090',
FromCountry: 'US',
ToCity: 'EL SEGUNDO',
FromCity: '',
CalledState: 'CA',
FromZip: '',
FromState: 'CA',
}
function camelize(str) {
return str.replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index == 0 ? match.toLowerCase() : match.toUpperCase();
});
}
var arr = Object.keys(obj)
newArr = [];
for(var i = 0; i<arr.length; i++){
newArr[i] = '"' + camelize(arr[i]) + '" : {"type":"String","optional":true}';
}
function out()
{
var args = Array.prototype.slice.call(arguments, 0);
document.getElementById('output').innerHTML += args.join(" ") + "\n";
}
out(newArr.join(',\r\n'))