Skip to content

Instantly share code, notes, and snippets.

Avatar

Martin tincho

View GitHub Profile
@tincho
tincho / README.md
Last active Jun 19, 2021
coding excercises
View README.md

these are some samples of coding excercises I have resolved in past interviews

View combine.js
const combine = (...fns) => (...args) => fns.reduce((returnValues, fn) => [...returnValues, fn(...args)], [])
View mergeOuts.js
// @params: N functions that should return an Object
// @returns a fn that will pass all arg to all fns and return an Obj with all outputs merged
const mergeOutputs = (...fns) => (...args) => fns.reduce((out, fn) => ({
...out,
...fn(...args)
}), {})
// takes one arg
const one = (name) => ({ hello: name, world: name })
@tincho
tincho / loop.js
Created Dec 23, 2019
simple vainilla JS util to call a function each N milliseconds
View loop.js
const loop = {
timer: null,
count: 0,
start(fn, time=1000, limit = 100) {
fn(this.stop.bind(this), this.count);
this.count++;
if (this.count < limit) {
this.timer = setTimeout(() => this.start(fn, time, limit), time)
} else {
this.stop()
@tincho
tincho / memoizeByProps.js
Last active Sep 19, 2019
memoize by props shortcut
View memoizeByProps.js
const shallowCompare = (obj1, obj2) =>
typeof obj1 === 'object' && typeof obj2 === 'object'
? Object.keys(obj1).length === Object.keys(obj2).length &&
Object.keys(obj1).every(
key => obj2.hasOwnProperty(key) && obj1[key] === obj2[key]
)
: obj1 === obj2
const memoizeByProps = (...props) => Component =>
React.memo(Component, (prevProps, nextProps) =>
@tincho
tincho / fetchVideoThumb.js
Last active Dec 18, 2018
Get an image preview from a video URL with vainilla JavaScript
View fetchVideoThumb.js
// 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);
View getHeader.js
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 / ary.js
Created Dec 6, 2018
ES5 vs ES6 function for fix a function to take up to N-arguments (see N-ary or arity)
View ary.js
// 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);
};
}
View chunkify.js
// 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 / roller.js
Created Nov 22, 2018
timer function to change contents of DOM element at given interval (default: 5s)
View roller.js
// first iteration
// v0.1
(function(globalObject, document) {
function randomItem(collection) {
var index = Math.floor(Math.random() * collection.length);
return collection[index];
}