1 | 'use strict'
|
2 |
|
3 | module.exports = filter
|
4 | function filter (data, include, exclude, opts) {
|
5 | return typeof data === 'object' &&
|
6 | filterWords(data, include, exclude, opts)
|
7 | }
|
8 |
|
9 | function getWords (data, opts) {
|
10 | return [ data.name ]
|
11 | .concat((opts && opts.description) ? data.description : [])
|
12 | .concat((data.maintainers || []).map(function (m) {
|
13 | return '=' + m.name
|
14 | }))
|
15 | .concat(data.versions && data.versions.length && data.url && ('<' + data.url + '>'))
|
16 | .concat(data.keywords || [])
|
17 | .map(function (f) { return f && f.trim && f.trim() })
|
18 | .filter(function (f) { return f })
|
19 | .join(' ')
|
20 | .toLowerCase()
|
21 | }
|
22 |
|
23 | function filterWords (data, include, exclude, opts) {
|
24 | var words = getWords(data, opts)
|
25 | for (var i = 0, l = include.length; i < l; i++) {
|
26 | if (!match(words, include[i])) return false
|
27 | }
|
28 | for (i = 0, l = exclude.length; i < l; i++) {
|
29 | if (match(words, exclude[i])) return false
|
30 | }
|
31 | return true
|
32 | }
|
33 |
|
34 | function match (words, pattern) {
|
35 | if (pattern.charAt(0) === '/') {
|
36 | pattern = pattern.replace(/\/$/, '')
|
37 | pattern = new RegExp(pattern.substr(1, pattern.length - 1))
|
38 | return words.match(pattern)
|
39 | }
|
40 | return words.indexOf(pattern) !== -1
|
41 | }
|