From 01d05fbc1def2ed420a66610007eb7bfd3d8e756 Mon Sep 17 00:00:00 2001 From: "Mx. Corey Frang" Date: Wed, 17 Jul 2024 11:36:18 -0400 Subject: [PATCH] more restrictive types for the helper functions --- src/shared/times-option.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/shared/times-option.js b/src/shared/times-option.js index 1e6c74f..82ee7d3 100644 --- a/src/shared/times-option.js +++ b/src/shared/times-option.js @@ -15,14 +15,27 @@ export const timesOption = { docReady: 2000, }; +/** + * @type AriaATCIShared.timesOption + */ const timesDefaults = { ...timesOption }; -function makeSnakeCasedOption(optionName, description) { +/** + * Convert from 'afterNav' to 'time-after-nav'. + * @param {keyof AriaATCIShared.timesOption} optionName + * @returns String + */ +function makeSnakeCasedOption(optionName) { const snakeCased = optionName.replace(/[A-Z]/g, cap => '-' + cap.toLowerCase()); const optionText = `time-${snakeCased}`; return optionText; } +/** + * Create a yargs description for the specified timesOption. + * @param {keyof AriaATCIShared.timesOption} optionName Key from timesOption + * @param {String} describe Description to be used in --show-help + */ function addOptionConfig(optionName, describe) { timesOptionsConfig[makeSnakeCasedOption(optionName)] = { hidden: true, @@ -61,10 +74,18 @@ addOptionConfig('docReady', 'Timeout used waiting for document ready (Safari).') /** * Convert the times dictionary to an array of strings to pass back to args. * @param {AriaATCIShared.timesOption} opts - * @returns [String] + * @returns [string] */ export function timesArgs(opts = timesOption) { - return Object.entries(opts).flatMap(([key, value]) => - value === timesDefaults[key] ? [] : ['--' + makeSnakeCasedOption(key), String(value)] - ); + const args = []; + for (const key of Object.keys(timesOption)) { + const value = timesOption[key]; + // no need to pass on "default" value + if (value === timesDefaults[key]) continue; + // casting in jsdoc syntax is complicated - the extra () around key are + // required to make the type annotation work. + args.push(makeSnakeCasedOption(/** @type {keyof AriaATCIShared.timesOption} */ (key))); + args.push(String(value)); + } + return args; }