collections.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.printIteratorEntries = printIteratorEntries;
  6. exports.printIteratorValues = printIteratorValues;
  7. exports.printListItems = printListItems;
  8. exports.printObjectProperties = printObjectProperties;
  9. /**
  10. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  11. *
  12. * This source code is licensed under the MIT license found in the
  13. * LICENSE file in the root directory of this source tree.
  14. *
  15. */
  16. const getKeysOfEnumerableProperties = object => {
  17. const keys = Object.keys(object).sort();
  18. if (Object.getOwnPropertySymbols) {
  19. Object.getOwnPropertySymbols(object).forEach(symbol => {
  20. if (Object.getOwnPropertyDescriptor(object, symbol).enumerable) {
  21. keys.push(symbol);
  22. }
  23. });
  24. }
  25. return keys;
  26. };
  27. /**
  28. * Return entries (for example, of a map)
  29. * with spacing, indentation, and comma
  30. * without surrounding punctuation (for example, braces)
  31. */
  32. function printIteratorEntries(
  33. iterator,
  34. config,
  35. indentation,
  36. depth,
  37. refs,
  38. printer, // Too bad, so sad that separator for ECMAScript Map has been ' => '
  39. // What a distracting diff if you change a data structure to/from
  40. // ECMAScript Object or Immutable.Map/OrderedMap which use the default.
  41. separator = ': '
  42. ) {
  43. let result = '';
  44. let current = iterator.next();
  45. if (!current.done) {
  46. result += config.spacingOuter;
  47. const indentationNext = indentation + config.indent;
  48. while (!current.done) {
  49. const name = printer(
  50. current.value[0],
  51. config,
  52. indentationNext,
  53. depth,
  54. refs
  55. );
  56. const value = printer(
  57. current.value[1],
  58. config,
  59. indentationNext,
  60. depth,
  61. refs
  62. );
  63. result += indentationNext + name + separator + value;
  64. current = iterator.next();
  65. if (!current.done) {
  66. result += ',' + config.spacingInner;
  67. } else if (!config.min) {
  68. result += ',';
  69. }
  70. }
  71. result += config.spacingOuter + indentation;
  72. }
  73. return result;
  74. }
  75. /**
  76. * Return values (for example, of a set)
  77. * with spacing, indentation, and comma
  78. * without surrounding punctuation (braces or brackets)
  79. */
  80. function printIteratorValues(
  81. iterator,
  82. config,
  83. indentation,
  84. depth,
  85. refs,
  86. printer
  87. ) {
  88. let result = '';
  89. let current = iterator.next();
  90. if (!current.done) {
  91. result += config.spacingOuter;
  92. const indentationNext = indentation + config.indent;
  93. while (!current.done) {
  94. result +=
  95. indentationNext +
  96. printer(current.value, config, indentationNext, depth, refs);
  97. current = iterator.next();
  98. if (!current.done) {
  99. result += ',' + config.spacingInner;
  100. } else if (!config.min) {
  101. result += ',';
  102. }
  103. }
  104. result += config.spacingOuter + indentation;
  105. }
  106. return result;
  107. }
  108. /**
  109. * Return items (for example, of an array)
  110. * with spacing, indentation, and comma
  111. * without surrounding punctuation (for example, brackets)
  112. **/
  113. function printListItems(list, config, indentation, depth, refs, printer) {
  114. let result = '';
  115. if (list.length) {
  116. result += config.spacingOuter;
  117. const indentationNext = indentation + config.indent;
  118. for (let i = 0; i < list.length; i++) {
  119. result +=
  120. indentationNext +
  121. printer(list[i], config, indentationNext, depth, refs);
  122. if (i < list.length - 1) {
  123. result += ',' + config.spacingInner;
  124. } else if (!config.min) {
  125. result += ',';
  126. }
  127. }
  128. result += config.spacingOuter + indentation;
  129. }
  130. return result;
  131. }
  132. /**
  133. * Return properties of an object
  134. * with spacing, indentation, and comma
  135. * without surrounding punctuation (for example, braces)
  136. */
  137. function printObjectProperties(val, config, indentation, depth, refs, printer) {
  138. let result = '';
  139. const keys = getKeysOfEnumerableProperties(val);
  140. if (keys.length) {
  141. result += config.spacingOuter;
  142. const indentationNext = indentation + config.indent;
  143. for (let i = 0; i < keys.length; i++) {
  144. const key = keys[i];
  145. const name = printer(key, config, indentationNext, depth, refs);
  146. const value = printer(val[key], config, indentationNext, depth, refs);
  147. result += indentationNext + name + ': ' + value;
  148. if (i < keys.length - 1) {
  149. result += ',' + config.spacingInner;
  150. } else if (!config.min) {
  151. result += ',';
  152. }
  153. }
  154. result += config.spacingOuter + indentation;
  155. }
  156. return result;
  157. }