Skip to content

Instantly share code, notes, and snippets.

View HugoDF's full-sized avatar

Hugo HugoDF

View GitHub Profile
@HugoDF
HugoDF / range.js
Last active June 26, 2017 19:09
Takes a start and end value for an integer range and returns an array containing the range.
const range = (first, last) => {
const firstValue = last ? first : 1;
const length = last ? last - first + 1 : first;
return Array.from({ length })
.map((_, i) => i + firstValue);
}
@HugoDF
HugoDF / test.js
Last active April 18, 2017 17:08
Stub getter with sinon.
const mockNode = {
get firstChild() {
return undefined;
}
};
const stubFirstChildGetter = sinon.stub().returns(false).onFirstCall().returns(true);
sinon.stub(mockNode, 'firstChild')
.get(stubFirstChildGetter);
@HugoDF
HugoDF / example.jsx
Last active January 12, 2017 13:12
Example of partial application in JavaScript applied to React Component event handlers
class MyComponent extends React.Component {
partialHandleLinkClick(type, activeType){
return function(e) {
const hasKeyboardModifier = e.ctrlKey || e.shiftKey || e.altKey || e.metaKey;
updateType(type, activeType, hasKeyboardModifier);
};
}
render() {
const types = [ 'Foo', 'Bar', 'Baz' ];
return (
@HugoDF
HugoDF / package.json
Created October 21, 2016 13:09
Library development with ES6, Babel, Mocha and npm scripts
{
"name": "library-js",
"version": "1.0.0",
"description": "Starter for library development with ES6, Babel, Mocha and npm scripts",
"main": "./dist/index.js",
"scripts": {
"start": "npm run dev",
"dev": "npm test -- -w",
"init": "mkdir dist",
"clean": "rm -rf dist",
@HugoDF
HugoDF / map-arrow.js
Last active October 11, 2016 23:04
ES6 map implementation using arrow function, recursion and destructuring
var map = ([ head, ... tail ], fn) =>
( (head !== undefined && tail.length) ? ( tail.length ? [ fn(head), ...(map(tail, fn)) ] : [ fn(head) ] ) : []);
@HugoDF
HugoDF / es5-map.js
Last active October 13, 2016 18:01
Recursive implementation of map in ES5.
function map(arr, fn) {
var head = arr[0];
var tail = arr.slice(1);
if(head === undefined && tail.length === 0) return [];
if(tail.length === 0) {
return [ fn(head) ];
}
return [].concat(fn(head), map(tail, fn));
}
@HugoDF
HugoDF / package.json
Created October 6, 2016 12:33
Frontend React/Mocha/Babel/Browserify setup with npm scripts and browser-sync. Requires a `./src` folder containing a react.js and an app.scss as well as a `./index.html` file.
{
"name": "frontend-starter",
"version": "1.0.0",
"description": "React, browserify and babel. With npm scripts and browser-sync",
"main": "index.js",
"config": {
"ut": "{,!(node_modules)/**/}*.test.js"
},
"scripts": {
"start": "npm run dev",
@HugoDF
HugoDF / join.js
Last active October 13, 2016 19:12
ES6 join implementation using recursion and destructuring
function join([ head, ...tail ], separator = ',') {
if (head === undefined && !tail.length) return '';
return tail.length ? head + separator + join(tail, separator) : head;
}
@HugoDF
HugoDF / reduce.js
Last active February 20, 2018 02:11
ES6 reduce implementation using recursion and destructuring
function reduce([ head, ...tail ], fn, initial) {
if(head === undefined && tail.length === 0) return initial;
if(!initial) {
const [ newHead, ...newTail] = tail;
return reduce(newTail, fn, fn(head, newHead));
}
return tail.length ? reduce(tail, fn, fn(initial, head)) : [ fn(initial, head) ];
}
@HugoDF
HugoDF / filter.js
Last active October 14, 2016 14:22
ES6 filter implementation using recursion and destructuring
function filter([ head, ...tail ], fn) {
const newHead = fn(head) ? [ head ] : [];
return tail.length ? [ ...newHead, ...(filter(tail, fn)) ] : newHead;
}