collections.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';Object.defineProperty(exports, "__esModule", { value: true });exports.
  2. printIteratorEntries = printIteratorEntries;exports.
  3. printIteratorValues = printIteratorValues;exports.
  4. printListItems = printListItems;exports.
  5. printObjectProperties = printObjectProperties;const getSymbols = Object.getOwnPropertySymbols || (obj => []); /**
  6. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  7. *
  8. * This source code is licensed under the MIT license found in the
  9. * LICENSE file in the root directory of this source tree.
  10. *
  11. *
  12. */const isSymbol = key => // $FlowFixMe string literal `symbol`. This value is not a valid `typeof` return value
  13. typeof key === 'symbol' || toString.call(key) === '[object Symbol]'; // Return entries (for example, of a map)
  14. // with spacing, indentation, and comma
  15. // without surrounding punctuation (for example, braces)
  16. function printIteratorEntries( // Flow 0.51.0: property `@@iterator` of $Iterator not found in Object
  17. // To allow simplistic getRecordIterator in immutable.js
  18. iterator, config, indentation, depth, refs, printer) {let separator = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : ': ';let result = '';let current = iterator.next();if (!current.done) {result += config.spacingOuter;const indentationNext = indentation + config.indent;while (!current.done) {const name = printer(current.value[0], config, indentationNext, depth, refs);const value = printer(current.value[1], config, indentationNext, depth, refs);result += indentationNext + name + separator + value;current = iterator.next();if (!current.done) {result += ',' + config.spacingInner;} else if (!config.min) {result += ',';}}result += config.spacingOuter + indentation;}return result;} // Return values (for example, of a set)
  19. // with spacing, indentation, and comma
  20. // without surrounding punctuation (braces or brackets)
  21. function printIteratorValues(iterator, config, indentation, depth, refs, printer) {let result = '';let current = iterator.next();if (!current.done) {result += config.spacingOuter;const indentationNext = indentation + config.indent;while (!current.done) {result += indentationNext + printer(current.value, config, indentationNext, depth, refs);current = iterator.next();if (!current.done) {result += ',' + config.spacingInner;} else if (!config.min) {result += ',';}}result += config.spacingOuter + indentation;}return result;} // Return items (for example, of an array)
  22. // with spacing, indentation, and comma
  23. // without surrounding punctuation (for example, brackets)
  24. function printListItems(list, config, indentation, depth, refs, printer) {let result = '';if (list.length) {result += config.spacingOuter;const indentationNext = indentation + config.indent;for (let i = 0; i < list.length; i++) {result += indentationNext + printer(list[i], config, indentationNext, depth, refs);if (i < list.length - 1) {result += ',' + config.spacingInner;} else if (!config.min) {result += ',';}}result += config.spacingOuter + indentation;}return result;} // Return properties of an object
  25. // with spacing, indentation, and comma
  26. // without surrounding punctuation (for example, braces)
  27. function printObjectProperties(val, config, indentation, depth, refs, printer) {let result = '';let keys = Object.keys(val).sort();const symbols = getSymbols(val);if (symbols.length) {keys = keys.filter(key => !isSymbol(key)).concat(symbols);}if (keys.length) {result += config.spacingOuter;const indentationNext = indentation + config.indent;for (let i = 0; i < keys.length; i++) {const key = keys[i];
  28. const name = printer(key, config, indentationNext, depth, refs);
  29. const value = printer(val[key], config, indentationNext, depth, refs);
  30. result += indentationNext + name + ': ' + value;
  31. if (i < keys.length - 1) {
  32. result += ',' + config.spacingInner;
  33. } else if (!config.min) {
  34. result += ',';
  35. }
  36. }
  37. result += config.spacingOuter + indentation;
  38. }
  39. return result;
  40. }