Hello, World!

node v6.17.1
version: 0.8.1
endpointsharetweet
Enhances RunKit's console logging functions to behave like those of Node.js.
console.log('Hello, %s!', 'World');
Using Node.js, above would print: Hello, World!
/** * Enhances the console's logging functions to be like those of Node.js, namely if * multiple arguments are passed, the first is used as the primary message and all * additional used as substitution values similar to printf(3) (the arguments are * all passed to util.format()). * https://nodejs.org/api/console.html#console_console_log_data_args */ enhanceConsoleLogFunctions = function() { ['log', 'info', 'warn', 'error'].forEach(function(fnName) { let origFn = console[fnName]; console[fnName] = function(...args) { if (args.length > 1) { origFn.call(console, require('util').format(...args)); } else { origFn.call(console, ...args); } } }); } enhanceConsoleLogFunctions(); console.log('Hello, %s!', 'World');
Additional tests:
['log', 'info', 'warn', 'error'].forEach(function(fnName, index) { console[fnName](fnName); });
['log', 'info', 'warn', 'error'].forEach(function(fnName, index) { console[fnName]("%d) %s", index, fnName); });
Loading…

no comments

    sign in to comment