Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lodash源码分析之differenceWith #31

Open
HeftyKoo opened this issue Oct 1, 2018 · 0 comments
Open

lodash源码分析之differenceWith #31

HeftyKoo opened this issue Oct 1, 2018 · 0 comments
Labels
api 暴露出来的接口 系列文章

Comments

@HeftyKoo
Copy link
Owner

HeftyKoo commented Oct 1, 2018

黑夜给了我黑色的眼睛,我却用它寻找光明

——顾城《一代人》

本文为读 lodash 源码的第三十篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash

gitbook也会同步仓库的更新,gitbook地址:pocket-lodash

依赖

import baseDifference from './.internal/baseDifference.js'
import baseFlatten from './.internal/baseFlatten.js'
import isArrayLikeObject from './isArrayLikeObject.js'
import last from './last.js'

lodash源码分析之数组的差集

lodash源码分析之baseFlatten

lodash源码分析之isArrayLikeObject

lodash源码分析之last

源码分析

differenceWithdifference 以及 differenceBy 的功能差不多,我们从上一篇文章《lodash源码分析之differenceBy》中已经知道,differenceBy 可以指定迭代器,那 differenceWith 又有什么不同呢?

differenceWithdifference 不同的地方在于,differenceWith 可以自定义比较函数。如果不指定比较函数,则 differenceWith 的功能和 difference 完全相同。

differenceWithdifferenceBy 的源码基本一致,如下:

function differenceWith(array, ...values) {
  let comparator = last(values)
  if (isArrayLikeObject(comparator)) {
    comparator = undefined
  }
  return isArrayLikeObject(array)
    ? baseDifference(array, baseFlatten(values, 1, isArrayLikeObject, true), undefined, comparator)
    : []
}

可以看到,comparator 是直接传给 baseDifference 的,comparator 便是自定义的比较函数,具体的分析见《lodash源码分析之数组的差集

License

署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)

最后,所有文章都会同步发送到微信公众号上,欢迎关注,欢迎提意见:

作者:对角另一面

@HeftyKoo HeftyKoo added 系列文章 api 暴露出来的接口 labels Oct 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api 暴露出来的接口 系列文章
Projects
None yet
Development

No branches or pull requests

1 participant