JavaScript strval
Get the string value of a variable
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 | function strval (str) { // Get the string value of a variable // // version: 1109.2015 // discuss at: http://phpjs.org/functions/strval // + original by: Brett Zamir (http://brett-zamir.me) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // % note 1: Comment out the entire switch if you want JS-like behavior instead of PHP behavior // - depends on: gettype // * example 1: strval({red: 1, green: 2, blue: 3, white: 4}); // * returns 1: 'Array' var type = ''; if (str === null) { return ''; } type = this.gettype(str); switch (type) { case 'boolean': if (str === true) { return '1'; } return ''; case 'array': return 'Array'; case 'object': return 'Object'; } return str; } |
Examples
Running
1 | strval({red: 1, green: 2, blue: 3, white: 4}); |
Should return
1 | 'Array' |
Dependencies
In order to use this function, you also need:
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have strval goodness in JavaScript.
@ Onno Marsman: Again, because this was such a controversial decision, I cannot be assured that we never change it's implementation. I think other functions should be aware of this uncertainty and allow for different approaches we may or may not take in the future.
Take Brett's point for example: for me this may be the most acceptable way of distinguishing objects from arrays I've seen so far. But inded: only if the length property is already present, I will never want php.js to pollute user data.
And so that basically means the length element will always have to be added by the users themselves. Which is very un-php-like and would require documentation throughout the project to keep pointing that out to everyone using associative array functions.
From a PHP point of view: What would be the need to even look at objects differently than associative arrays. Maybe it's just me but I cannot imagine many situations that would require this distinction. If you look at it from a JavaScript perspective: yes, but then, you should also just use javascript statements if you want that.
Sorry, Brett, didn't read your comments, you already mentioned this, though I wouldn't solve it the way you suggest because of the argument against this you provided yourself.
Didn't contribute much here, did I... sorry ;)
Np at all...
I guess you mean "class-like functions will be presented as 'function'"? Since PHP doesn't even allow classes or functions as data like in JavaScript (except callbacks represented as strings), I guess it wouldn't hurt to return "function" as it does now. But as it is now, there's no way to get "object" back, as in PHP, since you're treating them as arrays.
As far as polluting user variables, rather than adding the 'length' property to an object that doesn't have it, I'm now just suggesting that if objects are passed in without 'length', then they should be considered regular objects and not arrays. This will make type detection more clear, and it is a good practice to encourage adding 'length' if it is to be an "array-like object". Some extensions like array_push() could even increment this property for the user, if it were present (though that need not be enforced, as array-like objects often let the user handle the length handling, though I think it would be convenient for PHP-JS to handle). I think the danger of someone including a "length" property on an object and our treating it as having some other meaning (though we wouldn't need to change it, if you don't want) would be much less prevalent than someone expecting, for example, is_array() to return false for a regular object. JavaScript doesn't even guarantee that objects will be iterated in order (though they always seem to be), so I think it is more standard to only treat them as arrays if the user is doing something to make them seem that way. But it's of course your call.
Thanks for your hard work in keeping up with us! Your thoughtful quality control really adds clout to the project...
@ Brett Zamir: strval: yeah stupid, it was late last night ;)
This is actually the exact same discussion as with is_array. 'Polluting' user variables is never an option in my eyes.
But class-like objects will be presented as 'function' right?
Hi Kevin,
A couple things... First, "type" should not be added to the final return result. strval() just gives the value unless it is an array, object.
Also, by using is_array() which returns true for objects, putting in an object gives "Array". Maybe to resolve that issue, you could require that people who wish to use objects as arrays add a length property? I think that might be a good compromise (of course the functions would need to be programmed to ignore the length property).


Onno Marsman
15 Jan '09
But I really don't care much about these types of functions because I'll never use them. So as far as I'm concerned: feel free to do anything. I just can't help replying to stuff like this when I see things getting more complex than I think they need to be.