Skip to content

Instantly share code, notes, and snippets.

View HugoDF's full-sized avatar

Hugo HugoDF

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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);
}
swagger-codegen generate -i <path to your swagger file> -l html2 -o <path to output location>
@HugoDF
HugoDF / map-snake-to-camel.js
Created June 8, 2018 10:56
Map snake_case to camelCase recursively
const delimiterRegEx = /(_\w)/g;
export function convertSnakeToCamelCase (str) {
return str.replace(delimiterRegEx, ([delimiter, firstChar]) =>
firstChar.toUpperCase()
);
}
export const convertKeysToCamelCase = obj =>
Object.entries(obj).reduce((prev, [key, value]) => {
if (value && typeof value === 'object') {
@HugoDF
HugoDF / package.json
Created July 24, 2018 08:24
Express server rendered app with Pug and SCSS
{
"name": "express-pug-scss-starter",
"version": "1.0.0",
"description": "Express server rendered app with Pug and SCSS",
"main": "server.js",
"config": {
"app": "server.js",
"scss": "./src/scss",
"css": "./dist/css",
"img_src": "./src/img/",