Skip to content

Commit

Permalink
Negative-integer keys on lists must be treated as object keys
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed Aug 22, 2020
1 parent 0c004bb commit 591366d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/getObjectKeys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 13 additions & 0 deletions test/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
11 changes: 7 additions & 4 deletions test/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
10 changes: 10 additions & 0 deletions test/snapshots/format.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file modified test/snapshots/format.js.snap
Binary file not shown.

0 comments on commit 591366d

Please sign in to comment.