MCPcopy
hub / github.com/Exrick/xpay / chooseScalarStyle

Function chooseScalarStyle

xpay-code/src/main/resources/static/swagger/swagger-ui.js:10469–10529  ·  view source on GitHub ↗
(string, singleLineOnly, indentPerLevel, lineWidth, testAmbiguousType)

Source from the content-addressed store, hash-verified

10467// STYLE_LITERAL => no lines are suitable for folding (or lineWidth is -1).
10468// STYLE_FOLDED => a line > lineWidth and can be folded (and lineWidth != -1).
10469function chooseScalarStyle(string, singleLineOnly, indentPerLevel, lineWidth, testAmbiguousType) {
10470 var i;
10471 var char;
10472 var hasLineBreak = false;
10473 var hasFoldableLine = false; // only checked if shouldTrackWidth
10474 var shouldTrackWidth = lineWidth !== -1;
10475 var previousLineBreak = -1; // count the first line correctly
10476 var plain = isPlainSafeFirst(string.charCodeAt(0))
10477 && !isWhitespace(string.charCodeAt(string.length - 1));
10478
10479 if (singleLineOnly) {
10480 // Case: no block styles.
10481 // Check for disallowed characters to rule out plain and single.
10482 for (i = 0; i < string.length; i++) {
10483 char = string.charCodeAt(i);
10484 if (!isPrintable(char)) {
10485 return STYLE_DOUBLE;
10486 }
10487 plain = plain && isPlainSafe(char);
10488 }
10489 } else {
10490 // Case: block styles permitted.
10491 for (i = 0; i < string.length; i++) {
10492 char = string.charCodeAt(i);
10493 if (char === CHAR_LINE_FEED) {
10494 hasLineBreak = true;
10495 // Check if any line can be folded.
10496 if (shouldTrackWidth) {
10497 hasFoldableLine = hasFoldableLine ||
10498 // Foldable line = too long, and not more-indented.
10499 (i - previousLineBreak - 1 > lineWidth &&
10500 string[previousLineBreak + 1] !== ' ');
10501 previousLineBreak = i;
10502 }
10503 } else if (!isPrintable(char)) {
10504 return STYLE_DOUBLE;
10505 }
10506 plain = plain && isPlainSafe(char);
10507 }
10508 // in case the end is missing a \n
10509 hasFoldableLine = hasFoldableLine || (shouldTrackWidth &&
10510 (i - previousLineBreak - 1 > lineWidth &&
10511 string[previousLineBreak + 1] !== ' '));
10512 }
10513 // Although every style can represent \n without escaping, prefer block styles
10514 // for multiline, since they're more readable and they don't add empty lines.
10515 // Also prefer folding a super-long line.
10516 if (!hasLineBreak && !hasFoldableLine) {
10517 // Strings interpretable as another type have to be quoted;
10518 // e.g. the string 'true' vs. the boolean true.
10519 return plain && !testAmbiguousType(string)
10520 ? STYLE_PLAIN : STYLE_SINGLE;
10521 }
10522 // Edge case: block indentation indicator can only have one digit.
10523 if (string[0] === ' ' && indentPerLevel > 9) {
10524 return STYLE_DOUBLE;
10525 }
10526 // At this point we know block styles are valid.

Callers 1

writeScalarFunction · 0.85

Calls 4

isPlainSafeFirstFunction · 0.85
isWhitespaceFunction · 0.85
isPrintableFunction · 0.85
isPlainSafeFunction · 0.85

Tested by

no test coverage detected