Last active
September 25, 2020 22:29
-
-
Save chalu/934995943b06cbc28b873ffc9ddafde3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* The API | |
================== */ | |
const noop = () => { }; | |
// abstract matcher | |
const match = (field = '', delegate = noop) => { | |
return (data = []) => { | |
return data.filter(entry => delegate(entry[field])); | |
} | |
}; | |
// concrete matchers | |
const is = (target) => (value) => value === target; | |
const isOver = (target) => (value) => value > target; | |
const isUnder = (target) => (value) => value < target; | |
const isOrOver = (target) => (value) => value >= target; | |
const isOrUnder = (target) => (value) => value <= target; | |
const isMultipleOf = (target) => (value) => value % target === 0; | |
// abstract compute | |
const compute = (delegate = noop, field = '') => { | |
return (data) => delegate(data, field); | |
}; | |
// concrete compute | |
const sumOf = (data = [], field = '') => { | |
return data.reduce((sum, entry) => { | |
return sum += entry[field] || 0; | |
}, 0); | |
}; | |
const averageOf = (data = [], field = '') => { | |
return data.reduce((sum = 0, entry, index) => { | |
sum += (entry[field] || 0); | |
if (index === data.length - 1) return sum / data.length; | |
return sum; | |
}); | |
}; | |
const chain = (...fns) => { | |
return (data) => { | |
return fns.reduce((computed, fn) => { | |
return fn(computed); | |
}, data); | |
}; | |
}; | |
/* Client Code (what you will write to use the API) | |
===================================================== */ | |
const handleData = (developers = []) => { | |
const isFemale = match('gender', is('Female')); | |
const isYoungAdult = match('age', isOrOver(20)); | |
const canPlayNightOwl = match('age', isUnder(50)); | |
const ageIsSpecialNumber = match('age', isMultipleOf(3)); | |
const sortByAgeDescending = (data = []) => { | |
return data.slice().sort((a, b) => { | |
return b.age - a.age; | |
}); | |
} | |
const getSpecialFemaleDevs = chain( | |
isFemale, | |
isYoungAdult, | |
canPlayNightOwl, | |
ageIsSpecialNumber, | |
sortByAgeDescending | |
); | |
const specialFemales = getSpecialFemaleDevs(developers); | |
console.log(`special females => ${specialFemales.length} entries.`); | |
const [oldest] = specialFemales; | |
const lastEntryIndex = specialFemales.length - 1; | |
const {[lastEntryIndex]: youngest} = specialFemales; | |
console.log(oldest, youngest); | |
} | |
const takeOff = async () => { | |
try { | |
const API = 'https://my.api.mockaroo.com/ssa-developers.json?key=18865cd0'; | |
const response = await fetch(API); | |
const developers = await response.json(); | |
console.log(`Got ${developers.length} entries`); | |
handleData(developers); | |
} catch (error) { | |
console.warn(error); | |
} | |
}; | |
takeOff(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment