Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

util.js 10KB

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