forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSONUtils.js
More file actions
1 lines (1 loc) · 4.6 KB
/
JSONUtils.js
File metadata and controls
1 lines (1 loc) · 4.6 KB
1
define(function(require,exports,module){var TokenUtils=require("utils/TokenUtils");const TOKEN_KEY=1,TOKEN_VALUE=2;var valueTokenTypes=["atom","string","number","variable"],regexAllowedChars=/(?:^[:,\[]$)|(?:^\s+$)/;function _createContextInfo(token,tokenType,offset,keyName,valueName,parentKeyName,isArray,exclusionList,shouldReplace){return{token:token||null,tokenType:tokenType||null,offset:offset||0,keyName:keyName||null,valueName:valueName||null,parentKeyName:parentKeyName||null,isArray:isArray||!1,exclusionList:exclusionList||[],shouldReplace:shouldReplace||!1}}function stripQuotes(string){return string&&(/^['"]$/.test(string.charAt(0))&&(string=string.substr(1)),/^['"]$/.test(string.substr(-1,1))&&(string=string.substr(0,string.length-1))),string}function _getParentKeyName(ctx){for(var parentKeyName,braceParity=1,hasColon;TokenUtils.moveSkippingWhitespace(TokenUtils.movePrevToken,ctx);)if(null===ctx.token.type&&("}"===ctx.token.string?braceParity++:"{"===ctx.token.string&&braceParity--),0===braceParity){for(;TokenUtils.moveSkippingWhitespace(TokenUtils.movePrevToken,ctx);)if(null===ctx.token.type&&":"===ctx.token.string)hasColon=!0;else if("string property"===ctx.token.type){parentKeyName=stripQuotes(ctx.token.string);break}break}return parentKeyName&&hasColon?parentKeyName:null}function _getExclusionList(editor,constPos){var ctxPrev,ctxNext,exclusionList=[],pos,braceParity;for(pos=$.extend({},constPos),braceParity=1,ctxPrev=TokenUtils.getInitialContext(editor._codeMirror,pos);TokenUtils.moveSkippingWhitespace(TokenUtils.movePrevToken,ctxPrev);)if(null===ctxPrev.token.type&&("}"===ctxPrev.token.string?braceParity++:"{"===ctxPrev.token.string&&braceParity--),1===braceParity&&"string property"===ctxPrev.token.type)exclusionList.push(stripQuotes(ctxPrev.token.string));else if(0===braceParity)break;for(pos=$.extend({},constPos),braceParity=1,ctxNext=TokenUtils.getInitialContext(editor._codeMirror,pos);TokenUtils.moveSkippingWhitespace(TokenUtils.moveNextToken,ctxNext);)if(null===ctxNext.token.type&&("{"===ctxNext.token.string?braceParity++:"}"===ctxNext.token.string&&braceParity--),1===braceParity&&"string property"===ctxNext.token.type)exclusionList.push(stripQuotes(ctxNext.token.string));else if(0===braceParity)break;return exclusionList}function getContextInfo(editor,constPos,requireParent,requireNextToken){var pos,ctx,ctxPrev,ctxNext,offset,keyName,valueName,parentKeyName,isArray,exclusionList,hasColon,hasComma,hasBracket,shouldReplace;if(pos=$.extend({},constPos),ctx=TokenUtils.getInitialContext(editor._codeMirror,pos),offset=TokenUtils.offsetInToken(ctx),ctx.token&&"string property"===ctx.token.type)return/^['"]$/.test(ctx.token.string.substr(-1,1))&&1!==ctx.token.string.length&&ctx.token.end===pos.ch?null:(keyName=stripQuotes(ctx.token.string),requireParent&&(parentKeyName=stripQuotes(_getParentKeyName(ctxPrev=$.extend(!0,{},ctx)))),ctxNext=$.extend(!0,{},ctx),TokenUtils.moveSkippingWhitespace(TokenUtils.moveNextToken,ctxNext),null===ctxNext.token.type&&":"===ctxNext.token.string&&(shouldReplace=!0),exclusionList=_getExclusionList(editor,pos=$.extend({},constPos)),_createContextInfo(ctx.token,TOKEN_KEY,offset,keyName,valueName,parentKeyName,null,exclusionList,shouldReplace));if(ctx.token&&(-1!==valueTokenTypes.indexOf(ctx.token.type)||null===ctx.token.type&®exAllowedChars.test(ctx.token.string))){if("string"===ctx.token.type&&/^['"]$/.test(ctx.token.string.substr(-1,1))&&1!==ctx.token.string.length&&ctx.token.end===pos.ch)return null;for(valueName=ctx.token.string,null===ctx.token.type&&(":"===ctx.token.string?hasColon=!0:","===ctx.token.string?hasComma=!0:"["===ctx.token.string&&(hasBracket=!0)),ctxPrev=$.extend(!0,{},ctx);TokenUtils.moveSkippingWhitespace(TokenUtils.movePrevToken,ctxPrev);){if("string property"===ctxPrev.token.type){keyName=stripQuotes(ctxPrev.token.string);break}if(null===ctxPrev.token.type)if(":"===ctxPrev.token.string)hasColon=!0;else if(","===ctxPrev.token.string)hasComma=!0;else{if("["!==ctxPrev.token.string)return null;hasBracket=!0}else if(!hasComma)return null}return!keyName||!hasColon||hasComma&&!hasBracket?null:(isArray=hasBracket,requireParent&&(parentKeyName=stripQuotes(_getParentKeyName(ctxPrev=$.extend(!0,{},ctx)))),ctxNext=$.extend(!0,{},ctx),TokenUtils.moveNextToken(ctxNext),requireNextToken&&-1!==valueTokenTypes.indexOf(ctxNext.token.type)&&(shouldReplace=!0),_createContextInfo(shouldReplace?ctxNext.token:ctx.token,TOKEN_VALUE,offset,keyName,valueName,parentKeyName,isArray,null,shouldReplace))}return null}exports.TOKEN_KEY=TOKEN_KEY,exports.TOKEN_VALUE=TOKEN_VALUE,exports.regexAllowedChars=regexAllowedChars,exports.getContextInfo=getContextInfo,exports.stripQuotes=stripQuotes});