Skip to content

Instantly share code, notes, and snippets.

@jonahharris
Last active September 29, 2022 08:04
Show Gist options
  • Select an option

  • Save jonahharris/6989148 to your computer and use it in GitHub Desktop.

Select an option

Save jonahharris/6989148 to your computer and use it in GitHub Desktop.

Revisions

  1. jonahharris renamed this gist Oct 15, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jonahharris created this gist Oct 15, 2013.
    63 changes: 63 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    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 {};
    }