Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var specialConsole = (function () {
- function placeHolder(place, text) {
- this.place = place;
- this.text = text;
- }
- function formatText() {
- var placeHolders = [];
- var text = arguments[0].toString();
- if (arguments.length == 1) {
- text = arguments[0];
- } else if (arguments.length > 1) {
- for (var i = 1; i < arguments.length; i++) {
- placeHolders.push(new placeHolder('\\{' + (i - 1) + '\\}', arguments[i]));
- }
- for (var i = 0; i < placeHolders.length; i++) {
- var re = new RegExp(placeHolders[i].place, "g");
- text = text.replace(re, placeHolders[i].text);
- }
- }
- return text;
- }
- function writeLine() {
- var text = formatText.apply(null, arguments);
- console.log(text);
- }
- function writeError() {
- var text = formatText.apply(null, arguments);
- console.error(text);
- }
- function writeWarning() {
- var text = formatText.apply(null, arguments);
- console.warn(text);
- }
- return {
- writeLine: writeLine,
- writeError: writeError,
- writeWarning: writeWarning
- }
- })();
- specialConsole.writeLine("Message: hello");
- //logs to the console "Message: hello"
- specialConsole.writeLine("Message: {0}", "hello");
- //logs to the console "Message: hello"
- specialConsole.writeError("Error: {0}", "Something happened");
- specialConsole.writeWarning("Warning: {0}", "A warning");
- specialConsole.writeLine("My name is {0} {1}", "Petyr", "Petrov");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement