iterable-to-string.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. var slice = require("@sinonjs/commons").prototypes.string.slice;
  3. var typeOf = require("@sinonjs/commons").typeOf;
  4. var valueToString = require("@sinonjs/commons").valueToString;
  5. module.exports = function iterableToString(obj) {
  6. var representation = "";
  7. function stringify(item) {
  8. return typeof item === "string"
  9. ? "'" + item + "'"
  10. : valueToString(item);
  11. }
  12. function mapToString(map) {
  13. /* eslint-disable-next-line local-rules/no-prototype-methods */
  14. map.forEach(function(value, key) {
  15. representation +=
  16. "[" + stringify(key) + "," + stringify(value) + "],";
  17. });
  18. representation = slice(representation, 0, -1);
  19. return representation;
  20. }
  21. function genericIterableToString(iterable) {
  22. /* eslint-disable-next-line local-rules/no-prototype-methods */
  23. iterable.forEach(function(value) {
  24. representation += stringify(value) + ",";
  25. });
  26. representation = slice(representation, 0, -1);
  27. return representation;
  28. }
  29. if (typeOf(obj) === "map") {
  30. return mapToString(obj);
  31. }
  32. return genericIterableToString(obj);
  33. };