Skip to content

Instantly share code, notes, and snippets.

View pgarciacamou's full-sized avatar
:octocat:

Pablo Garcia pgarciacamou

:octocat:
View GitHub Profile
@pgarciacamou
pgarciacamou / DecoratedError.js
Last active September 21, 2022 18:14
Modern JavaScript Decorator Pattern
export class DecoratedError extends Error {
/**
* @param {any} error
* @param {...Object|...() => Object} decorators
*/
constructor(error, ...decorators) {
if (typeof error === "string" || error instanceof String) {
error = new Error(error);
}
@pgarciacamou
pgarciacamou / .zshrc
Created April 28, 2019 23:52
Use `.gitemail` and `.npmregistry` files to switch user per project.
# Automatically assign local git user email
# This code expects the following
# file ".gitemail" to exist and contain only 1 row with an email
# file ".npmregistry" to exist and contain only 1 row with the registry url
#
# Note: if any of the files don't exist then the default will be used
setup-dev-user() {
local expected_git_email=$(grep -hs ^ ./.gitemail)
local current_git_email="$(git config user.email)"
local default_git_email="example@company.com"
@pgarciacamou
pgarciacamou / requestAnimationFrameHoax.js
Created January 4, 2017 19:15
Request Animation Frame HOAX! requestAnimationFrame with setTimeout to push the fps-limited browsers like iOS8.
/*
* Tasks that are supposed to be executed every screen paint.
*/
var tasks = [
() => {
var now = Date.now();
// heavy algorithm example
for(var i = 0; i < 200/*000*/; i+=1){
var dummy = i * 0.1;
@pgarciacamou
pgarciacamou / pubsub.js
Created November 4, 2016 18:22
Publish Subscribe JS Pattern mini module.
/**
* Simple implementation of Publish and Subscribe Pattern
*
* This helps a lot specially when the components are
* not close enough that passing a function as props
* looks ugly.
*/
const topics = {};
module.exports = {
subscribe: function (topic, callback) {
@pgarciacamou
pgarciacamou / README.md
Created October 16, 2016 17:51
Slider done with pure CSS, JavaScript, and HTML. (prefixes are not added)
@pgarciacamou
pgarciacamou / grid-system.scss
Last active September 14, 2016 23:28
Small old bootstrap grid system
$breakpoints: 700px, 900px, 1100px, 1300px;
$sizes: "sm", "md", "lg", "xl";
$cols: 12;
.clearfix {
clear: both;
}
.row {
position: relative;
@pgarciacamou
pgarciacamou / execute.js
Created August 23, 2016 21:34
Callback executer wrapper.
function execute(){
var callbacks = Array.prototype.map.call(arguments, function(callback){
return callback instanceof Function && callback;
});
return function(){
var self = this;
var args = arguments;
callbacks.forEach(function (callback){
callback.apply(self, args);
});
@pgarciacamou
pgarciacamou / createObservable.js
Created July 26, 2016 16:31
Small observable implemented with getters and setters.
export default function createObservable(obj, callback) {
var getset = {};
var values = {};
var isFinalized = false;
for(let prop in obj) {
Object.defineProperty(getset, prop, {
get: _ => values[prop],
set: newValue => {
if(newValue === values[prop]) {
return values[prop];
@pgarciacamou
pgarciacamou / how-to-v2.0.js
Last active August 13, 2016 20:38
Pipe is a small function that pipes functionality to resemble RxJS
// HOW TO
pipe.pipeline(run => {
var sensorData = [];
document.addEventListener("mousemove", _ => {
sensorData.push(_.x.toFixed(0));
}, false);
requestAnimationFrame(function loop(){
if(sensorData.length > 0) {
sensorData.forEach(run);
sensorData = [];
@pgarciacamou
pgarciacamou / addListener.js
Created April 18, 2016 20:16
Event Listener with destroyers.
function addListener(elem, evtName, listener) {
elem.addEventListener(evtName, listener, false);
oneTimeListener(document, "motiondestroy", function () {
elem.removeEventListener(evtName, listener, false);
});
}