UNPKG

4.54 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.printIteratorEntries = printIteratorEntries;
7exports.printIteratorValues = printIteratorValues;
8exports.printListItems = printListItems;
9exports.printObjectProperties = printObjectProperties;
10
11/**
12 * Copyright (c) Facebook, Inc. and its affiliates. 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 */
18const getKeysOfEnumerableProperties = object => {
19 const keys = Object.keys(object).sort();
20
21 if (Object.getOwnPropertySymbols) {
22 Object.getOwnPropertySymbols(object).forEach(symbol => {
23 if (Object.getOwnPropertyDescriptor(object, symbol).enumerable) {
24 keys.push(symbol);
25 }
26 });
27 }
28
29 return keys;
30};
31/**
32 * Return entries (for example, of a map)
33 * with spacing, indentation, and comma
34 * without surrounding punctuation (for example, braces)
35 */
36
37function printIteratorEntries(
38 iterator,
39 config,
40 indentation,
41 depth,
42 refs,
43 printer, // Too bad, so sad that separator for ECMAScript Map has been ' => '
44 // What a distracting diff if you change a data structure to/from
45 // ECMAScript Object or Immutable.Map/OrderedMap which use the default.
46 separator = ': '
47) {
48 let result = '';
49 let current = iterator.next();
50
51 if (!current.done) {
52 result += config.spacingOuter;
53 const indentationNext = indentation + config.indent;
54
55 while (!current.done) {
56 const name = printer(
57 current.value[0],
58 config,
59 indentationNext,
60 depth,
61 refs
62 );
63 const value = printer(
64 current.value[1],
65 config,
66 indentationNext,
67 depth,
68 refs
69 );
70 result += indentationNext + name + separator + value;
71 current = iterator.next();
72
73 if (!current.done) {
74 result += ',' + config.spacingInner;
75 } else if (!config.min) {
76 result += ',';
77 }
78 }
79
80 result += config.spacingOuter + indentation;
81 }
82
83 return result;
84}
85/**
86 * Return values (for example, of a set)
87 * with spacing, indentation, and comma
88 * without surrounding punctuation (braces or brackets)
89 */
90
91function printIteratorValues(
92 iterator,
93 config,
94 indentation,
95 depth,
96 refs,
97 printer
98) {
99 let result = '';
100 let current = iterator.next();
101
102 if (!current.done) {
103 result += config.spacingOuter;
104 const indentationNext = indentation + config.indent;
105
106 while (!current.done) {
107 result +=
108 indentationNext +
109 printer(current.value, config, indentationNext, depth, refs);
110 current = iterator.next();
111
112 if (!current.done) {
113 result += ',' + config.spacingInner;
114 } else if (!config.min) {
115 result += ',';
116 }
117 }
118
119 result += config.spacingOuter + indentation;
120 }
121
122 return result;
123}
124/**
125 * Return items (for example, of an array)
126 * with spacing, indentation, and comma
127 * without surrounding punctuation (for example, brackets)
128 **/
129
130function printListItems(list, config, indentation, depth, refs, printer) {
131 let result = '';
132
133 if (list.length) {
134 result += config.spacingOuter;
135 const indentationNext = indentation + config.indent;
136
137 for (let i = 0; i < list.length; i++) {
138 result += indentationNext;
139
140 if (i in list) {
141 result += printer(list[i], config, indentationNext, depth, refs);
142 }
143
144 if (i < list.length - 1) {
145 result += ',' + config.spacingInner;
146 } else if (!config.min) {
147 result += ',';
148 }
149 }
150
151 result += config.spacingOuter + indentation;
152 }
153
154 return result;
155}
156/**
157 * Return properties of an object
158 * with spacing, indentation, and comma
159 * without surrounding punctuation (for example, braces)
160 */
161
162function printObjectProperties(val, config, indentation, depth, refs, printer) {
163 let result = '';
164 const keys = getKeysOfEnumerableProperties(val);
165
166 if (keys.length) {
167 result += config.spacingOuter;
168 const indentationNext = indentation + config.indent;
169
170 for (let i = 0; i < keys.length; i++) {
171 const key = keys[i];
172 const name = printer(key, config, indentationNext, depth, refs);
173 const value = printer(val[key], config, indentationNext, depth, refs);
174 result += indentationNext + name + ': ' + value;
175
176 if (i < keys.length - 1) {
177 result += ',' + config.spacingInner;
178 } else if (!config.min) {
179 result += ',';
180 }
181 }
182
183 result += config.spacingOuter + indentation;
184 }
185
186 return result;
187}