Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The numbers are nothing like this, but I had a really similar experience to the author when doing Umbrella JS. With exceptions, but I've tried to keep every function down to few lines of code by doing heavy code reuse:

    // src/addclass/addclass.js
    // Add class(es) to the matched nodes
    u.prototype.addClass = function () {
      return this.eacharg(arguments, function (el, name) {
        el.classList.add(name);
      });
    };
While they don't do exactly the same (Umbrella JS is more flexible but jQuery supports IE9), compare that to jQuery's addClass():

    addClass: function( value ) {
    	var classes, elem, cur, curValue, clazz, j, finalValue,
    		i = 0;

    	if ( jQuery.isFunction( value ) ) {
    		return this.each( function( j ) {
    			jQuery( this ).addClass( value.call( this, j, getClass( this ) ) );
    		} );
    	}

    	if ( typeof value === "string" && value ) {
    		classes = value.match( rnothtmlwhite ) || [];

    		while ( ( elem = this[ i++ ] ) ) {
    			curValue = getClass( elem );
    			cur = elem.nodeType === 1 && ( " " + stripAndCollapse( curValue ) + " " );

    			if ( cur ) {
    				j = 0;
    				while ( ( clazz = classes[ j++ ] ) ) {
    					if ( cur.indexOf( " " + clazz + " " ) < 0 ) {
    						cur += clazz + " ";
    					}
    				}

    				// Only assign if different to avoid unneeded rendering.
    				finalValue = stripAndCollapse( cur );
    				if ( curValue !== finalValue ) {
    					elem.setAttribute( "class", finalValue );
    				}
    			}
    		}
    	}

    	return this;
    },


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: