fracturedjsonjs
Version:
JSON formatter that produces highly readable but fairly compact output
125 lines (124 loc) • 5.37 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PaddedFormattingTokens = void 0;
const BracketPaddingType_1 = require("./BracketPaddingType");
const EolStyle_1 = require("./EolStyle");
const JsonItemType_1 = require("./JsonItemType");
class PaddedFormattingTokens {
constructor(opts, strLenFunc) {
this._arrStart = [];
this._arrStart[BracketPaddingType_1.BracketPaddingType.Empty] = "[";
this._arrStart[BracketPaddingType_1.BracketPaddingType.Simple] = (opts.SimpleBracketPadding) ? "[ " : "[";
this._arrStart[BracketPaddingType_1.BracketPaddingType.Complex] = (opts.NestedBracketPadding) ? "[ " : "[";
this._arrEnd = [];
this._arrEnd[BracketPaddingType_1.BracketPaddingType.Empty] = "]";
this._arrEnd[BracketPaddingType_1.BracketPaddingType.Simple] = (opts.SimpleBracketPadding) ? " ]" : "]";
this._arrEnd[BracketPaddingType_1.BracketPaddingType.Complex] = (opts.NestedBracketPadding) ? " ]" : "]";
this._objStart = [];
this._objStart[BracketPaddingType_1.BracketPaddingType.Empty] = "{";
this._objStart[BracketPaddingType_1.BracketPaddingType.Simple] = (opts.SimpleBracketPadding) ? "{ " : "{";
this._objStart[BracketPaddingType_1.BracketPaddingType.Complex] = (opts.NestedBracketPadding) ? "{ " : "{";
this._objEnd = [];
this._objEnd[BracketPaddingType_1.BracketPaddingType.Empty] = "}";
this._objEnd[BracketPaddingType_1.BracketPaddingType.Simple] = (opts.SimpleBracketPadding) ? " }" : "}";
this._objEnd[BracketPaddingType_1.BracketPaddingType.Complex] = (opts.NestedBracketPadding) ? " }" : "}";
this._comma = (opts.CommaPadding) ? ", " : ",";
this._colon = (opts.ColonPadding) ? ": " : ":";
this._comment = (opts.CommentPadding) ? " " : "";
this._eol = (opts.JsonEolStyle === EolStyle_1.EolStyle.Crlf) ? "\r\n" : "\n";
this._arrStartLen = this._arrStart.map(strLenFunc);
this._arrEndLen = this._arrEnd.map(strLenFunc);
this._objStartLen = this._objStart.map(strLenFunc);
this._objEndLen = this._objEnd.map(strLenFunc);
// Create pre-made indent strings for levels 0 and 1 now. We'll construct and cache others as needed.
this._indentStrings = [
"",
(opts.UseTabToIndent) ? "\t" : " ".repeat(opts.IndentSpaces),
];
// Same with spaces
this._spaceStrings = [
"",
" ",
];
this._commaLen = strLenFunc(this._comma);
this._colonLen = strLenFunc(this._colon);
this._commentLen = strLenFunc(this._comment);
this._literalNullLen = strLenFunc("null");
this._literalTrueLen = strLenFunc("true");
this._literalFalseLen = strLenFunc("false");
this._prefixStringLen = strLenFunc(opts.PrefixString);
this._dummyComma = " ".repeat(this._commaLen);
}
get Comma() { return this._comma; }
;
get Colon() { return this._colon; }
;
get Comment() { return this._comment; }
;
get EOL() { return this._eol; }
;
get CommaLen() { return this._commaLen; }
;
get ColonLen() { return this._colonLen; }
;
get CommentLen() { return this._commentLen; }
;
get LiteralNullLen() { return this._literalNullLen; }
get LiteralTrueLen() { return this._literalTrueLen; }
get LiteralFalseLen() { return this._literalFalseLen; }
get PrefixStringLen() { return this._prefixStringLen; }
;
get DummyComma() { return this._dummyComma; }
;
ArrStart(type) {
return this._arrStart[type];
}
ArrEnd(type) {
return this._arrEnd[type];
}
ObjStart(type) {
return this._objStart[type];
}
ObjEnd(type) {
return this._objEnd[type];
}
Start(elemType, bracketType) {
return (elemType === JsonItemType_1.JsonItemType.Array) ? this.ArrStart(bracketType) : this.ObjStart(bracketType);
}
End(elemType, bracketType) {
return (elemType === JsonItemType_1.JsonItemType.Array) ? this.ArrEnd(bracketType) : this.ObjEnd(bracketType);
}
ArrStartLen(type) {
return this._arrStartLen[type];
}
ArrEndLen(type) {
return this._arrEndLen[type];
}
ObjStartLen(type) {
return this._objStartLen[type];
}
ObjEndLen(type) {
return this._objEndLen[type];
}
StartLen(elemType, bracketType) {
return (elemType === JsonItemType_1.JsonItemType.Array) ? this.ArrStartLen(bracketType) : this.ObjStartLen(bracketType);
}
EndLen(elemType, bracketType) {
return (elemType === JsonItemType_1.JsonItemType.Array) ? this.ArrEndLen(bracketType) : this.ObjEndLen(bracketType);
}
Indent(level) {
if (level >= this._indentStrings.length) {
for (let i = this._indentStrings.length; i <= level; ++i)
this._indentStrings.push(this._indentStrings[i - 1] + this._indentStrings[1]);
}
return this._indentStrings[level];
}
Spaces(level) {
if (level >= this._spaceStrings.length) {
for (let i = this._spaceStrings.length; i <= level; ++i)
this._spaceStrings.push(" ".repeat(i));
}
return this._spaceStrings[level];
}
}
exports.PaddedFormattingTokens = PaddedFormattingTokens;