aboutsummaryrefslogtreecommitdiff
path: root/node_modules/concordance/lib/getObjectKeys.js
blob: 4b0c3e368eb5c4e1adf2f21b47266e4a8b7c4efb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict'

function getObjectKeys (obj, excludeListItemAccessorsBelowLength) {
  const keys = []
  let size = 0

  // Sort property names, they should never be order-sensitive
  const nameCandidates = Object.getOwnPropertyNames(obj).sort()
  // Comparators should verify symbols in an order-insensitive manner if
  // possible.
  const symbolCandidates = Object.getOwnPropertySymbols(obj)

  for (let i = 0; i < nameCandidates.length; i++) {
    const name = nameCandidates[i]

    let accept = true
    if (excludeListItemAccessorsBelowLength > 0) {
      const index = Number(name)
      accept = (index % 1 !== 0) || index >= excludeListItemAccessorsBelowLength
    }

    if (accept && Object.getOwnPropertyDescriptor(obj, name).enumerable) {
      keys[size++] = name
    }
  }

  for (let i = 0; i < symbolCandidates.length; i++) {
    const symbol = symbolCandidates[i]
    if (Object.getOwnPropertyDescriptor(obj, symbol).enumerable) {
      keys[size++] = symbol
    }
  }

  return { keys, size }
}
module.exports = getObjectKeys