Last active
March 17, 2017 02:02
-
-
Save skipme/9a63aae7c9e24d4af773be2089a77dcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// exports.some = "eat" | |
var fs = require('fs'); | |
var diagError = function() | |
{ | |
var line = "", err = new Error(); | |
for (var i = 0; i < arguments.length; i++) { | |
line += (i==0?"":"; ") + | |
(typeof arguments[i] === "function" ? arguments[i].toString() | |
: JSON.stringify(arguments[i])); | |
}; | |
// TODO: debug level... | |
line += "; " + err.stack; | |
fs.writeSync(2, line + "\n");// One is stdout, two is stderr, three is stdin | |
line += "; at " + (new Date()).toUTCString(); | |
// c.errorList.push(line); | |
return line; | |
} | |
var defer = function(callback, args) | |
{ | |
//callback.apply(undefined, args); | |
var err = new Error(); | |
setTimeout(function(){ | |
try{ | |
callback.apply(undefined, args); | |
}catch(exc) | |
{ | |
diagError(exc.message, err.stack); | |
} | |
}, 0); | |
} | |
function array_to_string(array) | |
{ | |
var outstr = ""; | |
for (var i = 0; i < array.length; i++) | |
{ | |
switch(typeof array[i]) | |
{ | |
case "boolean": outstr += array[i]?"true_":"false_";break; | |
case "string": outstr += array[i];break; | |
case "number": outstr += array[i].toString();break; | |
case "object": outstr += JSON.stringify(array[i]).replace(/},/g, " },\n").replace(/{/g, "\t{ ");break; | |
default: outstr += "typeof("+typeof array[i]+")";break; | |
} | |
if( i +1 < array.length) | |
outstr+=" | "; | |
} | |
return outstr; | |
} | |
exports.console_log_sync = function() // SYNC console.log replacement | |
{ | |
fs.writeSync(1, array_to_string(arguments)); // One is stdout, two is stderr, three is stdin | |
} | |
exports.console_log_sync_line = function() // SYNC console.log replacement aka writeline | |
{ | |
fs.writeSync(1, array_to_string(arguments) + "\n"); // One is stdout, two is stderr, three is stdin | |
} | |
exports.isEmptyOrWhiteSpace = function(str){ | |
return str === null || str.match(/^ *$/) !== null; | |
} | |
exports.checkFunction = function(fn) | |
{ | |
return fn === null || fn === undefined || typeof fn !== "function"; | |
} | |
exports.shuffle = function(o) | |
{ | |
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x); | |
return o; | |
} | |
exports.getRandomInt = function(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
} | |
exports.writeFile = function(path, dataString) | |
{ | |
var options = { encoding: 'utf16le' }; | |
var wstream = fs.createWriteStream(path, options); | |
wstream.write(dataString, 'utf16le'); | |
wstream.end(); | |
} | |
exports.diagError = diagError; | |
exports.defer = defer; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment