Skip to content

Commit

Permalink
Improve robustness of options.type (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronccasanova committed Jun 6, 2022
1 parent 42df341 commit 6afde95
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const typeMappings = {
};

function checkType(type) {
if (type in typeMappings) {
if (Object.hasOwnProperty.call(typeMappings, type)) {
return;
}

throw new Error(`Invalid type specified: ${type}`);
}

const matchType = (type, stat) => type === undefined || stat[typeMappings[type]]();
const matchType = (type, stat) => stat[typeMappings[type]]();

const toPath = urlOrPath => urlOrPath instanceof URL ? fileURLToPath(urlOrPath) : urlOrPath;

Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ test('async', async t => {
message: 'Invalid type specified: rainbows',
});

await t.throwsAsync(locatePath(['fixture'], {type: 'toString'}), {
instanceOf: Error,
message: 'Invalid type specified: toString',
});

await t.throwsAsync(locatePath(['fixture'], {type: 1}), {
instanceOf: Error,
message: 'Invalid type specified: 1',
Expand Down Expand Up @@ -56,6 +61,13 @@ test('sync', t => {
message: 'Invalid type specified: rainbows',
});

t.throws(() => {
locatePathSync(['fixture'], {type: 'toString'});
}, {
instanceOf: Error,
message: 'Invalid type specified: toString',
});

t.throws(() => {
locatePathSync(['fixture'], {type: 1});
}, {
Expand Down

0 comments on commit 6afde95

Please sign in to comment.