1

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.

1 Answer 1

1

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)();
Sign up to request clarification or add additional context in comments.

3 Comments

Do you know of any alternatives? I'm using json.parse right now because it does the most for me but as you can see it throws errors. Do you know of any other functions/modules I can use to the same effect but for everything? or at least that I can use for the half that isnt working
Added it to the end of the answer. It will evaluate the string as javascript and return it. You should put it in a try-catch though since it will throw an error if is undefined.
Thanks! It works great, the only problem I'm having now is the fact that null and {} come up as undefined instead of an object, but those are close enough unless there is a quick fix

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.