Skip to content

Commit aa516b5

Browse files
authored
fix: faster parse options (#535)
1 parent 61e6ea1 commit aa516b5

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

bin/semver.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ let identifier
2626
let identifierBase
2727

2828
const semver = require('../')
29+
const parseOptions = require('../internal/parse-options')
2930

3031
let reverse = false
3132

@@ -93,7 +94,7 @@ const main = () => {
9394
}
9495
}
9596

96-
options = { loose: loose, includePrerelease: includePrerelease, rtl: rtl }
97+
options = parseOptions({ loose, includePrerelease, rtl })
9798

9899
versions = versions.map((v) => {
99100
return coerce ? (semver.coerce(v, options) || { version: v }).version : v

classes/comparator.js

-7
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ class Comparator {
7878
throw new TypeError('a Comparator is required')
7979
}
8080

81-
if (!options || typeof options !== 'object') {
82-
options = {
83-
loose: !!options,
84-
includePrerelease: false,
85-
}
86-
}
87-
8881
if (this.operator === '') {
8982
if (this.value === '') {
9083
return true

internal/parse-options.js

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
// parse out just the options we care about so we always get a consistent
2-
// obj with keys in a consistent order.
3-
const opts = ['includePrerelease', 'loose', 'rtl']
4-
const parseOptions = options =>
5-
!options ? {}
6-
: typeof options !== 'object' ? { loose: true }
7-
: opts.filter(k => options[k]).reduce((o, k) => {
8-
o[k] = true
9-
return o
10-
}, {})
1+
// parse out just the options we care about
2+
const looseOption = Object.freeze({ loose: true })
3+
const emptyOpts = Object.freeze({ })
4+
const parseOptions = options => {
5+
if (!options) {
6+
return emptyOpts
7+
}
8+
9+
if (typeof options !== 'object') {
10+
return looseOption
11+
}
12+
13+
return options
14+
}
1115
module.exports = parseOptions

test/internal/parse-options.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@ t.test('truthy non-objects always loose mode, for backwards comp', t => {
1818
t.end()
1919
})
2020

21-
t.test('objects only include truthy flags we know about, set to true', t => {
22-
t.strictSame(parseOptions(/asdf/), {})
23-
t.strictSame(parseOptions(new Error('hello')), {})
24-
t.strictSame(parseOptions({ loose: true, a: 1, rtl: false }), { loose: true })
21+
t.test('any object passed is returned', t => {
22+
t.strictSame(parseOptions(/asdf/), /asdf/)
23+
t.strictSame(parseOptions(new Error('hello')), new Error('hello'))
24+
t.strictSame(parseOptions({ loose: true, a: 1, rtl: false }), { loose: true, a: 1, rtl: false })
2525
t.strictSame(parseOptions({ loose: 1, rtl: 2, includePrerelease: 10 }), {
26+
loose: 1,
27+
rtl: 2,
28+
includePrerelease: 10,
29+
})
30+
t.strictSame(parseOptions({ loose: true }), { loose: true })
31+
t.strictSame(parseOptions({ rtl: true }), { rtl: true })
32+
t.strictSame(parseOptions({ includePrerelease: true }), { includePrerelease: true })
33+
t.strictSame(parseOptions({ loose: true, rtl: true }), { loose: true, rtl: true })
34+
t.strictSame(parseOptions({ loose: true, includePrerelease: true }), {
2635
loose: true,
36+
includePrerelease: true,
37+
})
38+
t.strictSame(parseOptions({ rtl: true, includePrerelease: true }), {
2739
rtl: true,
2840
includePrerelease: true,
2941
})

0 commit comments

Comments
 (0)