UNPKG

funcunit

Version:
135 lines (129 loc) 3.95 kB
(function(global){ // helpers var camelize = function(str){ return str.replace(/-+(.)?/g, function(match, chr){ return chr ? chr.toUpperCase() : '' }); }, each = function( o, cb){ var i, len; // weak array detection, but we only use this internally so don't // pass it weird stuff if ( typeof o.length == 'number' && (o.length - 1) in o) { for ( i = 0, len = o.length; i < len; i++ ) { cb.call(o[i], o[i], i, o); } } else { for ( i in o ) { if(o.hasOwnProperty(i)){ cb.call(o[i], o[i], i, o); } } } return o; }, map = function(o, cb) { var arr = []; each(o, function(item, i){ arr[i] = cb(item, i); }); return arr; }, isString = function(o) { return typeof o == "string"; }, extend = function(d,s){ each(s, function(v, p){ d[p] = v; }); return d; }, dir = function(uri){ var lastSlash = uri.lastIndexOf("/"); //if no / slashes, check for \ slashes since it might be a windows path if(lastSlash === -1) lastSlash = uri.lastIndexOf("\\"); if(lastSlash !== -1) { return uri.substr(0, lastSlash); } else { return uri; } }, last = function(arr){ return arr[arr.length - 1]; }, parseURI = function(url) { var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/); // authority = '//' + user + ':' + pass '@' + hostname + ':' port return (m ? { href : m[0] || '', protocol : m[1] || '', authority: m[2] || '', host : m[3] || '', hostname : m[4] || '', port : m[5] || '', pathname : m[6] || '', search : m[7] || '', hash : m[8] || '' } : null); }, joinURIs = function(base, href) { function removeDotSegments(input) { var output = []; input.replace(/^(\.\.?(\/|$))+/, '') .replace(/\/(\.(\/|$))+/g, '/') .replace(/\/\.\.$/, '/../') .replace(/\/?[^\/]*/g, function (p) { if (p === '/..') { output.pop(); } else { output.push(p); } }); return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : ''); } href = parseURI(href || ''); base = parseURI(base || ''); return !href || !base ? null : (href.protocol || base.protocol) + (href.protocol || href.authority ? href.authority : base.authority) + removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname)) + (href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) + href.hash; }, relativeURI = function(base, path) { var uriParts = path.split("/"), baseParts = base.split("/"), result = []; while ( uriParts.length && baseParts.length && uriParts[0] == baseParts[0] ) { uriParts.shift(); baseParts.shift(); } for(var i = 0 ; i< baseParts.length-1; i++) { result.push("../"); } return "./" + result.join("") + uriParts.join("/"); }, isWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope, isNode = typeof process === "object" && {}.toString.call(process) === "[object process]", isBrowserWithWindow = !isNode && typeof window !== "undefined", isNW = isNode && (function(){ try { return require("nw.gui") !== "undefined"; } catch(e) { return false; } })(), getStealScript = function(){ if(isBrowserWithWindow || isNW) { if(document.currentScript) { return document.currentScript; } var scripts = document.scripts; if (scripts.length) { var currentScript = scripts[scripts.length - 1]; return currentScript; } } }, stealScript = getStealScript(); isNode = isNode && !isNW;