UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

58 lines (53 loc) 1.9 kB
'use strict'; 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_int(value) { // eslint-disable-line camelcase // original by: Rafał Kukawski // example 1: _php_cast_int(false) // returns 1: 0 // example 2: _php_cast_int(true) // returns 2: 1 // example 3: _php_cast_int(0) // returns 3: 0 // example 4: _php_cast_int(1) // returns 4: 1 // example 5: _php_cast_int(3.14) // returns 5: 3 // example 6: _php_cast_int('') // returns 6: 0 // example 7: _php_cast_int('0') // returns 7: 0 // example 8: _php_cast_int('abc') // returns 8: 0 // example 9: _php_cast_int(null) // returns 9: 0 // example 10: _php_cast_int(undefined) // returns 10: 0 // example 11: _php_cast_int('123abc') // returns 11: 123 // example 12: _php_cast_int('123e4') // returns 12: 123 // example 13: _php_cast_int(0x200000001) // returns 13: 8589934593 var type = typeof value === 'undefined' ? 'undefined' : _typeof(value); switch (type) { case 'number': if (isNaN(value) || !isFinite(value)) { // from PHP 7, NaN and Infinity are casted to 0 return 0; } return value < 0 ? Math.ceil(value) : Math.floor(value); case 'string': return parseInt(value, 10) || 0; case 'boolean': // fall through default: // Behaviour for types other than float, string, boolean // is undefined and can change any time. // To not invent complex logic // that mimics PHP 7.0 behaviour // casting value->bool->number is used return +!!value; } }; //# sourceMappingURL=_php_cast_int.js.map