The resources

Github repository address

Source copy repository address (read source version)

Chinese document

English document

isSymbol(value)

getTag(value)

Returns the value of toStringTag, compared with the Object. The prototype. ToString, add the tag of null, and undefined

const toString = Object.prototype.toString

/** * returns value toStringTag **@param {*} Value passes in the object * to get the tag@returns {string} Returns the tag */ corresponding to the object
function getTag(value) {
  if (value == null) {
    // If value is null or undefined, return the corresponding tag
    return value === undefined ? '[object Undefined]' : '[object Null]'
  }
  return toString.call(value)
}

export default getTag
Copy the code

isSymbol(value)

Checks whether the value passed is a Symbol data type or Symbol object

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

/** * Checks whether the passed 'value' is a 'Symbol' data type or a 'Symbol' object **@param {*} Value Indicates the value to be detected *@returns {boolean} Return test result *@example
 *
 * isSymbol(Symbol.iterator)
 * // => true
 *
 * isSymbol('abc')
 * // => false
 */
function isSymbol(value) {
  const type = typeof value
  / / | | behind the role of the object is used to judge Symbol polyfill generated Symbol
  return type == 'symbol' || (type === 'object'&& value ! =null && getTag(value) == '[object Symbol]')}export default isSymbol
Copy the code

Checks whether the typeof value passed in is object or function, except for null

isObject(value)

/** * Checks whether the typeof the passed value is object or function, except for null **@param {*} Value Indicates the value to be detected *@returns {boolean} Return test result *@example
 *
 * isObject({})
 * // => true
 *
 * isObject([1, 2, 3])
 * // => true
 *
 * isObject(Function)
 * // => true
 *
 * isObject(null)
 * // => false
 */
function isObject(value) {
  const type = typeof value
  returnvalue ! =null && (type === 'object' || type === 'function')}export default isObject
Copy the code

toNumber(value)

Converts the passed value to the number type

import isObject from './isObject.js'
import isSymbol from './isSymbol.js'

/** NAN constant */
const NAN = 0 / 0

/** Matches the null character */ at the beginning and end of the string
const reTrim = /^\s+|\s+$/g

/** The regular checks if it is a signed hexadecimal string */
const reIsBadHex = /^[-+]0x[0-9a-f]+$/i

/** Regular checks if it is a binary string */
const reIsBinary = /^0b[01]+$/i

/** Regex checks if it is an octal string */
const reIsOctal = /^0o[0-7]+$/i

/** does not rely on method references */ with built-in root
const freeParseInt = parseInt

/** * Converts the passed value to type number **@param {*} The value to be converted *@returns {number} Returns the converted result *@see isInteger, toInteger, isNumber
 * @example* * toNumber(3.2) * // => 3.2 * * toNumber(number.min_value) * // => 5e-324 * * toNumber(Infinity) * // => Infinity * * ToNumber ('3.2') */
function toNumber(value) {
  // Check whether value is of type number
  if (typeof value === 'number') {
    return value
  }
  // Determine if value is a Symbol data type or Symbol object, if so, return NAN
  if (isSymbol(value)) {
    return NAN
  }
  // Determine if value is an object
  if (isObject(value)) {
    // If value is an object and the valueOf attribute is a function, execute valueOf and assign the result to other. If the valueOf attribute is not function, assign value to other
    const other = typeof value.valueOf === 'function' ? value.valueOf() : value
    // If other is an object, toString is converted to a string and assigned to value. If not, assign the value directly to value
    value = isObject(other) ? `${other}` : other
  }
  // If value is not a string
  if (typeofvalue ! = ='string') {
    // Check whether value is 0. If value is directly returned, if not, convert value to number
    return value === 0 ? value : +value
  }
  // Remove empty strings at the beginning and end of value
  value = value.replace(reTrim, ' ')
  const isBinary = reIsBinary.test(value)
  // Check whether the value is a binary or octal string, returned if converted to a decimal number using parseInt
  // If it is a hexadecimal string with a sign, NAN is returned
  // Otherwise, the string is returned as number
  return (isBinary || reIsOctal.test(value))
    ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
    : (reIsBadHex.test(value) ? NAN : +value)
}

export default toNumber
Copy the code

Reading the source code, toNumber returns NAN if it passes in a signed binary, octal, or hexadecimal string

