项目原始demo,不改动
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.
 
 
 
 

335 řádky
8.3 KiB

  1. 'use strict';
  2. var bind = require('./helpers/bind');
  3. var isBuffer = require('is-buffer');
  4. /*global toString:true*/
  5. // utils is a library of generic helper functions non-specific to axios
  6. var toString = Object.prototype.toString;
  7. /**
  8. * Determine if a value is an Array
  9. *
  10. * @param {Object} val The value to test
  11. * @returns {boolean} True if value is an Array, otherwise false
  12. */
  13. function isArray(val) {
  14. return toString.call(val) === '[object Array]';
  15. }
  16. /**
  17. * Determine if a value is an ArrayBuffer
  18. *
  19. * @param {Object} val The value to test
  20. * @returns {boolean} True if value is an ArrayBuffer, otherwise false
  21. */
  22. function isArrayBuffer(val) {
  23. return toString.call(val) === '[object ArrayBuffer]';
  24. }
  25. /**
  26. * Determine if a value is a FormData
  27. *
  28. * @param {Object} val The value to test
  29. * @returns {boolean} True if value is an FormData, otherwise false
  30. */
  31. function isFormData(val) {
  32. return (typeof FormData !== 'undefined') && (val instanceof FormData);
  33. }
  34. /**
  35. * Determine if a value is a view on an ArrayBuffer
  36. *
  37. * @param {Object} val The value to test
  38. * @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
  39. */
  40. function isArrayBufferView(val) {
  41. var result;
  42. if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
  43. result = ArrayBuffer.isView(val);
  44. } else {
  45. result = (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
  46. }
  47. return result;
  48. }
  49. /**
  50. * Determine if a value is a String
  51. *
  52. * @param {Object} val The value to test
  53. * @returns {boolean} True if value is a String, otherwise false
  54. */
  55. function isString(val) {
  56. return typeof val === 'string';
  57. }
  58. /**
  59. * Determine if a value is a Number
  60. *
  61. * @param {Object} val The value to test
  62. * @returns {boolean} True if value is a Number, otherwise false
  63. */
  64. function isNumber(val) {
  65. return typeof val === 'number';
  66. }
  67. /**
  68. * Determine if a value is undefined
  69. *
  70. * @param {Object} val The value to test
  71. * @returns {boolean} True if the value is undefined, otherwise false
  72. */
  73. function isUndefined(val) {
  74. return typeof val === 'undefined';
  75. }
  76. /**
  77. * Determine if a value is an Object
  78. *
  79. * @param {Object} val The value to test
  80. * @returns {boolean} True if value is an Object, otherwise false
  81. */
  82. function isObject(val) {
  83. return val !== null && typeof val === 'object';
  84. }
  85. /**
  86. * Determine if a value is a Date
  87. *
  88. * @param {Object} val The value to test
  89. * @returns {boolean} True if value is a Date, otherwise false
  90. */
  91. function isDate(val) {
  92. return toString.call(val) === '[object Date]';
  93. }
  94. /**
  95. * Determine if a value is a File
  96. *
  97. * @param {Object} val The value to test
  98. * @returns {boolean} True if value is a File, otherwise false
  99. */
  100. function isFile(val) {
  101. return toString.call(val) === '[object File]';
  102. }
  103. /**
  104. * Determine if a value is a Blob
  105. *
  106. * @param {Object} val The value to test
  107. * @returns {boolean} True if value is a Blob, otherwise false
  108. */
  109. function isBlob(val) {
  110. return toString.call(val) === '[object Blob]';
  111. }
  112. /**
  113. * Determine if a value is a Function
  114. *
  115. * @param {Object} val The value to test
  116. * @returns {boolean} True if value is a Function, otherwise false
  117. */
  118. function isFunction(val) {
  119. return toString.call(val) === '[object Function]';
  120. }
  121. /**
  122. * Determine if a value is a Stream
  123. *
  124. * @param {Object} val The value to test
  125. * @returns {boolean} True if value is a Stream, otherwise false
  126. */
  127. function isStream(val) {
  128. return isObject(val) && isFunction(val.pipe);
  129. }
  130. /**
  131. * Determine if a value is a URLSearchParams object
  132. *
  133. * @param {Object} val The value to test
  134. * @returns {boolean} True if value is a URLSearchParams object, otherwise false
  135. */
  136. function isURLSearchParams(val) {
  137. return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
  138. }
  139. /**
  140. * Trim excess whitespace off the beginning and end of a string
  141. *
  142. * @param {String} str The String to trim
  143. * @returns {String} The String freed of excess whitespace
  144. */
  145. function trim(str) {
  146. return str.replace(/^\s*/, '').replace(/\s*$/, '');
  147. }
  148. /**
  149. * Determine if we're running in a standard browser environment
  150. *
  151. * This allows axios to run in a web worker, and react-native.
  152. * Both environments support XMLHttpRequest, but not fully standard globals.
  153. *
  154. * web workers:
  155. * typeof window -> undefined
  156. * typeof document -> undefined
  157. *
  158. * react-native:
  159. * navigator.product -> 'ReactNative'
  160. * nativescript
  161. * navigator.product -> 'NativeScript' or 'NS'
  162. */
  163. function isStandardBrowserEnv() {
  164. if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
  165. navigator.product === 'NativeScript' ||
  166. navigator.product === 'NS')) {
  167. return false;
  168. }
  169. return (
  170. typeof window !== 'undefined' &&
  171. typeof document !== 'undefined'
  172. );
  173. }
  174. /**
  175. * Iterate over an Array or an Object invoking a function for each item.
  176. *
  177. * If `obj` is an Array callback will be called passing
  178. * the value, index, and complete array for each item.
  179. *
  180. * If 'obj' is an Object callback will be called passing
  181. * the value, key, and complete object for each property.
  182. *
  183. * @param {Object|Array} obj The object to iterate
  184. * @param {Function} fn The callback to invoke for each item
  185. */
  186. function forEach(obj, fn) {
  187. // Don't bother if no value provided
  188. if (obj === null || typeof obj === 'undefined') {
  189. return;
  190. }
  191. // Force an array if not already something iterable
  192. if (typeof obj !== 'object') {
  193. /*eslint no-param-reassign:0*/
  194. obj = [obj];
  195. }
  196. if (isArray(obj)) {
  197. // Iterate over array values
  198. for (var i = 0, l = obj.length; i < l; i++) {
  199. fn.call(null, obj[i], i, obj);
  200. }
  201. } else {
  202. // Iterate over object keys
  203. for (var key in obj) {
  204. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  205. fn.call(null, obj[key], key, obj);
  206. }
  207. }
  208. }
  209. }
  210. /**
  211. * Accepts varargs expecting each argument to be an object, then
  212. * immutably merges the properties of each object and returns result.
  213. *
  214. * When multiple objects contain the same key the later object in
  215. * the arguments list will take precedence.
  216. *
  217. * Example:
  218. *
  219. * ```js
  220. * var result = merge({foo: 123}, {foo: 456});
  221. * console.log(result.foo); // outputs 456
  222. * ```
  223. *
  224. * @param {Object} obj1 Object to merge
  225. * @returns {Object} Result of all merge properties
  226. */
  227. function merge(/* obj1, obj2, obj3, ... */) {
  228. var result = {};
  229. function assignValue(val, key) {
  230. if (typeof result[key] === 'object' && typeof val === 'object') {
  231. result[key] = merge(result[key], val);
  232. } else {
  233. result[key] = val;
  234. }
  235. }
  236. for (var i = 0, l = arguments.length; i < l; i++) {
  237. forEach(arguments[i], assignValue);
  238. }
  239. return result;
  240. }
  241. /**
  242. * Function equal to merge with the difference being that no reference
  243. * to original objects is kept.
  244. *
  245. * @see merge
  246. * @param {Object} obj1 Object to merge
  247. * @returns {Object} Result of all merge properties
  248. */
  249. function deepMerge(/* obj1, obj2, obj3, ... */) {
  250. var result = {};
  251. function assignValue(val, key) {
  252. if (typeof result[key] === 'object' && typeof val === 'object') {
  253. result[key] = deepMerge(result[key], val);
  254. } else if (typeof val === 'object') {
  255. result[key] = deepMerge({}, val);
  256. } else {
  257. result[key] = val;
  258. }
  259. }
  260. for (var i = 0, l = arguments.length; i < l; i++) {
  261. forEach(arguments[i], assignValue);
  262. }
  263. return result;
  264. }
  265. /**
  266. * Extends object a by mutably adding to it the properties of object b.
  267. *
  268. * @param {Object} a The object to be extended
  269. * @param {Object} b The object to copy properties from
  270. * @param {Object} thisArg The object to bind function to
  271. * @return {Object} The resulting value of object a
  272. */
  273. function extend(a, b, thisArg) {
  274. forEach(b, function assignValue(val, key) {
  275. if (thisArg && typeof val === 'function') {
  276. a[key] = bind(val, thisArg);
  277. } else {
  278. a[key] = val;
  279. }
  280. });
  281. return a;
  282. }
  283. module.exports = {
  284. isArray: isArray,
  285. isArrayBuffer: isArrayBuffer,
  286. isBuffer: isBuffer,
  287. isFormData: isFormData,
  288. isArrayBufferView: isArrayBufferView,
  289. isString: isString,
  290. isNumber: isNumber,
  291. isObject: isObject,
  292. isUndefined: isUndefined,
  293. isDate: isDate,
  294. isFile: isFile,
  295. isBlob: isBlob,
  296. isFunction: isFunction,
  297. isStream: isStream,
  298. isURLSearchParams: isURLSearchParams,
  299. isStandardBrowserEnv: isStandardBrowserEnv,
  300. forEach: forEach,
  301. merge: merge,
  302. deepMerge: deepMerge,
  303. extend: extend,
  304. trim: trim
  305. };