collections.js 4.7 KB

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