wzp
2021-05-13 7d694a9113118daec5be7ac224dab46a3b20f106
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict'
 
function SemVerStore () {
  if (!(this instanceof SemVerStore)) {
    return new SemVerStore()
  }
  this.tree = new Node()
}
 
SemVerStore.prototype.set = function (version, store) {
  if (typeof version !== 'string') {
    throw new TypeError('Version should be a string')
  }
  var currentNode = this.tree
  version = version.split('.')
  while (version.length) {
    currentNode = currentNode.addChild(
      new Node(version.shift())
    )
  }
  currentNode.setStore(store)
  return this
}
 
SemVerStore.prototype.get = function (version) {
  if (typeof version !== 'string') return null
  if (version === '*') version = 'x.x.x'
  var node = this.tree
  var firstDot = version.indexOf('.')
  var secondDot = version.indexOf('.', firstDot + 1)
  var major = version.slice(0, firstDot)
  var minor = secondDot === -1
    ? version.slice(firstDot + 1)
    : version.slice(firstDot + 1, secondDot)
  var patch = secondDot === -1
    ? 'x'
    : version.slice(secondDot + 1)
 
  node = node.getChild(major)
  if (node === null) return null
  node = node.getChild(minor)
  if (node === null) return null
  node = node.getChild(patch)
  if (node === null) return null
  return node.store
}
 
SemVerStore.prototype.del = function (version) {
  if (typeof version !== 'string') {
    throw new TypeError('Version should be a string')
  }
  var firstDot = version.indexOf('.')
  var secondDot = version.indexOf('.', firstDot + 1)
  var major = version.slice(0, firstDot)
  var minor = secondDot === -1
    ? version.slice(firstDot + 1)
    : version.slice(firstDot + 1, secondDot)
  var patch = secondDot === -1
    ? 'x'
    : version.slice(secondDot + 1)
 
  // check existence of major node
  var majorNode = this.tree.children[major]
  if (majorNode == null) return this
 
  // if minor is the wildcard, then remove the full major node
  if (minor === 'x') {
    this.tree.removeChild(major)
    return this
  }
 
  // check existence of minor node
  var minorNode = majorNode.children[minor]
  if (minorNode == null) return this
 
  // if patch is the wildcard, then remove the full minor node
  // and also the major if there are no more children
  if (patch === 'x') {
    this.tree.children[major].removeChild(minor)
    if (this.tree.children[major].length === 0) {
      this.tree.removeChild(major)
    }
    return this
  }
 
  // check existence of patch node
  var patchNode = minorNode.children[patch]
  if (patchNode == null) return this
 
  // Specific delete
  this.tree
    .children[major]
    .children[minor]
    .removeChild(patch)
 
  // check if the minor node has no more children, if so removes it
  // same for the major node
  if (this.tree.children[major].children[minor].length === 0) {
    this.tree.children[major].removeChild(minor)
    if (this.tree.children[major].length === 0) {
      this.tree.removeChild(major)
    }
  }
 
  return this
}
 
SemVerStore.prototype.empty = function () {
  this.tree = new Node()
  return this
}
 
function getMax (arr) {
  var l = arr.length
  var max = arr[0]
  for (var i = 1; i < l; i++) {
    if (arr[i] > max) {
      max = arr[i]
    }
  }
  return max
}
 
function Node (prefix, children, store) {
  this.prefix = Number(prefix) || 0
  this.children = children || null
  this.childrenPrefixes = children ? Object.keys(children) : []
  this.store = store || null
}
 
Node.prototype.getChild = function (prefix) {
  if (this.children === null) return null
  if (prefix === 'x') {
    var max = getMax(this.childrenPrefixes)
    return this.children[max]
  }
  return this.children[prefix] || null
}
 
Node.prototype.addChild = function (node) {
  this.children = this.children || {}
  var child = this.getChild(node.prefix)
  if (child === null) {
    this.children[node.prefix] = node
    this.childrenPrefixes.push(node.prefix)
  }
  return child || node
}
 
Node.prototype.removeChild = function (prefix) {
  if (prefix === 'x') {
    this.children = null
    this.childrenPrefixes = []
    return this
  }
  if (this.children[prefix] !== undefined) {
    prefix = Number(prefix)
    delete this.children[prefix]
    this.childrenPrefixes.splice(
      this.childrenPrefixes.indexOf(prefix), 1
    )
  }
  return this
}
 
Node.prototype.setStore = function (store) {
  this.store = store
  return this
}
 
Object.defineProperty(Node.prototype, 'length', {
  get: function () {
    return this.childrenPrefixes.length
  }
})
 
module.exports = SemVerStore