Scrape the first non-null value returned by an array of `rules` functions for
a single property in the metadata result dictionary.

@param {Cheerio} $
@param {String} url
@param {Array or Function} rules
@return {Promise} value
480821
function scrapeMetadatum ($, url, rules) {
  if (!Array.isArray(rules)) rules = [rules]

  return rules.reduce(function (promise, rule) {
    return promise.then(function (value) {
      if (value != null && value !== '') return value
      var next = rule($, url)
      if (next != null && next !== '') return next
      return null
    })
  }, Promise.resolve())
}
500179
function isMatchRule(req, pathname, rule) {
  if (typeof rule === 'string') {
    return pathname.indexOf(rule) > -1;
  } else if (rule instanceof RegExp) {
    return rule.test(pathname);
  } else if (typeof rule === 'function') {
    return rule(pathname, req);
  }
}
776281
function _traverseRules(sheet, base) {
            var i,
                href = sheet.href,
                cssRules;

            // Deal with Firefox's SecurityError when accessing sheets
            // from other domains. Chrome will safely return `undefined`.
            try {
                cssRules = sheet.cssRules;
            } catch (e) {
                if (e.name !== "SecurityError") {
                    throw e;
                }
            }

            if (href && cssRules) {
                if (rel.stylesheets[href] === undefined) {
                    rel.stylesheets[href] = [];
                }
                rel.stylesheets[href].push(base);

                for (i = 0; i < cssRules.length; i++) {
                    if (cssRules[i].href) {
                        traverseRules(cssRules[i].styleSheet, base);
                    }
                }
            }
        }
622120
function (url) {
	var result;

	LIST.some(function (state) {
		var params = state.pattern.match(url);

		if (params) {
			result       = state;
			state.params = params;
			return true;
		}
	});

	if (result) {
		var state        = result,
			parent_index = state.name.lastIndexOf('.'), parent_name;

		while (parent_index !== -1) {
			parent_name  = state.name.substring(0, parent_index);
			state.parent = STATES[parent_name];
			state        = state.parent;

			parent_index = state.name.lastIndexOf('.');
		}

		return result;
	}
}
298614
function getUrls(urls) {
    var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
    var settings = {
        mimeType: options.mimeType || null,
        onBeforeSend: options.onBeforeSend || Function.prototype,
        onSuccess: options.onSuccess || Function.prototype,
        onError: options.onError || Function.prototype,
        onComplete: options.onComplete || Function.prototype
    };
    var urlArray = Array.isArray(urls) ? urls : [ urls ];
    var urlQueue = Array.apply(null, Array(urlArray.length)).map(function(x) {
        return null;
    });
    function isValidCss() {
        var cssText = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
        var isHTML = cssText.trim().charAt(0) === "<";
        return !isHTML;
    }
    function onError(xhr, urlIndex) {
        settings.onError(xhr, urlArray[urlIndex], urlIndex);
    }
    function onSuccess(responseText, urlIndex) {
        var returnVal = settings.onSuccess(responseText, urlArray[urlIndex], urlIndex);
        responseText = returnVal === false ? "" : returnVal || responseText;
        urlQueue[urlIndex] = responseText;
        if (urlQueue.indexOf(null) === -1) {
            settings.onComplete(urlQueue);
        }
    }
    var parser = document.createElement("a");
    urlArray.forEach(function(url, i) {
        parser.setAttribute("href", url);
        parser.href = String(parser.href);
        var isIElte9 = Boolean(document.all && !window.atob);
        var isIElte9CORS = isIElte9 && parser.host.split(":")[0] !== location.host.split(":")[0];
        if (isIElte9CORS) {
            var isSameProtocol = parser.protocol === location.protocol;
            if (isSameProtocol) {
                var xdr = new XDomainRequest();
                xdr.open("GET", url);
                xdr.timeout = 0;
                xdr.onprogress = Function.prototype;
                xdr.ontimeout = Function.prototype;
                xdr.onload = function() {
                    if (isValidCss(xdr.responseText)) {
                        onSuccess(xdr.responseText, i);
                    } else {
                        onError(xdr, i);
                    }
                };
                xdr.onerror = function(err) {
                    onError(xdr, i);
                };
                setTimeout(function() {
                    xdr.send();
                }, 0);
            } else {
                console.warn("Internet Explorer 9 Cross-Origin (CORS) requests must use the same protocol (".concat(url, ")"));
                onError(null, i);
            }
        } else {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", url);
            if (settings.mimeType && xhr.overrideMimeType) {
                xhr.overrideMimeType(settings.mimeType);
            }
            settings.onBeforeSend(xhr, url, i);
            xhr.onreadystatechange = function() {
                if (xhr.readyState === 4) {
                    if (xhr.status === 200 && isValidCss(xhr.responseText)) {
                        onSuccess(xhr.responseText, i);
                    } else {
                        onError(xhr, i);
                    }
                }
            };
            xhr.send();
        }
    });
}
1063537
function getFromNpm(url) {
    var deferred = Q.defer();
    request({url: url, json: true}, function(err, res, data){
        if (err) {
            return deferred.reject(err);
        }

        if (!data) {
            return deferred.reject('No data?');
        }

        deferred.resolve(data);
    });
    return deferred.promise;
}
772835
function query (options$$1, next) {

    var urlParams = Object.keys(Url.options.params), query = {}, url = next(options$$1);

    each(options$$1.params, function (value, key) {
        if (urlParams.indexOf(key) === -1) {
            query[key] = value;
        }
    });

    query = Url.params(query);

    if (query) {
        url += (url.indexOf('?') == -1 ? '?' : '&') + query;
    }

    return url;
}
951614
function scrapeSearchPage(pageUrl) {

    return co(function *() {
        //Load the HTML page
        const $ = cheerio.load(yield request(pageUrl));

        //For each card, scrape some data
        const cards = Array.from($("#cardResults li")).map(el => {
            const $card = $(el);

            //Just scrape the database URL and the image for now
            const url = Url.resolve(SCRAPE_URL, $card.find("a").attr("href"));
            return {
                url: url,
                image: Url.resolve(SCRAPE_URL, $card.find("img").attr("src")),
                id: cardIdFromUrl(url)
            };
        });

        //Work out how many pages in total there are
        const totalText = $('#cards-load-more>div>span').text();
        const numPages = parseInt(/\d of (\d+)/.exec(totalText)[1]);

        return {
            numPages: numPages,
            cards: cards
        };

    })();
}
27066
function httpGetChecksum (algorithm, url) {
  if (arguments.length === 1) {
    url = algorithm;
    algorithm = 'sha1';
  }
  var deferred = Q.defer();
  var http;
  if (url.slice(0, 5) === 'https') {
    http = require('https');
  } else if (url.slice(0, 4) === 'http')  {
    http = require('http');
  } else {
    deferred.resolve('[ERROR] Unknown protocol.');
    return deferred.promise;
  }
  var parts = Url.parse(url);
  http.get({
    host: parts.hostname,
    port: parts.port,
    path: parts.pathname,
    agent: false
  }, function(res) {
    var code = res.statusCode;
    if (code === 301 || code === 302) {
      return deferred.resolve(httpGetChecksum(algorithm, res.headers.location));
    } else if (code !== 200) {
      return deferred.resolve('[ERROR] Fail to get the page. Status: ' + code);
    }
    var sum = crypto.createHash(algorithm);
    res.on('data', function(data) {
      sum.update(data);
    });
    res.on('end', function() {
      deferred.resolve(sum.digest('hex'));
    });
  }).on('error', function (e) {
    deferred.resolve('[ERROR] ' + e.toString());
  });
  return deferred.promise;
}
896801
function rules () {
        var pub = {};
        pub.maxLength = function (val, max) {

            if (!Array.isArray(val) && typeof val !== 'string' || typeof max !== 'number') {
                return getResult(new Error('rules.maxLength(fail): value must be a string or array, max must be a number'));
            }

            if (val.length > max) {
                return getResult(getError('maxLength', max, val));
            }

            return getResult(null);
        };
        pub.minLength = function (val, min) {

            if (!Array.isArray(val) && typeof val !== 'string' || typeof min !== 'number') {
                return getResult(new Error('rules.minLength(fail): value must be a string or array, min must be a number'));
            }

            if (val.length < min) {
                return getResult(getError('minLength', min, val));
            }

            return getResult(null);
        };
        pub.minimum = function (val, min) {

            if (typeof val !== 'number' || typeof min !== 'number') {
                return getResult(new Error('rules.minimum(fail): value must be a number, min must be a number'));
            }

            if (val < min) {
                return getResult(getError('minimum', min, val));
            }

            return getResult(null);
        };
        pub.maximum = function (val, max) {

            if (typeof val !== 'number' || typeof max !== 'number') {
                return getResult(new Error('rules.maximum(fail): value must be a number, max must be a number'));
            }

            if (val > max) {
                return getResult(getError('maximum', max, val));
            }

            return getResult(null);
        };
        pub.exclusiveMinimum = function (val, min) {

            if (typeof val !== 'number' || typeof min !== 'number') {
                return getResult(new Error('rules.exclusiveMinimum(fail): value must be a number, min must be a number'));
            }

            if (val <= min) {
                return getResult(getError('exclusiveMinimum', min, val));
            }

            return getResult(null);
        };
        pub.exclusiveMaximum = function (val, max) {

            if (typeof val !== 'number' || typeof max !== 'number') {
                return getResult(new Error('rules.exclusiveMaximum(fail): value must be a number, max must be a number'));
            }

            if (val >= max) {
                return getResult(getError('exclusiveMinimum', max, val));
            }

            return getResult(null);
        };
        pub.divisibleBy = function (val, div) {

            if (typeof val !== 'number' || typeof div !== 'number') {
                return getResult(new Error('rules.divisibleBy(fail): value must be a number, div must be a number'));
            }

            var multiplier = Math.max((val - Math.floor(val)).toString().length - 2,
                (div - Math.floor(div)).toString().length - 2);
            multiplier = multiplier > 0 ? Math.pow(10, multiplier) : 1;

            if ((val * multiplier) % (div * multiplier) !== 0) {
                return getResult(getError('divisibleBy', div, val));
            }

            return getResult(null);
        };
        pub.minItems = function (val, min) {

            if (!Array.isArray(val) || typeof min !== 'number') {
                return getResult(new Error('rules.minItems(fail): value must be a array, min must be a number'));
            }

            if (val.length < min) {
                return getResult(getError('minItems', min, val.length));
            }

            return getResult(null);
        };
        pub.maxItems = function (val, max) {

            if (!Array.isArray(val) || typeof max !== 'number') {
                return getResult(new Error('rules.maxItems(fail): value must be a array, max must be a number'));
            }

            if (val.length > max) {
                return getResult(getError('maxItems', max, val.length));
            }

            return getResult(null);
        };
        pub.uniqueItems = function (val) {

            if (!Array.isArray(val)) {
                return getResult(new Error('rules.uniqueItems(fail): value must be a array'));
            }

            if (!(uniqueArrayHelper(val))) {
                return getResult(getError('uniqueItems', val, true));
            }

            return getResult(null);
        };
        pub['enum'] = function (val, en) {

            if (typeof val === 'undefined' || !Array.isArray(en)) {
                return getResult(new Error('rules.enum(fail): value must be a defined and enum mus be a array'));
            }

            if (en.indexOf(val) === -1) {
                return getResult(getError('enum', en, val));
            }

            return getResult(null);
        };

        return pub;
    }
1276238
function ( rule ) {
    var exports = {};

    if ( ! ( this instanceof UrlHelper ) ) {
        return new UrlHelper( rule );
    }

    this._originalURI = this._extractURI( rule );

    if ( !this._originalURI && this._originalURI !== '') { return false; }

    this._originalRule = rule;

    this.URI = URI( this._originalURI );

    exports.URI             = this.URI;
    exports.getOriginalURI  = this.getOriginalURI.bind(  this );
    exports.getModifiedRule = this.getModifiedRule.bind( this );
    exports.getOriginalRule = this.getOriginalRule.bind( this );

    return exports;
}
616654
function getUrl (url) {
  var opt = {
    url: url
  , timeout: 10000 // 10 seconds
  }
  ;
  return new Promise(function(resolve, reject){
    request(opt, function(err, res, body){
      if (err) { return reject(err); }
      resolve(body)
    });
  });
}
860481
function RouteRule_parseRequest(method, url) {

        var matches,
            route,
            key,
            result = [],
            escape = [],
            params = {},
            current;
        //match pathname
        matches = this.match(this.pattern, this.trim(url.pathname, '/'));


        if (!matches.length || !this.checkMethod(method)) {
            return false;
        }
        params = url.query;


        for (key in params) {
            current = this.find(matches, key);
            if (!current || current.value === '') {
                matches.push({
                    key: key,
                    value: params[key]
                });
            }
        }

        matches.forEach(function (item) {
            var current = this.find(this.routeParams, item.key);
            if (current) {
                escape.push({
                    key: '<' + current.key + '>',
                    value: item.value
                });
                delete params[item.name];
            } else if (this.find(this.paramRules, item.key)) {
                params[item.key] = item.value;
            }
        }.bind(this));


        if (this.routeParams.length) {
            route = this.escape(this.route, escape);
        } else {
            route = this.route;
        }

        result.push(route);
        result.push(params);

        return result;
    }
