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