utils.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. */
  161. function isStandardBrowserEnv() {
  162. if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
  163. return false;
  164. }
  165. return (
  166. typeof window !== 'undefined' &&
  167. typeof document !== 'undefined'
  168. );
  169. }
  170. /**
  171. * Iterate over an Array or an Object invoking a function for each item.
  172. *
  173. * If `obj` is an Array callback will be called passing
  174. * the value, index, and complete array for each item.
  175. *
  176. * If 'obj' is an Object callback will be called passing
  177. * the value, key, and complete object for each property.
  178. *
  179. * @param {Object|Array} obj The object to iterate
  180. * @param {Function} fn The callback to invoke for each item
  181. */
  182. function forEach(obj, fn) {
  183. // Don't bother if no value provided
  184. if (obj === null || typeof obj === 'undefined') {
  185. return;
  186. }
  187. // Force an array if not already something iterable
  188. if (typeof obj !== 'object') {
  189. /*eslint no-param-reassign:0*/
  190. obj = [obj];
  191. }
  192. if (isArray(obj)) {
  193. // Iterate over array values
  194. for (var i = 0, l = obj.length; i < l; i++) {
  195. fn.call(null, obj[i], i, obj);
  196. }
  197. } else {
  198. // Iterate over object keys
  199. for (var key in obj) {
  200. if (Object.prototype.hasOwnProperty.call(obj, key)) {
  201. fn.call(null, obj[key], key, obj);
  202. }
  203. }
  204. }
  205. }
  206. /**
  207. * Accepts varargs expecting each argument to be an object, then
  208. * immutably merges the properties of each object and returns result.
  209. *
  210. * When multiple objects contain the same key the later object in
  211. * the arguments list will take precedence.
  212. *
  213. * Example:
  214. *
  215. * ```js
  216. * var result = merge({foo: 123}, {foo: 456});
  217. * console.log(result.foo); // outputs 456
  218. * ```
  219. *
  220. * @param {Object} obj1 Object to merge
  221. * @returns {Object} Result of all merge properties
  222. */
  223. function merge(/* obj1, obj2, obj3, ... */) {
  224. var result = {};
  225. function assignValue(val, key) {
  226. if (typeof result[key] === 'object' && typeof val === 'object') {
  227. result[key] = merge(result[key], val);
  228. } else {
  229. result[key] = val;
  230. }
  231. }
  232. for (var i = 0, l = arguments.length; i < l; i++) {
  233. forEach(arguments[i], assignValue);
  234. }
  235. return result;
  236. }
  237. /**
  238. * Extends object a by mutably adding to it the properties of object b.
  239. *
  240. * @param {Object} a The object to be extended
  241. * @param {Object} b The object to copy properties from
  242. * @param {Object} thisArg The object to bind function to
  243. * @return {Object} The resulting value of object a
  244. */
  245. function extend(a, b, thisArg) {
  246. forEach(b, function assignValue(val, key) {
  247. if (thisArg && typeof val === 'function') {
  248. a[key] = bind(val, thisArg);
  249. } else {
  250. a[key] = val;
  251. }
  252. });
  253. return a;
  254. }
  255. module.exports = {
  256. isArray: isArray,
  257. isArrayBuffer: isArrayBuffer,
  258. isBuffer: isBuffer,
  259. isFormData: isFormData,
  260. isArrayBufferView: isArrayBufferView,
  261. isString: isString,
  262. isNumber: isNumber,
  263. isObject: isObject,
  264. isUndefined: isUndefined,
  265. isDate: isDate,
  266. isFile: isFile,
  267. isBlob: isBlob,
  268. isFunction: isFunction,
  269. isStream: isStream,
  270. isURLSearchParams: isURLSearchParams,
  271. isStandardBrowserEnv: isStandardBrowserEnv,
  272. forEach: forEach,
  273. merge: merge,
  274. extend: extend,
  275. trim: trim
  276. };