UNPKG

@pxpcandy/js-wrapper

Version:

This is a wrapper of JavaScript, hopefully making everything easier to use

110 lines (100 loc) 3.66 kB
const path = require('path'); const fs = require('fs'); const err_c = 1; var isModuleAvailable = function(moduleName, callback) { var counter = 0; var dirSeparator = require("path").sep; module.paths.forEach(function(nodeModulesPath) { var path = nodeModulesPath + dirSeparator + moduleName; fs.exists(path, function(exists){if(exists){callback(true);}else{counter++;if(counter === module.paths.length){callback(false);}}}); }); }; // check if module "http" exists isModuleAvailable("http", function(exists) { if(exists){}else{console.log("[ERROR] Module HTTP was not found, exiting on code 1"); process.exit(err_c);} }); const http = require('http'); function log(msg) {console.log(msg);} function wait(ms, callback) {setTimeout(()=>{return callback();},ms);} function loop(ms, callback) {setInterval(()=>{return callback();},ms);} function inf() {return Infinity;} function ninf() {return -Infinity;} var array = { clear : function(array) { array.length = 0; }, length : function(array) { return array.length; }, add : function(array, index, item) { array[index] = item; }, remove : function(array, index) { array.splice(index); } } var math = { add : function() { var num_add = 0; for (var i=0; i < arguments.length; i++) { num_add += arguments[i]; } if(arguments.length == 0 || arguments.length == 1) return console.log("[ERROR] Not enough arguments provided"); return num_add; }, multiply : function() { var num_multi = 0; for (var i=0; i < arguments.length; i++) { num_multi *= arguments[i]; } if(arguments.length == 0 || arguments.length == 1) return console.log("[ERROR] Not enough arguments provided"); return num_multi; }, divide : function() { var num_div = 0; for (var i=0; i < arguments.length; i++) { num_div /= arguments[i]; } if(arguments.length == 0 || arguments.length == 1) return console.log("[ERROR] Not enough arguments provided"); return num_div; }, subtract : function() { var num_sub = 0; for (var i=0; i < arguments.length; i++) { num_sub -= arguments[i]; } if(arguments.length == 0 || arguments.length == 1) return console.log("[ERROR] Not enough arguments provided"); return num_sub; } } var utils = { webserver : function(index) { const server = http.createServer((req, res) => { if(!index) { console.log("[ERROR] No pages were provided to build webserver, exiting on code 1"); process.exit(err_c); return; } if(req.url == '/') { fs.readFile(path.join(index), (err, content) => { if(err){console.log("[ERROR] Error was found in " + items[0] + "\n[ERROR] Error given was: " + err);} res.writeHead(200, { 'Content-Type': 'text/html' }); res.end(content); }); } }); const PORT = process.env.PORT || 5000; server.listen(PORT, () => console.log(`Server running on port ${PORT}, started with @pxpcandy/js-wrapper`)); } } module.exports.log = log; module.exports.wait = wait; module.exports.loop = loop; module.exports.inf = inf; module.exports.ninf = ninf; module.exports.array = array; module.exports.math = math; module.exports.utils = utils;