_.isEqualWith(value, other, [customizer])

137

_.isEqualWith(value, other, [customizer])

_.isEqualWith此方法类似isEqual区别是它接受一个自定义比较方法。如果自定义比较方法返回值是undefined,比较结果就由baseIsEqual来处理

参数

value (*): 需要比较的值
other (*): 需要比较的另外一个值
[customizer] (Function): 自定义比较方法

返回值

(boolean): 如果两个值相等返回true,否则false

例子

function isGreeting(value) {
  return /^h(?:i|ello)$/.test(value);
}
 
function customizer(objValue, othValue) {
  if (isGreeting(objValue) && isGreeting(othValue)) {
    return true;
  }
}
 
var array = ['hello', 'goodbye'];
var other = ['hi', 'goodbye'];
 
_.isEqualWith(array, other, customizer);
// => true

源代码

import baseIsEqual from './.internal/baseIsEqual.js'

/**
 * This method is like `isEqual` except that it accepts `customizer` which
 * is invoked to compare values. If `customizer` returns `undefined`, comparisons
 * are handled by the method instead. The `customizer` is invoked with up to
 * six arguments: (objValue, othValue [, index|key, object, other, stack]).
 *
 * @since 4.0.0
 * @category Lang
 * @param {*} value The value to compare.
 * @param {*} other The other value to compare.
 * @param {Function} [customizer] The function to customize comparisons.
 * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
 * @example
 *
 * function isGreeting(value) {
 *   return /^h(?:i|ello)$/.test(value)
 * }
 *
 * function customizer(objValue, othValue) {
 *   if (isGreeting(objValue) && isGreeting(othValue)) {
 *     return true
 *   }
 * }
 *
 * const array = ['hello', 'goodbye']
 * const other = ['hi', 'goodbye']
 *
 * isEqualWith(array, other, customizer)
 * // => true
 */
//此方法类似isEqual区别是它接受一个自定义比较方法。如果自定义比较方法返回值是undefined,比较结果就由baseIsEqual来处理
function isEqualWith(value, other, customizer) {
  customizer = typeof customizer == 'function' ? customizer : undefined//判断是否传递了customizer
  const result = customizer ? customizer(value, other) : undefined//由customizer计算比较结果
  return result === undefined ? baseIsEqual(value, other, undefined, customizer) : !!result//如果customizer的结果是undefined,调用baseIsEqual处理
}

export default isEqualWith

 

posted @ 2018-12-22 16:34  hahazexia  阅读(1025)  评论(0)    收藏  举报