Skip to content

Instantly share code, notes, and snippets.

View albertoperezf's full-sized avatar
💭
if (hungry) eat() ;

Alberto Jose Perez Faria albertoperezf

💭
if (hungry) eat() ;
View GitHub Profile
@albertoperezf
albertoperezf / js
Created December 9, 2017 09:21
Delete a cache file when a new one is ON with active
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys().then(function (cacheNames) {
return Promise.all(
cacheNames.filter(function (cacheNames) {
return cacheNames.startsWith('wittr-') &&
cacheNames !== staticCacheName;
}).map(function (cacheNames) {
return caches.delete(cacheNames);
})
@albertoperezf
albertoperezf / js
Created December 6, 2017 11:09
Service Worker template
self.addEventListener('fetch', function(event) {
// TODO: respond to all requests with an html response
// containing an element with class="a-winner-is-me".
// Ensure the Content-Type of the response is "text/html"
console.log(event.request);
event.respondWith(
new Response('<strong class="a-winner-is-me">Hello Alberto</strong>', {
headers: {
'foo': 'bar',
'content-type': 'text/html'
@albertoperezf
albertoperezf / js
Created December 4, 2017 08:40
Validation Icon component
{/* This code is for the Icon of validation (Good & Bad) */}
{
showValidationStyles &&
<Icon
className={classNames('validationIcon')}
name={validationIconName}
size='18px'
/>
}
@albertoperezf
albertoperezf / removeAccents.js
Created October 17, 2017 08:14
Function to remove special characters (Accents, etc) from strings (es6)
// This function delete the especial characters in the strings (es6)
handleAccents = (str) => {
const accents = 'ÀÁÂÃÄÅàáâãäåßÒÓÔÕÕÖØòóôõöøÈÉÊËèéêëðÇçÐÌÍÎÏìíîïÙÚÛÜùúûüÑñŠšŸÿýŽž'
const accentsOut = 'AAAAAAaaaaaaBOOOOOOOooooooEEEEeeeeeCcDIIIIiiiiUUUUuuuuNnSsYyyZz'
str = str.split('')
str.forEach((letter, index) => {
const i = accents.indexOf(letter)
if (i !== -1) { str[index] = accentsOut[i] }
})
return str.join('')
@albertoperezf
albertoperezf / js
Created September 22, 2017 08:45
Function for Browser Language
const getLocale = () => {
let browserLanguage = 'en'
if (window.navigator && (window.navigator.language || window.navigator.userLanguage)) {
browserLanguage = (window.navigator.language || window.navigator.userLanguage).substr(0, 2);
}
return browserLanguage
}
@albertoperezf
albertoperezf / js
Created September 10, 2017 07:55
Fetch petition to unsplash.com
fetch(`https://api.unsplash.com/search/photos?page=1&query=${searchedForText}`, {
headers: {
Authorization: 'Client-ID abc123'
}
}).then(response => response.json())
.then(addImage);
function addImage(data) {
debugger;
}
@albertoperezf
albertoperezf / js
Created June 20, 2017 13:59
Get JWT from cookie
let cname = 'jwt'
let name = cname + '='
let decodedCookie = decodeURIComponent(document.cookie)
let ca = decodedCookie.split(';')
for (var i = 0; i < ca.length; i++) {
var c = ca[i]
if (c.indexOf(name) === 0) {
console.log('works')
let token = c.substring(name.length, c.length)
} else {
@albertoperezf
albertoperezf / js
Created June 20, 2017 10:49
Span to decrease opacity for date
{/* TODO remove in 100 days */}
<span
className={styles.bodyBg}
style={{opacity: `${
1.0 - ((new Date().getTime() - new Date('2017-06-09').getTime()) / 8640000000)
}`}}
/>
@albertoperezf
albertoperezf / js
Created June 15, 2017 12:02
Sum arrays in one line
var arr = [
1,
2,
3,
4,
5,
6
];
const sum = (arr) => arr.reduce((a, b) => a + b, 0);
@albertoperezf
albertoperezf / .gitconfig
Last active June 16, 2017 08:35
Git config for development
[user]
email = a.perez@medlabmg.com
name = Alberto Perez
[alias]
h = "! cat ~/.gitconfig"
# List of shortcuts
co = checkout