Last active
February 1, 2021 21:35
-
-
Save nachoaguirre/e188bf0f3420da106d861091366f8797 to your computer and use it in GitHub Desktop.
common helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Añadir class según breakpoint al body | |
*/ | |
var breakpoint = (function(body) { | |
let bp_cur = ""; | |
var bp_dom = { | |
get: function (o) { var obj = Object.create(this); obj.element = (typeof o == "object") ? o : document.createElement(o); return obj; }, | |
app: function (o) { var obj = bp_dom.get(o); this.element.prepend(obj.element); return obj; }, | |
att: function (k, v) { this.element.setAttribute(k, v); return this; }, | |
cls: function (c) { this.element.className = c; return this; }, | |
}; | |
var bp_szs = ['xs', 'sm', 'md', 'lg', 'xl']; | |
var bp_obj = {"xs": "d-block d-sm-inline", "sm": "d-sm-block d-md-inline", "md": "d-md-block d-lg-inline", "lg": "d-lg-block d-xl-inline", "xl": "d-xl-block" }; | |
var bp_ele = bp_dom.get(body).app('div').att('style','display:none;').att('id','bp_check'); | |
for (const [key, val] of Object.entries(bp_obj)) { bp_ele.app('span').cls(key+' '+val); } | |
function isBlock(size){ return (getComputedStyle(document.querySelector('#bp_check .'+size)).display == 'block'); } | |
function bp_checkCurrent(){ body.classList.remove(...bp_szs); for (const [key, val] of Object.entries(bp_obj)) { if( isBlock(key) ) bp_cur = key; breakpoint = key; } body.classList.add(bp_cur); } | |
bp_checkCurrent(); | |
window.onresize = bp_checkCurrent; | |
return bp_cur; | |
}(document.body)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Chain DOM | |
*/ | |
// source: https://www.orantec.uk/blog/2015/03/31/an-introduction-to-javascript-chaining | |
var cdom = { | |
element: null, | |
get: function (o) { var obj = Object.create(this); obj.element = (typeof o == "object") ? o : document.createElement(o); return obj; }, | |
append: function (o) { var obj = cdom.get(o); this.element.appendChild(obj.element); return obj; }, | |
text: function (t) { this.element.appendChild(document.createTextNode(t)); return this; }, | |
attribute: function (k, v) { this.element.setAttribute(k, v); return this; }, | |
css: function (c) { this.element.classList.add(c); return this; }, | |
event: function (e, f) { this.element.addEventListener(e, f, false); return this; } | |
} | |
var overlay = cdom.get(document.body).append("DIV").css("overlay"); | |
overlay.append("H2").text("Hello World!"); | |
overlay.append("P").text("Click the button below to close"); | |
overlay.append("INPUT").attribute("type", "button").attribute("value", "Close").event("click", function () { document.body.removeChild(overlay.element); }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source: https://gist.github.com/YagoLopez/1c2fe87d255fc64d5f1bf6a920b67484 | |
// Buscar por key y val | |
function getObjects(obj, key, val) { | |
var objects = []; | |
for (var i in obj) { | |
if(!obj.hasOwnProperty(i)) continue; | |
if(typeof obj[i] == 'object') { objects = objects.concat(getObjects(obj[i], key, val)); } | |
else if(i == key && obj[i] == val || i == key && val == '') { objects.push(obj); } | |
else if(obj[i] == val && key == ''){ if(objects.lastIndexOf(obj) == -1){ objects.push(obj); } } | |
} | |
return objects; | |
} | |
// Buscar por key | |
function getValues(obj, key) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { objects = objects.concat(getValues(obj[i], key)); } else if (i == key) { objects.push(obj[i]); } | |
} | |
return objects; | |
} | |
// Buscar por val | |
function getKeys(obj, val) { | |
var objects = []; | |
for (var i in obj) { | |
if (!obj.hasOwnProperty(i)) continue; | |
if (typeof obj[i] == 'object') { objects = objects.concat(getKeys(obj[i], val)); } else if (obj[i] == val) { objects.push(i); } | |
} | |
return objects; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* String a slug */ | |
function toSlug(str) { | |
str = str.replace(/^\s+|\s+$/g, ''); str = str.toLowerCase(); | |
var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;"; var to = "aaaaaeeeeeiiiiooooouuuunc------"; | |
for (var i=0, l=from.length ; i<l ; i++) { str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); } | |
str = str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-'); return str; | |
} | |
/* fixPrecio(5000) > $5.000 */ | |
function fixPrecio(number, showsign=true) { | |
showsign = !showsign ? "" : "$"; | |
var i = String(parseInt(number = Math.abs(Number(number) || 0))); | |
var j = (j = i.length) > 3 ? j % 3 : 0; return showsign + (j ? i.substr(0, j) + "." : "") + i.substr(j); | |
}; | |
/* Primera letra en mayuscula */ | |
function capitalize(str) { | |
var lower = str.toLowerCase(); return str.charAt(0).toUpperCase() + lower.slice(1); | |
} | |
/* Limita caracteres de un string */ | |
function (string, limit=20, end="...") { | |
if(string){ | |
var end; | |
if(string.length > limit) { string = string.substring(0,limit)+end; } | |
return string; | |
} else { return ""; } | |
} | |
/* Valida email */ | |
function isEmail(email) { var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; return regex.test(email); } | |
/* Agregar ceros a un número si es menor a 10 */ | |
function addZero(i) { if (i < 10) { i = "0" + i; } return i; } | |
/* Compartir url por whatsapp */ | |
function shareWA() { window.location='https://wa.me/?text='+encodeURIComponent(window.location.href); } | |
function shareWA2(url){ window.open("https://wa.me/?text="+url, "_blank") || window.location.replace("https://wa.me/?text="+url); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment