|
@@ -0,0 +1,279 @@
|
|
|
+export default {
|
|
|
+ /**
|
|
|
+ * Determine if a value is an Array
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is an Array, otherwise false
|
|
|
+ */
|
|
|
+ isArray(val) {
|
|
|
+ return toString.call(val) === '[object Array]';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is an ArrayBuffer
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
|
|
+ */
|
|
|
+ isArrayBuffer(val) {
|
|
|
+ return toString.call(val) === '[object ArrayBuffer]';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a FormData
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is an FormData, otherwise false
|
|
|
+ */
|
|
|
+ isFormData(val) {
|
|
|
+ return (typeof FormData !== 'undefined') && (val instanceof FormData);
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a view on an ArrayBuffer
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
|
|
+ */
|
|
|
+ isArrayBufferView(val) {
|
|
|
+ let result;
|
|
|
+ if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
|
|
+ result = ArrayBuffer.isView(val);
|
|
|
+ } else {
|
|
|
+ result = (val) && (val.buffer) && (val.buffer instanceof ArrayBuffer);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a String
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a String, otherwise false
|
|
|
+ */
|
|
|
+ isString(val) {
|
|
|
+ return typeof val === 'string';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a Number
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a Number, otherwise false
|
|
|
+ */
|
|
|
+ isNumber(val) {
|
|
|
+ return typeof val === 'number';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is undefined
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if the value is undefined, otherwise false
|
|
|
+ */
|
|
|
+ isUndefined(val) {
|
|
|
+ return typeof val === 'undefined';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is an Object
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is an Object, otherwise false
|
|
|
+ */
|
|
|
+ isObject(val) {
|
|
|
+ return val !== null && typeof val === 'object';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a Date
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a Date, otherwise false
|
|
|
+ */
|
|
|
+ isDate(val) {
|
|
|
+ return toString.call(val) === '[object Date]';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a File
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a File, otherwise false
|
|
|
+ */
|
|
|
+ isFile(val) {
|
|
|
+ return toString.call(val) === '[object File]';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a Blob
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a Blob, otherwise false
|
|
|
+ */
|
|
|
+ isBlob(val) {
|
|
|
+ return toString.call(val) === '[object Blob]';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a Function
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a Function, otherwise false
|
|
|
+ */
|
|
|
+ isFunction(val) {
|
|
|
+ return toString.call(val) === '[object Function]';
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a Stream
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a Stream, otherwise false
|
|
|
+ */
|
|
|
+ isStream(val) {
|
|
|
+ return this.isObject(val) && this.isFunction(val.pipe);
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if a value is a URLSearchParams object
|
|
|
+ *
|
|
|
+ * @param {Object} val The value to test
|
|
|
+ * @returns {boolean} True if value is a URLSearchParams object, otherwise false
|
|
|
+ */
|
|
|
+ isURLSearchParams(val) {
|
|
|
+ return typeof URLSearchParams !== 'undefined' && val instanceof URLSearchParams;
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Trim excess whitespace off the beginning and end of a string
|
|
|
+ *
|
|
|
+ * @param {String} str The String to trim
|
|
|
+ * @returns {String} The String freed of excess whitespace
|
|
|
+ */
|
|
|
+ trim(str) {
|
|
|
+ return str.replace(/^\s*/, '')
|
|
|
+ .replace(/\s*$/, '');
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Determine if we're running in a standard browser environment
|
|
|
+ *
|
|
|
+ * This allows axios to run in a web worker, and react-native.
|
|
|
+ * Both environments support XMLHttpRequest, but not fully standard globals.
|
|
|
+ *
|
|
|
+ * web workers:
|
|
|
+ * typeof window -> undefined
|
|
|
+ * typeof document -> undefined
|
|
|
+ *
|
|
|
+ * react-native:
|
|
|
+ * navigator.product -> 'ReactNative'
|
|
|
+ */
|
|
|
+ isStandardBrowserEnv() {
|
|
|
+ if (typeof navigator !== 'undefined' && navigator.product === 'ReactNative') {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return (
|
|
|
+ typeof window !== 'undefined' &&
|
|
|
+ typeof document !== 'undefined'
|
|
|
+ );
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Iterate over an Array or an Object invoking a function for each item.
|
|
|
+ *
|
|
|
+ * If `obj` is an Array callback will be called passing
|
|
|
+ * the value, index, and complete array for each item.
|
|
|
+ *
|
|
|
+ * If 'obj' is an Object callback will be called passing
|
|
|
+ * the value, key, and complete object for each property.
|
|
|
+ *
|
|
|
+ * @param {Object|Array} obj The object to iterate
|
|
|
+ * @param {Function} fn The callback to invoke for each item
|
|
|
+ */
|
|
|
+ forEach(obj, fn) {
|
|
|
+ // Don't bother if no value provided
|
|
|
+ if (obj === null || typeof obj === 'undefined') {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // Force an array if not already something iterable
|
|
|
+ if (typeof obj !== 'object') {
|
|
|
+ /* eslint no-param-reassign:0 */
|
|
|
+ obj = [obj];
|
|
|
+ }
|
|
|
+ if (this.isArray(obj)) {
|
|
|
+ // Iterate over array values
|
|
|
+ for (let i = 0, l = obj.length; i < l; i += 1) {
|
|
|
+ fn.call(null, obj[i], i, obj);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // Iterate over object keys
|
|
|
+ // eslint-disable-next-line
|
|
|
+ for (const key in obj) {
|
|
|
+ if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
|
+ fn.call(null, obj[key], key, obj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Accepts varargs expecting each argument to be an object, then
|
|
|
+ * immutably merges the properties of each object and returns result.
|
|
|
+ *
|
|
|
+ * When multiple objects contain the same key the later object in
|
|
|
+ * the arguments list will take precedence.
|
|
|
+ *
|
|
|
+ * Example:
|
|
|
+ *
|
|
|
+ * ```js
|
|
|
+ * var result = merge({foo: 123}, {foo: 456});
|
|
|
+ * console.log(result.foo); // outputs 456
|
|
|
+ * ```
|
|
|
+ *
|
|
|
+ * @param {Object} obj1 Object to merge
|
|
|
+ * @returns {Object} Result of all merge properties
|
|
|
+ */
|
|
|
+ merge(/* obj1, obj2, obj3, ... */) {
|
|
|
+ const result = {};
|
|
|
+
|
|
|
+ function assignValue(val, key) {
|
|
|
+ if (typeof result[key] === 'object' && typeof val === 'object') {
|
|
|
+ result[key] = this.merge(result[key], val);
|
|
|
+ } else {
|
|
|
+ result[key] = val;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (let i = 0, l = arguments.length; i < l; i += 1) {
|
|
|
+ // eslint-disable-next-line
|
|
|
+ this.forEach(arguments[i], assignValue);
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * Extends object a by mutably adding to it the properties of object b.
|
|
|
+ *
|
|
|
+ * @param {Object} a The object to be extended
|
|
|
+ * @param {Object} b The object to copy properties from
|
|
|
+ * @param {Object} thisArg The object to bind function to
|
|
|
+ * @return {Object} The resulting value of object a
|
|
|
+ */
|
|
|
+ extend(a, b, thisArg) {
|
|
|
+ function assignValue(val, key) {
|
|
|
+ if (thisArg && typeof val === 'function') {
|
|
|
+ a[key] = this.bind(val, thisArg);
|
|
|
+ } else {
|
|
|
+ a[key] = val;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ this.forEach(b, assignValue);
|
|
|
+ return a;
|
|
|
+ },
|
|
|
+ bind(fn, thisArg) {
|
|
|
+ return function wrap() {
|
|
|
+ const args = new Array(arguments.length);
|
|
|
+ for (let i = 0; i < args.length; i += 1) {
|
|
|
+ // eslint-disable-next-line
|
|
|
+ args[i] = arguments[i];
|
|
|
+ }
|
|
|
+ return fn.apply(thisArg, args);
|
|
|
+ };
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ * 检测当前系统会否属于测试环境
|
|
|
+ * @return {Boolean} true 为测试环境;false 为生产环境
|
|
|
+ * */
|
|
|
+ isDev() {
|
|
|
+ const url = location.href;
|
|
|
+ let dev = false;
|
|
|
+ if (url.indexOf('common.yiguanjia.me') < 0) {
|
|
|
+ dev = true;
|
|
|
+ }
|
|
|
+ return dev;
|
|
|
+ }
|
|
|
+};
|