/**
* a bit of css/utility scripting is loaded from
* http://www.nicon.nl/genericsvc/fiddle/fiddlemeta.js
*/
var someObj = runner();
Helper.log(
//-- Info --//
String.Format(
'<code>{0}</code>our test object (someObj)'+
'<pre><code>{2}{1}</code>our extension method</pre>'
,JSON.stringify(someObj)
,Object.tryPath
,'Object.tryPath = '
)
//-- Tests output --//
,String.Format(
[ '<b>Some testing (using the test object)</b>'
,"<code>Object.tryPath(someObj,'a.b.c.1.d.e')</code>{0}"
,"<code>Object.tryPath(someObj,'a.b.c.2.d.e')</code>{1}"
,"<code>Object.tryPath(someObj,'a.b.c.0.d.e')</code>{2}"
,"<code>Object.tryPath(someObj,'a,b,c')</code>{3}"
,"<code>Object.tryPath(someObj,'a.c')</code>{4}"].join('<br>')
,Object.tryPath(someObj,'a.b.c.1.d.e') //=> 6
,Object.tryPath(someObj,'a.b.c.2.d.e') //=> null
,Object.tryPath(someObj,'a.b.c.0.d.e') //=> 5
,Object.tryPath(someObj,'a,b,c') //=> [Object, Object]
,Object.tryPath(someObj,'a,c') //=> null
)
);
function runner(){
Object.tryPath = function(obj,path) {
path = path.split(/[.,]/);
while (path.length && obj) {
obj = obj[path.shift()];
}
return obj || null;
};
return {
a: {
b: {
c: [{
d: {
e: 5
}
},
{
d: {
e: 6
}
}
]
}
}
};
}