File: lib/combine.js

Recommend this page to a friend!
  Classes of Gamaiel Zavala  >  cmbn  >  lib/combine.js  >  Download  
File: lib/combine.js
Role: Class source
Content type: text/plain
Description: Class source
Class: cmbn
Generate combos of JavaScript code served via CDNs
Author: By
Last change: Add copyright and license
Date: 2 months ago
Size: 1,781 bytes
 

Contents

Class file image Download
/* Copyright (c) 2012 Yahoo! Inc.  All rights reserved.
   Copyrights licensed under the MIT License. See the accompanying LICENSE file for terms. */

;(function(){

var combine = function (urls, options)
{
    var opts = options || {},
        combined = '',
        lastCdn,
        segment;
    
    urls.forEach(function(u)
    {
        var ch = u.charAt(0);
        
        if('~' === ch)
        {
            segment = '/' + u;
        }
        else
        {
            var matches = new RegExp('^https?://([^/]+)([^?#]+)').exec(u),
                host = matches[1],
                path = matches[2],
                cdn = cdns.getCdnByHost(host) || [],
                c = cdn.code,
                asset;
            
            if(cdn)
            {
                var start = 1 + (cdn.prefix ? cdn.prefix.length : 0),
                    asset;
                
                asset = cdns.isFunc(cdn.deformatter) ? cdn.deformatter(path) : path.substr(start).replace(/\//g, ',');
                if(asset instanceof Array)
                {
                    c += '@' + asset[1];
                    asset = asset[0];
                }
                
                segment = (lastCdn === c ? '' : '/~' + c) + ';' + asset;
                lastCdn = c;
            }
        }
        
        combined += segment;
    });
    
    return (opts.protocol || 'http') + '://' + (opts.host || 'cmbn.us') + combined;
}

// STRIP
if(typeof module !== 'undefined')
{
    module.exports.combine = combine;
    var cdns = require('./cdns');
}
else
{
// END_STRIP
    var win = function(){ return this; }(),
        cmbn = win.cmbn;
    
    if(cmbn)
    {
        var cdns = cmbn.cdns;
        cmbn.client = {combine: combine};
    }
// STRIP
}
// END_STRIP

})();