Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Leo Dutra leodutra

🏠
Working from home
View GitHub Profile
@leodutra
leodutra / getGlobalOffset.js
Created May 5, 2012
Gets HTMLElement Global Offset Left/Top (JS)
View getGlobalOffset.js
function getGlobalOffset(el) {
var x = 0, y = 0
while (el) {
x += el.offsetLeft
y += el.offsetTop
el = el.offsetParent
}
return { left: x, top: y }
}
@leodutra
leodutra / leftPad.js
Last active Oct 6, 2015
Very fast left pad, anything (JavaScript)
View leftPad.js
leftPad = function leftPad(value, size, pad) { // very very fast
if (value.length < size) {
size -= value.length;
var res = '';
for(;;) {
if (size & 1) res += pad;
size >>= 1;
if (size) pad += pad;
else break;
}
@leodutra
leodutra / latin1-letter-regex.js
Created Jul 4, 2012
Basic Latin-1 Letter Regular Expression (JavaScript)
View latin1-letter-regex.js
(/[A-z\u00C0-\u00ff]+/g)
@leodutra
leodutra / parseNumber.js
Last active Jun 8, 2019
A parseNumber accepting: currency / decimal / float / whatever number ( for JavaScript )
View parseNumber.js
function parseNumber(str)
{
str = (str + '').replace(/[^\d,.-]/g, '') // just digits, separators and sign
var sign = str.charAt(0) === '-' ? '-' : '+' // store sign
var minor = str.match(/[.,](\d+)$/) // filter decimals
str = str.replace(/[.,]\d*$/, '').replace(/\D/g, '') // remove decimals and any integer separator
return Number(sign + str + (minor ? '.' + minor[1] : '')) // build number
}
@leodutra
leodutra / request-animation-frame-polyfill.js
Last active Dec 3, 2015 — forked from paulirish/rAF.js
requestAnimationFrame polyfill
View request-animation-frame-polyfill.js
// http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
// requestAnimationFrame polyfill by Erik Möller
// fixes from Paul Irish and Tino Zijdel
(function() {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
@leodutra
leodutra / getAbsolutePath.js
Created Sep 19, 2012
Get Absolute Path (JavaScript)
View getAbsolutePath.js
function getAbsolutePath(url) {
return url.match(/^\w+:\/\/[^\/]+\/*(?:[^\/\.]+(?:\r|\/+))*/)[0];
}
@leodutra
leodutra / removeComments.js
Last active Mar 1, 2021
Remove HTML/JavaScript Comments
View removeComments.js
function removeComments(html) {
return (''+html)
.replace/*HTMLComments*/(/<!-[\S\s]*?-->/gm, '')
.replace/*JSBlockComments*/(/\/\*[\S\s]*?\*\//gm,'')
.replace/*JSLineComments*/(/^.*?\/\/.*/gm, '$1') // FIXME
}
@leodutra
leodutra / recaptcha_loader.js
Last active Dec 15, 2015
Loads current ReCaptcha version using an intelligent import and a <div>
View recaptcha_loader.js
/**
* Usage:
* - import jQuery;
* - import this;
* - create an element (maybe a div) with id="js-reCaptchaContainer" and data-publickey="<your public key>"
* ex: <div id="js-reCaptchaContainer" data-publickey="6LfSXd4SAAAAAH0wvCuC164"></div>
*/
if (this.jQuery) {
jQuery(document).ready(function ($) {
@leodutra
leodutra / toUnicode.js
Last active Dec 15, 2015
Converts Array, String or Number to Unicode String
View toUnicode.js
function toUnicode(val/* :Array|Number|String */) {
var res = '';
var type = $.type(val);
var i;
switch(type) {
case 'array':
val = s.join('');
case 'string':
case 'number':
i = (val + '').length;
@leodutra
leodutra / Web References
Last active Dec 15, 2015
JavaScript & Web Related References
View Web References
JAVASCRIPT, CSS, HTML5 AND WEB RELATED REFERENCES
=================================================
FIXES AND HACKS
---------------
HTML5 DOCTYPE <------------ HTML5 DOCTYPE IS TOTALLY CROSS-COMPATIBLE WITH IE 5.5, 6, 7, 8, 9, 10, 11, ... !!!
<!DOCTYPE html>
(yeah! just that!)