-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5734120
commit 11bdbfc
Showing
2 changed files
with
72 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
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* This function takes in an object of options and an object of molds. | ||
* Filters the options based on the moldObject and returns the filtered options. | ||
* Logs out friendly warnings if the user options are not of the correct type or value. | ||
* | ||
* @param {object} optionsObject - options provided by the user | ||
* @param {object} moldObject - an object defining how the user option should be filtered | ||
* @returns {object} - filtered options | ||
*/ | ||
function handleOptions(optionsObject, moldObject) { | ||
const options = {}; | ||
|
||
for (const key in moldObject) { | ||
const userValue = optionsObject[key]; | ||
const type = moldObject[key].type; | ||
const defaultValue = moldObject[key].default; | ||
|
||
if (userValue === undefined) { | ||
options[key] = defaultValue; | ||
} else if (typeof userValue !== type) { | ||
console.warn( | ||
`The value of ${key} is not of type ${type}. Using default value ${defaultValue} instead.` | ||
); | ||
options[key] = defaultValue; | ||
} else { | ||
switch (type) { | ||
case "string": | ||
break; | ||
case "number": | ||
break; | ||
case "boolean": | ||
break; | ||
case "object": | ||
break; | ||
default: | ||
throw new Error(`Unknown type ${type} for ${key} in moldObject.`); | ||
} | ||
} | ||
} | ||
|
||
return options; | ||
} | ||
|
||
export default handleOptions; |