123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 'use strict';
- var has = Object.prototype.hasOwnProperty;
- /**
- * Gather environment variables from various locations.
- *
- * @param {Object} environment The default environment variables.
- * @returns {Object} environment.
- * @api public
- */
- function env(environment) {
- environment = environment || {};
- if ('object' === typeof process && 'object' === typeof process.env) {
- env.merge(environment, process.env);
- }
- if ('undefined' !== typeof window) {
- if ('string' === window.name && window.name.length) {
- env.merge(environment, env.parse(window.name));
- }
- if (window.localStorage) {
- try { env.merge(environment, env.parse(window.localStorage.env || window.localStorage.debug)); }
- catch (e) {}
- }
- if (
- 'object' === typeof window.location
- && 'string' === typeof window.location.hash
- && window.location.hash.length
- ) {
- env.merge(environment, env.parse(window.location.hash.charAt(0) === '#'
- ? window.location.hash.slice(1)
- : window.location.hash
- ));
- }
- }
- //
- // Also add lower case variants to the object for easy access.
- //
- var key, lower;
- for (key in environment) {
- lower = key.toLowerCase();
- if (!(lower in environment)) {
- environment[lower] = environment[key];
- }
- }
- return environment;
- }
- /**
- * A poor man's merge utility.
- *
- * @param {Object} base Object where the add object is merged in.
- * @param {Object} add Object that needs to be added to the base object.
- * @returns {Object} base
- * @api private
- */
- env.merge = function merge(base, add) {
- for (var key in add) {
- if (has.call(add, key)) {
- base[key] = add[key];
- }
- }
- return base;
- };
- /**
- * A poor man's query string parser.
- *
- * @param {String} query The query string that needs to be parsed.
- * @returns {Object} Key value mapped query string.
- * @api private
- */
- env.parse = function parse(query) {
- var parser = /([^=?&]+)=([^&]*)/g
- , result = {}
- , part;
- if (!query) return result;
- for (;
- part = parser.exec(query);
- result[decodeURIComponent(part[1])] = decodeURIComponent(part[2])
- );
- return result.env || result;
- };
- //
- // Expose the module
- //
- module.exports = env;
|