Skip to content

Instantly share code, notes, and snippets.

@Marak
Created December 27, 2009 11:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Marak/264247 to your computer and use it in GitHub Desktop.
Save Marak/264247 to your computer and use it in GitHub Desktop.
// helper function for lazy initing objects
function jset( o,d ){
if(typeof eval(o) == 'undefined'){ eval( o + ' = ' + d + ';' ); }
return true;
}
// jset() usage
jset ( 'drip' , '{}' );
jset ( 'drip.value' , 42 );
jset ( 'drip.URI' , 'google.com' );
// Pseudo code, won't run
function jset( o , d , t){
if( typeof eval( o ) == 'undefined' ){
switch(t)
case 'numeric' : //numeric valication
case 'date' : // date validation / formating logic
case 'URI' : //URI validation logic
case 'route' : //route validation logic
if(all_validation_tests_pass)
eval( o + ' = ' + d + ';' );
}
return true;
}
// jset() usage
jset ( 'drip' , '{}' );
jset ( 'drip.value' , 42 , "numeric");
jset ( 'drip.startDate' , "12/29/2009 03:46:00" , "date");
jset ( 'drip.endDate' , "1/1/2010" , "date");
jset ( 'drip.route' , "/blah/path/to/resource" , "route");
jset ( 'drip.URI' , 'google.com', "URI" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment