Skip to content

Instantly share code, notes, and snippets.

View Jberivera's full-sized avatar

Juan Bernardo Rivera Arias Jberivera

View GitHub Profile
/**
* getLastWord - Splits the given string in
* its last word and pass the result into the callback
*
* @param {String} str
* @param {Function} callback - Takes as arguments the first part of the text (without the last word) and the last word.
*/
export function getLastWord(str, callback) {
if (typeof str !== 'string') str = '';
function capitalize (str) {
return str.replace(/(\w)\w+/g, (match, g1) => `${g1.toUpperCase()}${match.substring(1)}`)
}
@Jberivera
Jberivera / equal.js
Last active September 25, 2018 19:05
function isEqual(obj1, obj2, customizer = {}) {
const keys1 = Object.keys(obj1);
const keys2 = Object.keys(obj2);
if (keys1.length !== keys2.length) return false;
return keys1.every(key => {
const item1 = obj1[key];
const item2 = obj2[key];
/**
* renderTemplate - Renders the context on the template
*
* @param {String} template - A string based template
* @param {Object} context - Given context to replace in template
*
* @return {String} Template rendered
*/
function renderTemplate (template, context) {
return template.replace(/\{(\w+)\}/g, function (match, group) {
function serializeObject(obj) {
return Object.keys(obj)
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`)
.join('&');
}
regex = {
'email': /[a-zA-Z0-9]\S+[a-zA-Z0-9]@[a-zA-Z]+\.[a-zA-Z]+(.[a-zA-Z]+)?/,
'tel': /(:?\+\d{1,2}[\-\s])?((\(\d{3}\))|(\d{3}))[\.\s\-]?\d{3}[\.\s\-]?\d{4}(:?\sx\d{1,4})?(:?x\d{1,4})?/
};
var lockScroll = (function () {
var lastScrollPosition,
LOCK_SCROLL = 'lock-scroll';
return function (element, state) {
if (state) {
if (!lastScrollPosition) {
lastScrollPosition = window.pageYOffset || document.documentElement.scrollTop;
document.body.classList.add(LOCK_SCROLL);
document.body.style.transform = 'translateY(-' + lastScrollPosition + 'px)';
function getDataPromise () {
var random = Math.random() * 5000 | 0;
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(random);
}, random);
});
}
function runner (generator) {
/**
* createTakeLastOneContext - Returns a function that uses its context
* to know the last time it was called
* @returns {Function} takeLastOne function.
*/
function createTakeLastOneContext () {
var _lastOne = 0,
_timeStamp = 0;
return function takeLastOne (callback) {
function getQueryParams (url) {
if (!url) {
url = window.location.href
}
const match = url.match(/\?(((.+)(?=\#))|(.+))/);
if (!match) {
return null;
}