/** * Determine if a value is an Array * * @param {Object}val The value to test * @returns {boolean}True if value is an Array, otherwise false */ functionisArray(val) { returnArray.isArray(val) }
isUndefined
1 2 3 4 5 6 7 8 9
/** * Determine if a value is undefined 判断值是否是undefined * * @param {Object}val The value to test * @returns {boolean}True if the value is undefined, otherwise false */ functionisUndefined(val) { returntypeof val === 'undefined'; }
isBuffer
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * Determine if a value is a Buffer 判断一个值是否是Buffer类型 * * @param {Object}val The value to test * @returns {boolean}True if value is a Buffer, otherwise false */ functionisBuffer(val) { return val !== null && !isUndefined(val) && val.constructor !== null && !isUndefined(val.constructor) && typeof val.constructor.isBuffer === 'function' && val.constructor.isBuffer(val); // 通过构造函数的isBuffer方法来判断 }
/** * Determine if a value is a FormData * * @param {Object}val The value to test * @returns {boolean}True if value is an FormData, otherwise false */ functionisFormData(val) { return toString.call(val) === '[object FormData]'; }
isArrayBufferView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/** * 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 */ functionisArrayBufferView(val) { var result; if ((typeofArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) { result = ArrayBuffer.isView(val); } else { result = (val) && (val.buffer) && (isArrayBuffer(val.buffer)); } return result; }
正常情况下, typeof ArrayBuffer === ‘function’
Array.isView 如果参数是ArrayBuffer的视图实例则返回true
isString
1 2 3 4 5 6 7 8 9
/** * Determine if a value is a String * * @param {Object}val The value to test * @returns {boolean}True if value is a String, otherwise false */ functionisString(val) { returntypeof val === 'string'; }
isNumber
1 2 3 4 5 6 7 8 9
/** * Determine if a value is a Number * * @param {Object}val The value to test * @returns {boolean}True if value is a Number, otherwise false */ functionisNumber(val) { returntypeof val === 'number'; }
isObject
1 2 3
functionisObject(val) { return val !== null && typeof val === 'object'; }
typeof null === ‘object’;
isPlainObject
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/** * Determine if a value is a plain Object 判断是否是一个纯对象 * * @param {Object}val The value to test * @return {boolean}True if value is a plain Object, otherwise false */ functionisPlainObject(val) { if (toString.call(val) !== '[object Object]') { returnfalse; }
/** * Determine if a value is a Date * * @param {Object}val The value to test * @returns {boolean}True if value is a Date, otherwise false */ functionisDate(val) { return toString.call(val) === '[object Date]'; }
isFile
1 2 3 4 5 6 7 8 9
/** * Determine if a value is a File * * @param {Object}val The value to test * @returns {boolean}True if value is a File, otherwise false */ functionisFile(val) { return toString.call(val) === '[object File]'; }
isBlob
1 2 3 4 5 6 7 8 9
/** * Determine if a value is a Blob * * @param {Object}val The value to test * @returns {boolean}True if value is a Blob, otherwise false */ functionisBlob(val) { return toString.call(val) === '[object Blob]'; }
/** * Determine if a value is a Stream * * @param {Object}val The value to test * @returns {boolean}True if value is a Stream, otherwise false */ functionisStream(val) { return isObject(val) && isFunction(val.pipe); }
isURLSearchParams
1 2 3 4 5 6 7 8 9
/** * 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 */ functionisURLSearchParams(val) { return toString.call(val) === '[object URLSearchParams]'; }
trim
去除首尾空格
1 2 3 4 5 6 7 8 9 10
/** * 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 */ functiontrim(str) { return str.trim ? str.trim() : str.replace(/^\s+|\s+$/g, ''); }
/** * Iterate over an Array or an Object invoking a function for each item. * * // fn.call(null, obj[i], i, obj); * If `obj` is an Array callback will be called passing * the value, index, and complete array for each item. * * // fn.call(null, obj[key], key, obj); * 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 */ functionforEach(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 (isArray(obj)) { // Iterate over array values for (var i = 0, l = obj.length; i < l; i++) { fn.call(null, obj[i], i, obj); } } else { // Iterate over object keys for (var 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 */ functionmerge(/* obj1, obj2, obj3, ... */) { var result = {}; functionassignValue(val, key) { if (isPlainObject(result[key]) && isPlainObject(val)) { result[key] = merge(result[key], val); } elseif (isPlainObject(val)) { result[key] = merge({}, val); } elseif (isArray(val)) { result[key] = val.slice(); } else { result[key] = val; } }
for (var i = 0, l = arguments.length; i < l; i++) { forEach(arguments[i], assignValue); } return result; }
// bind实现 functionbind(fn, thisArg) { returnfunctionwrap() { var args = newArray(arguments.length); for (var i = 0; i < args.length; i++) { args[i] = arguments[i]; } return fn.apply(thisArg, args); }; }; /** * 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 */ functionextend(a, b, thisArg) { forEach(b, functionassignValue(val, key) { if (thisArg && typeof val === 'function') { a[key] = bind(val, thisArg); } else { a[key] = val; } }); return a; }