-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDomainName.js
41 lines (32 loc) · 903 Bytes
/
DomainName.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
// A single level of a domain name
const debug = require('debug')('d::domain-tree::DomainName')
const reverse = require('lodash/reverse')
const TreeNode = require('./TreeNode.js')
module.exports = class DomainName extends TreeNode {
domain(){
let domain = ''
if ( !this.parent ) return domain
if ( this.level > 1 ) domain = '.' + this.parent.domain()
return `${this.id}${domain}`
}
domainComponents(){
let components = this.domain().split('.')
return reverse(components)
}
toJSON(){
debug('toJson', this.id)
let o = {}
if (this.data) o._data = this.data
let child_obj = o
if ( !this.root ) {
o.domain = this.domain()
if ( this.children.length > 0 ) {
o.subdomain = {}
child_obj = o.subdomain
}
}
this.children.forEach( i => child_obj[i.id] = i.toJSON() )
debug('toJson o', o)
return o
}
}