The resources

Github repository address

Source copy repository address (read source version)

Chinese document

English document

isLength(value)

Check whether the value passed in is a valid class array length value (an integer greater than -1 and less than number.max_safe_INTEGER)

// Number.MAX_SAFE_INTEGER
const MAX_SAFE_INTEGER = 9007199254740991

/** * Check if the value passed is a valid class array length value **@param {*} value 
 * @returns {boolean} 
 * @example
 *
 * isLength(3)
 * // => true
 *
 * isLength(Number.MIN_VALUE)
 * // => false
 *
 * isLength(Infinity)
 * // => false
 *
 * isLength('3')
 * // => false
 */
function isLength(value) {
  return typeof value === 'number' &&
    value > -1 && value % 1= =0 && value <= MAX_SAFE_INTEGER
}

export default isLength
Copy the code

isArrayLike(value)

Check whether value is an array of classes. If value is a class array, value is not a function and has a length attribute of type number and is an integer greater than -1 and less than number.max_safe_INTEGER

import isLength from './isLength.js'

/** * check if value is an array of classes. If value is a class array, value is not a function and has a length attribute * that is of type number and is an integer greater than -1 and less than number.max_safe_INTEGER@since 4.0.0
 * @category Lang
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
 * @example
 *
 * isArrayLike([1, 2, 3])
 * // => true
 *
 * isArrayLike(document.body.children)
 * // => true
 *
 * isArrayLike('abc')
 * // => true
 *
 * isArrayLike(Function)
 * // => false
 */
function isArrayLike(value) {
  returnvalue ! =null && typeofvalue ! = ='function' && isLength(value.length)
}

export default isArrayLike
Copy the code

isObjectLike(value)

IsObjectLike Checks if value is typeof and value is ‘object’ and value is not null

/** * isObjectLike Checks if the value is typeof and the value is not null **@param {*} value 
 * @returns {boolean} 
 * @example
 *
 * isObjectLike({})
 * // => true
 *
 * isObjectLike([1, 2, 3])
 * // => true
 *
 * isObjectLike(Function)
 * // => false
 *
 * isObjectLike(null)
 * // => false
 */
function isObjectLike(value) {
  return typeof value === 'object'&& value ! = =null
}

export default isObjectLike
Copy the code

isArrayLikeObject(value)

Check if value is an array-like object

import isArrayLike from './isArrayLike.js'
import isObjectLike from './isObjectLike.js'

/** * check if value is an array-like object@since 4.0.0
 * @category Lang
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is an array-like object,
 *  else `false`.
 * @example* * isArrayLikeObject([1, 2, 3]) * // => true * * isArrayLikeObject(document.body.children) * // => true * * isArrayLikeObject('abc') * // => false *  * isArrayLikeObject(Function) * // => false */
function isArrayLikeObject(value) {
  return isObjectLike(value) && isArrayLike(value)
}

export default isArrayLikeObject
Copy the code

map(array, iteratee)

Creates an array whose elements are the results of iteratee iteratee iterating through each element in the array.

Iteratee receive three parameters (the value, the index | key, array)

/** * Creates an array whose elements are the results of iteratee iteratee iterating through each element in the array. * iteratee accepts three parameters (value, the index | key, array) * *@since 5.0.0
 * @category Array
 * @param {Array} array The array to iterate over.
 * @param {Function} iteratee The function invoked per iteration.
 * @returns {Array} Returns the new mapped array.
 * @example
 *
 * function square(n) {
 *   return n * n
 * }
 *
 * map([4, 8], square)
 * // => [16, 64]
 */
function map(array, iteratee) {
  let index = -1
  const length = array == null ? 0 : array.length
  // Create a new array
  const result = new Array(length)

  while (++index < length) {
    // Execute iteratee with the parameters passed in
    // And add the result to result
    result[index] = iteratee(array[index], index, array)
  }
  / / return the result
  return result
}

export default map
Copy the code

Cancat (array, values) and private dependencies

isArray(value)

The isArray method uses the static isArray method directly

/** * check if value is an array **@param {*} Value is checked if it is an array value *@returns {boolean} Returns a Boolean *@example
 *
 * _.isArray([1, 2, 3]);
 * // => true
 *
 * _.isArray(document.body.children);
 * // => false
 *
 * _.isArray('abc');
 * // => false
 *
 * _.isArray(_.noop);
 * // => false
 */
const isArray = Array.isArray;

export default isArray
Copy the code

copyArray(source, array)

Shallow copies elements from array source into another array

