Skip to content

Instantly share code, notes, and snippets.

View leodutra's full-sized avatar
🦙
Llama and other LLMs

Leo Dutra leodutra

🦙
Llama and other LLMs
View GitHub Profile
@leodutra
leodutra / getGlobalOffset.js
Created May 5, 2012 03:30
Gets HTMLElement Global Offset Left/Top (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 October 6, 2015 19:28
Very fast left pad, anything (JavaScript)
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 July 4, 2012 00:20
Basic Latin-1 Letter Regular Expression (JavaScript)
(/[A-z\u00C0-\u00ff]+/g)
@leodutra
leodutra / parseNumber.js
Last active June 8, 2019 10:37
A parseNumber accepting: currency / decimal / float / whatever number ( for JavaScript )
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 December 3, 2015 14:52 — forked from paulirish/rAF.js
requestAnimationFrame polyfill
// 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 September 19, 2012 16:18
Get Absolute Path (JavaScript)
function getAbsolutePath(url) {
return url.match(/^\w+:\/\/[^\/]+\/*(?:[^\/\.]+(?:\r|\/+))*/)[0];
}
@leodutra
leodutra / removeComments.js
Last active March 1, 2021 04:08
Remove HTML/JavaScript Comments
function removeComments(html) {
return (''+html)
.replace/*HTMLComments*/(/<!-[\S\s]*?-->/gm, '')
.replace/*JSBlockComments*/(/\/\*[\S\s]*?\*\//gm,'')
.replace/*JSLineComments*/(/^.*?\/\/.*/gm, '$1') // FIXME
}
@leodutra
leodutra / toUnicode.js
Last active December 15, 2015 03:39
Converts Array, String or Number to Unicode String
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 December 15, 2015 04:59
JavaScript & Web Related 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!)
@leodutra
leodutra / htmlSpecialCharsEntityEncode
Last active December 15, 2015 06:19
Encodes (escapes) HTML Special Chars
function htmlSpecialCharsEntityEncode(str) {
return str.replace(/[<>&\r\n"']/gm, function (match) {
return '&' + {
'<': 'lt;',
'>': 'gt;',
'&': 'amp;',
'\r': "#13;",
'\n': "#10;",
'"': 'quot;',
"'": 'apos;' /*single quotes just to be safe*/