3 Star 54 Fork 14

Yaohaixiao / dom.js

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
filter.js 959 Bytes
一键复制 编辑 原始数据 按行查看 历史
import isFunction from './utils/types/isFunction'
import isString from './utils/types/isString'
import isCollection from './isCollection'
import matches from './matches'
/**
* 返回 NodeList 的元素中与 selector 过滤条件匹配的 DOM 元素(数组)
* ========================================================================
* @method filter
* @since 1.2.0
* @param {NodeList} collection
* @param {String|Function} selector
* @return {Array}
*/
const filter = (collection, selector) => {
let callback
let items
if (
!isCollection(collection) ||
(!isString(selector) && !isFunction(selector))
) {
return []
}
items = [...collection]
if (isString(selector)) {
callback = (item) => {
return matches(item, selector)
}
} else {
callback = (item, i) => {
return selector(item, i)
}
}
return items.filter(callback)
}
export default filter
JavaScript
1
https://gitee.com/yaohaixiao/dom.js.git
git@gitee.com:yaohaixiao/dom.js.git
yaohaixiao
dom.js
dom.js
main

搜索帮助

53164aa7 5694891 3bd8fe86 5694891