Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code refactoring #119

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/fs-access/file-open.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ export default async (options = [{}]) => {
if (!Array.isArray(options)) {
options = [options];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to mutate parameters

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you propose as an alternative if the function should accept arrays and single values at the same time?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const opts = Array.isArray(options) ? options : [options];

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but what would be the advantage? Honestly curious.

}
const types = [];
options.forEach((option, i) => {
types[i] = {
const types = options.map((option) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map looks much better here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks more “intended” indeed.

const type = {
description: option.description || 'Files',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to use destructuring assignment for options and move const type to the end of this arrow function

accept: {},
};
if (option.mimeTypes) {
option.mimeTypes.map((mimeType) => {
types[i].accept[mimeType] = option.extensions || [];
});
for (const mimeType of option.mimeTypes) {
type.accept[mimeType] = option.extensions || [];
}
} else {
types[i].accept['*/*'] = option.extensions || [];
type.accept['*/*'] = option.extensions || [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code duplication, see line 39

}
return type;
});
const handleOrHandles = await window.showOpenFilePicker({
id: options[0].id,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we can use destructuring assignment for arrays and put options[0] to intermediate variable: const [first] = options; or even const [{ id, startIn, multiple, excludeAcceptAllOption }] = options;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this can work.

Expand Down