util.js 7.7 KB

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