Skip to content

Instantly share code, notes, and snippets.

View tincho's full-sized avatar

Martin tincho

View GitHub Profile
@tincho
tincho / Ajaxify.js
Created June 28, 2017 19:14
simple jquery snippet to override a native form's with an ajax call
// requires jQuery!
const noop = () => {};
const constant = v => () => v;
const beTrue = constant(true); // () => true;
function $Ajaxify(form, ...callbacks) {
if (typeof callbacks[0] === 'object') {
var {
onSuccess = noop, onError = noop, beforeSend = beTrue
@tincho
tincho / download.js
Created October 25, 2017 16:15
script for downloading HTTP-Auth'ed file through XHR
var form = document.getElementById("dlFile");
form.onsubmit = function(e) {
e.preventDefault();
var user = form.user.value; // "mayoristas";
var pass = form.pass.value;
requestFile(user, pass);
return false;
}
/**
* @license MIT
* @requires AngularJs 1.x
* @author github.com/tincho
* Tiny helper for listener deregistration on scope destroy
* You'll have to wrap it up in a module/provider/whatever
* (that's out of my focus)
*/
function LDisposer(scope) {
var _list = [];
@tincho
tincho / copyValues.js
Last active January 28, 2019 17:04
utility to extract values at given paths from given objects
// iteration 0
// adding deep diving for key paths
// based on getProperty()
// try on tddbin.com
function copyFrom(obj) {
const values = (...keys) => keys.map(k => getProperty(k)(obj));
return { values };
}
@tincho
tincho / roller.js
Created November 22, 2018 19:09
timer function to change contents of DOM element at given interval (default: 5s)
// first iteration
// v0.1
(function(globalObject, document) {
function randomItem(collection) {
var index = Math.floor(Math.random() * collection.length);
return collection[index];
}
// alternative version using ES6 Array.from
const chunkify = (chunkSize, src) => Array.from(
{ length: Math.ceil(src.length/chunkSize) },
(_, i) => src.slice(i * chunkSize, i * chunkSize + chunkSize)
)
@tincho
tincho / ary.js
Created December 6, 2018 14:53
ES5 vs ES6 function for fix a function to take up to N-arguments (see N-ary or arity)
// pre-ES6
function _ary(fn, arity) {
arity = arity || 1;
return function() {
// could use Array.from(arguments).slice(0, arity) ?
var args = Array.prototype.slice.call(arguments, 0, arity);
return fn.apply(null, args);
};
}
function getHeader(url, header) {
return new Promise(function(resolve, reject) {
var http = new XMLHttpRequest();
http.open('HEAD', url);
http.onreadystatechange = function() {
if (this.readyState !== this.DONE) return;
if (this.status != 200) {
reject(this.status);
} else {
@tincho
tincho / fetchVideoThumb.js
Last active December 18, 2018 15:15
Get an image preview from a video URL with vainilla JavaScript
// make a data uri encoded image from a <video> element using canvas
// (thank you stackoverflow people)
function videoElementToImage(videoElement, width, height) {
width = width || 360;
height = height || 240;
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d');
context.drawImage(videoElement, 0, 0, canvas.width, canvas.height);