Kyle Coberly
kylecoberly.com
"Compose a data transformation pipeline with `lodash/fp`"
{
users: {
1: {
name: "Amelia Bedelia",
},
2: {
name: "Bob Barker",
}
},
availabilities: [{
date: "2020-09-01",
startTime: 12,
duration: 1,
user_id: 1,
},{
date: "2020-09-01",
startTime: 14,
duration: 2,
user_id: 1,
},{
date: "2020-09-03",
startTime: 18,
duration: 2,
user_id: 2,
}],
}
{
users: [{
id: 1,
name: "Amelia Bedelia",
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
},{
id: 2,
name: "Bob Barker",
days: {
"2020-09-03": [{
startTime: 18,
duration: 2,
}],
},
}],
}
// Plain ole JS
[1, 2, 3].map(number => number * 2)
// Sexy Lodash
const { map } = require("lodash/fp")
map(number => number *2 )([1, 2, 3])
Not a method
Curried
Data Last
const { map } = require("lodash/fp")
const double = number => number * 2
const doubleAll = map(double)
doubleAll([1, 2, 3])
const { flow } = require("lodash/fp")
const someMath = flow([
doubleAll,
doubleAll,
onlyOver10,
halfAll
sum,
])
someMath(numbers)
{
users: {
1: {
name: "Amelia Bedelia",
},
2: {
name: "Bob Barker",
}
},
availabilities: [{
date: "2020-09-01",
startTime: 12,
duration: 1,
user_id: 1,
},{
date: "2020-09-01",
startTime: 14,
duration: 2,
user_id: 1,
},{
date: "2020-09-03",
startTime: 18,
duration: 2,
user_id: 2,
}],
}
{
users: [{
id: 1,
name: "Amelia Bedelia",
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
},{
id: 2,
name: "Bob Barker",
days: {
"2020-09-03": [{
startTime: 18,
duration: 2,
}],
},
}],
}
function transformImperative({ users, availabilities }){
const transformedUsers = []
for (let id in users){
const user = users[id]
user.id = +id
user.days = {}
for (let availability of availabilities){
if (availability.user_id != id){
continue;
}
const date = availability.date
if (!user.days[date]){
user.days[date] = []
}
delete availability.date
delete availability.user_id
user.days[date].push(availability)
}
transformedUsers.push(user)
}
return { users: transformedUsers }
}
function transformImperative({ users, availabilities }){
const transformedUsers = []
for (let id in users){
const user = users[id]
user.id = +id // Mutation
user.days = {} // Mutation
for (let availability of availabilities){
if (availability.user_id != id){
continue;
}
const date = availability.date
if (!user.days[date]){
user.days[date] = [] // Mutation
}
delete availability.date // Mutation
delete availability.user_id // Mutation
user.days[date].push(availability) // Mutation
}
transformedUsers.push(user) // Mutation
}
return { users: transformedUsers }
}
function transformImperative({ users, availabilities }){
const transformedUsers = []
for (let id in users){
const user = users[id]
user.id = +id // Mutation
user.days = {} // Mutation
for (let availability of availabilities){
if (availability.user_id != id){
continue;
}
const date = availability.date
if (!user.days[date]){
user.days[date] = [] // Mutation
}
delete availability.date // Mutation
delete availability.user_id // Mutation
// vvv ALSO RELIES ON MUTABLE STATE!! vvv
user.days[date].push(availability) // Mutation
}
transformedUsers.push(user) // Mutation
}
return { users: transformedUsers }
}
[{
date: "2020-09-01",
startTime: 12,
duration: 1,
user_id: 1,
},{
date: "2020-09-01",
startTime: 14,
duration: 2,
user_id: 1,
},{
date: "2020-09-03",
startTime: 18,
duration: 2,
user_id: 2,
}]
{
1: [{
date: "2020-09-01",
startTime: 12,
duration: 1,
},{
date: "2020-09-01",
startTime: 14,
duration: 2,
}],
2: [{
date: "2020-09-03",
startTime: 18,
duration: 2,
}]
}
[{
date: "2020-09-01",
startTime: 12,
duration: 1,
},{
date: "2020-09-01",
startTime: 14,
duration: 2,
},{
date: "2020-09-03",
startTime: 18,
duration: 2,
}]
{
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
"2020-09-03": [{
startTime: 18,
duration: 2,
}],
}
{
1: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
2: {
"2020-09-03": [{
startTime: 14,
duration: 2,
}],
}
}
{
1: {
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
},
2: {
days: {
"2020-09-03": [{
startTime: 14,
duration: 2,
}],
},
},
}
{
1: {
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
},
}
{
1: {
name: "Amelia Bedelia",
},
}
{
1: {
name: "Amelia Bedelia",
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
},
}
{
1: {
name: "Amelia Bedelia",
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
},
}
[{
name: "Amelia Bedelia",
id: 1,
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
}]
{
users: {
1: {
name: "Amelia Bedelia",
},
2: {
name: "Bob Barker",
}
},
availabilities: [{
date: "2020-09-01",
startTime: 12,
duration: 1,
user_id: 1,
},{
date: "2020-09-01",
startTime: 14,
duration: 2,
user_id: 1,
},{
date: "2020-09-03",
startTime: 18,
duration: 2,
user_id: 2,
}],
}
{
users: [{
id: 1,
name: "Amelia Bedelia",
days: {
"2020-09-01": [{
startTime: 12,
duration: 1,
},{
startTime: 14,
duration: 2,
}],
},
},{
id: 2,
name: "Bob Barker",
days: {
"2020-09-03": [{
startTime: 18,
duration: 2,
}],
},
}],
}