UNPKG

locutus

Version:

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

23 lines (22 loc) 760 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._php_cast_int = _php_cast_int; function _php_cast_int(value) { // original by: Rafał Kukawski if (typeof value === '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); } if (typeof value === 'string') { return parseInt(value, 10) || 0; } // 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; }