Search code examples
javascripthtmljsonstring-parsingtypeof

Parsing variable types from Strings in javaScript


I'm working on an html/js project in which I'm trying to pull two strings from my html script, send them over to js and parse them into their variable types in a way similar to typeof. For example, "true" would eventually come out to be boolean, "Infinity" would be a number, "[]" would be an object, etc. I've gotten pretty close to the result I'm looking for however I've reached a part in my code where a set of variables that typeof would normally parse jut fine if you threw them(not as a string). Although they aren't being parsed properly. Another example: if you do return typeof console.log; it will return function. However, with my current code the more "uncommon" variables to parse are coming out as null and undefined.

Here is the Javascript I wrote to parse the strings:

function valueTypeWriter(a, b) {
    var value = a;
    var valueB = b;
    if (a == 'string') {
        value = ('"' + b + '"');
        valueB = (typeof b);
    }
    else if (a == 'notString'){
        value = b;
        valueB = valueTypeSplicer(b);
    }

    return [value, valueB];
}

function valueTypeSplicer(c) {
    var valueB2 = JSON.parse(c);
    // var valueB2 = c;
    if (Number.isInteger(valueB2) == true) {
        return "number";
    }
    else if ((typeof valueB2 === "undefined")  && (valueB2 !== null)) {
        return "undefined";
    }
    else if((typeof valueB2 === 'function') && (valueB2 !== null)) {
        return "function";
    }
    if((typeof valueB2 === "object") && (valueB2 !== null)) {
        return "object";
    }
    else if ((typeof valueB2 === "boolean")  && (valueB2 !== null)) {
        return "boolean";
    }
    else if (valueB2 !== null){
        return valueB2;
    }
}

And here is a snippet of the html that starts and ends the proccess

<tr>
<script>value = valueTypeWriter("string", "");</script>
<td><script>document.write(value[0]);</script></td>
<td><script>document.write(value[1]);</script></td>
</tr>

This is the table I've already created but is the result I'm going for with this code:

wanted table

And this is how my table keeps coming out:

current table

I've been working at this for a while now, but I'm stuck again and am not sure on how to further this, although I'm still actively looking into it.


Solution

  • Your code throws an error on the types that are null. JSON.parse() only parses valid json strings. Those that it was able to match are all valid types in json.

    https://tc39.es/ecma262/#sec-json.parse

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse

    That said, the json strings will not work:

    JSON.parse('"document"') 
    // variable string
    JSON.parse("\""+ c + "\"")
    

    enter image description here

    src: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

    var valueB2 =  Function("return " + c)();