UNPKG

locutus

Version:

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

35 lines (34 loc) 1.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.strval = strval; const gettype_ts_1 = require("../var/gettype.js"); function strval(str) { // discuss at: https://locutus.io/php/strval/ // original by: Brett Zamir (https://brett-zamir.me) // improved by: Kevin van Zonneveld (https://kvz.io) // bugfixed by: Brett Zamir (https://brett-zamir.me) // example 1: strval({red: 1, green: 2, blue: 3, white: 4}) // returns 1: 'Object' let type = ''; if (typeof str === 'undefined') { throw new Error('strval() expects exactly 1 argument, 0 given'); } if (str === null) { return ''; } type = (0, gettype_ts_1.gettype)(str); // Comment out the entire switch if you want JS-like // behavior instead of PHP behavior switch (type) { case 'boolean': if (str === true) { return '1'; } return ''; case 'array': return 'Array'; case 'object': return 'Object'; } return String(str); }