console.log(toNumber('+0x1')) // NAN
console.log(toNumber('-0x1')) // NAN
console.log(toNumber('+0b1')) // NAN
console.log(toNumber('-0b1')) // NAN
console.log(toNumber('+0o1')) // NAN
console.log(toNumber('-0o1')) // NAN
Copy the code

One small change you can make in this final string detection is that the above example returns the correct value

.const reIsBadHex = /^[+-]0x[0-9a-f]+$/i

const reIsHex = / ^ (+ -)? 0x[0-9a-f]+$/i

const reIsBinary = / ^ (+ -)? 0b[01]+$/i

const reIsOctal = / ^ (+ -)? 0o[0-7]+$/i

const reNumberBase = /0[box]/.return (isBinary || reIsOctal.test(value))
  ? freeParseInt(value.replace(reNumberBase, ' '), isBinary ? 2 : 8)
  : (reIsHex.test(value) ? freeParseInt(value.replace(reNumberBase, ' '), 16) : +value)
Copy the code

toFinite(value)

Converts the passed value to the finite number type number

import toNumber from './toNumber.js'

/** INFINITY constant */
const INFINITY = 1 / 0
/** Maximum value */
const MAX_INTEGER = 1.7976931348623157 e+308

/** * Converts the passed value to the finite number type **@param {*} Value Value to be converted *@returns {number} Return result *@example* * toFinite(3.2) * // => 3.2 * * toFinite(number.min_value) * // => 5e-324 * * toFinite(Infinity) * // => 1.7976931348623157e * * toFinite('3.2') */ / => 3.2 */
function toFinite(value) {
   // Check whether value is valid. If the value is 0 or invalid, 0 is returned
  if(! value) {return value === 0 ? value : 0
  }
  // Convert the value to type number
  value = toNumber(value)
  if (value === INFINITY || value === -INFINITY) {
    // Determine whether the value is INFINITY, if so, return the maximum or minimum value
    const sign = (value < 0 ? -1 : 1)
    return sign * MAX_INTEGER
  }
  // Determine if value is a NAN, if so, return 0, not value
  return value === value ? value : 0
}

export default toFinite
Copy the code

toInteger(value)

Convert value to integer value (rounded down)

import toFinite from './toFinite.js'

/** * Converts value to integer value (rounded down) **@param {*} Value Value to be converted *@returns {number} Returns the conversion result *@see isInteger, isNumber, toNumber
 * @example* * toInteger(3.2) * // => 3 * * toInteger(number.min_value) * // => 0 * * toInteger(Infinity) * // => 1.7976931348623157e+308 * * toInteger('3.2') */ => 3 */
function toInteger(value) {
  // Convert value to a finite number
  const result = toFinite(value)
  // The number after the decimal point
  const remainder = result % 1
  // If the value after the decimal point is not 0, return result
  return remainder ? result - remainder : result
}

export default toInteger
Copy the code

slice(array, start, end)

Shallow-copy the elements of the array passed from start to end (excluding end) to form an array and return it

/** * Shallowly copies the elements of the array passed from start to end (excluding end) to form an array and returns **@param {Array} Array Array of elements to be intercepted@param {number} [start=0] Intercepts the start position. Default is 0 *@param {number} [end=array.length] Specifies the end position of the interception. The default value is the length of the array *@returns {Array} Returns the truncated array *@example
 *
 * var array = [1, 2, 3, 4]
 *
 * _.slice(array, 2)
 * // => [3, 4]
 */
function slice(array, start, end) {
  // The length of the array, if no array is passed in or the length of the array is 0, returns an empty array
  let length = array == null ? 0 : array.length
  if(! length) {return[]}// Start and end positions, if not passed, use the default values
  start = start == null ? 0 : start
  end = end === undefined ? length : end

  if (start < 0) {
    // If start is less than 0, count backwards from the end of the array
    start = -start > length ? 0 : (length + start)
  }
  // If end is greater than length, end is assigned to length
  end = end > length ? length : end
  if (end < 0) {
    // If end is less than 0, count backwards from the end of the array
    end += length
  }
  // Calculates the length of the array to intercept
  length = start > end ? 0 : ((end - start) >>> 0)
  // start round down
  start >>>= 0

  let index = -1
  const result = new Array(length)
  while (++index < length) {
    result[index] = array[index + start]
  }
  // Returns the truncated array
  return result
}

export default slice
Copy the code