Skip to content

Commit

Permalink
fix: fix array and undefined entry support (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
unional authored Jan 14, 2018
1 parent 9eee703 commit 8cfa9ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/createSatisfier.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ test('expect [undefined]', t => {
t.true(createSatisfier([undefined]).test([undefined]))
})

test('expect [undefined] should work with [null]', t => {
t.false(createSatisfier([undefined]).test([null]))
})

test('expect array and test against non-array', t => {
const s = createSatisfier([1])
t.false(s.test(null))
t.false(s.test(1))
t.false(s.test('a'))
t.false(s.test(true))
t.false(s.test(undefined as any))
})

test('array with number', t => {
t.true(createSatisfier([1, 2]).test([1, 2]))
})
Expand Down
19 changes: 14 additions & 5 deletions src/createSatisfier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function detectDiff(actual, expected, path: string[] = []) {
})
}
}
else if (expected === null) {
else if (expected === null || expected === undefined) {
if (expected !== actual)
diff.push({
path,
Expand All @@ -81,10 +81,19 @@ function detectDiff(actual, expected, path: string[] = []) {
}
}
else if (Array.isArray(expected)) {
expected.forEach((e, i) => {
const actualValue = actual[i]
diff.push(...detectDiff(actualValue, e, path.concat([`[${i}]`])))
})
if (!Array.isArray(actual)) {
diff.push({
path,
expected,
actual
})
}
else {
expected.forEach((e, i) => {
const actualValue = actual[i]
diff.push(...detectDiff(actualValue, e, path.concat([`[${i}]`])))
})
}
}
else {
// expected is object. If actual is not, then it is diff.
Expand Down

0 comments on commit 8cfa9ea

Please sign in to comment.