Skip to content

Commit

Permalink
Implemented option population.
Browse files Browse the repository at this point in the history
  • Loading branch information
NotTimTam committed Sep 12, 2024
1 parent c6e852a commit 7c98fb3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nottimtam/file-converter",
"version": "1.2.2",
"version": "1.2.3",
"description": "A headless, self-hostable, open-source file conversion express middleware.",
"main": "index.js",
"type": "module",
Expand Down
31 changes: 13 additions & 18 deletions util/Job.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,21 @@ export default class Job {
* @returns {Object} The populated options.
*/
__populateOptions(options) {
return Object.fromEntries(
Object.entries(options).map(([name, value]) => {
// Find default value.
const option =
this.module &&
this.module.options.find(({ label }) => label === name);
const defaultValue = option && option.default;

// Populate default value when no value is provided.
if (
(value === undefined || value === null) &&
defaultValue !== undefined &&
defaultValue !== null
// Get all default values.
const defaults = Object.fromEntries(
this.module.options
.filter(
({ default: defaultValue }) =>
defaultValue !== undefined && defaultValue !== null
)
return [name, defaultValue];

// Return unpopulated value otherwise.
return [name, value];
})
.map(({ default: defaultValue, label }) => [
label,
defaultValue,
])
);

// Filter in selected options.
return { ...defaults, ...options };
}

/**
Expand Down
8 changes: 6 additions & 2 deletions util/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Option {
* @param {string} config.description An optional option description.
* @param {"string"|"number"|"boolean"} config.type The option's input type.
* @param {string|number|boolean} config.default (optional) The option's default value.
* @param {boolean} config.required (optional) Whether or not to require a value to be provided for this option. Default `false`, if `false`, the option's `validateInput` callback will not be run when no value is provided.
* @param {boolean} config.required (optional) Whether or not to require a value to be provided for this option. Default `false`, if `false`, the option's `validateInput` callback will not be run when no value is provided.
* @param {function} config.validateInput An asynchronous callback function, used to validate the value provided to this option, which is passed as the first and only parameter. Should throw an exception if the value is invalid.
* @param {Object}
*/
Expand Down Expand Up @@ -50,7 +50,11 @@ class Option {
this.description = config.description;
}

if (config.default) {
if (
config.hasOwnProperty("default") &&
config.default !== undefined &&
config.default !== null
) {
if (!Option.typeEnum.includes(typeof config.default))
throw new SyntaxError(
`Module.Option constructor config.default is invalid. Must be of type string, number, boolean, or a nullish value.`
Expand Down

0 comments on commit 7c98fb3

Please sign in to comment.