MCPcopy
hub / github.com/npm/node-semver / constructor

Method constructor

classes/range.js:7–71  ·  view source on GitHub ↗
(range, options)

Source from the content-addressed store, hash-verified

5// hoisted class for cyclic dependency
6class Range {
7 constructor (range, options) {
8 options = parseOptions(options)
9
10 if (range instanceof Range) {
11 if (
12 range.loose === !!options.loose &&
13 range.includePrerelease === !!options.includePrerelease
14 ) {
15 return range
16 } else {
17 return new Range(range.raw, options)
18 }
19 }
20
21 if (range instanceof Comparator) {
22 // just put it in the set and return
23 this.raw = range.value
24 this.set = [[range]]
25 this.formatted = undefined
26 return this
27 }
28
29 this.options = options
30 this.loose = !!options.loose
31 this.includePrerelease = !!options.includePrerelease
32
33 // First reduce all whitespace as much as possible so we do not have to rely
34 // on potentially slow regexes like \s*. This is then stored and used for
35 // future error messages as well.
36 this.raw = range.trim().replace(SPACE_CHARACTERS, ' ')
37
38 // First, split on ||
39 this.set = this.raw
40 .split('||')
41 // map the range to a 2d array of comparators
42 .map(r => this.parseRange(r.trim()))
43 // throw out any comparator lists that are empty
44 // this generally means that it was not a valid range, which is allowed
45 // in loose mode, but will still throw if the WHOLE range is invalid.
46 .filter(c => c.length)
47
48 if (!this.set.length) {
49 throw new TypeError(`Invalid SemVer Range: ${this.raw}`)
50 }
51
52 // if we have any that are not the null set, throw out null sets.
53 if (this.set.length > 1) {
54 // keep the first one, in case they're all null sets
55 const first = this.set[0]
56 this.set = this.set.filter(c => !isNullSet(c[0]))
57 if (this.set.length === 0) {
58 this.set = [first]
59 } else if (this.set.length > 1) {
60 // if we have any that are *, then the range is just *
61 for (const c of this.set) {
62 if (c.length === 1 && isAny(c[0])) {
63 this.set = [c]
64 break

Callers

nothing calls this directly

Calls 4

parseRangeMethod · 0.95
parseOptionsFunction · 0.85
isNullSetFunction · 0.85
isAnyFunction · 0.85

Tested by

no test coverage detected