tweetnacl-es6
Version:
Port of TweetNaCl cryptographic library to JavaScript es6
52 lines (43 loc) • 1.26 kB
JavaScript
export function NodeLogger() {
this.print = function() {
console.log.apply(console, arguments);
};
this.ok = function() {
process.stdout.write('.');
};
this.error = function() {
console.error.apply(console, arguments);
process.exit(1);
};
this.start = this.print.bind(this, '\n');
}
export function BrowserLogger() {
var el = document.createElement('pre');
document.body.appendChild(el);
function escape(s) {
var reps = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/'
};
return String(s).replace(/[&<>"'\/]/g, function(x) { return reps[x]; });
}
this.print = function() {
console.log.apply(console, arguments);
var s = escape(arguments.length ? Array.prototype.slice.call(arguments).join(' ') : '');
el.innerHTML += s + '\n';
};
this.ok = function() {
// No console output.
el.innerHTML += '.';
};
this.error = function() {
console.error.apply(console, arguments);
var s = escape(arguments.length ? Array.prototype.slice.call(arguments) : '');
el.innerHTML += 'ERROR: ' + s + '\n';
};
this.start = this.print.bind(this, '\n');
}