diff --git a/lib/getObjectKeys.js b/lib/getObjectKeys.js index 290b701..fb6b581 100644 --- a/lib/getObjectKeys.js +++ b/lib/getObjectKeys.js @@ -14,7 +14,7 @@ function getObjectKeys (obj, excludeListItemAccessorsBelowLength) { let accept = true if (excludeListItemAccessorsBelowLength > 0) { const index = Number(name) - accept = (index % 1 !== 0) || index >= excludeListItemAccessorsBelowLength + accept = !Number.isInteger(index) || index < 0 || index >= excludeListItemAccessorsBelowLength } if (accept && Object.getOwnPropertyDescriptor(obj, name).enumerable) { diff --git a/test/compare.js b/test/compare.js index e9abdf7..e385978 100644 --- a/test/compare.js +++ b/test/compare.js @@ -53,3 +53,16 @@ test('survives odd circular references', t => { foo2.foo = foo t.false(compare(foo, foo2).pass) }) + +test('arrays are also compared by property', t => { + const a1 = [1, 2, 3] + a1.p = 'a1' + const a2 = [1, 2, 3] + a2.p = 'a2' + t.false(compare(a1, a2).pass) + + const a3 = [1, 2, 3] + const a4 = [1, 2, 3] + a4[-1] = -1 + t.false(compare(a3, a4).pass) +}) diff --git a/test/format.js b/test/format.js index 8959f5e..b92ea6a 100644 --- a/test/format.js +++ b/test/format.js @@ -160,10 +160,13 @@ test('formats a simple, nested array', t => { }) test('formats an array with additional properties', t => { - const arr = ['foo', 'bar'] - arr.baz = 'qux' - const actual = format(arr) - t.snapshot(actual) + const arr1 = ['foo', 'bar'] + arr1.baz = 'qux' + t.snapshot(format(arr1)) + + const arr2 = [1, 2, 3] + arr2[-1] = -1 + t.snapshot(format(arr2)) }) test('formats a multiline string inside an array', t => { diff --git a/test/snapshots/format.js.md b/test/snapshots/format.js.md index c06b998..30a6f53 100644 --- a/test/snapshots/format.js.md +++ b/test/snapshots/format.js.md @@ -359,6 +359,16 @@ Generated by [AVA](https://avajs.dev). baz%property.separator#: %%string.line.open#'%%string.open%qux%string.close%%string.line.close#'%%property.after#,%␊ %list.closeBracket#]%` +> Snapshot 2 + + `%list.openBracket#[%␊ + %number.open%1%number.close%%item.after#,%␊ + %number.open%2%number.close%%item.after#,%␊ + %number.open%3%number.close%%item.after#,%␊ + %stats.separator#---%␊ + -1%property.separator#: %%number.open%-1%number.close%%property.after#,%␊ + %list.closeBracket#]%` + ## formats anonymous functions > Snapshot 1 diff --git a/test/snapshots/format.js.snap b/test/snapshots/format.js.snap index c86e138..13ac92a 100644 Binary files a/test/snapshots/format.js.snap and b/test/snapshots/format.js.snap differ