Immutable.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. const 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. };
  65. }
  66. };
  67. };
  68. const printImmutableRecord = (
  69. val,
  70. config,
  71. indentation,
  72. depth,
  73. refs,
  74. printer
  75. ) => {
  76. // _name property is defined only for an Immutable Record instance
  77. // which was constructed with a second optional descriptive name arg
  78. const name = getImmutableName(val._name || 'Record');
  79. return ++depth > config.maxDepth
  80. ? printAsLeaf(name)
  81. : name +
  82. SPACE +
  83. '{' +
  84. (0, _collections.printIteratorEntries)(
  85. getRecordEntries(val),
  86. config,
  87. indentation,
  88. depth,
  89. refs,
  90. printer
  91. ) +
  92. '}';
  93. };
  94. const printImmutableSeq = (val, config, indentation, depth, refs, printer) => {
  95. const name = getImmutableName('Seq');
  96. if (++depth > config.maxDepth) {
  97. return printAsLeaf(name);
  98. }
  99. if (val[IS_KEYED_SENTINEL]) {
  100. return (
  101. name +
  102. SPACE +
  103. '{' + // from Immutable collection of entries or from ECMAScript object
  104. (val._iter || val._object
  105. ? (0, _collections.printIteratorEntries)(
  106. val.entries(),
  107. config,
  108. indentation,
  109. depth,
  110. refs,
  111. printer
  112. )
  113. : LAZY) +
  114. '}'
  115. );
  116. }
  117. return (
  118. name +
  119. SPACE +
  120. '[' +
  121. (val._iter || // from Immutable collection of values
  122. val._array || // from ECMAScript array
  123. val._collection || // from ECMAScript collection in immutable v4
  124. val._iterable // from ECMAScript collection in immutable v3
  125. ? (0, _collections.printIteratorValues)(
  126. val.values(),
  127. config,
  128. indentation,
  129. depth,
  130. refs,
  131. printer
  132. )
  133. : LAZY) +
  134. ']'
  135. );
  136. };
  137. const printImmutableValues = (
  138. val,
  139. config,
  140. indentation,
  141. depth,
  142. refs,
  143. printer,
  144. type
  145. ) =>
  146. ++depth > config.maxDepth
  147. ? printAsLeaf(getImmutableName(type))
  148. : getImmutableName(type) +
  149. SPACE +
  150. '[' +
  151. (0, _collections.printIteratorValues)(
  152. val.values(),
  153. config,
  154. indentation,
  155. depth,
  156. refs,
  157. printer
  158. ) +
  159. ']';
  160. const serialize = (val, config, indentation, depth, refs, printer) => {
  161. if (val[IS_MAP_SENTINEL]) {
  162. return printImmutableEntries(
  163. val,
  164. config,
  165. indentation,
  166. depth,
  167. refs,
  168. printer,
  169. val[IS_ORDERED_SENTINEL] ? 'OrderedMap' : 'Map'
  170. );
  171. }
  172. if (val[IS_LIST_SENTINEL]) {
  173. return printImmutableValues(
  174. val,
  175. config,
  176. indentation,
  177. depth,
  178. refs,
  179. printer,
  180. 'List'
  181. );
  182. }
  183. if (val[IS_SET_SENTINEL]) {
  184. return printImmutableValues(
  185. val,
  186. config,
  187. indentation,
  188. depth,
  189. refs,
  190. printer,
  191. val[IS_ORDERED_SENTINEL] ? 'OrderedSet' : 'Set'
  192. );
  193. }
  194. if (val[IS_STACK_SENTINEL]) {
  195. return printImmutableValues(
  196. val,
  197. config,
  198. indentation,
  199. depth,
  200. refs,
  201. printer,
  202. 'Stack'
  203. );
  204. }
  205. if (val[IS_SEQ_SENTINEL]) {
  206. return printImmutableSeq(val, config, indentation, depth, refs, printer);
  207. } // For compatibility with immutable v3 and v4, let record be the default.
  208. return printImmutableRecord(val, config, indentation, depth, refs, printer);
  209. }; // Explicitly comparing sentinel properties to true avoids false positive
  210. // when mock identity-obj-proxy returns the key as the value for any key.
  211. exports.serialize = serialize;
  212. const test = val =>
  213. val &&
  214. (val[IS_ITERABLE_SENTINEL] === true || val[IS_RECORD_SENTINEL] === true);
  215. exports.test = test;
  216. const plugin = {
  217. serialize,
  218. test
  219. };
  220. var _default = plugin;
  221. exports.default = _default;