immutable.js 5.4 KB

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