Skip to content

Commit

Permalink
Added Module option population feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
NotTimTam committed Sep 11, 2024
1 parent d6197d7 commit e970650
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 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.1.5",
"version": "1.1.51",
"description": "A headless, self-hostable, open-source file conversion express middleware.",
"main": "index.js",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion util/Job.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class Job {

await module.convert(
files,
async ({ size }, newFile) => {
async ({ size }) => {
fileConverter.stats.dataConverted += size / 1e6;
fileConverter.stats.filesConverted++;
this.status.filesConverted++;
Expand Down
27 changes: 26 additions & 1 deletion util/Module.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,30 @@ export default class Module {
this._id = uuid();
}

/**
* If an option value is not provided, but the `Option` has a default value configured, we populate in the default value.
* @param {Object} options Optional options object configuration to pass to the module conversion job.
* @returns {Object} The populated options.
*/
__populateOptions(options) {
return Object.fromEntries(
Object.entries(options).map(([name, value]) => {
// Find default value.
const option =
this.options &&
this.options.find(({ label }) => label === name);
const defaultValue = option && option.default;

// Populate default value when no value is provided.
if ((value === undefined || value === null) && defaultValue)
return [name, defaultValue];

// Return unpopulated value otherwise.
return [name, value];
})
);
}

/**
* Get whether this module can convert files **from** a certain mimetype.
* @param {string} mimetype The mimetype to check.
Expand Down Expand Up @@ -301,11 +325,12 @@ export default class Module {
* Convert an array of files using the converter's method.
* @param {Array<*>} files The array of files to convert.
* @param {function} callback An optional asynchronous callback that is passed each file before/after it is converted. `(old, new)`
* @param {Object} options Optional options object configuration to pass to the module conversion job.
* @param {Object} options Optional options object configuration to pass to the module conversion job.
* @returns {string} The path to a zip containing the converted files.
*/
async convert(files, callback, options) {
const { label, customReturn } = this;
if (options) options = this.__populateOptions(options);

files = await Promise.all(
files.map(async (file) => {
Expand Down

0 comments on commit e970650

Please sign in to comment.