Skip to content

Instantly share code, notes, and snippets.

@nachoaguirre
Last active February 1, 2021 21:35
Show Gist options
  • Select an option

  • Save nachoaguirre/e188bf0f3420da106d861091366f8797 to your computer and use it in GitHub Desktop.

Select an option

Save nachoaguirre/e188bf0f3420da106d861091366f8797 to your computer and use it in GitHub Desktop.

Revisions

  1. nachoaguirre revised this gist Feb 1, 2021. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion strings_helper.js
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,14 @@ function (string, limit=20, 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); }
  2. nachoaguirre revised this gist Feb 1, 2021. 4 changed files with 84 additions and 37 deletions.
    21 changes: 21 additions & 0 deletions bootstrap_helpers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    /*
    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));
    39 changes: 2 additions & 37 deletions common_helpers.js
    Original file line number Diff line number Diff line change
    @@ -1,38 +1,4 @@
    /*
    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);
    };

    /****************************************/

    /*
    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));

    /****************************************/
    /*
    /*
    Chain DOM
    */
    // source: https://www.orantec.uk/blog/2015/03/31/an-introduction-to-javascript-chaining
    @@ -48,5 +14,4 @@ var cdom = {
    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); });

    overlay.append("INPUT").attribute("type", "button").attribute("value", "Close").event("click", function () { document.body.removeChild(overlay.element); });
    33 changes: 33 additions & 0 deletions objetos_helper.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    // 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;
    }
    28 changes: 28 additions & 0 deletions strings_helper.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    /* 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 ""; }
    }
  3. nachoaguirre created this gist Feb 1, 2021.
    52 changes: 52 additions & 0 deletions common_helpers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    /*
    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);
    };

    /****************************************/

    /*
    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));

    /****************************************/
    /*
    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); });