851045
function get(url, opts){
	opts = opts || {};

	return requestp(_.extend(defaults, opts, {
		url: url
	}));
}
761108
function (url) {
			//ensure this is only the hash portion
			var params = PageUtil.matchResourceParameters(url);
			if (typeof params[this.token] !== "undefined") { //$NON-NLS-0$
				this.parameterValue = params[this.token];
				return this;
			}
			return null;
		}
1301660
function getJSONP(url, options) {
  return new Promise(function (resolve, reject) {
    if (!url) {
      reject(new Error('No request url'));
    }

    var _ref = options || {};

    var _ref$params = _ref.params;
    var params = _ref$params === undefined ? {} : _ref$params;
    var _ref$timeout = _ref.timeout;
    var timeout = _ref$timeout === undefined ? 15000 : _ref$timeout;
    // the funtion name of callback function

    var name = void 0;
    var timer = void 0;
    var requestUrl = parseUrl(url, params);
    // check whether the callbacl name has been defined
    var match = /callback=(\w+)/.exec(requestUrl);

    if (match && match[1]) {
      name = match[1];
    } else {
      // generate a random function name if no callback name was defined
      // return a function name generated by timestamp and random number
      // eg: jsonp_1355750852040_8260732076596469
      name = 'jsonp_' + getNow() + '_' + getRandomNumber();
      requestUrl += '&callback=' + name;
    }

    // create a script element
    var script = document.createElement('script');
    script.type = 'text/javascript';
    // set the request url
    script.src = requestUrl;
    // set the id of dom node which can be used to find this node
    script.id = 'id_' + name;

    var cleanUp = function cleanUp() {
      // exec callback and remove the callback
      window[name] = undefined;
      // get the script node
      var elem = document.getElementById('id_' + name);
      // remove added script node
      removeElement(elem);

      if (timer) {
        clearTimeout(timer);
      }
    };

    if (timeout) {
      timer = setTimeout(function () {
        cleanUp();
        reject(new Error('Timeout'));
      }, timeout);
    }

    // the callback when request finish
    window[name] = function (json) {
      cleanUp();
      // exec callback
      resolve(json);
    };

    // add script node in head
    var head = document.getElementsByTagName('head');

    if (head && head[0]) {
      head[0].appendChild(script);
    }
  });
}
1128855
function URLGET(url, fields, method, callback)
	{
		// method variable is optional
		if (typeof method == "function")
		{
			callback = method;
			method = "GET";
		}

		_URLGET(url, fields, GetHeaders({"Access-Control-Request-Method": method}), method, function(err, body) {
			if (err)
			{
				// got error, bounce it up
				if (callback) callback(err);
				return;
			}
			else
			{
				var JSONBody;
				if (typeof body == "object")
				{
					// already a JSON object?
					JSONBody = body;
				}
				else
				{
					// try to parse JSON body
					if (!body || !/\S/.test(body))
					{
						// string is empty, return empty object
						if (callback) callback(false, {});
						return;
					}
					else
					{
						try
						{
							JSONBody = JSON.parse(body);
						}
						catch(parse_err)
						{
							// error parsing JSON result
							Log("Parse JSON error: " + parse_err + "\r\nURL:\r\n" + url + "\r\nBody:\r\n" + body);
							if (callback) callback("Parse JSON error: " + parse_err);
							return;
						}
					}
				}

				// success! return JSON object
				if (callback) callback(false, JSONBody);
			}
		});
	}