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. const getSymbols = Object.getOwnPropertySymbols || (obj => []);
  10. /**
  11. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  12. *
  13. * This source code is licensed under the MIT license found in the
  14. * LICENSE file in the root directory of this source tree.
  15. *
  16. *
  17. */
  18. const isSymbol = key =>
  19. // $FlowFixMe string literal `symbol`. This value is not a valid `typeof` return value
  20. typeof key === 'symbol' || toString.call(key) === '[object Symbol]';
  21. // Return entries (for example, of a map)
  22. // with spacing, indentation, and comma
  23. // without surrounding punctuation (for example, braces)
  24. function printIteratorEntries(
  25. // Flow 0.51.0: property `@@iterator` of $Iterator not found in Object
  26. // To allow simplistic getRecordIterator in immutable.js
  27. iterator,
  28. config,
  29. indentation,
  30. depth,
  31. refs,
  32. printer
  33. ) {
  34. let separator =
  35. arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : ': ';
  36. let result = '';
  37. let current = iterator.next();
  38. if (!current.done) {
  39. result += config.spacingOuter;
  40. const indentationNext = indentation + config.indent;
  41. while (!current.done) {
  42. const name = printer(
  43. current.value[0],
  44. config,
  45. indentationNext,
  46. depth,
  47. refs
  48. );
  49. const value = printer(
  50. current.value[1],
  51. config,
  52. indentationNext,
  53. depth,
  54. refs
  55. );
  56. result += indentationNext + name + separator + value;
  57. current = iterator.next();
  58. if (!current.done) {
  59. result += ',' + config.spacingInner;
  60. } else if (!config.min) {
  61. result += ',';
  62. }
  63. }
  64. result += config.spacingOuter + indentation;
  65. }
  66. return result;
  67. }
  68. // Return values (for example, of a set)
  69. // with spacing, indentation, and comma
  70. // without surrounding punctuation (braces or brackets)
  71. function printIteratorValues(
  72. iterator,
  73. config,
  74. indentation,
  75. depth,
  76. refs,
  77. printer
  78. ) {
  79. let result = '';
  80. let current = iterator.next();
  81. if (!current.done) {
  82. result += config.spacingOuter;
  83. const indentationNext = indentation + config.indent;
  84. while (!current.done) {
  85. result +=
  86. indentationNext +
  87. printer(current.value, config, indentationNext, depth, refs);
  88. current = iterator.next();
  89. if (!current.done) {
  90. result += ',' + config.spacingInner;
  91. } else if (!config.min) {
  92. result += ',';
  93. }
  94. }
  95. result += config.spacingOuter + indentation;
  96. }
  97. return result;
  98. }
  99. // Return items (for example, of an array)
  100. // with spacing, indentation, and comma
  101. // without surrounding punctuation (for example, brackets)
  102. function printListItems(list, config, indentation, depth, refs, printer) {
  103. let result = '';
  104. if (list.length) {
  105. result += config.spacingOuter;
  106. const indentationNext = indentation + config.indent;
  107. for (let i = 0; i < list.length; i++) {
  108. result +=
  109. indentationNext +
  110. printer(list[i], config, indentationNext, depth, refs);
  111. if (i < list.length - 1) {
  112. result += ',' + config.spacingInner;
  113. } else if (!config.min) {
  114. result += ',';
  115. }
  116. }
  117. result += config.spacingOuter + indentation;
  118. }
  119. return result;
  120. }
  121. // Return properties of an object
  122. // with spacing, indentation, and comma
  123. // without surrounding punctuation (for example, braces)
  124. function printObjectProperties(val, config, indentation, depth, refs, printer) {
  125. let result = '';
  126. let keys = Object.keys(val).sort();
  127. const symbols = getSymbols(val);
  128. if (symbols.length) {
  129. keys = keys.filter(key => !isSymbol(key)).concat(symbols);
  130. }
  131. if (keys.length) {
  132. result += config.spacingOuter;
  133. const indentationNext = indentation + config.indent;
  134. for (let i = 0; i < keys.length; i++) {
  135. const key = keys[i];
  136. const name = printer(key, config, indentationNext, depth, refs);
  137. const value = printer(val[key], config, indentationNext, depth, refs);
  138. result += indentationNext + name + ': ' + value;
  139. if (i < keys.length - 1) {
  140. result += ',' + config.spacingInner;
  141. } else if (!config.min) {
  142. result += ',';
  143. }
  144. }
  145. result += config.spacingOuter + indentation;
  146. }
  147. return result;
  148. }