1b23d73fb2e2d7b5518387f69440c84bf38f7258c2ffb0a9b20421c9bb24b54f3fd1dd1b35a7cc188b732b670273df7f0dd1474522c05f0f1074830768e975 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 empty Object
  112. *
  113. * @param {Object} val The value to test
  114. * @return {boolean} True if value is a empty Object, otherwise false
  115. */
  116. function isEmptyObject(val) {
  117. return val && Object.keys(val).length === 0 && Object.getPrototypeOf(val) === Object.prototype;
  118. }
  119. /**
  120. * Determine if a value is a Date
  121. *
  122. * @function
  123. * @param {Object} val The value to test
  124. * @returns {boolean} True if value is a Date, otherwise false
  125. */
  126. var isDate = kindOfTest('Date');
  127. /**
  128. * Determine if a value is a File
  129. *
  130. * @function
  131. * @param {Object} val The value to test
  132. * @returns {boolean} True if value is a File, otherwise false
  133. */
  134. var isFile = kindOfTest('File');
  135. /**
  136. * Determine if a value is a Blob
  137. *
  138. * @function
  139. * @param {Object} val The value to test
  140. * @returns {boolean} True if value is a Blob, otherwise false
  141. */
  142. var isBlob = kindOfTest('Blob');
  143. /**
  144. * Determine if a value is a FileList
  145. *
  146. * @function
  147. * @param {Object} val The value to test
  148. * @returns {boolean} True if value is a File, otherwise false
  149. */
  150. var isFileList = kindOfTest('FileList');
  151. /**
  152. * Determine if a value is a Function
  153. *
  154. * @param {Object} val The value to test
  155. * @returns {boolean} True if value is a Function, otherwise false
  156. */
  157. function isFunction(val) {
  158. return toString.call(val) === '[object Function]';
  159. }
  160. /**
  161. * Determine if a value is a Stream
  162. *
  163. * @param {Object} val The value to test
  164. * @returns {boolean} True if value is a Stream, otherwise false
  165. */
  166. function isStream(val) {
  167. return isObject(val) && isFunction(val.pipe);
  168. }
  169. /**
  170. * Determine if a value is a FormData
  171. *
  172. * @param {Object} thing The value to test
  173. * @returns {boolean} True if value is an FormData, otherwise false
  174. */
  175. function isFormData(thing) {
  176. var pattern = '[object FormData]';
  177. return thing && (
  178. (typeof FormData === 'function' && thing instanceof FormData) ||
  179. toString.call(thing) === pattern ||
  180. (isFunction(thing.toString) && thing.toString() === pattern)
  181. );
  182. }
  183. /**
  184. * Determine if a value is a URLSearchParams object
  185. * @function
  186. * @param {Object} val The value to test
  187. * @returns {boolean} True if value is a URLSearchParams object, otherwise false
  188. */
  189. var isURLSearchParams = kindOfTest('URLSearchParams');
  190. /**
  191. * Trim excess whitespace off the beginning and end of a string
  192. *
  193. * @param {String} str The String to trim
  194. * @returns {String} The String freed of excess whitespace
  195. */
  196. function trim(str) {
  197. return str.trim ? str.trim() : str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  198. }
  199. /**
  200. * Determine if we're running in a standard browser environment
  201. *
  202. * This allows axios to run in a web worker, and react-native.
  203. * Both environments support XMLHttpRequest, but not fully standard globals.
  204. *
  205. * web workers:
  206. * typeof window -> undefined
  207. * typeof document -> undefined
  208. *
  209. * react-native:
  210. * navigator.product -> 'ReactNative'
  211. * nativescript
  212. * navigator.product -> 'NativeScript' or 'NS'
  213. */
  214. function isStandardBrowserEnv() {
  215. var product;
  216. if (typeof navigator !== 'undefined' && (
  217. (product = navigator.product) === 'ReactNative' ||
  218. product === 'NativeScript' ||
  219. product === 'NS')
  220. ) {
  221. return false;
  222. }
  223. return typeof window !== 'undefined' && typeof document !== 'undefined';
  224. }
  225. /**
  226. * Iterate over an Array or an Object invoking a function for each item.
  227. *
  228. * If `obj` is an Array callback will be called passing
  229. * the value, index, and complete array for each item.
  230. *
  231. * If 'obj' is an Object callback will be called passing
  232. * the value, key, and complete object for each property.
  233. *
  234. * @param {Object|Array} obj The object to iterate
  235. * @param {Function} fn The callback to invoke for each item
  236. */
  237. function forEach(obj, fn) {
  238. // Don't bother if no value provided
  239. if (obj === null || typeof obj === 'undefined') {
  240. return;
  241. }
  242. // Force an array if not already something iterable
  243. if (typeof obj !== 'object') {
  244. /*eslint no-param-reassign:0*/
  245. obj = [obj];
  246. }
  247. if (isArray(obj)) {
  248. // Iterate over array values
  249. for (var i = 0, l = obj.length; i < l; i++) {
  250. fn.call(null, obj[i], i, obj);
  251. }
  252. } else {
  253. // Iterate over object keys
  254. for (var key in obj) {
  255. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  256. fn.call(null, obj[key], key, obj);
  257. }
  258. }
  259. }
  260. }
  261. /**
  262. * Accepts varargs expecting each argument to be an object, then
  263. * immutably merges the properties of each object and returns result.
  264. *
  265. * When multiple objects contain the same key the later object in
  266. * the arguments list will take precedence.
  267. *
  268. * Example:
  269. *
  270. * ```js
  271. * var result = merge({foo: 123}, {foo: 456});
  272. * console.log(result.foo); // outputs 456
  273. * ```
  274. *
  275. * @param {Object} obj1 Object to merge
  276. * @returns {Object} Result of all merge properties
  277. */
  278. function merge(/* obj1, obj2, obj3, ... */) {
  279. var result = {};
  280. function assignValue(val, key) {
  281. if (isPlainObject(result[key]) && isPlainObject(val)) {
  282. result[key] = merge(result[key], val);
  283. } else if (isPlainObject(val)) {
  284. result[key] = merge({}, val);
  285. } else if (isArray(val)) {
  286. result[key] = val.slice();
  287. } else {
  288. result[key] = val;
  289. }
  290. }
  291. for (var i = 0, l = arguments.length; i < l; i++) {
  292. forEach(arguments[i], assignValue);
  293. }
  294. return result;
  295. }
  296. /**
  297. * Extends object a by mutably adding to it the properties of object b.
  298. *
  299. * @param {Object} a The object to be extended
  300. * @param {Object} b The object to copy properties from
  301. * @param {Object} thisArg The object to bind function to
  302. * @return {Object} The resulting value of object a
  303. */
  304. function extend(a, b, thisArg) {
  305. forEach(b, function assignValue(val, key) {
  306. if (thisArg && typeof val === 'function') {
  307. a[key] = bind(val, thisArg);
  308. } else {
  309. a[key] = val;
  310. }
  311. });
  312. return a;
  313. }
  314. /**
  315. * Remove byte order marker. This catches EF BB BF (the UTF-8 BOM)
  316. *
  317. * @param {string} content with BOM
  318. * @return {string} content value without BOM
  319. */
  320. function stripBOM(content) {
  321. if (content.charCodeAt(0) === 0xFEFF) {
  322. content = content.slice(1);
  323. }
  324. return content;
  325. }
  326. /**
  327. * Inherit the prototype methods from one constructor into another
  328. * @param {function} constructor
  329. * @param {function} superConstructor
  330. * @param {object} [props]
  331. * @param {object} [descriptors]
  332. */
  333. function inherits(constructor, superConstructor, props, descriptors) {
  334. constructor.prototype = Object.create(superConstructor.prototype, descriptors);
  335. constructor.prototype.constructor = constructor;
  336. props && Object.assign(constructor.prototype, props);
  337. }
  338. /**
  339. * Resolve object with deep prototype chain to a flat object
  340. * @param {Object} sourceObj source object
  341. * @param {Object} [destObj]
  342. * @param {Function|Boolean} [filter]
  343. * @param {Function} [propFilter]
  344. * @returns {Object}
  345. */
  346. function toFlatObject(sourceObj, destObj, filter, propFilter) {
  347. var props;
  348. var i;
  349. var prop;
  350. var merged = {};
  351. destObj = destObj || {};
  352. // eslint-disable-next-line no-eq-null,eqeqeq
  353. if (sourceObj == null) return destObj;
  354. do {
  355. props = Object.getOwnPropertyNames(sourceObj);
  356. i = props.length;
  357. while (i-- > 0) {
  358. prop = props[i];
  359. if ((!propFilter || propFilter(prop, sourceObj, destObj)) && !merged[prop]) {
  360. destObj[prop] = sourceObj[prop];
  361. merged[prop] = true;
  362. }
  363. }
  364. sourceObj = filter !== false && Object.getPrototypeOf(sourceObj);
  365. } while (sourceObj && (!filter || filter(sourceObj, destObj)) && sourceObj !== Object.prototype);
  366. return destObj;
  367. }
  368. /*
  369. * determines whether a string ends with the characters of a specified string
  370. * @param {String} str
  371. * @param {String} searchString
  372. * @param {Number} [position= 0]
  373. * @returns {boolean}
  374. */
  375. function endsWith(str, searchString, position) {
  376. str = String(str);
  377. if (position === undefined || position > str.length) {
  378. position = str.length;
  379. }
  380. position -= searchString.length;
  381. var lastIndex = str.indexOf(searchString, position);
  382. return lastIndex !== -1 && lastIndex === position;
  383. }
  384. /**
  385. * Returns new array from array like object or null if failed
  386. * @param {*} [thing]
  387. * @returns {?Array}
  388. */
  389. function toArray(thing) {
  390. if (!thing) return null;
  391. if (isArray(thing)) return thing;
  392. var i = thing.length;
  393. if (!isNumber(i)) return null;
  394. var arr = new Array(i);
  395. while (i-- > 0) {
  396. arr[i] = thing[i];
  397. }
  398. return arr;
  399. }
  400. // eslint-disable-next-line func-names
  401. var isTypedArray = (function(TypedArray) {
  402. // eslint-disable-next-line func-names
  403. return function(thing) {
  404. return TypedArray && thing instanceof TypedArray;
  405. };
  406. })(typeof Uint8Array !== 'undefined' && Object.getPrototypeOf(Uint8Array));
  407. function forEachEntry(obj, fn) {
  408. var generator = obj && obj[Symbol.iterator];
  409. var iterator = generator.call(obj);
  410. var result;
  411. while ((result = iterator.next()) && !result.done) {
  412. var pair = result.value;
  413. fn.call(obj, pair[0], pair[1]);
  414. }
  415. }
  416. function matchAll(regExp, str) {
  417. var matches;
  418. var arr = [];
  419. while ((matches = regExp.exec(str)) !== null) {
  420. arr.push(matches);
  421. }
  422. return arr;
  423. }
  424. var isHTMLForm = kindOfTest('HTMLFormElement');
  425. var hasOwnProperty = (function resolver(_hasOwnProperty) {
  426. return function(obj, prop) {
  427. return _hasOwnProperty.call(obj, prop);
  428. };
  429. })(Object.prototype.hasOwnProperty);
  430. module.exports = {
  431. isArray: isArray,
  432. isArrayBuffer: isArrayBuffer,
  433. isBuffer: isBuffer,
  434. isFormData: isFormData,
  435. isArrayBufferView: isArrayBufferView,
  436. isString: isString,
  437. isNumber: isNumber,
  438. isObject: isObject,
  439. isPlainObject: isPlainObject,
  440. isEmptyObject: isEmptyObject,
  441. isUndefined: isUndefined,
  442. isDate: isDate,
  443. isFile: isFile,
  444. isBlob: isBlob,
  445. isFunction: isFunction,
  446. isStream: isStream,
  447. isURLSearchParams: isURLSearchParams,
  448. isStandardBrowserEnv: isStandardBrowserEnv,
  449. forEach: forEach,
  450. merge: merge,
  451. extend: extend,
  452. trim: trim,
  453. stripBOM: stripBOM,
  454. inherits: inherits,
  455. toFlatObject: toFlatObject,
  456. kindOf: kindOf,
  457. kindOfTest: kindOfTest,
  458. endsWith: endsWith,
  459. toArray: toArray,
  460. isTypedArray: isTypedArray,
  461. isFileList: isFileList,
  462. forEachEntry: forEachEntry,
  463. matchAll: matchAll,
  464. isHTMLForm: isHTMLForm,
  465. hasOwnProperty: hasOwnProperty
  466. };