locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
52 lines (48 loc) • 1.73 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
module.exports = function _php_cast_float(value) {
// eslint-disable-line camelcase
// original by: Rafał Kukawski
// example 1: _php_cast_float(false)
// returns 1: 0
// example 2: _php_cast_float(true)
// returns 2: 1
// example 3: _php_cast_float(0)
// returns 3: 0
// example 4: _php_cast_float(1)
// returns 4: 1
// example 5: _php_cast_float(3.14)
// returns 5: 3.14
// example 6: _php_cast_float('')
// returns 6: 0
// example 7: _php_cast_float('0')
// returns 7: 0
// example 8: _php_cast_float('abc')
// returns 8: 0
// example 9: _php_cast_float(null)
// returns 9: 0
// example 10: _php_cast_float(undefined)
// returns 10: 0
// example 11: _php_cast_float('123abc')
// returns 11: 123
// example 12: _php_cast_float('123e4')
// returns 12: 1230000
// example 13: _php_cast_float(0x200000001)
// returns 13: 8589934593
// example 14: _php_cast_float('3.14abc')
// returns 14: 3.14
var type = typeof value === 'undefined' ? 'undefined' : _typeof(value);
switch (type) {
case 'number':
return value;
case 'string':
return parseFloat(value) || 0;
case 'boolean':
// fall through
default:
// PHP docs state, that for types other than string
// conversion is {input type}->int->float
return require('./_php_cast_int')(value);
}
};
//# sourceMappingURL=_php_cast_float.js.map