-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: faster parse options using cached objects
- Loading branch information
Showing
2 changed files
with
60 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters