immutable.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.test = exports.serialize = undefined;
  6. var _collections = require('../collections');
  7. // SENTINEL constants are from https://github.com/facebook/immutable-js
  8. /**
  9. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. *
  14. *
  15. */
  16. const IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@';
  17. const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
  18. const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
  19. const IS_MAP_SENTINEL = '@@__IMMUTABLE_MAP__@@';
  20. const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
  21. const IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@'; // immutable v4
  22. const IS_SEQ_SENTINEL = '@@__IMMUTABLE_SEQ__@@';
  23. const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
  24. const IS_STACK_SENTINEL = '@@__IMMUTABLE_STACK__@@';
  25. const getImmutableName = name => 'Immutable.' + name;
  26. const printAsLeaf = name => '[' + name + ']';
  27. const SPACE = ' ';
  28. const LAZY = '…'; // Seq is lazy if it calls a method like filter
  29. const printImmutableEntries = (val, config, indentation, depth, refs, printer, type) => ++depth > config.maxDepth ? printAsLeaf(getImmutableName(type)) : getImmutableName(type) + SPACE + '{' + (0, _collections.printIteratorEntries)(val.entries(), config, indentation, depth, refs, printer) + '}';
  30. // Record has an entries method because it is a collection in immutable v3.
  31. // Return an iterator for Immutable Record from version v3 or v4.
  32. const getRecordEntries = val => {
  33. let i = 0;
  34. return {
  35. next() {
  36. if (i < val._keys.length) {
  37. const key = val._keys[i++];
  38. return { done: false, value: [key, val.get(key)] };
  39. }
  40. return { done: true };
  41. }
  42. };
  43. };
  44. const printImmutableRecord = (val, config, indentation, depth, refs, printer) => {
  45. // _name property is defined only for an Immutable Record instance
  46. // which was constructed with a second optional descriptive name arg
  47. const name = getImmutableName(val._name || 'Record');
  48. return ++depth > config.maxDepth ? printAsLeaf(name) : name + SPACE + '{' + (0, _collections.printIteratorEntries)(getRecordEntries(val), config, indentation, depth, refs, printer) + '}';
  49. };
  50. const printImmutableSeq = (val, config, indentation, depth, refs, printer) => {
  51. const name = getImmutableName('Seq');
  52. if (++depth > config.maxDepth) {
  53. return printAsLeaf(name);
  54. }
  55. if (val[IS_KEYED_SENTINEL]) {
  56. return name + SPACE + '{' + (
  57. // from Immutable collection of entries or from ECMAScript object
  58. val._iter || val._object ? (0, _collections.printIteratorEntries)(val.entries(), config, indentation, depth, refs, printer) : LAZY) + '}';
  59. }
  60. return name + SPACE + '[' + (val._iter || // from Immutable collection of values
  61. val._array || // from ECMAScript array
  62. val._collection || // from ECMAScript collection in immutable v4
  63. val._iterable // from ECMAScript collection in immutable v3
  64. ? (0, _collections.printIteratorValues)(val.values(), config, indentation, depth, refs, printer) : LAZY) + ']';
  65. };
  66. const printImmutableValues = (val, config, indentation, depth, refs, printer, type) => ++depth > config.maxDepth ? printAsLeaf(getImmutableName(type)) : getImmutableName(type) + SPACE + '[' + (0, _collections.printIteratorValues)(val.values(), config, indentation, depth, refs, printer) + ']';
  67. const serialize = exports.serialize = (val, config, indentation, depth, refs, printer) => {
  68. if (val[IS_MAP_SENTINEL]) {
  69. return printImmutableEntries(val, config, indentation, depth, refs, printer, val[IS_ORDERED_SENTINEL] ? 'OrderedMap' : 'Map');
  70. }
  71. if (val[IS_LIST_SENTINEL]) {
  72. return printImmutableValues(val, config, indentation, depth, refs, printer, 'List');
  73. }
  74. if (val[IS_SET_SENTINEL]) {
  75. return printImmutableValues(val, config, indentation, depth, refs, printer, val[IS_ORDERED_SENTINEL] ? 'OrderedSet' : 'Set');
  76. }
  77. if (val[IS_STACK_SENTINEL]) {
  78. return printImmutableValues(val, config, indentation, depth, refs, printer, 'Stack');
  79. }
  80. if (val[IS_SEQ_SENTINEL]) {
  81. return printImmutableSeq(val, config, indentation, depth, refs, printer);
  82. }
  83. // For compatibility with immutable v3 and v4, let record be the default.
  84. return printImmutableRecord(val, config, indentation, depth, refs, printer);
  85. };
  86. // Explicitly comparing sentinel properties to true avoids false positive
  87. // when mock identity-obj-proxy returns the key as the value for any key.
  88. const test = exports.test = val => val && (val[IS_ITERABLE_SENTINEL] === true || val[IS_RECORD_SENTINEL] === true);
  89. exports.default = { serialize, test };