object.js 900 B

12345678910111213141516171819202122232425262728293031
  1. import { inspectProperty, inspectList } from './helpers'
  2. export default function inspectObject(object, options) {
  3. const properties = Object.getOwnPropertyNames(object)
  4. const symbols = Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(object) : []
  5. if (properties.length === 0 && symbols.length === 0) {
  6. return '{}'
  7. }
  8. options.truncate -= 4
  9. options.seen = options.seen || []
  10. if (options.seen.indexOf(object) >= 0) {
  11. return '[Circular]'
  12. }
  13. options.seen.push(object)
  14. const propertyContents = inspectList(
  15. properties.map(key => [key, object[key]]),
  16. options,
  17. inspectProperty
  18. )
  19. const symbolContents = inspectList(
  20. symbols.map(key => [key, object[key]]),
  21. options,
  22. inspectProperty
  23. )
  24. options.seen.pop()
  25. let sep = ''
  26. if (propertyContents && symbolContents) {
  27. sep = ', '
  28. }
  29. return `{ ${propertyContents}${sep}${symbolContents} }`
  30. }