-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
62 lines (49 loc) · 1.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict'
const revalidator = require('revalidator')
const schema = require('./lib/schema')
const pick = require('lodash').pick
const omit = require('lodash').omit
const clean = require('./lib/clean')
module.exports = class Package {
constructor (doc, opts) {
var pkg = clean(doc)
if (!pkg) return
if (opts && opts.pick) {
if (typeof opts.pick === 'string') opts.pick = opts.pick.split(',').map(prop => prop.trim())
pkg = pick(pkg, opts.pick)
}
if (opts && opts.omit) {
if (typeof opts.omit === 'string') opts.omit = opts.omit.split(',').map(prop => prop.trim())
pkg = omit(pkg, opts.omit)
}
Object.assign(this, pkg)
return this
}
mentions (string) {
return !!JSON.stringify(this).toLowerCase().includes(string.toLowerCase())
}
dependsOn (dep) {
return this.depNames.indexOf(dep) > -1
}
devDependsOn (dep) {
return this.devDepNames.indexOf(dep) > -1
}
somehowDependsOn (dep) {
return this.dependsOn(dep) || this.devDependsOn(dep)
}
get depNames () {
return this.dependencies ? Object.keys(this.dependencies).sort() : []
}
get devDepNames () {
return this.devDependencies ? Object.keys(this.devDependencies).sort() : []
}
get allDepNames () {
return this.depNames.concat(this.devDepNames).sort()
}
get valid () {
return revalidator.validate(this, schema).valid
}
get validationErrors () {
return revalidator.validate(this, schema).errors
}
}