utils.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. 'use strict';
  2. var bind = require('./helpers/bind');
  3. // utils is a library of generic helper functions non-specific to axios
  4. var toString = Object.prototype.toString;
  5. // eslint-disable-next-line func-names
  6. var kindOf = (function(cache) {
  7. // eslint-disable-next-line func-names
  8. return function(thing) {
  9. var str = toString.call(thing);
  10. return cache[str] || (cache[str] = str.slice(8, -1).toLowerCase());
  11. };
  12. })(Object.create(null));
  13. function kindOfTest(type) {
  14. type = type.toLowerCase();
  15. return function isKindOf(thing) {
  16. return kindOf(thing) === type;
  17. };
  18. }
  19. /**
  20. * Determine if a value is an Array
  21. *
  22. * @param {Object} val The value to test
  23. * @returns {boolean} True if value is an Array, otherwise false
  24. */
  25. function isArray(val) {
  26. return Array.isArray(val);
  27. }
  28. /**
  29. * Determine if a value is undefined
  30. *
  31. * @param {Object} val The value to test
  32. * @returns {boolean} True if the value is undefined, otherwise false
  33. */
  34. function isUndefined(val) {
  35. return typeof val === 'undefined';
  36. }
  37. /**
  38. * Determine if a value is a Buffer
  39. *
  40. * @param {Object} val The value to test
  41. * @returns {boolean} True if value is a Buffer, otherwise false
  42. */
  43. function isBuffer(val) {
  44. return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor)
  45. && typeof val.constructor.isBuffer === 'function' && val.constructor.isBuffer(val);
  46. }
  47. /**
  48. * Determine if a value is an ArrayBuffer
  49. *
  50. * @function
  51. * @param {Object} val The value to test
  52. * @returns {boolean} True if value is an ArrayBuffer, otherwise false
  53. */
  54. var isArrayBuffer = kindOfTest('ArrayBuffer');
  55. /**
  56. * Determine if a value is a view on an ArrayBuffer
  57. *
  58. * @param {Object} val The value to test
  59. * @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
  60. */
  61. function isArrayBufferView(val) {
  62. var result;
  63. if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
  64. result = ArrayBuffer.isView(val);
  65. } else {
  66. result = (val) && (val.buffer) && (isArrayBuffer(val.buffer));
  67. }
  68. return result;
  69. }
  70. /**
  71. * Determine if a value is a String
  72. *
  73. * @param {Object} val The value to test
  74. * @returns {boolean} True if value is a String, otherwise false
  75. */
  76. function isString(val) {
  77. return typeof val === 'string';
  78. }
  79. /**
  80. * Determine if a value is a Number
  81. *
  82. * @param {Object} val The value to test
  83. * @returns {boolean} True if value is a Number, otherwise false
  84. */
  85. function isNumber(val) {
  86. return typeof val === 'number';
  87. }
  88. /**
  89. * Determine if a value is an Object
  90. *
  91. * @param {Object} val The value to test
  92. * @returns {boolean} True if value is an Object, otherwise false
  93. */
  94. function isObject(val) {
  95. return val !== null && typeof val === 'object';
  96. }
  97. /**
  98. * Determine if a value is a plain Object
  99. *
  100. * @param {Object} val The value to test
  101. * @return {boolean} True if value is a plain Object, otherwise false
  102. */
  103. function isPlainObject(val) {
  104. if (kindOf(val) !== 'object') {
  105. return false;
  106. }
  107. var prototype = Object.getPrototypeOf(val);
  108. return prototype === null || prototype === Object.prototype;
  109. }
  110. /**
  111. * Determine if a value is a Date
  112. *
  113. * @function
  114. * @param {Object} val The value to test
  115. * @returns {boolean} True if value is a Date, otherwise false
  116. */
  117. var isDate = kindOfTest('Date');
  118. /**
  119. * Determine if a value is a File
  120. *
  121. * @function
  122. * @param {Object} val The value to test
  123. * @returns {boolean} True if value is a File, otherwise false
  124. */
  125. var isFile = kindOfTest('File');
  126. /**
  127. * Determine if a value is a Blob
  128. *
  129. * @function
  130. * @param {Object} val The value to test
  131. * @returns {boolean} True if value is a Blob, otherwise false
  132. */
  133. var isBlob = kindOfTest('Blob');
  134. /**
  135. * Determine if a value is a FileList
  136. *
  137. * @function
  138. * @param {Object} val The value to test
  139. * @returns {boolean} True if value is a File, otherwise false
  140. */
  141. var isFileList = kindOfTest('FileList');
  142. /**
  143. * Determine if a value is a Function
  144. *
  145. * @param {Object} val The value to test
  146. * @returns {boolean} True if value is a Function, otherwise false
  147. */
  148. function isFunction(val) {
  149. return toString.call(val) === '[object Function]';
  150. }
  151. /**
  152. * Determine if a value is a Stream
  153. *
  154. * @param {Object} val The value to test
  155. * @returns {boolean} True if value is a Stream, otherwise false
  156. */
  157. function isStream(val) {
  158. return isObject(val) && isFunction(val.pipe);
  159. }
  160. /**
  161. * Determine if a value is a FormData
  162. *
  163. * @param {Object} thing The value to test
  164. * @returns {boolean} True if value is an FormData, otherwise false
  165. */
  166. function isFormData(thing) {
  167. var pattern = '[object FormData]';
  168. return thing && (
  169. (typeof FormData === 'function' && thing instanceof FormData) ||
  170. toString.call(thing) === pattern ||
  171. (isFunction(thing.toString) && thing.toString() === pattern)
  172. );
  173. }
  174. /**
  175. * Determine if a value is a URLSearchParams object
  176. * @function
  177. * @param {Object} val The value to test
  178. * @returns {boolean} True if value is a URLSearchParams object, otherwise false
  179. */
  180. var isURLSearchParams = kindOfTest('URLSearchParams');
  181. /**
  182. * Trim excess whitespace off the beginning and end of a string
  183. *
  184. * @param {String} str The String to trim
  185. * @returns {String} The String freed of excess whitespace
  186. */
  187. function trim(str) {
  188. return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
  189. }
  190. /**
  191. * Determine if we're running in a standard browser environment
  192. *
  193. * This allows axios to run in a web worker, and react-native.
  194. * Both environments support XMLHttpRequest, but not fully standard globals.
  195. *
  196. * web workers:
  197. * typeof window -> undefined
  198. * typeof document -> undefined
  199. *
  200. * react-native:
  201. * navigator.product -> 'ReactNative'
  202. * nativescript
  203. * navigator.product -> 'NativeScript' or 'NS'
  204. */
  205. function isStandardBrowserEnv() {
  206. if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
  207. navigator.product === 'NativeScript' ||
  208. navigator.product === 'NS')) {
  209. return false;
  210. }
  211. return (
  212. typeof window !== 'undefined' &&
  213. typeof document !== 'undefined'
  214. );
  215. }
  216. /**
  217. * Iterate over an Array or an Object invoking a function for each item.
  218. *
  219. * If `obj` is an Array callback will be called passing
  220. * the value, index, and complete array for each item.
  221. *
  222. * If 'obj' is an Object callback will be called passing
  223. * the value, key, and complete object for each property.
  224. *
  225. * @param {Object|Array} obj The object to iterate
  226. * @param {Function} fn The callback to invoke for each item
  227. */
  228. function forEach(obj, fn) {
  229. // Don't bother if no value provided
  230. if (obj === null || typeof obj === 'undefined') {
  231. return;
  232. }
  233. // Force an array if not already something iterable
  234. if (typeof obj !== 'object') {
  235. /*eslint no-param-reassign:0*/
  236. obj = [obj];
  237. }
  238. if (isArray(obj)) {
  239. // Iterate over array values
  240. for (var i = 0, l = obj.length; i < l; i++) {
  241. fn.call(null, obj[i], i, obj);
  242. }
  243. } else {
  244. // Iterate over object keys
  245. for (var key in obj) {
  246. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  247. fn.call(null, obj[key], key, obj);
  248. }
  249. }
  250. }
  251. }
  252. /**
  253. * Accepts varargs expecting each argument to be an object, then
  254. * immutably merges the properties of each object and returns result.
  255. *
  256. * When multiple objects contain the same key the later object in
  257. * the arguments list will take precedence.
  258. *
  259. * Example:
  260. *
  261. * ```js
  262. * var result = merge({foo: 123}, {foo: 456});
  263. * console.log(result.foo); // outputs 456
  264. * ```
  265. *
  266. * @param {Object} obj1 Object to merge
  267. * @returns {Object} Result of all merge properties
  268. */
  269. function merge(/* obj1, obj2, obj3, ... */) {
  270. var result = {};
  271. function assignValue(val, key) {
  272. if (isPlainObject(result[key]) && isPlainObject(val)) {
  273. result[key] = merge(result[key], val);
  274. } else if (isPlainObject(val)) {
  275. result[key] = merge({}, val);
  276. } else if (isArray(val)) {
  277. result[key] = val.slice();
  278. } else {
  279. result[key] = val;
  280. }
  281. }
  282. for (var i = 0, l = arguments.length; i < l; i++) {
  283. forEach(arguments[i], assignValue);
  284. }
  285. return result;
  286. }
  287. /**
  288. * Extends object a by mutably adding to it the properties of object b.
  289. *
  290. * @param {Object} a The object to be extended
  291. * @param {Object} b The object to copy properties from
  292. * @param {Object} thisArg The object to bind function to
  293. * @return {Object} The resulting value of object a
  294. */
  295. function extend(a, b, thisArg) {
  296. forEach(b, function assignValue(val, key) {
  297. if (thisArg && typeof val === 'function') {
  298. a[key] = bind(val, thisArg);
  299. } else {
  300. a[key] = val;
  301. }
  302. });
  303. return a;
  304. }
  305. /**
  306. * Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  307. *
  308. * @param {string} content with BOM
  309. * @return {string} content value without BOM
  310. */
  311. function stripBOM(content) {
  312. if (content.charCodeAt(0) === 0xFEFF) {
  313. content = content.slice(1);
  314. }
  315. return content;
  316. }
  317. /**
  318. * Inherit the prototype methods from one constructor into another
  319. * @param {function} constructor
  320. * @param {function} superConstructor
  321. * @param {object} [props]
  322. * @param {object} [descriptors]
  323. */
  324. function inherits(constructor, superConstructor, props, descriptors) {
  325. constructor.prototype = Object.create(superConstructor.prototype, descriptors);
  326. constructor.prototype.constructor = constructor;
  327. props && Object.assign(constructor.prototype, props);
  328. }
  329. /**
  330. * Resolve object with deep prototype chain to a flat object
  331. * @param {Object} sourceObj source object
  332. * @param {Object} [destObj]
  333. * @param {Function} [filter]
  334. * @returns {Object}
  335. */
  336. function toFlatObject(sourceObj, destObj, filter) {
  337. var props;
  338. var i;
  339. var prop;
  340. var merged = {};
  341. destObj = destObj || {};
  342. do {
  343. props = Object.getOwnPropertyNames(sourceObj);
  344. i = props.length;
  345. while (i-- > 0) {
  346. prop = props[i];
  347. if (!merged[prop]) {
  348. destObj[prop] = sourceObj[prop];
  349. merged[prop] = true;
  350. }
  351. }
  352. sourceObj = Object.getPrototypeOf(sourceObj);
  353. } while (sourceObj && (!filter || filter(sourceObj, destObj)) && sourceObj !== Object.prototype);
  354. return destObj;
  355. }
  356. /*
  357. * determines whether a string ends with the characters of a specified string
  358. * @param {String} str
  359. * @param {String} searchString
  360. * @param {Number} [position= 0]
  361. * @returns {boolean}
  362. */
  363. function endsWith(str, searchString, position) {
  364. str = String(str);
  365. if (position === undefined || position > str.length) {
  366. position = str.length;
  367. }
  368. position -= searchString.length;
  369. var lastIndex = str.indexOf(searchString, position);
  370. return lastIndex !== -1 && lastIndex === position;
  371. }
  372. /**
  373. * Returns new array from array like object
  374. * @param {*} [thing]
  375. * @returns {Array}
  376. */
  377. function toArray(thing) {
  378. if (!thing) return null;
  379. var i = thing.length;
  380. if (isUndefined(i)) return null;
  381. var arr = new Array(i);
  382. while (i-- > 0) {
  383. arr[i] = thing[i];
  384. }
  385. return arr;
  386. }
  387. // eslint-disable-next-line func-names
  388. var isTypedArray = (function(TypedArray) {
  389. // eslint-disable-next-line func-names
  390. return function(thing) {
  391. return TypedArray && thing instanceof TypedArray;
  392. };
  393. })(typeof Uint8Array !== 'undefined' && Object.getPrototypeOf(Uint8Array));
  394. module.exports = {
  395. isArray: isArray,
  396. isArrayBuffer: isArrayBuffer,
  397. isBuffer: isBuffer,
  398. isFormData: isFormData,
  399. isArrayBufferView: isArrayBufferView,
  400. isString: isString,
  401. isNumber: isNumber,
  402. isObject: isObject,
  403. isPlainObject: isPlainObject,
  404. isUndefined: isUndefined,
  405. isDate: isDate,
  406. isFile: isFile,
  407. isBlob: isBlob,
  408. isFunction: isFunction,
  409. isStream: isStream,
  410. isURLSearchParams: isURLSearchParams,
  411. isStandardBrowserEnv: isStandardBrowserEnv,
  412. forEach: forEach,
  413. merge: merge,
  414. extend: extend,
  415. trim: trim,
  416. stripBOM: stripBOM,
  417. inherits: inherits,
  418. toFlatObject: toFlatObject,
  419. kindOf: kindOf,
  420. kindOfTest: kindOfTest,
  421. endsWith: endsWith,
  422. toArray: toArray,
  423. isTypedArray: isTypedArray,
  424. isFileList: isFileList
  425. };