Skip to content

Instantly share code, notes, and snippets.

View erovas's full-sized avatar

Emanuel R. Vásquez erovas

View GitHub Profile
@erovas
erovas / ThrowError.js
Created May 24, 2026 20:34
Simple stack trace error.
/**
*
* @param {String} title
* @param {String} description
* @returns
*/
function ThrowError(title, description) {
let error;
try { throw new Error(); } catch (e) { error = e; }
const stack = error.stack.split('\n').slice(2).join('\n');
@erovas
erovas / HTMLTableToXLS.js
Created May 24, 2026 20:10
Converts an HTML table into a downloadable .xls file.
/**
* Converts an HTML table into a downloadable .xls file.
* Generates an HTML file with Microsoft Excel metadata and encodes it in Base64.
*
* @param {string|HTMLTableElement} tableSelector - CSS selector (ID/class) or the DOM element of the table.
* @param {string} [filename='Table'] - The desired filename for the download (without extension).
* @returns {void}
*/
function HTMLTable2XLS(tableSelector, filename = 'Table') {
// 1. Resolve the table element
@erovas
erovas / Base64Weight.js
Created May 24, 2026 19:56
Calculates the estimated weight in bytes of a Base64 string.
/**
* Calculates the estimated weight in bytes of a Base64 string.
* @param {String} base64 - Base64 string (may include data: prefix)
* @returns {Object} - { Bytes, KBytes, MBytes }
*/
function Base64Weight(base64) {
if (!base64) return { Bytes: 0, KBytes: 0, MBytes: 0 };
// 1. Remove data: prefix if present
if (base64.indexOf("data:") >= 0) {
@erovas
erovas / ObjectIsPlain.js
Created May 24, 2026 19:37
Check if a value is a generic object (created literally with {} or new Object()) and discard instances of primitives, native objects (Math, Date), arrays, and DOM objects.
Object.IsPlain = function(obj){
// 1. It must be an object and not null
if (typeof obj !== 'object' || obj === null) return false;
// 2. It must be strictly a generic object, not Arrays, Dates, etc.
if (Object.prototype.toString.call(obj) !== '[object Object]') return false;
// 3. Verify the prototype
const proto = Object.getPrototypeOf(obj);
@erovas
erovas / Map.js
Created May 24, 2026 19:11
The Map function recalculates an input value to project it from an input range (inMin, inMax) to an output range (outMin, outMax), first ensuring that the input is within its original range.
/**
*
* @param {Number} input
* @param {Number} inMin
* @param {Number} inMax
* @param {Number} outMin
* @param {Number} outMax
* @returns
*/
function Map(input, inMin, inMax, outMin, outMax){
@erovas
erovas / Clamp.js
Created May 24, 2026 19:08
The Clamp function returns the input number restricted to be within the defined range between min and max.
/**
*
* @param {Number} input
* @param {Number} min
* @param {Number} max
* @returns
*/
function Clamp(input, min, max){
return input > max? max : (input < min? min : input)
}
@erovas
erovas / IsNullOrEmptyOrWhiteSpace.js
Created May 24, 2026 19:01
Is null or empty or white space String
/**
*
* @param {String} value
* @returns
*/
String.IsNullOrEmpty = function(value) {
return value === null || value === undefined || value === "";
};
/**
@erovas
erovas / ZoomEvent.js
Created May 24, 2026 18:25
Zoom event
document.addEventListener('wheel', e =>{
if (e.ctrlKey || e.metaKey){
const zoomEvent = new CustomEvent('zoom', {
detail: { deltaY: e.deltaY, ctrlKey: true },
bubbles: true,
cancelable: true
});
// Copiar todas las propiedades del evento wheel
Object.assign(zoomEvent, {
@erovas
erovas / TrueLoadImageSync.js
Created May 24, 2026 17:57
How to Load an Image Synchronously with JavaScript
const exts = ['jpg', 'jpeg', 'jfif', 'pjpeg', 'pjp', 'png', 'gif', 'webp', 'avif', 'svg', 'apng', 'bmp', 'ico'];
/**
* Get extension from string (try "name.xls.doc.js" result "js")
* @param {String} fileName
* @returns
*/
function getExtension(fileName){
let regex = /(?:\.([^.]+))?$/;
return regex.exec(fileName+"")[1];