今天我们又来学习源码了,我的axios对应的版本是 “version”:”0.25.0”

工具函数

今天的主角是utils.js文件, 以下列出了文件中的工具函数:

isArray 判断数组

  1. /**
  2. * Determine if a value is an Array
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is an Array, otherwise false
  6. */
  7. // 判断是否是数组,调用了Array类上的isArray方法
  8. function isArray(val) {
  9. return Array.isArray(val);
  10. }

isUndefined 判断Undefined

  1. /**
  2. * Determine if a value is undefined
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if the value is undefined, otherwise false
  6. */
  7. // 判断值是否为undefined
  8. function isUndefined(val) {
  9. return typeof val === 'undefined';
  10. }

isBuffer 判断 buffer

  1. /**
  2. * Determine if a value is a Buffer
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is a Buffer, otherwise false
  6. */
  7. function isBuffer(val) {
  8. return val !== null &&
  9. !isUndefined(val) &&
  10. val.constructor !== null &&
  11. !isUndefined(val.constructor) &&
  12. typeof val.constructor.isBuffer === 'function' &&
  13. val.constructor.isBuffer(val);
  14. }

什么是Buffer?
JavaScript 语言自身只有字符串数据类型,没有二进制数据类型。
但在处理像TCP流或文件流时,必须使用到二进制数据。因此在 Node.js中,定义了一个Buffer 类,该类用来创建一个专门存放二进制数据的缓存区

isArrayBuffer判断ArrayBuffer

  1. /**
  2. * Determine if a value is an ArrayBuffer
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is an ArrayBuffer, otherwise false
  6. */
  7. function isArrayBuffer(val) {
  8. return toString.call(val) === '[object ArrayBuffer]';
  9. }

isFormData 判断FormData

  1. /**
  2. * Determine if a value is a FormData
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is an FormData, otherwise false
  6. */
  7. function isFormData(val) {
  8. return toString.call(val) === '[object FormData]';
  9. }

isObject 判断对象

  1. /**
  2. * Determine if a value is an Object
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is an Object, otherwise false
  6. */
  7. function isObject(val) {
  8. return val !== null && typeof val === 'object';
  9. }

isPlainObject 判断 纯对象

纯对象: 用{}或new Object()创建的对象

  1. /**
  2. * Determine if a value is a plain Object
  3. *
  4. * @param {Object} val The value to test
  5. * @return {boolean} True if value is a plain Object, otherwise false
  6. */
  7. function isPlainObject(val) {
  8. if (toString.call(val) !== '[object Object]') {
  9. return false;
  10. }

isDate 判断Date

  1. /**
  2. * Determine if a value is a Date
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is a Date, otherwise false
  6. */
  7. function isDate(val) {
  8. return toString.call(val) === '[object Date]';
  9. }

isFile 判断文件类型

  1. /**
  2. * Determine if a value is a File
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is a File, otherwise false
  6. */
  7. function isFile(val) {
  8. return toString.call(val) === '[object File]';
  9. }

isBlob 判断Blob

  1. /**
  2. * Determine if a value is a Blob
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is a Blob, otherwise false
  6. */
  7. function isBlob(val) {
  8. return toString.call(val) === '[object Blob]';
  9. }

Blob 对象表示一个不可变、原始数据的类文件对象。它的数据可以按文本或二进制的格式进行读取。

isFunction 判断函数

  1. /**
  2. * Determine if a value is a Function
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is a Function, otherwise false
  6. */
  7. function isFunction(val) {
  8. return toString.call(val) === '[object Function]';
  9. }

isStram 判断 数据流

  1. /**
  2. * Determine if a value is a Stream
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is a Stream, otherwise false
  6. */
  7. function isStream(val) {
  8. return isObject(val) && isFunction(val.pipe);
  9. }

isURLSearchParams 判断URLSearchParams

  1. /**
  2. * Determine if a value is a URLSearchParams object
  3. *
  4. * @param {Object} val The value to test
  5. * @returns {boolean} True if value is a URLSearchParams object, otherwise false
  6. */
  7. function isURLSearchParams(val) {
  8. return toString.call(val) === '[object URLSearchParams]';
  9. }

trim 去除首尾空格

  1. // `trim`方法不存在的话,用正则
  2. function trim(str) {
  3. return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, '');
  4. }

isStandardBrowserEnv 判断标准浏览器环境

  1. /**
  2. * Determine if we're running in a standard browser environment
  3. *
  4. * This allows axios to run in a web worker, and react-native.
  5. * Both environments support XMLHttpRequest, but not fully standard globals.
  6. *
  7. * web workers:
  8. * typeof window -> undefined
  9. * typeof document -> undefined
  10. *
  11. * react-native:
  12. * navigator.product -> 'ReactNative'
  13. * nativescript
  14. * navigator.product -> 'NativeScript' or 'NS'
  15. */
  16. function isStandardBrowserEnv() {
  17. if (typeof navigator !== 'undefined' && (navigator.product === 'ReactNative' ||
  18. navigator.product === 'NativeScript' ||
  19. navigator.product === 'NS')) {
  20. return false;
  21. }
  22. return (
  23. typeof window !== 'undefined' &&
  24. typeof document !== 'undefined'
  25. );
  26. }

forEach 遍历对象或数组

  1. /**
  2. * Accepts varargs expecting each argument to be an object, then
  3. * immutably merges the properties of each object and returns result.
  4. *
  5. * When multiple objects contain the same key the later object in
  6. * the arguments list will take precedence.
  7. *
  8. * Example:
  9. *
  10. * ```js
  11. * var result = merge({foo: 123}, {foo: 456});
  12. * console.log(result.foo); // outputs 456
  13. *

*

  • @param {Object} obj1 Object to merge
  • @returns {Object} Result of all merge properties / function merge(/ obj1, obj2, obj3, … */) { var result = {}; function assignValue(val, key) { if (isPlainObject(result[key]) && isPlainObject(val)) {

    1. result[key] = merge(result[key], val);

    } else if (isPlainObject(val)) {

    1. result[key] = merge({}, val);

    } else if (isArray(val)) {

    1. result[key] = val.slice();

    } else {

    1. result[key] = val;

    } }

    for (var i = 0, l = arguments.length; i < l; i++) { forEach(arguments[i], assignValue); } return result; } ```

    stripBOM删除UTF-8编码中BOM

    ```javascript /**

  • Remove byte order marker. This catches EF BB BF (the UTF-8 BOM) *
  • @param {string} content with BOM
  • @return {string} content value without BOM */

function stripBOM(content) { if (content.charCodeAt(0) === 0xFEFF) { content = content.slice(1); } return content; } 。 ```

总结

通过读源码的方式,从源码中获取知识与经验,可以更好的帮助自己成长。