Skip to content

Commit 585125b

Browse files
committed
fix: simplify parse options
1 parent 7e4e016 commit 585125b

File tree

2 files changed

+9
-58
lines changed

2 files changed

+9
-58
lines changed

internal/parse-options.js

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,15 @@
11
// parse out just the options we care about
2-
const isParsedConfigSymbol = Symbol('isParsedConfig')
3-
const var1 = Object.freeze({ includePrerelease: true, loose: true, rtl: true, [isParsedConfigSymbol]: true })
4-
const var2 = Object.freeze({ includePrerelease: true, loose: true, [isParsedConfigSymbol]: true })
5-
const var3 = Object.freeze({ includePrerelease: true, rtl: true, [isParsedConfigSymbol]: true })
6-
const var4 = Object.freeze({ includePrerelease: true, [isParsedConfigSymbol]: true })
7-
const var5 = Object.freeze({ loose: true, rtl: true, [isParsedConfigSymbol]: true })
8-
const var6 = Object.freeze({ loose: true, [isParsedConfigSymbol]: true })
9-
const var7 = Object.freeze({ rtl: true, [isParsedConfigSymbol]: true })
10-
const emptyOpts = Object.freeze({ [isParsedConfigSymbol]: true })
11-
2+
const looseOption = Object.freeze({ loose: true })
3+
const emptyOpts = Object.freeze({ })
124
const parseOptions = options => {
135
if (!options) {
146
return emptyOpts
157
}
168

179
if (typeof options !== 'object') {
18-
return var6
19-
}
20-
21-
if (options[isParsedConfigSymbol]) {
22-
return options
10+
return looseOption
2311
}
2412

25-
if (options.includePrerelease) {
26-
if (options.loose && options.rtl) {
27-
return var1
28-
}
29-
30-
if (options.loose) {
31-
return var2
32-
}
33-
34-
if (options.rtl) {
35-
return var3
36-
}
37-
38-
return var4
39-
} else if (options.loose) {
40-
if (options.rtl) {
41-
return var5
42-
}
43-
44-
return var6
45-
} else if (options.rtl) {
46-
return var7
47-
} else {
48-
return emptyOpts
49-
}
13+
return options
5014
}
5115
module.exports = parseOptions

test/internal/parse-options.js

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,11 @@ 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 })
25-
t.strictSame(parseOptions({ loose: 1, rtl: 2, includePrerelease: 10 }), {
26-
loose: true,
27-
rtl: true,
28-
includePrerelease: true,
29-
})
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 })
25+
t.strictSame(parseOptions({ loose: 1, rtl: 2, includePrerelease: 10 }), { loose: 1, rtl: 2, includePrerelease: 10 })
3026
t.strictSame(parseOptions({ loose: true }), { loose: true })
3127
t.strictSame(parseOptions({ rtl: true }), { rtl: true })
3228
t.strictSame(parseOptions({ includePrerelease: true }), { includePrerelease: true })
@@ -35,12 +31,3 @@ t.test('objects only include truthy flags we know about, set to true', t => {
3531
t.strictSame(parseOptions({ rtl: true, includePrerelease: true }), { rtl: true, includePrerelease: true })
3632
t.end()
3733
})
38-
39-
t.test('should skip validation when options is already parsed', t => {
40-
const options = { loose: true, rtl: true }
41-
const parsedOptions = parseOptions(options)
42-
43-
t.equal(parseOptions(parsedOptions) === parsedOptions, true)
44-
t.not(parseOptions(options) === parsedOptions, false)
45-
t.end()
46-
})

0 commit comments

Comments
 (0)