Files
taimed/node_modules/@prantlf/jsonlint/web/printer.min.js
2025-07-24 17:21:45 +08:00

2 lines
5.0 KiB
JavaScript

(function(global,factory){typeof exports==="object"&&typeof module!=="undefined"?factory(exports):typeof define==="function"&&define.amd?define("jsonlint-printer",["exports"],factory):(global=global||self,factory(global.jsonlintPrinter={}))})(this,function(exports){"use strict";function noop(){}function isIdentifierName(value){return/^[a-zA-Z$_][a-zA-Z0-9$_]*$/.test(value)}function concatenateTokens(tokens){var outputString="";var tokenCount=tokens.length;var tokenIndex;for(tokenIndex=0;tokenIndex<tokenCount;++tokenIndex){outputString+=tokens[tokenIndex].raw}return outputString}function print(tokens,options){if(!(tokens&&tokens.length)){throw new Error("JSON tokens missing.")}if(!(tokens[0]&&tokens[0].raw)){throw new Error("JSON tokens lack raw values.")}if(!options){return concatenateTokens(tokens)}var indentString=options.indent;if(typeof indentString==="number"){indentString=new Array(indentString+1).join(" ")}var prettyPrint=indentString!==undefined;var pruneComments=options.pruneComments;var stripObjectKeys=options.stripObjectKeys;var enforceDoubleQuotes=options.enforceDoubleQuotes;var enforceSingleQuotes=options.enforceSingleQuotes;var trimTrailingCommas=options.trimTrailingCommas;var outputString="";var foundLineBreak,addedLineBreak,needsLineBreak;var addedSpace,needsSpace;var indentLevel=0;var scopes=[];var scopeType;var isValue;var tokenCount=tokens.length;var tokenIndex,token,tokenType,tokenContent;function peekAtNextToken(){var nextTokenIndex=tokenIndex;var nextToken;do{nextToken=tokens[++nextTokenIndex]}while(nextToken&&(nextToken.type==="whitespace"||nextToken.type==="comment"));return nextToken}var addIndent;if(prettyPrint&&indentString){addIndent=function(){for(var i=0;i<indentLevel;++i){outputString+=indentString}}}else{addIndent=noop}var addLineBreak,addDelayedSpaceOrLineBreak;if(prettyPrint){addLineBreak=function(){outputString+="\n"};addDelayedSpaceOrLineBreak=function(){if(needsLineBreak){addLineBreak();addIndent()}else if(needsSpace){outputString+=" "}needsSpace=needsLineBreak=false}}else{addLineBreak=addDelayedSpaceOrLineBreak=noop}var addStandaloneComment,tryAddingInlineComment;if(pruneComments){addStandaloneComment=tryAddingInlineComment=noop}else{if(prettyPrint){addStandaloneComment=function(){if(!addedLineBreak&&tokenIndex>0){addLineBreak();addIndent()}outputString+=tokenContent;foundLineBreak=false;addedLineBreak=false;needsLineBreak=true};tryAddingInlineComment=function(){foundLineBreak=false;addedLineBreak=false;addedSpace=false;var tryTokenIndex=tokenIndex+1;function skipWhitespace(){var token=tokens[tryTokenIndex];if(token&&token.type==="whitespace"){foundLineBreak=token.raw.indexOf("\n")>=0;token=tokens[++tryTokenIndex]}return token}var token=skipWhitespace();if(!foundLineBreak&&token&&token.type==="comment"){if(needsLineBreak){if(!addedLineBreak){addLineBreak();addIndent()}}else{if(!addedSpace){outputString+=" "}}outputString+=token.raw;tokenIndex=tryTokenIndex++;skipWhitespace();if(foundLineBreak){needsSpace=false;needsLineBreak=true}else{needsSpace=true;needsLineBreak=false}}}}else{addStandaloneComment=function(){if(tokenContent[1]==="/"){outputString+="/*";outputString+=tokenContent.substr(2,tokenContent.length-2);outputString+=" */"}else{outputString+=tokenContent}};tryAddingInlineComment=noop}}function addLiteral(){addDelayedSpaceOrLineBreak();var tokenValue=token.value;if(stripObjectKeys&&scopeType==="{"&&!isValue&&isIdentifierName(tokenValue)){outputString+=tokenValue}else if(typeof tokenValue==="string"){if(enforceDoubleQuotes&&tokenContent[0]!=='"'){outputString+=JSON.stringify(tokenValue)}else if(enforceSingleQuotes&&tokenContent[0]!=="'"){outputString+="'"+tokenValue.replace(/'/g,"\\'")+"'"}else{outputString+=tokenContent}}else{outputString+=tokenContent}tryAddingInlineComment()}function openScope(){addDelayedSpaceOrLineBreak();scopes.push(scopeType);scopeType=tokenContent;isValue=scopeType==="[";outputString+=tokenContent;tryAddingInlineComment();++indentLevel;needsLineBreak=true}function closeScope(){scopeType=scopes.pop();addLineBreak();--indentLevel;addIndent();needsSpace=needsLineBreak=false;outputString+=tokenContent;tryAddingInlineComment()}function addComma(){if(trimTrailingCommas){var nextToken=peekAtNextToken();if(nextToken&&nextToken.type==="symbol"){return tryAddingInlineComment()}}addDelayedSpaceOrLineBreak();outputString+=",";tryAddingInlineComment();addLineBreak();addIndent();addedLineBreak=true;needsLineBreak=false;isValue=scopeType==="["}function addColon(){addDelayedSpaceOrLineBreak();outputString+=":";needsSpace=true;tryAddingInlineComment();isValue=true}for(tokenIndex=0;tokenIndex<tokenCount;++tokenIndex){token=tokens[tokenIndex];tokenType=token.type;tokenContent=token.raw;switch(tokenType){case"literal":addLiteral();break;case"comment":addStandaloneComment();break;case"symbol":switch(tokenContent){case"{":case"[":openScope();break;case"}":case"]":closeScope();break;case",":addComma();break;case":":addColon()}break;default:foundLineBreak=tokenContent.indexOf("\n")>=0}}return outputString}exports.print=print;Object.defineProperty(exports,"__esModule",{value:true})});
//# sourceMappingURL=printer.min.js.map