JavaScript settype
Set the type of the 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 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 104 105106 107 108 109 110111 112 113 114 115116 117 118 119 120121 122 123 | function settype (vr, type) { // Set the type of the variable // // version: 1109.2015 // discuss at: http://phpjs.org/functions/settype // + original by: Waldo Malqui Silva // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + revised by: Brett Zamir (http://brett-zamir.me) // % note 1: Credits to Crockford also // % note 2: only works on global variables, and "vr" must be passed in as a string // * example 1: foo = '5bar'; // * example 1: settype('foo', 'integer'); // * results 1: foo === 5 // * returns 1: true // * example 2: foo = true; // * example 2: settype('foo', 'string'); // * results 2: foo === '1' // * returns 2: true var is_array = function (arr) { return typeof arr === 'object' && typeof arr.length === 'number' && !(arr.propertyIsEnumerable('length')) && typeof arr.splice === 'function'; }; var v, mtch, i, obj; v = this[vr] ? this[vr] : vr; try { switch (type) { case 'boolean': if (is_array(v) && v.length === 0) { this[vr] = false; } else if (v === '0') { this[vr] = false; } else if (typeof v === 'object' && !is_array(v)) { var lgth = false; for (i in v) { lgth = true; } this[vr] = lgth; } else { this[vr] = !! v; } break; case 'integer': if (typeof v === 'number') { this[vr] = parseInt(v, 10); } else if (typeof v === 'string') { mtch = v.match(/^([+\-]?)(\d+)/); if (!mtch) { this[vr] = 0; } else { this[vr] = parseInt(v, 10); } } else if (v === true) { this[vr] = 1; } else if (v === false || v === null) { this[vr] = 0; } else if (is_array(v) && v.length === 0) { this[vr] = 0; } else if (typeof v === 'object') { this[vr] = 1; } break; case 'float': if (typeof v === 'string') { mtch = v.match(/^([+\-]?)(\d+(\.\d+)?|\.\d+)([eE][+\-]?\d+)?/); if (!mtch) { this[vr] = 0; } else { this[vr] = parseFloat(v, 10); } } else if (v === true) { this[vr] = 1; } else if (v === false || v === null) { this[vr] = 0; } else if (is_array(v) && v.length === 0) { this[vr] = 0; } else if (typeof v === 'object') { this[vr] = 1; } break; case 'string': if (v === null || v === false) { this[vr] = ''; } else if (is_array(v)) { this[vr] = 'Array'; } else if (typeof v === 'object') { this[vr] = 'Object'; } else if (v === true) { this[vr] = '1'; } else { this[vr] += ''; } // numbers (and functions?) break; case 'array': if (v === null) { this[vr] = []; } else if (typeof v !== 'object') { this[vr] = [v]; } break; case 'object': if (v === null) { this[vr] = {}; } else if (is_array(v)) { for (i = 0, obj = {}; i < v.length; i++) { obj[i] = v; } this[vr] = obj; } else if (typeof v !== 'object') { this[vr] = { scalar: v }; } break; case 'null': delete this[vr]; break; } return true; } catch (e) { return false; } } |
Examples
» Example 1
Running
1 2 | foo = '5bar'; settype('foo', 'integer'); |
Should result in
1 | foo === 5 |
» Example 2
Running
1 2 | foo = true; settype('foo', 'string'); |
Should result in
1 | foo === '1' |
Dependencies
No dependencies, you can use this function standalone.
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 settype goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>