Commit 5f1c179d by Scott

Merge pull request #65 from pupi1985/patch-27

Added support for boolean values to the qa_js function
parents 6c5d47da ed7e88f5
......@@ -864,10 +864,14 @@
function qa_js($value, $forcequotes=false)
/*
Return JavaScript representation of $value, putting in quotes if non-numeric or if $forcequotes is true
Return JavaScript representation of $value, putting in quotes if non-numeric or if $forcequotes is true. In the
case of boolean values they are returned as the appropriate true or false string
*/
{
if (is_numeric($value) && !$forcequotes)
$boolean = is_bool($value);
if ($boolean)
$value = $value ? 'true' : 'false';
if ((is_numeric($value) || $boolean) && !$forcequotes)
return $value;
else
return "'".strtr($value, array(
......
......@@ -155,4 +155,23 @@ class UtilStringTest extends PHPUnit_Framework_TestCase
$this->assertFalse( qa_string_matches_one($this->strBasic, $nonMatches) );
}
function test__qa_js() {
$test = qa_js('test');
$this->assertEquals($test, "'test'");
$test = qa_js('test', true);
$this->assertEquals($test, "'test'");
$test = qa_js(123);
$this->assertEquals($test, 123);
$test = qa_js(123, true);
$this->assertEquals($test, "'123'");
$test = qa_js(true);
$this->assertEquals($test, 'true');
$test = qa_js(true, true);
$this->assertEquals($test, "'true'");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment