Skip to content

Instantly share code, notes, and snippets.

View HugoDF's full-sized avatar

Hugo HugoDF

View GitHub Profile
@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;
}
@HugoDF
HugoDF / map.js
Last active October 18, 2016 19:00
ES6 map implementation using recursion and destructuring
function map([ head, ...tail ], fn) {
if (head === undefined && !tail.length) return [];
return 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 / 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 / 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 / settings.json
Created February 3, 2019 20:53
Disable autocomplete and IntelliSense in Visual Studio Code, put this in your `.vscode` folder, researched as part of https://codewithhugo.com/async-js
{
"editor.quickSuggestions": {
"other": false,
"comments": false,
"strings": false
},
"editor.parameterHints.enabled": false
}