Immutable.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = exports.test = exports.serialize = void 0;
  6. var _collections = require('../collections');
  7. /**
  8. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  9. *
  10. * This source code is licensed under the MIT license found in the
  11. * LICENSE file in the root directory of this source tree.
  12. */
  13. // SENTINEL constants are from https://github.com/facebook/immutable-js
  14. const IS_ITERABLE_SENTINEL = '@@__IMMUTABLE_ITERABLE__@@';
  15. const IS_LIST_SENTINEL = '@@__IMMUTABLE_LIST__@@';
  16. const IS_KEYED_SENTINEL = '@@__IMMUTABLE_KEYED__@@';
  17. const IS_MAP_SENTINEL = '@@__IMMUTABLE_MAP__@@';
  18. const IS_ORDERED_SENTINEL = '@@__IMMUTABLE_ORDERED__@@';
  19. const IS_RECORD_SENTINEL = '@@__IMMUTABLE_RECORD__@@'; // immutable v4
  20. const IS_SEQ_SENTINEL = '@@__IMMUTABLE_SEQ__@@';
  21. const IS_SET_SENTINEL = '@@__IMMUTABLE_SET__@@';
  22. const IS_STACK_SENTINEL = '@@__IMMUTABLE_STACK__@@';
  23. const getImmutableName = name => 'Immutable.' + name;
  24. const printAsLeaf = name => '[' + name + ']';
  25. const SPACE = ' ';
  26. const LAZY = '…'; // Seq is lazy if it calls a method like filter
  27. const printImmutableEntries = (
  28. val,
  29. config,
  30. indentation,
  31. depth,
  32. refs,
  33. printer,
  34. type
  35. ) =>
  36. ++depth > config.maxDepth
  37. ? printAsLeaf(getImmutableName(type))
  38. : getImmutableName(type) +
  39. SPACE +
  40. '{' +
  41. (0, _collections.printIteratorEntries)(
  42. val.entries(),
  43. config,
  44. indentation,
  45. depth,
  46. refs,
  47. printer
  48. ) +
  49. '}'; // Record has an entries method because it is a collection in immutable v3.
  50. // Return an iterator for Immutable Record from version v3 or v4.
  51. function getRecordEntries(val) {
  52. let i = 0;
  53. return {
  54. next() {
  55. if (i < val._keys.length) {
  56. const key = val._keys[i++];
  57. return {
  58. done: false,
  59. value: [key, val.get(key)]
  60. };
  61. }
  62. return {
  63. done: true,
  64. value: undefined
  65. };
  66. }
  67. };
  68. }
  69. const printImmutableRecord = (
  70. val,
  71. config,
  72. indentation,
  73. depth,
  74. refs,
  75. printer
  76. ) => {
  77. // _name property is defined only for an Immutable Record instance
  78. // which was constructed with a second optional descriptive name arg
  79. const name = getImmutableName(val._name || 'Record');
  80. return ++depth > config.maxDepth
  81. ? printAsLeaf(name)
  82. : name +
  83. SPACE +
  84. '{' +
  85. (0, _collections.printIteratorEntries)(
  86. getRecordEntries(val),
  87. config,
  88. indentation,
  89. depth,
  90. refs,
  91. printer
  92. ) +
  93. '}';
  94. };
  95. const printImmutableSeq = (val, config, indentation, depth, refs, printer) => {
  96. const name = getImmutableName('Seq');
  97. if (++depth > config.maxDepth) {
  98. return printAsLeaf(name);
  99. }
  100. if (val[IS_KEYED_SENTINEL]) {
  101. return (
  102. name +
  103. SPACE +
  104. '{' + // from Immutable collection of entries or from ECMAScript object
  105. (val._iter || val._object
  106. ? (0, _collections.printIteratorEntries)(
  107. val.entries(),
  108. config,
  109. indentation,
  110. depth,
  111. refs,
  112. printer
  113. )
  114. : LAZY) +
  115. '}'
  116. );
  117. }
  118. return (
  119. name +
  120. SPACE +
  121. '[' +
  122. (val._iter || // from Immutable collection of values
  123. val._array || // from ECMAScript array
  124. val._collection || // from ECMAScript collection in immutable v4
  125. val._iterable // from ECMAScript collection in immutable v3
  126. ? (0, _collections.printIteratorValues)(
  127. val.values(),
  128. config,
  129. indentation,
  130. depth,
  131. refs,
  132. printer
  133. )
  134. : LAZY) +
  135. ']'
  136. );
  137. };
  138. const printImmutableValues = (
  139. val,
  140. config,
  141. indentation,
  142. depth,
  143. refs,
  144. printer,
  145. type
  146. ) =>
  147. ++depth > config.maxDepth
  148. ? printAsLeaf(getImmutableName(type))
  149. : getImmutableName(type) +
  150. SPACE +
  151. '[' +
  152. (0, _collections.printIteratorValues)(
  153. val.values(),
  154. config,
  155. indentation,
  156. depth,
  157. refs,
  158. printer
  159. ) +
  160. ']';
  161. const serialize = (val, config, indentation, depth, refs, printer) => {
  162. if (val[IS_MAP_SENTINEL]) {
  163. return printImmutableEntries(
  164. val,
  165. config,
  166. indentation,
  167. depth,
  168. refs,
  169. printer,
  170. val[IS_ORDERED_SENTINEL] ? 'OrderedMap' : 'Map'
  171. );
  172. }
  173. if (val[IS_LIST_SENTINEL]) {
  174. return printImmutableValues(
  175. val,
  176. config,
  177. indentation,
  178. depth,
  179. refs,
  180. printer,
  181. 'List'
  182. );
  183. }
  184. if (val[IS_SET_SENTINEL]) {
  185. return printImmutableValues(
  186. val,
  187. config,
  188. indentation,
  189. depth,
  190. refs,
  191. printer,
  192. val[IS_ORDERED_SENTINEL] ? 'OrderedSet' : 'Set'
  193. );
  194. }
  195. if (val[IS_STACK_SENTINEL]) {
  196. return printImmutableValues(
  197. val,
  198. config,
  199. indentation,
  200. depth,
  201. refs,
  202. printer,
  203. 'Stack'
  204. );
  205. }
  206. if (val[IS_SEQ_SENTINEL]) {
  207. return printImmutableSeq(val, config, indentation, depth, refs, printer);
  208. } // For compatibility with immutable v3 and v4, let record be the default.
  209. return printImmutableRecord(val, config, indentation, depth, refs, printer);
  210. }; // Explicitly comparing sentinel properties to true avoids false positive
  211. // when mock identity-obj-proxy returns the key as the value for any key.
  212. exports.serialize = serialize;
  213. const test = val =>
  214. val &&
  215. (val[IS_ITERABLE_SENTINEL] === true || val[IS_RECORD_SENTINEL] === true);
  216. exports.test = test;
  217. const plugin = {
  218. serialize,
  219. test
  220. };
  221. var _default = plugin;
  222. exports.default = _default;