Skip to content

Commit

Permalink
fix: faster parse options using cached objects
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Apr 6, 2023
1 parent da08e01 commit 9edf714
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 10 deletions.
55 changes: 45 additions & 10 deletions internal/parse-options.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,46 @@
// parse out just the options we care about so we always get a consistent
// obj with keys in a consistent order.
const opts = ['includePrerelease', 'loose', 'rtl']
const parseOptions = options =>
!options ? {}
: typeof options !== 'object' ? { loose: true }
: opts.filter(k => options[k]).reduce((o, k) => {
o[k] = true
return o
}, {})
// 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 parseOptions = options => {
if (!options) return emptyOpts

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

if (options[isParsedConfigSymbol])
return options;

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
}
}
module.exports = parseOptions
15 changes: 15 additions & 0 deletions test/internal/parse-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,20 @@ t.test('objects only include truthy flags we know about, set to true', t => {
rtl: true,
includePrerelease: true,
})
t.strictSame(parseOptions({ loose: true }), {loose: true })
t.strictSame(parseOptions({ rtl: true }), { rtl: true })
t.strictSame(parseOptions({ includePrerelease: true }), { includePrerelease: true })
t.strictSame(parseOptions({ loose: true, rtl: true }), { loose: true, rtl: true })
t.strictSame(parseOptions({ loose: true, includePrerelease: true }), { loose: true, includePrerelease: true })
t.strictSame(parseOptions({ rtl: true, includePrerelease: true }), { rtl: true, includePrerelease: true })
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 9edf714

Please sign in to comment.