immutable.js 4.7 KB

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