map.js 664 B

123456789101112131415161718192021222324252627
  1. import { inspectList } from './helpers'
  2. function inspectMapEntry([key, value], options) {
  3. options.truncate -= 4
  4. key = options.inspect(key, options)
  5. options.truncate -= key.length
  6. value = options.inspect(value, options)
  7. return `${key} => ${value}`
  8. }
  9. // IE11 doesn't support `map.entries()`
  10. function mapToEntries(map) {
  11. const entries = []
  12. map.forEach((value, key) => {
  13. entries.push([key, value])
  14. })
  15. return entries
  16. }
  17. export default function inspectMap(map, options) {
  18. const size = map.size - 1
  19. if (size <= 0) {
  20. return 'Map{}'
  21. }
  22. options.truncate -= 7
  23. return `Map{ ${inspectList(mapToEntries(map), options, inspectMapEntry)} }`
  24. }