Created
December 11, 2017 08:31
-
-
Save fgandellini/6bef0333f1b043ea0ebaf543156ba8fe to your computer and use it in GitHub Desktop.
refactoring-video-store-js
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
const arraySum = prop => arr => | |
arr.reduce((acc, r) => acc + r[prop], 0) | |
const amountByCode = r => ({ | |
'regular': 2 + (r.days > 2 ? ((r.days - 2) * 1.5) : 0), | |
'new': r.days * 3, | |
'childrens': 1.5 + (r.days > 3 ? ((r.days - 3) * 1.5) : 0), | |
}) | |
const frequentRenterPointsByCode = r => ({ | |
'regular': 1, | |
// add bonus for a two day new release rental | |
'new': r.days > 2 ? 2 : 1, | |
'childrens': 1 | |
}) | |
const buildRecord = movies => r => { | |
const movie = movies[r.movieID] | |
const amounts = amountByCode(r) | |
const points = frequentRenterPointsByCode(r) | |
return { | |
movie, | |
amount: amounts[movie.code], | |
points: points[movie.code], | |
} | |
} | |
const print = (customer, records) => { | |
const sumAmounts = arraySum('amount') | |
const sumPoints = arraySum('points') | |
const amounts = | |
records | |
.map(r => `\t${r.movie.title}\t${r.amount}\n`) | |
.join('') | |
return ( | |
`Rental Record for ${customer.name}\n` + | |
amounts + | |
`Amount owed is ${sumAmounts(records)}\n` + | |
`You earned ${sumPoints(records)} frequent renter points\n` | |
) | |
} | |
function statement(customer, movies) { | |
const records = | |
customer.rentals | |
.map(buildRecord(movies)) | |
return print(customer, records) | |
} | |
module.exports = statement |
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
const assert = require('assert') | |
const statement = require('./index') | |
const MOVIES = { | |
F001: { title: 'Ran', code: 'regular'}, | |
F002: { title: 'Trois Couleurs: Bleu', code: 'regular'}, | |
F003: { title: 'Logan', code: 'new' }, | |
F004: { title: 'Robin Hood', code: 'childrens' }, | |
} | |
const customer1 = { | |
name: 'martin', | |
rentals: [ | |
{ movieID: 'F001', days: 3 }, | |
{ movieID: 'F002', days: 1 }, | |
] | |
} | |
const output1 = | |
'Rental Record for martin' + | |
'\n\tRan\t3.5' + | |
'\n\tTrois Couleurs: Bleu\t2' + | |
'\nAmount owed is 5.5' + | |
'\nYou earned 2 frequent renter points' + | |
'\n' | |
const customer2 = { | |
name: 'fede', | |
rentals: [ | |
{ movieID: 'F003', days: 4 }, | |
{ movieID: 'F004', days: 5 }, | |
] | |
} | |
const output2 = | |
'Rental Record for fede' + | |
'\n\tLogan\t12' + | |
'\n\tRobin Hood\t4.5' + | |
'\nAmount owed is 16.5' + | |
'\nYou earned 3 frequent renter points' + | |
'\n' | |
const customer3 = { | |
name: 'rob', | |
rentals: [ | |
{ movieID: 'F001', days: 1 }, | |
{ movieID: 'F004', days: 2 }, | |
] | |
} | |
const output3 = | |
'Rental Record for rob' + | |
'\n\tRan\t2' + | |
'\n\tRobin Hood\t1.5' + | |
'\nAmount owed is 3.5' + | |
'\nYou earned 2 frequent renter points' + | |
'\n' | |
describe('statement', () => { | |
it('should print statement for customer 1', () => { | |
assert.equal(output1, statement(customer1, MOVIES)) | |
}) | |
it('should print statement for customer 2', () => { | |
assert.equal(output2, statement(customer2, MOVIES)) | |
}) | |
it('should print statement for customer 3', () => { | |
assert.equal(output3, statement(customer3, MOVIES)) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment