twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

util.js (11379B)


      1 "use strict";
      2 var es5 = require("./es5");
      3 var canEvaluate = typeof navigator == "undefined";
      4 
      5 var errorObj = {e: {}};
      6 var tryCatchTarget;
      7 var globalObject = typeof self !== "undefined" ? self :
      8     typeof window !== "undefined" ? window :
      9     typeof global !== "undefined" ? global :
     10     this !== undefined ? this : null;
     11 
     12 function tryCatcher() {
     13     try {
     14         var target = tryCatchTarget;
     15         tryCatchTarget = null;
     16         return target.apply(this, arguments);
     17     } catch (e) {
     18         errorObj.e = e;
     19         return errorObj;
     20     }
     21 }
     22 function tryCatch(fn) {
     23     tryCatchTarget = fn;
     24     return tryCatcher;
     25 }
     26 
     27 var inherits = function(Child, Parent) {
     28     var hasProp = {}.hasOwnProperty;
     29 
     30     function T() {
     31         this.constructor = Child;
     32         this.constructor$ = Parent;
     33         for (var propertyName in Parent.prototype) {
     34             if (hasProp.call(Parent.prototype, propertyName) &&
     35                 propertyName.charAt(propertyName.length-1) !== "$"
     36            ) {
     37                 this[propertyName + "$"] = Parent.prototype[propertyName];
     38             }
     39         }
     40     }
     41     T.prototype = Parent.prototype;
     42     Child.prototype = new T();
     43     return Child.prototype;
     44 };
     45 
     46 
     47 function isPrimitive(val) {
     48     return val == null || val === true || val === false ||
     49         typeof val === "string" || typeof val === "number";
     50 
     51 }
     52 
     53 function isObject(value) {
     54     return typeof value === "function" ||
     55            typeof value === "object" && value !== null;
     56 }
     57 
     58 function maybeWrapAsError(maybeError) {
     59     if (!isPrimitive(maybeError)) return maybeError;
     60 
     61     return new Error(safeToString(maybeError));
     62 }
     63 
     64 function withAppended(target, appendee) {
     65     var len = target.length;
     66     var ret = new Array(len + 1);
     67     var i;
     68     for (i = 0; i < len; ++i) {
     69         ret[i] = target[i];
     70     }
     71     ret[i] = appendee;
     72     return ret;
     73 }
     74 
     75 function getDataPropertyOrDefault(obj, key, defaultValue) {
     76     if (es5.isES5) {
     77         var desc = Object.getOwnPropertyDescriptor(obj, key);
     78 
     79         if (desc != null) {
     80             return desc.get == null && desc.set == null
     81                     ? desc.value
     82                     : defaultValue;
     83         }
     84     } else {
     85         return {}.hasOwnProperty.call(obj, key) ? obj[key] : undefined;
     86     }
     87 }
     88 
     89 function notEnumerableProp(obj, name, value) {
     90     if (isPrimitive(obj)) return obj;
     91     var descriptor = {
     92         value: value,
     93         configurable: true,
     94         enumerable: false,
     95         writable: true
     96     };
     97     es5.defineProperty(obj, name, descriptor);
     98     return obj;
     99 }
    100 
    101 function thrower(r) {
    102     throw r;
    103 }
    104 
    105 var inheritedDataKeys = (function() {
    106     var excludedPrototypes = [
    107         Array.prototype,
    108         Object.prototype,
    109         Function.prototype
    110     ];
    111 
    112     var isExcludedProto = function(val) {
    113         for (var i = 0; i < excludedPrototypes.length; ++i) {
    114             if (excludedPrototypes[i] === val) {
    115                 return true;
    116             }
    117         }
    118         return false;
    119     };
    120 
    121     if (es5.isES5) {
    122         var getKeys = Object.getOwnPropertyNames;
    123         return function(obj) {
    124             var ret = [];
    125             var visitedKeys = Object.create(null);
    126             while (obj != null && !isExcludedProto(obj)) {
    127                 var keys;
    128                 try {
    129                     keys = getKeys(obj);
    130                 } catch (e) {
    131                     return ret;
    132                 }
    133                 for (var i = 0; i < keys.length; ++i) {
    134                     var key = keys[i];
    135                     if (visitedKeys[key]) continue;
    136                     visitedKeys[key] = true;
    137                     var desc = Object.getOwnPropertyDescriptor(obj, key);
    138                     if (desc != null && desc.get == null && desc.set == null) {
    139                         ret.push(key);
    140                     }
    141                 }
    142                 obj = es5.getPrototypeOf(obj);
    143             }
    144             return ret;
    145         };
    146     } else {
    147         var hasProp = {}.hasOwnProperty;
    148         return function(obj) {
    149             if (isExcludedProto(obj)) return [];
    150             var ret = [];
    151 
    152             /*jshint forin:false */
    153             enumeration: for (var key in obj) {
    154                 if (hasProp.call(obj, key)) {
    155                     ret.push(key);
    156                 } else {
    157                     for (var i = 0; i < excludedPrototypes.length; ++i) {
    158                         if (hasProp.call(excludedPrototypes[i], key)) {
    159                             continue enumeration;
    160                         }
    161                     }
    162                     ret.push(key);
    163                 }
    164             }
    165             return ret;
    166         };
    167     }
    168 
    169 })();
    170 
    171 var thisAssignmentPattern = /this\s*\.\s*\S+\s*=/;
    172 function isClass(fn) {
    173     try {
    174         if (typeof fn === "function") {
    175             var keys = es5.names(fn.prototype);
    176 
    177             var hasMethods = es5.isES5 && keys.length > 1;
    178             var hasMethodsOtherThanConstructor = keys.length > 0 &&
    179                 !(keys.length === 1 && keys[0] === "constructor");
    180             var hasThisAssignmentAndStaticMethods =
    181                 thisAssignmentPattern.test(fn + "") && es5.names(fn).length > 0;
    182 
    183             if (hasMethods || hasMethodsOtherThanConstructor ||
    184                 hasThisAssignmentAndStaticMethods) {
    185                 return true;
    186             }
    187         }
    188         return false;
    189     } catch (e) {
    190         return false;
    191     }
    192 }
    193 
    194 function toFastProperties(obj) {
    195     /*jshint -W027,-W055,-W031*/
    196     function FakeConstructor() {}
    197     FakeConstructor.prototype = obj;
    198     var receiver = new FakeConstructor();
    199     function ic() {
    200         return typeof receiver.foo;
    201     }
    202     ic();
    203     ic();
    204     return obj;
    205     eval(obj);
    206 }
    207 
    208 var rident = /^[a-z$_][a-z$_0-9]*$/i;
    209 function isIdentifier(str) {
    210     return rident.test(str);
    211 }
    212 
    213 function filledRange(count, prefix, suffix) {
    214     var ret = new Array(count);
    215     for(var i = 0; i < count; ++i) {
    216         ret[i] = prefix + i + suffix;
    217     }
    218     return ret;
    219 }
    220 
    221 function safeToString(obj) {
    222     try {
    223         return obj + "";
    224     } catch (e) {
    225         return "[no string representation]";
    226     }
    227 }
    228 
    229 function isError(obj) {
    230     return obj instanceof Error ||
    231         (obj !== null &&
    232            typeof obj === "object" &&
    233            typeof obj.message === "string" &&
    234            typeof obj.name === "string");
    235 }
    236 
    237 function markAsOriginatingFromRejection(e) {
    238     try {
    239         notEnumerableProp(e, "isOperational", true);
    240     }
    241     catch(ignore) {}
    242 }
    243 
    244 function originatesFromRejection(e) {
    245     if (e == null) return false;
    246     return ((e instanceof Error["__BluebirdErrorTypes__"].OperationalError) ||
    247         e["isOperational"] === true);
    248 }
    249 
    250 function canAttachTrace(obj) {
    251     return isError(obj) && es5.propertyIsWritable(obj, "stack");
    252 }
    253 
    254 var ensureErrorObject = (function() {
    255     if (!("stack" in new Error())) {
    256         return function(value) {
    257             if (canAttachTrace(value)) return value;
    258             try {throw new Error(safeToString(value));}
    259             catch(err) {return err;}
    260         };
    261     } else {
    262         return function(value) {
    263             if (canAttachTrace(value)) return value;
    264             return new Error(safeToString(value));
    265         };
    266     }
    267 })();
    268 
    269 function classString(obj) {
    270     return {}.toString.call(obj);
    271 }
    272 
    273 function copyDescriptors(from, to, filter) {
    274     var keys = es5.names(from);
    275     for (var i = 0; i < keys.length; ++i) {
    276         var key = keys[i];
    277         if (filter(key)) {
    278             try {
    279                 es5.defineProperty(to, key, es5.getDescriptor(from, key));
    280             } catch (ignore) {}
    281         }
    282     }
    283 }
    284 
    285 var asArray = function(v) {
    286     if (es5.isArray(v)) {
    287         return v;
    288     }
    289     return null;
    290 };
    291 
    292 if (typeof Symbol !== "undefined" && Symbol.iterator) {
    293     var ArrayFrom = typeof Array.from === "function" ? function(v) {
    294         return Array.from(v);
    295     } : function(v) {
    296         var ret = [];
    297         var it = v[Symbol.iterator]();
    298         var itResult;
    299         while (!((itResult = it.next()).done)) {
    300             ret.push(itResult.value);
    301         }
    302         return ret;
    303     };
    304 
    305     asArray = function(v) {
    306         if (es5.isArray(v)) {
    307             return v;
    308         } else if (v != null && typeof v[Symbol.iterator] === "function") {
    309             return ArrayFrom(v);
    310         }
    311         return null;
    312     };
    313 }
    314 
    315 var isNode = typeof process !== "undefined" &&
    316         classString(process).toLowerCase() === "[object process]";
    317 
    318 var hasEnvVariables = typeof process !== "undefined" &&
    319     typeof process.env !== "undefined";
    320 
    321 function env(key) {
    322     return hasEnvVariables ? process.env[key] : undefined;
    323 }
    324 
    325 function getNativePromise() {
    326     if (typeof Promise === "function") {
    327         try {
    328             var promise = new Promise(function(){});
    329             if (classString(promise) === "[object Promise]") {
    330                 return Promise;
    331             }
    332         } catch (e) {}
    333     }
    334 }
    335 
    336 var reflectHandler;
    337 function contextBind(ctx, cb) {
    338     if (ctx === null ||
    339         typeof cb !== "function" ||
    340         cb === reflectHandler) {
    341         return cb;
    342     }
    343 
    344     if (ctx.domain !== null) {
    345         cb = ctx.domain.bind(cb);
    346     }
    347 
    348     var async = ctx.async;
    349     if (async !== null) {
    350         var old = cb;
    351         cb = function() {
    352             var $_len = arguments.length + 2;var args = new Array($_len); for(var $_i = 2; $_i < $_len ; ++$_i) {args[$_i] = arguments[$_i  - 2];};
    353             args[0] = old;
    354             args[1] = this;
    355             return async.runInAsyncScope.apply(async, args);
    356         };
    357     }
    358     return cb;
    359 }
    360 
    361 var ret = {
    362     setReflectHandler: function(fn) {
    363         reflectHandler = fn;
    364     },
    365     isClass: isClass,
    366     isIdentifier: isIdentifier,
    367     inheritedDataKeys: inheritedDataKeys,
    368     getDataPropertyOrDefault: getDataPropertyOrDefault,
    369     thrower: thrower,
    370     isArray: es5.isArray,
    371     asArray: asArray,
    372     notEnumerableProp: notEnumerableProp,
    373     isPrimitive: isPrimitive,
    374     isObject: isObject,
    375     isError: isError,
    376     canEvaluate: canEvaluate,
    377     errorObj: errorObj,
    378     tryCatch: tryCatch,
    379     inherits: inherits,
    380     withAppended: withAppended,
    381     maybeWrapAsError: maybeWrapAsError,
    382     toFastProperties: toFastProperties,
    383     filledRange: filledRange,
    384     toString: safeToString,
    385     canAttachTrace: canAttachTrace,
    386     ensureErrorObject: ensureErrorObject,
    387     originatesFromRejection: originatesFromRejection,
    388     markAsOriginatingFromRejection: markAsOriginatingFromRejection,
    389     classString: classString,
    390     copyDescriptors: copyDescriptors,
    391     isNode: isNode,
    392     hasEnvVariables: hasEnvVariables,
    393     env: env,
    394     global: globalObject,
    395     getNativePromise: getNativePromise,
    396     contextBind: contextBind
    397 };
    398 ret.isRecentNode = ret.isNode && (function() {
    399     var version;
    400     if (process.versions && process.versions.node) {
    401         version = process.versions.node.split(".").map(Number);
    402     } else if (process.version) {
    403         version = process.version.split(".").map(Number);
    404     }
    405     return (version[0] === 0 && version[1] > 10) || (version[0] > 0);
    406 })();
    407 ret.nodeSupportsAsyncResource = ret.isNode && (function() {
    408     var supportsAsync = false;
    409     try {
    410         var res = require("async_hooks").AsyncResource;
    411         supportsAsync = typeof res.prototype.runInAsyncScope === "function";
    412     } catch (e) {
    413         supportsAsync = false;
    414     }
    415     return supportsAsync;
    416 })();
    417 
    418 if (ret.isNode) ret.toFastProperties(process);
    419 
    420 try {throw new Error(); } catch (e) {ret.lastLineError = e;}
    421 module.exports = ret;