Skip to content

Instantly share code, notes, and snippets.

View GianlucaGuarini's full-sized avatar
🎸
Rocking the world with few lines of code

Gianluca Guarini GianlucaGuarini

🎸
Rocking the world with few lines of code
View GitHub Profile
@GianlucaGuarini
GianlucaGuarini / login.js
Created January 8, 2020 21:10
programming like a modern artist
;(async function() {
// puppeteer boilerplate code
const browser = await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
// notice the small device size
defaultViewport: {
width: 375,
height: 812
}
@GianlucaGuarini
GianlucaGuarini / wait.js
Last active January 19, 2018 10:41
Functional advanced async (cancellable/abortable) `setTimeout` helper
/**
* Advanced async (cancellable/abortable) `setTimeout` helper
*/
function wait(delay) {
return (val) => {
let timer
let abort
const p = new Promise(function(resolve, reject) {
abort = reject
@GianlucaGuarini
GianlucaGuarini / curry.js
Last active November 18, 2017 21:55
Function to curry any javascript method
/**
* Function to curry any javascript method
* @param {Function} fn - the target function we want to curry
* @param {...[args]} acc - initial arguments
* @returns {Function|*} it will return a function until the target function
* will receive all its arguments
*/
function curry(fn, ...acc) {
return (...args) => {
args = [...acc, ...args]
@GianlucaGuarini
GianlucaGuarini / accessible-overlays.js
Last active November 1, 2017 10:57
Simple function to make your overlays element accessible
// forked by a script of https://github.com/nilssolanki
import { add } from 'bianco.events'
/**
* A list of all the open overlays, tooltips, sidebars etc.
* @type {Map}
*/
const openOverlays = new Map()
/**
@GianlucaGuarini
GianlucaGuarini / real-device-px-ratio.js
Created October 9, 2017 12:48
Because devicePixelRatio is not relyable enough
const resolutions = [
'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACwAAAAAAQABAAACAkQBADs=',
'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=',
'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQYV2P4DwABAQEAWk1v8QAAAABJRU5ErkJggg==',
];
const img = document.createElement('img');
img.src = resolutions[0];
img.srcset = resolutions.map((itm, i) => `${itm} ${i + 1}x`).join(',');
@GianlucaGuarini
GianlucaGuarini / promise-sequence.js
Created May 5, 2017 15:08
Run promises as sequence
function sequence(promises) {
return new Promise((resolve, reject) => {
const rets = []
const gen = (function*() {
// loop as long as we have promises in the queue
while (promises.length) {
// take always the first promise
const promise = promises.shift()
// wait until it's resolved to step to the next iteration
promise
@GianlucaGuarini
GianlucaGuarini / local-storage.js
Last active May 2, 2017 11:59
A simple script to deal safely with the localStorage
/**
* Deal with the localStorage avoiding odd issues due to paranoids that have disabled it by default
*/
const ls = window.localStorage
/**
* Call any method on the localStorage avoiding to throw errors
* @param {string} method - method we want to call
* @param {array} args - serialized params that will be proxied to the method we are going to call
* @returns {null|string} whatever the method call will return
@GianlucaGuarini
GianlucaGuarini / main.tag
Last active March 1, 2018 21:11
Simple example to demonstarte how to use es2015 imports with the default riot cli. Install riot via `npm i riot -g` and then run `npm run build`
<main-tag>
<h1>I got the power!</h1>
<h2>The answer is { answer }</h2>
<script>
import something from './something'
this.answer = something.answer
</script>
</main-tag>
@GianlucaGuarini
GianlucaGuarini / read-cookie.js
Last active December 12, 2016 14:37
Simple function to read a named cookie from the document.cookie string
@GianlucaGuarini
GianlucaGuarini / a.js
Last active April 18, 2019 14:17
Use always `module.exports = Object.freeze({})`
const b = require('./b')
b.b = 'c'
module.exports = {
a: 'a'
}