UNPKG

fracturedjsonjs

Version:

JSON formatter that produces highly readable but fairly compact output

103 lines (102 loc) 4.5 kB
/** * Collects spacing information about the columns of a potential table. Each TableTemplate corresponds to * a part of a row, and they're nested recursively to match the JSON structure. (Also used in complex multiline * arrays to try to fit them all nicely together.) * * Say you have an object/array where each item would make a nice row all by itself. We want to try to line up * everything about it - comments, prop names, values. If the row items are themselves objects/arrays, ideally * we'd like to line up all of their children, too, recursively. This only works as long as the structure/types * are consistent. */ import { JsonItemType } from "./JsonItemType"; import { BracketPaddingType } from "./BracketPaddingType"; import { PaddedFormattingTokens } from "./PaddedFormattingTokens"; import { JsonItem } from "./JsonItem"; import { NumberListAlignment } from "./NumberListAlignment"; import { IBuffer } from "./IBuffer"; export declare class TableTemplate { /** * The property name in the table that this segment matches up with. */ LocationInParent: string | undefined; /** * The type of values in the column, if they're uniform. There's some wiggle-room here: for instance, * true and false have different JsonItemTypes but are considered the same type for table purposes. */ Type: JsonItemType; /** * Assessment of whether this is a viable column. The main qualifying factor is that all corresponding pieces * of each row are the same type. */ IsRowDataCompatible: boolean; RowCount: number; NameLength: number; SimpleValueLength: number; PrefixCommentLength: number; MiddleCommentLength: number; PostfixCommentLength: number; IsAnyPostCommentLineStyle: boolean; PadType: BracketPaddingType; /** * True if this is a number column and we're allowed by settings to normalize numbers (rewrite them with the same * precision), and if none of the numbers have too many digits or require scientific notation. */ AllowNumberNormalization: boolean; /** * True if this column contains only numbers and nulls. Number columns are formatted specially, depending on * settings. */ IsNumberList: boolean; /** * Length of the value for this template when things are complicated. For arrays and objects, it's the sum of * all the child templates' lengths, plus brackets and commas and such. For number lists, it's the space * required to align them as appropriate. */ CompositeValueLength: number; /** * If the row contains non-empty array or objects whose value is shorter than the literal null, an extra adjustment * is needed. */ ShorterThanNullAdjustment: number; /** * True if at least one row in the column this represents has a null value. */ ContainsNull: boolean; /** * Length of the entire template, including space for the value, property name, and all comments. */ TotalLength: number; /** * If this TableTemplate corresponds to an object or array, Children contains sub-templates * for the array/object's children. */ Children: TableTemplate[]; constructor(pads: PaddedFormattingTokens, numberListAlignment: NumberListAlignment); /** * Analyzes an object/array for formatting as a potential table. The tableRoot is a container that * is split out across many lines. Each "row" is a single child written inline. */ MeasureTableRoot(tableRoot: JsonItem): void; TryToFit(maximumLength: number): boolean; /** * Added the number, properly aligned and possibly reformatted, according to our measurements. * This assumes that the segment is a number list, and therefore that the item is a number or null. */ FormatNumber(buffer: IBuffer, item: JsonItem, commaBeforePadType: string): void; private static readonly _trulyZeroValString; private static readonly _dotOrE; private readonly _pads; private _numberListAlignment; private _maxDigBeforeDecRaw; private _maxDigAfterDecRaw; private _maxDigBeforeDecNorm; private _maxDigAfterDecNorm; /** * Adjusts this TableTemplate (and its children) to make room for the given rowSegment (and its children). */ private MeasureRowSegment; private PruneAndRecompute; private GetTemplateComplexity; private ContainsDuplicateKeys; private GetNumberFieldWidth; }