/** * shallowly copies elements from array source to another array **@param {Array} Source Source of the array to be copied *@param {Array} [array=[]] Adds an array of copied elements. Default is empty array *@returns {Array} Returns an array * /
function copyArray(source, array) {
  let index = -1
  const length = source.length

  // If no array is passed, initialize an empty array with the same length as source
  array || (array = new Array(length))
  // Iterate over the source to add elements to the array
  while (++index < length) {
    array[index] = source[index]
  }
  return array
}

export default copyArray
Copy the code

arrayPush(array, values)

ArrayPush adds elements from an array to the target array (starting at the end of the target array)

/** * arrayPush adds elements from an array to the target array (starting at the end of the target array) **@private
 * @param {Array} Array Target array *@param {Array} Values The array of elements to add *@returns {Array} Returns the target array array */
function arrayPush(array, values) {
  var index = -1.// Initialize the index
      length = values.length, / / values
      offset = array.length; // The offset of the element's insertion position

  while (++index < length) {
    // Iterate through values to add elements to array
    array[offset + index] = values[index];
  }
  return array;
}

export default arrayPush
Copy the code

isArguments(value)

IsArguments Checks whether value is a Arguments object

import getTag from './.internal/getTag.js'
import isObjectLike from './isObjectLike.js'

/** * isArguments Checks whether value is the arguments object **@since 0.1.0 from *@category Lang
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is an `arguments` object, else `false`.
 * @example
 *
 * isArguments(function() { return arguments }())
 * // => true
 *
 * isArguments([1, 2, 3])
 * // => false
 */
function isArguments(value) {
  return isObjectLike(value) && getTag(value) == '[object Arguments]'
}

export default isArguments
Copy the code

isFlattenable(value)

An isFlattenable test that a value is either a Arguments object or an array or a flattenable object

import isArguments from '.. /isArguments.js'

/** Built-in value reference. */
const spreadableSymbol = Symbol.isConcatSpreadable

/** * isflattener able to determine if a value is either an array or an expandable object **@private
 * @param {*} value The value to check.
 * @returns {boolean} Returns `true` if `value` is flattenable, else `false`.
 */
function isFlattenable(value) {
  return Array.isArray(value) || isArguments(value) || !! (value && value[spreadableSymbol]) }export default isFlattenable
Copy the code

About Symbol. IsConcatSpreadable MDN document has detailed instructions Symbol. IsConcatSpreadable

baseFlatten(array, depth, predicate, isStrict, result)

BaseFlatten expands elements from a multidimensional array to add to the specified destination array

import isFlattenable from './isFlattenable.js'

/** * baseFlatten adds elements from a multidimensional array to the specified destination array **@param {Array} Array The array to be expanded *@param {number} Depth Depth of expansion *@param {boolean} [predicate=isFlattenable] a checksum of array elements *@param {boolean} [isStrict] Whether the verification of the predicate function is strictly performed *@param {Array} [result=[]] specifies the target array to add elements to@returns {Array} Returns the new flattened array.
 */
function baseFlatten(array, depth, predicate, isStrict, result) {
  // Predicate defaults to an isFlattenable
  predicate || (predicate = isFlattenable)
  // result defaults to an empty array
  result || (result = [])

  if (array == null) {
    // If the array does not exist, return an empty array
    return result
  }

  for (const value of array) {
    // Iterate over the array array
    // If the expansion is greater than zero and the elements pass the predicate function
    if (depth > 0 && predicate(value)) {
      if (depth > 1) {
        // If the expansion depth is greater than 1, the current element is added to result recursively
        baseFlatten(value, depth - 1, predicate, isStrict, result)
      } else {
        // If the expansion depth is 1, add the element to resultresult.push(... value) } }else if(! isStrict) {// If the element fails the predicate function
      // isStrict is false to add the element to result
      result[result.length] = value
    }
  }
  / / return the result
  return result
}

export default baseFlatten
Copy the code

concat(array, values)

Creates a new array and adds array and other values to the new array and returns the new array

import isArray from './isArray'
import copyArray from './.internal/copyArray'
import baseFlatten from './.internal/baseFlatten'
import arrayPush from './.internal/arrayPush'

/** * creates a new array, adds array and other values to the new array and returns the new array **@param {Array} array 
 * @param {... *} [values] 
 * @returns {Array} 
 * @example
 *
 * var array = [1];
 * var other = _.concat(array, 2, [3], [[4]]);
 *
 * console.log(other);
 * // => [1, 2, 3, [4]]
 *
 * console.log(array);
 * // => [1]
 */
function concat() {
  // The length of the argument passed in
  var length = arguments.length;
  If no arguments are passed, an empty array is returned
  if(! length) {return [];
  }
  var args = Array(length - 1), // Set all elements except array
      array = arguments[0].// array
      index = length;

  while (index--) {
    // All parameters except array are added to args
    args[index - 1] = arguments[index];
  }
  // isArray(array) ? CopyArray (array) : [array] Generates an array as an element if array is not an array, or copies a new array as a data source if array is an array
  // baseFlatten(args, 1) Expand the args element to form a new array to add to the array
  return arrayPush(isArray(array) ? copyArray(array) : [array], baseFlatten(args, 1));
}

export default concat
Copy the code