Skip to content

Instantly share code, notes, and snippets.

@adnanalbeda
Last active April 19, 2022 10:49
Show Gist options
  • Save adnanalbeda/d7cf03a51dfb2870f7ec0769d3b7cccd to your computer and use it in GitHub Desktop.
Save adnanalbeda/d7cf03a51dfb2870f7ec0769d3b7cccd to your computer and use it in GitHub Desktop.
A group of extension methods to debug and log data.
/* eslint-disable no-extend-native */
/*
* Too lazy to debug?
* This file provides basic (alert and console) commands as extension methods for development purposes.
* It can be anywhere inside the project and it should work just fine.
* However, for better readability, it's better to be located inside /src/extensions directory.
*/
// Export as Module
export { }
// To have access to primitive data types
declare global {
// Data type to extend
interface Object {
// Extensions
Dev_Alert<T>(this: T): T;
Dev_Log<T>(this: T): T;
Dev_Info<T>(this: T, message: string): T;
Dev_Warn<T>(this: T, message: string): T;
Dev_Error<T>(this: T, message: string): T;
}
}
// Defining extensions behavior
Object.prototype.Dev_Alert = function () {
alert(this);
return this;
}
Object.prototype.Dev_Log = function () {
console.log(this);
return this;
}
Object.prototype.Dev_Info = function (message: string = "Info: ") {
console.info(message, this);
return this;
}
Object.prototype.Dev_Warn = function (message: string = "Warning: ") {
console.warn(message, this);
return this;
}
Object.prototype.Dev_Error = function (message: string = "Error: ") {
console.error(message, this);
return this;
}
/*
* I just learnt about how to extend prototypes in TS.
* So I made this which might be helpful in tracking data through console or alert.
* Also, to refere this whenever I forget how to create extensions.
* These extensions will be used in development stage only, (or so I hope).
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment