UNPKG

pybuiltinfunc

Version:

Python built-in Functions and Modules in Javascript

58 lines 1.53 kB
module.exports.abs = Math.abs module.exports.all = (some) => some.every(x=>x); module.exports.str = (some = 0) => some.toString(); module.exports.int = parseInt; module.exports.float = function (some = "" | 0) { if (typeof some === "number") { return parseFloat(some).toFixed(2); } if (some === "nan") { return NaN; } if (some === "inf" || some === "inf") { return Infinity; } return parseFloat(some); }; module.exports.len = (some = "" | 0 | []) => some.length; module.exports.chr = String.fromCharCode module.exports.ord = (some = 0) => some.charCodeAt(); module.exports.bin = function (number = 0) { let num = number; let binary = (num % 2).toString(); for (; num > 1; ) { num = parseInt(num / 2); binary = (num % 2) + binary; } const bla = "0b" + binary.toString(); return bla }; module.exports.min = Math.min module.exports.map = (func = (value, index) => {}, list = []) => list.map(func); module.exports.sum = (list = [0, 0, 0, 0]) => { let sum = 0; for (let i = 0; i < list.length; i++) { sum += list[i]; } return sum; }; module.exports.range = (start = 0, stop, step = 1) => { let list = []; let i = 0; if (!stop) { while (i < start) { list.push(i); i = i + step; } return list; } else { i = start; while (i < stop) { list.push(i); i = i + step; } return list; } }; module.exports.type = (some) => typeof some; module.exports.print = console.log