|
| 1 | +/** |
| 2 | + * Copyright (c) 2014-present, Facebook, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the BSD-style license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 6 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 7 | + * |
| 8 | + * @flow |
| 9 | + */ |
| 10 | + |
| 11 | +import type {Config, Printer, Refs} from 'types/PrettyFormat'; |
| 12 | + |
| 13 | +const getSymbols = Object.getOwnPropertySymbols || (obj => []); |
| 14 | + |
| 15 | +const isSymbol = key => |
| 16 | + // $FlowFixMe string literal `symbol`. This value is not a valid `typeof` return value |
| 17 | + typeof key === 'symbol' || toString.call(key) === '[object Symbol]'; |
| 18 | + |
| 19 | +// Return entries (for example, of a map) |
| 20 | +// with spacing, indentation, and comma |
| 21 | +// without surrounding punctuation (for example, braces) |
| 22 | +export function printIteratorEntries( |
| 23 | + iterator: Iterator<[any, any]>, |
| 24 | + config: Config, |
| 25 | + indentation: string, |
| 26 | + depth: number, |
| 27 | + refs: Refs, |
| 28 | + printer: Printer, |
| 29 | + // Too bad, so sad that separator for ECMAScript Map has been ' => ' |
| 30 | + // What a distracting diff if you change a data structure to/from |
| 31 | + // ECMAScript Object or Immutable.Map/OrderedMap which use the default. |
| 32 | + separator: string = ': ', |
| 33 | +): string { |
| 34 | + let result = ''; |
| 35 | + let current = iterator.next(); |
| 36 | + |
| 37 | + if (!current.done) { |
| 38 | + result += config.spacingOuter; |
| 39 | + |
| 40 | + const indentationNext = indentation + config.indent; |
| 41 | + |
| 42 | + while (!current.done) { |
| 43 | + const name = printer( |
| 44 | + current.value[0], |
| 45 | + config, |
| 46 | + indentationNext, |
| 47 | + depth, |
| 48 | + refs, |
| 49 | + ); |
| 50 | + const value = printer( |
| 51 | + current.value[1], |
| 52 | + config, |
| 53 | + indentationNext, |
| 54 | + depth, |
| 55 | + refs, |
| 56 | + ); |
| 57 | + |
| 58 | + result += indentationNext + name + separator + value; |
| 59 | + |
| 60 | + current = iterator.next(); |
| 61 | + |
| 62 | + if (!current.done) { |
| 63 | + result += ',' + config.spacingInner; |
| 64 | + } else if (!config.min) { |
| 65 | + result += ','; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + result += config.spacingOuter + indentation; |
| 70 | + } |
| 71 | + |
| 72 | + return result; |
| 73 | +} |
| 74 | + |
| 75 | +// Return values (for example, of a set) |
| 76 | +// with spacing, indentation, and comma |
| 77 | +// without surrounding punctuation (braces or brackets) |
| 78 | +export function printIteratorValues( |
| 79 | + iterator: Iterator<any>, |
| 80 | + config: Config, |
| 81 | + indentation: string, |
| 82 | + depth: number, |
| 83 | + refs: Refs, |
| 84 | + printer: Printer, |
| 85 | +): string { |
| 86 | + let result = ''; |
| 87 | + let current = iterator.next(); |
| 88 | + |
| 89 | + if (!current.done) { |
| 90 | + result += config.spacingOuter; |
| 91 | + |
| 92 | + const indentationNext = indentation + config.indent; |
| 93 | + |
| 94 | + while (!current.done) { |
| 95 | + result += |
| 96 | + indentationNext + |
| 97 | + printer(current.value, config, indentationNext, depth, refs); |
| 98 | + |
| 99 | + current = iterator.next(); |
| 100 | + |
| 101 | + if (!current.done) { |
| 102 | + result += ',' + config.spacingInner; |
| 103 | + } else if (!config.min) { |
| 104 | + result += ','; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + result += config.spacingOuter + indentation; |
| 109 | + } |
| 110 | + |
| 111 | + return result; |
| 112 | +} |
| 113 | + |
| 114 | +// Return items (for example, of an array) |
| 115 | +// with spacing, indentation, and comma |
| 116 | +// without surrounding punctuation (for example, brackets) |
| 117 | +export function printListItems( |
| 118 | + list: any, |
| 119 | + config: Config, |
| 120 | + indentation: string, |
| 121 | + depth: number, |
| 122 | + refs: Refs, |
| 123 | + printer: Printer, |
| 124 | +): string { |
| 125 | + let result = ''; |
| 126 | + |
| 127 | + if (list.length) { |
| 128 | + result += config.spacingOuter; |
| 129 | + |
| 130 | + const indentationNext = indentation + config.indent; |
| 131 | + |
| 132 | + for (let i = 0; i < list.length; i++) { |
| 133 | + result += |
| 134 | + indentationNext + |
| 135 | + printer(list[i], config, indentationNext, depth, refs); |
| 136 | + |
| 137 | + if (i < list.length - 1) { |
| 138 | + result += ',' + config.spacingInner; |
| 139 | + } else if (!config.min) { |
| 140 | + result += ','; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + result += config.spacingOuter + indentation; |
| 145 | + } |
| 146 | + |
| 147 | + return result; |
| 148 | +} |
| 149 | + |
| 150 | +// Return properties of an object |
| 151 | +// with spacing, indentation, and comma |
| 152 | +// without surrounding punctuation (for example, braces) |
| 153 | +export function printObjectProperties( |
| 154 | + val: Object, |
| 155 | + config: Config, |
| 156 | + indentation: string, |
| 157 | + depth: number, |
| 158 | + refs: Refs, |
| 159 | + printer: Printer, |
| 160 | +): string { |
| 161 | + let result = ''; |
| 162 | + let keys = Object.keys(val).sort(); |
| 163 | + const symbols = getSymbols(val); |
| 164 | + |
| 165 | + if (symbols.length) { |
| 166 | + keys = keys.filter(key => !isSymbol(key)).concat(symbols); |
| 167 | + } |
| 168 | + |
| 169 | + if (keys.length) { |
| 170 | + result += config.spacingOuter; |
| 171 | + |
| 172 | + const indentationNext = indentation + config.indent; |
| 173 | + |
| 174 | + for (let i = 0; i < keys.length; i++) { |
| 175 | + const key = keys[i]; |
| 176 | + const name = printer(key, config, indentationNext, depth, refs); |
| 177 | + const value = printer(val[key], config, indentationNext, depth, refs); |
| 178 | + |
| 179 | + result += indentationNext + name + ': ' + value; |
| 180 | + |
| 181 | + if (i < keys.length - 1) { |
| 182 | + result += ',' + config.spacingInner; |
| 183 | + } else if (!config.min) { |
| 184 | + result += ','; |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + result += config.spacingOuter + indentation; |
| 189 | + } |
| 190 | + |
| 191 | + return result; |
| 192 | +} |
0 commit comments