Skip to content

Commit

Permalink
fix(interopDefault): handle non-object inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Oct 18, 2021
1 parent 54c1fb6 commit 0d73a44
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/index.mjs
Expand Up @@ -108,7 +108,7 @@ export function createResolve (defaults) {
}

export function interopDefault (sourceModule) {
if (!(sourceModule && 'default' in sourceModule)) {
if (!isObject(sourceModule) || !('default' in sourceModule)) {
return sourceModule
}
const newModule = sourceModule.default
Expand Down Expand Up @@ -314,3 +314,7 @@ function _perr (_err) {
Error.captureStackTrace(err, _pcall)
return Promise.reject(err)
}

function isObject (val) {
return val !== null && typeof val === 'object'
}
6 changes: 5 additions & 1 deletion test/interop.test.mjs
Expand Up @@ -4,6 +4,8 @@ import { interopDefault } from '../lib/index.mjs'
const tests = [
[{}, {}],
[{ default: {} }, {}],
[true, true],
[[1, 2, 3], [1, 2, 3]],
[{ default: { x: 2 } }, { x: 2 }],
[{ named: 2 }, { named: 2 }],
[{ named: 2, default: {} }, { named: 2 }],
Expand All @@ -15,8 +17,10 @@ describe('interopDefault', () => {
it(JSON.stringify(input), () => {
const interop = interopDefault(input)
expect(interop).to.deep.equal(result)
if ('default' in input) {
if (typeof input === 'object' && 'default' in input) {
expect(interop.default).to.deep.equal(result)
} else {
expect(interop).to.deep.equal(result)
}
})
}
Expand Down

0 comments on commit 0d73a44

Please sign in to comment.