const badgeFormat = '[%s] ';
const pad = ' ';
const bind = Function.prototype.bind;
export function bindToConsole(
methodName: string,
args: Array<any>,
badgeName: string,
): () => any {
let offset = 0;
switch (methodName) {
case 'dir':
case 'dirxml':
case 'groupEnd':
case 'table': {
return bind.apply(console[methodName], [console].concat(args));
}
case 'assert': {
offset = 1;
}
}
const newArgs = args.slice(0);
if (typeof newArgs[offset] === 'string') {
newArgs.splice(
offset,
1,
badgeFormat + newArgs[offset],
pad + badgeName + pad,
);
} else {
newArgs.splice(offset, 0, badgeFormat, pad + badgeName + pad);
}
newArgs.unshift(console);
return bind.apply(console[methodName], newArgs);
}