Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Created May 24, 2012 19:00
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 brianswisher/2783557 to your computer and use it in GitHub Desktop.
Save brianswisher/2783557 to your computer and use it in GitHub Desktop.
Functional Javascript Programming
var util = (function( getElementById,
createElement,
argumentsToArray,
innerHTML,
style,
inside,
after,
undefined ){
var cache = function( key, value ){
var rtn, func = arguments.callee;
rtn = func._cache = func._cache || {};
if( arguments.length < 1 ) { rtn = func._cache; }
else if( arguments.length < 2 ) { rtn = func._cache[ key ]; }
else if( arguments.length > 1 ) { func._cache[ key ] = value; }
return rtn;
}, chain = function(){
var scope, func, args = argumentsToArray( arguments );
scope = args.shift();
func = args.shift();
if (func) { func.apply( scope, args ); }
return scope;
}, map = function( obj, func ){
for( var i in obj ){
if ( obj.hasOwnProperty(i) ) {
func.apply( this, [ i, obj[i] ] );
}
}
};
return {
cache: cache,
id: function( id ){
return chain( this, function(){
cache( 'id', id );
cache( 'target', getElementById( id ) );
});
},
target: function( target ){
return chain(this, function( type, func ){
if(type==='function') {
func.apply( this, [ cache('target') ] );
} else {
cache( 'target', target );
}
}, typeof( target ), target);
},
content:function( content ){
return chain(this, function(){
cache( 'content', content );
innerHTML( cache( 'target' ), content );
});
},
css:function( css ){
return chain( this, function(){
cache( 'css', css );
map( css, function( i, item ){
style( cache( 'target' ), i, css[i] );
});
});
},
container: function( container ){
return chain( this, function(){
cache( 'container', container );
});
},
html: function( html ){
return chain( this, function(){
cache( 'html', html );
});
},
insert: function( insert ){
return chain( this, function(){
if( insert != null ){ cache( 'insert', insert ) };
cache( 'element', createElement( cache( 'container' ) ) );
innerHTML( cache( 'element' ), cache( 'html' ) );
switch ( cache( 'insert' ) ) {
case 'inside':
inside( cache( 'target' ), cache( 'element' ) );
break;
case 'after':
after( cache( 'target' ), cache( 'element' ) );
break;
}
cache( 'target' , cache( 'element' ));
});
},
style: function( style ){
return chain( this, function(){
cache( 'style', style );
});
},
format: function( format ){
return chain( this, function(){
cache( 'format', format );
style( cache( 'target' ), cache( 'style' ), format );
});
}
};
}( function( id ){ return document.getElementById( id ); }, //getElementById
function( tag ){ return document.createElement( tag ); }, //createElement
function( arguments ){ return Array.prototype.slice.apply( arguments ); }, //argumentsToArray
function( target, html ){ target.innerHTML = html; }, //innerHTML
function( target, style, format ){ target.style[style] = format; }, //style
function( target, node ){ target.appendChild( node ); }, //inside
function( target, node ){ target.parentNode.insertBefore( node, target.nextSibling ); } )); //after
util.id('lga').content('<div id="myStuff"></div>').id('myStuff').css({'font-size':'100px','font-family':'serif','text-align':'center'}).container('span').html('B').insert('inside').style('color').format('blue').html('o').insert('after').format('red').insert().format('orange').html('g').insert().format('blue').html('i').insert().format('green').html('e').insert().format('red').id('gbqfq').target(function(t){t.value='all night long'});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment