Skip to content

Instantly share code, notes, and snippets.

View jrrio's full-sized avatar

João Rodrigues jrrio

View GitHub Profile
@jrrio
jrrio / Debounce.js
Created August 25, 2024 21:22
Debounce is used to limit the number of times a function is called in a short period of time.
/**
* Debounce is used to limit the number of times a function is called in a short period of time.
* @param {Function} func
* @param {Number} wait - a delay in milliseconds, usually 250 ms by default.
* @returns {Function} a new function that will delay the execution of the original function.
*/
const debounce = (func, wait = 250) => {
let timeout;
return function (event) {
// console.log(`debounced ${func.name} called`)
@jrrio
jrrio / loader.css
Created February 8, 2022 20:27
CSS loader animation
/* From https://github.com/prisma/deployment-example-vercel/blob/main/styles/Home.module.css */
.loader,
.loader:after {
border-radius: 50%;
width: 10em;
height: 10em;
}
.loader {
top: 30px;
@jrrio
jrrio / fetch.js
Last active December 25, 2021 02:45
Fetch wrapper
async function getData(url) {
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`${response.status}: ${response.statusText}`);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
}
@jrrio
jrrio / dateutils.js
Last active January 17, 2021 13:38
Date Utilities in vanilla JavaScript
/**
*
* @param date - may be either a Date object or a String representing a Date
*/
function lastDayOfMonth(date) {
var d = new Date(date);
d.setFullYear(d.getFullYear(), d.getMonth() + 1, 0);
return d.getDate();
}
@jrrio
jrrio / formatMask.js
Created July 15, 2019 17:23
Formatting mask for Brazilian CPF, CNPJ and CEP
/**
* @param {String} str - a string to be formatted.
* @param {String} msk - 'CNPJ', 'CPF' or 'CEP' used in Brazil.
*/
function formatMask(str, msk) {
var a = str.replace(/[^\d]+/g, ''); // remove non digit chars.
switch (msk) {
case "CNPJ":
a = a.replace(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/, '$1.$2.$3/$4-$5');
break;
@jrrio
jrrio / utils.js
Last active April 5, 2019 21:16
JavaScript Utilility functions
// Test for numeric value.
function isNumeric(n) {
return !isNaN(parseFloat(n)) && isFinite(n);
}
/**
* @param {Event} e
* @see <https://www.w3.org/TR/uievents/#interface-keyboardevent>
* All browsers (IE11, Chrome, FF, etc.) support event.key.
* event.keyCode, charCode and event.which are deprecated.
@jrrio
jrrio / DetectingIE11.md
Last active June 7, 2018 06:46
Detecting IE11

Extracted from comp.lang.javascript: Re: Detecting IE11 ?
Data: Wed, 23 May 2018 23:52:33 -0300
De: Joao Rodrigues jr@yahoo.com
Reference: 3120d5a3-6a6a-4a1d-88f5-da7510b252da@googlegroups.com

Is there a good way of detecting IE11 - or, better, any IE ?

Starting with Internet Explorer (IE) 4 until IE 10 in standards mode, we could use Conditional Compilation (CC) for detecting MSIE [1]. Activating CC in JScript was done with the @cc_on statement:

@jrrio
jrrio / assignstyles.js
Created May 7, 2018 19:53
Assigning styles to an HTML element
var el = document.getElementById("elemID");
el.style.cssText = "color:#fff;width:320px;height:240px;";
// Or:
Object.assign(el.style,
{
"color": "#fff",
"width": "320px",
"height": "240px"
@jrrio
jrrio / Object.assign.js
Created April 24, 2018 02:14
Object.assign() polyfill for IE11
/**
* Object.assign() polyfill for IE11
* @see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign>
*/
if (typeof Object.assign != "function") {
Object.defineProperty(Object, "assign", {
value: function assign(target, varArgs) {
"use strict";
if (target == null) {
throw new TypeError("Cannot convert undefined or null to object");
@jrrio
jrrio / getElementText.md
Last active April 23, 2018 23:17
Difference between innerText and textContent

Difference between innerText and textContent

The differences between innerText and textContent methods are outlined in this Stack Overflow article.

  • innerText -> implemented in IE8 and earlier. So innerText is probably not what you want.
  • textContent -> DOM Level 3 - IE9 and W3C standards compliant browsers.
  • Google Chrome implements both innerText and textContent.

Let's define a getElementText() method at load-time in order to use either textContent (preferably) or innerText depending on the browser running this code. This function will also trim the text.