Last active
September 29, 2022 08:04
-
-
Save jonahharris/6989148 to your computer and use it in GitHub Desktop.
Oracle TNS Connect String Parser in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseConnectString(str) { | |
var strLen = str.length; | |
var isFound = false; | |
var sb = []; | |
var par = []; | |
for (var jj = 0; jj < strLen; ++jj) { | |
var c = str[jj]; | |
if (' ' === c) | |
continue; | |
if ('(' === c) { | |
isFound = false; | |
} else if (')' === c) { | |
var child = par.shift(); | |
if (true === isFound) { | |
var value = sb.join(''); | |
if (true === /^\+?(0|[1-9]\d*)$/.test(value)) | |
child.value = parseInt(value); | |
else | |
child.value = value; | |
sb = []; | |
isFound = false; | |
} | |
if (0 === par.length) { | |
var _objectify = function (arr) { | |
var rv = {}; | |
if ('object' === typeof arr['value']) { | |
var val = {}; | |
for (var ii = 0; ii < arr['value'].length; ++ii) { | |
var obj = _objectify(arr['value'][ii]); | |
for (var pair in obj) | |
val[pair] = obj[pair]; | |
} | |
rv[arr['name']] = val; | |
} else { | |
rv[arr['name']] = arr['value']; | |
} | |
return rv; | |
} | |
return _objectify(child); | |
} else { | |
par[0].value.push(child); | |
} | |
} else if ('=' === c) { | |
var child = { | |
name: sb.join('') | |
.toLowerCase(), | |
value: [] | |
}; | |
par.unshift(child); | |
sb = []; | |
isFound = true; | |
} else { | |
sb.push(c); | |
} | |
} | |
return {}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This parses:
into: