Skip to content

Commit

Permalink
fix: simplify parse options
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Apr 6, 2023
1 parent 7e4e016 commit 585125b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 58 deletions.
44 changes: 4 additions & 40 deletions internal/parse-options.js
@@ -1,51 +1,15 @@
// parse out just the options we care about
const isParsedConfigSymbol = Symbol('isParsedConfig')
const var1 = Object.freeze({ includePrerelease: true, loose: true, rtl: true, [isParsedConfigSymbol]: true })
const var2 = Object.freeze({ includePrerelease: true, loose: true, [isParsedConfigSymbol]: true })
const var3 = Object.freeze({ includePrerelease: true, rtl: true, [isParsedConfigSymbol]: true })
const var4 = Object.freeze({ includePrerelease: true, [isParsedConfigSymbol]: true })
const var5 = Object.freeze({ loose: true, rtl: true, [isParsedConfigSymbol]: true })
const var6 = Object.freeze({ loose: true, [isParsedConfigSymbol]: true })
const var7 = Object.freeze({ rtl: true, [isParsedConfigSymbol]: true })
const emptyOpts = Object.freeze({ [isParsedConfigSymbol]: true })

const looseOption = Object.freeze({ loose: true })
const emptyOpts = Object.freeze({ })
const parseOptions = options => {
if (!options) {
return emptyOpts
}

if (typeof options !== 'object') {
return var6
}

if (options[isParsedConfigSymbol]) {
return options
return looseOption
}

if (options.includePrerelease) {
if (options.loose && options.rtl) {
return var1
}

if (options.loose) {
return var2
}

if (options.rtl) {
return var3
}

return var4
} else if (options.loose) {
if (options.rtl) {
return var5
}

return var6
} else if (options.rtl) {
return var7
} else {
return emptyOpts
}
return options
}
module.exports = parseOptions
23 changes: 5 additions & 18 deletions test/internal/parse-options.js
Expand Up @@ -18,15 +18,11 @@ t.test('truthy non-objects always loose mode, for backwards comp', t => {
t.end()
})

t.test('objects only include truthy flags we know about, set to true', t => {
t.strictSame(parseOptions(/asdf/), {})
t.strictSame(parseOptions(new Error('hello')), {})
t.strictSame(parseOptions({ loose: true, a: 1, rtl: false }), { loose: true })
t.strictSame(parseOptions({ loose: 1, rtl: 2, includePrerelease: 10 }), {
loose: true,
rtl: true,
includePrerelease: true,
})
t.test('any object passed is returned', t => {
t.strictSame(parseOptions(/asdf/), /asdf/)
t.strictSame(parseOptions(new Error('hello')), new Error('hello'))
t.strictSame(parseOptions({ loose: true, a: 1, rtl: false }), { loose: true, a: 1, rtl: false })
t.strictSame(parseOptions({ loose: 1, rtl: 2, includePrerelease: 10 }), { loose: 1, rtl: 2, includePrerelease: 10 })

Check failure on line 25 in test/internal/parse-options.js

View workflow job for this annotation

GitHub Actions / Lint

This line has a length of 118. Maximum allowed is 100
t.strictSame(parseOptions({ loose: true }), { loose: true })
t.strictSame(parseOptions({ rtl: true }), { rtl: true })
t.strictSame(parseOptions({ includePrerelease: true }), { includePrerelease: true })
Expand All @@ -35,12 +31,3 @@ t.test('objects only include truthy flags we know about, set to true', t => {
t.strictSame(parseOptions({ rtl: true, includePrerelease: true }), { rtl: true, includePrerelease: true })

Check failure on line 31 in test/internal/parse-options.js

View workflow job for this annotation

GitHub Actions / Lint

This line has a length of 108. Maximum allowed is 100
t.end()
})

t.test('should skip validation when options is already parsed', t => {
const options = { loose: true, rtl: true }
const parsedOptions = parseOptions(options)

t.equal(parseOptions(parsedOptions) === parsedOptions, true)
t.not(parseOptions(options) === parsedOptions, false)
t.end()
})

0 comments on commit 585125b

Please sign in to comment.