Skip to content

Instantly share code, notes, and snippets.

View JasonBBelcher's full-sized avatar
🎯
Focusing

Jason Belcher JasonBBelcher

🎯
Focusing
View GitHub Profile
@JasonBBelcher
JasonBBelcher / searchAndGetPaginated.js
Last active July 21, 2019 19:37
Async currying factory to get all paginated results
// Async, curried, recursive factory to get all paginated results
function searchAndGetPaginated(baseUrl) {
return function(resource, searchQuery) {
return new Promise((resolve, reject) => {
search(`${baseUrl}/${resource}/${searchQuery ? searchQuery : ""}`);
const allPages = [];
function search(url) {
fetch(url)
.then(res => res.json())
@JasonBBelcher
JasonBBelcher / factories.js
Created July 21, 2019 22:20
Composing with factory functions
// Composition over inheritance with Factory functions
// No classes were hurt in the making of this file.
// single factory functions utilizing higher order functions
// to pass arguments without returning the object at same time
function synthFactory(manufacturer, type, model) {
return function() {
return {
instrument: "synth",
@JasonBBelcher
JasonBBelcher / inheritWithoutClasses.js
Created July 22, 2019 19:39
Inheritance and composition without classes.
// inherit stuff without classes
function Dinosaur(name) {
function poo() {
console.log(`${name} poo poos.`);
}
function bite() {
console.log(`${name} bites and chomps!`);
}
return { poo, bite };
@JasonBBelcher
JasonBBelcher / inherit-with-classes.js
Created July 22, 2019 23:29
Inherit with classes
class Dinosaur {
constructor(name) {
this.poo = this.poo.bind(this);
this.name = name;
}
poo() {
console.log(`${this.name} poo poos. wooo it stinks!`);
}
// create a dataStore lib in JavaScript using a factory
function dataStore(dataset, interface) {
/* ***********************
PRIVATE FIELDS & METHODS
**************************/
// create a random id for lookup
function createId() {
return Math.random()
// implement map
function map(array, cb) {
let i;
let total = [];
for (i = 0; i < array.length; i += 1) {
total.push(cb(array[i]));
}
return total;
}
// implement map// implement map
function map(array, cb) {
let i;
let mapped = [];
for (i = 0; i < array.length; i += 1) {
total.push(cb(array[i]));
}
return mapped;
}
// Simple recursion example that walks down a bunch of nested objects till it finds a function.
const testObj = {
value: {
node: {
value: {
node: {
value: {
node: {
value: {