Skip to content

Instantly share code, notes, and snippets.

View tincho's full-sized avatar

Martin tincho

View GitHub Profile
/**
* vainilla js simple slider
* source: http://cssdeck.com/labs/image-slider-1
*/
/**
css:
.image-slider-wrapper{
overflow: hidden;
}
@tincho
tincho / daysbetween.py
Created December 29, 2016 15:21
days between two dates
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import sys
def compare(dates):
if len(dates) < 2:
@tincho
tincho / ajaxSubmit.js
Created December 29, 2016 15:23
intercept form and submit it via ajax (requires jQuery)
function _noop() {}
function _constant(v) { return function() { return v; }; }
/**
* intercept <form> and submit it via XHR
* @param {String} formSelector
* @param {Function} successCallback
* @param {Function} errorCallback
* @param {Function} beforeSubmit
*/
@tincho
tincho / array.prefixEach.js
Created December 29, 2016 15:41
Array.prefixEach preppends string to every element in array
/**
* Prefixes each array element with given
* @param {String} prefix
* @usage e.g var pfixed = ["one", "two", "three"].prefixEach("number: ");
*/
Array.prototype.prefixEach = function(prefix) {
return this.map(_ary(String.prototype.concat.bind(prefix)));
};
function _ary(fn, arity) {
@tincho
tincho / element.fillWith.js
Last active December 29, 2016 15:44
Element.fillWith basic "template filling" function
/**
* replaces variables in element innerHTML, e.g.
* DOM: <span id="fillMe">hey {{#}}, whats up {{#}}</span>
* Code: document.querySelector('#fillMe').fillWith('bro')
* Output: hey bro, whats up bro
*
* DOM: <span id="fillMe">the {{animal}} is sitting at the {{forniture}}</span>
* Code: document.querySelector('#fillMe').fillWith({'animal': 'cat', 'forniture': 'table'});
* Output: the cat is sitting at the table
*/
@tincho
tincho / getData.js
Created December 29, 2016 16:00
Sample vainilla XHR get function
function getData(uri, done, evt) {
var request = new XMLHttpRequest();
request.open('GET', uri);
request.onreadystatechange = function() {
if(! (request.readyState == 4 && request.status === 200)) {
return;
}
return done(request);
@tincho
tincho / compose.js
Last active January 9, 2017 20:30
ES6 compose
// both answers copied from challange posted at e-mail list of my former job
// not-so oneliner
var compose = function () {
var composableFunctions = Array.prototype.slice.call(arguments).reverse();
return (x) => composableFunctions.reduce((result, composable) => composable(result), x);
/*
ES5:
return function(x) {
return composableFunctions.reduce(function(result, composable) {
@tincho
tincho / alltogethernow.js
Last active June 11, 2018 16:17
all functional-programming js tools i've written
// var logHello = _partial(console.log, "hello ");
// logHello("world");
// accepts _partial (fn reference) as placeholder just like underscore accepts _
// var partialApplied = _partial(originalFn, _partial, "second", _partial, "fifth");
// partialApplied("first", "third") will call originalFn("first", "second", "third", "fifth")
function _partial() {
var args = Array.prototype.slice.call(arguments);
var method = args.shift();
var self = this;
@tincho
tincho / introrx.md
Created March 2, 2017 13:30 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
@tincho
tincho / date-utils.js
Last active July 13, 2022 04:17
some utils for JS Date handling
/*
why fixDate?
if we create a JS Date writing:
var d = new Date("2015-05-15")
we are not specifying any timezone
the browser/environment will asume it is UTC+000,
and always represent it in user's LOCAL TIMEZONE!
so, running in an environment located in a UTC-3 zone,
d would actually show 2015-05-14 at 21:00
and may lead to errors.