Created
March 19, 2020 18:12
-
-
Save TechnotronicOz/2748213e04e54fd4d69427794d03825f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 comb = require('/Users/mattcarter/git/c2fo/node_modules/comb'); | |
const dateFormat = (dt) => comb.date.format(dt, 'yyyy-MM-dd'); | |
/** | |
* Issues: | |
* 1. Cannot use Date instances as Map keys, they'll never hit unless you | |
* use the same instance. | |
* 2. Setting an empty object in the due date for a cache and having another | |
* invoice come in with that due date after will result in that second invoice | |
* not getting it's dueDate updated as we return an empty {} | |
*/ | |
const callCounts = { | |
set: 0, | |
get: 0, | |
has: 0, | |
}; | |
class MMap extends Map { | |
setData(k, v) { | |
// console.log(`setData to map [k=${k}, v=${JSON.stringify(v)}]`); | |
callCounts.set++; | |
return this.set(k, v); | |
} | |
getData(k) { | |
// console.log(`getData from map [k=${k}]`); | |
callCounts.get++; | |
return this.get(k); | |
} | |
hasData(k) { | |
// console.log(`hasData from map k=${k}]`); | |
callCounts.has++; | |
return this.has(k); | |
} | |
} | |
class PaymentSettings { | |
constructor() { | |
this.cache = new MMap(); | |
this.fakerCallCount = 0; | |
} | |
_rRuleFaker(dueDate) { | |
this.fakerCallCount++; | |
if (this.fakerCallCount === 1) { | |
return dueDate; | |
} else if (this.fakerCallCount === 2) { | |
return comb.date.add(dueDate, 'weekdays', 1); | |
} else if (this.fakerCallCount === 3) { | |
return comb.date.add(dueDate, 'weekdays', 2); | |
} else { | |
return comb.date.add(dueDate, 'weekdays', 1); | |
} | |
} | |
process(invoice = {}) { | |
const {dueDate} = invoice; | |
const formattedDueDate = dateFormat(dueDate); | |
console.log(`process invoice [id=${invoice.id}, dueDate=${dueDate}]`); | |
if (this.cache.hasData(formattedDueDate)) { | |
console.log(` invoice dueDate in cache! [dueDate=${formattedDueDate}, cache=${JSON.stringify(this.cache.getData(formattedDueDate))}]`); | |
return { | |
dueDate: this.cache.getData(formattedDueDate), | |
updated: new Date(), | |
}; | |
} | |
const nextValidDueDate = this._rRuleFaker(dueDate); | |
if (comb.date.compare(nextValidDueDate, dueDate, 'days')) { | |
console.log(' caching next valid due date'); | |
this.cache.setData(formattedDueDate, { | |
dueDate: nextValidDueDate, | |
}); | |
} else { | |
console.log(' else, setting empty data'); | |
this.cache.setData(formattedDueDate, {}); | |
} | |
console.log(` done -> [id=${invoice.id}, fromCache=${JSON.stringify(this.cache.getData(formattedDueDate))}]`); | |
return this.cache.getData(formattedDueDate); | |
} | |
done() { | |
console.log('\ndone', JSON.stringify(callCounts)); | |
} | |
} | |
const invoices = [ | |
{ | |
id: 1, | |
dueDate: new Date(2020, 2, 19) | |
}, | |
{ | |
id: 2, | |
dueDate: new Date(2020, 2, 19), | |
}, | |
{ | |
id: 3, | |
dueDate: new Date(2020, 2, 21), | |
}, | |
{ | |
id: 4, | |
dueDate: new Date(2020, 2, 21), | |
}, | |
{ | |
id: 5, | |
dueDate: new Date(2020, 2, 22), | |
} | |
]; | |
const p = new PaymentSettings(); | |
const updates = invoices.map(invoice => Object.assign({id: invoice.id}, p.process(invoice))); | |
p.done(); | |
console.log('\n', JSON.stringify(updates)); |
This file contains hidden or 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
process invoice [id=1, dueDate=Thu Mar 19 2020 00:00:00 GMT+0000 (UTC)] | |
else, setting empty data | |
done -> [id=1, fromCache={}] | |
process invoice [id=2, dueDate=Thu Mar 19 2020 00:00:00 GMT+0000 (UTC)] | |
invoice dueDate in cache! [dueDate=2020-03-19, cache={}] | |
process invoice [id=3, dueDate=Sat Mar 21 2020 00:00:00 GMT+0000 (UTC)] | |
caching next valid due date | |
done -> [id=3, fromCache={"dueDate":"2020-03-23T00:00:00.000Z"}] | |
process invoice [id=4, dueDate=Sat Mar 21 2020 00:00:00 GMT+0000 (UTC)] | |
invoice dueDate in cache! [dueDate=2020-03-21, cache={"dueDate":"2020-03-23T00:00:00.000Z"}] | |
process invoice [id=5, dueDate=Sun Mar 22 2020 00:00:00 GMT+0000 (UTC)] | |
caching next valid due date | |
done -> [id=5, fromCache={"dueDate":"2020-03-24T00:00:00.000Z"}] | |
done {"set":3,"get":10,"has":5} | |
[{"id":1},{"id":2,"dueDate":{},"updated":"2020-03-19T17:59:20.374Z"},{"id":3,"dueDate":"2020-03-23T00:00:00.000Z"},{"id":4,"dueDate":{"dueDate":"2020-03-23T00:00:00.000Z"},"updated":"2020-03-19T17:59:20.376Z"},{"id":5,"dueDate":"2020-03-24T00:00:00.000Z"}] |
This file contains hidden or 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 comb = require('/Users/mattcarter/git/c2fo/node_modules/comb'); | |
const dateFormat = (dt) => comb.date.format(dt, 'yyyy-MM-dd'); | |
/** | |
* Issues: | |
* 1. Cannot use Date instances as Map keys, they'll never hit unless you | |
* use the same instance. | |
* 2. Setting an empty object in the due date for a cache and having another | |
* invoice come in with that due date after will result in that second invoice | |
* not getting it's dueDate updated as we return an empty {} | |
*/ | |
const callCounts = { | |
set: 0, | |
get: 0, | |
has: 0, | |
}; | |
class MMap extends Map { | |
setData(k, v) { | |
// console.log(`setData to map [k=${k}, v=${JSON.stringify(v)}]`); | |
callCounts.set++; | |
return this.set(k, v); | |
} | |
getData(k) { | |
// console.log(`getData from map [k=${k}]`); | |
callCounts.get++; | |
return this.get(k); | |
} | |
hasData(k) { | |
// console.log(`hasData from map k=${k}]`); | |
callCounts.has++; | |
return this.has(k); | |
} | |
} | |
class PaymentSettings { | |
constructor() { | |
this.cache = new MMap(); | |
this.fakerCallCount = 0; | |
} | |
_rRuleFaker(dueDate) { | |
this.fakerCallCount++; | |
if (this.fakerCallCount === 1) { | |
return dueDate; | |
} else if (this.fakerCallCount === 2) { | |
return comb.date.add(dueDate, 'weekdays', 1); | |
} else if (this.fakerCallCount === 3) { | |
return comb.date.add(dueDate, 'weekdays', 2); | |
} else { | |
return comb.date.add(dueDate, 'weekdays', 1); | |
} | |
} | |
process(invoice = {}) { | |
const {dueDate} = invoice; | |
const formattedDueDate = dateFormat(dueDate); | |
console.log(`process invoice [id=${invoice.id}, dueDate=${dueDate}]`); | |
if (this.cache.hasData(formattedDueDate)) { | |
console.log(` invoice dueDate in cache! [dueDate=${formattedDueDate}, cache=${JSON.stringify(this.cache.getData(formattedDueDate))}]`); | |
return { | |
dueDate: this.cache.getData(formattedDueDate), | |
updated: new Date(), | |
}; | |
} | |
const nextValidDueDate = this._rRuleFaker(dueDate); | |
this.cache.setData(formattedDueDate, nextValidDueDate); | |
if (comb.date.compare(nextValidDueDate, dueDate, 'days')) { | |
console.log(' caching next valid due date'); | |
return { | |
dueDate: nextValidDueDate, | |
} | |
} | |
console.log(` done -> [id=${invoice.id}, fromCache=${JSON.stringify(this.cache.getData(formattedDueDate))}]`); | |
return {}; | |
} | |
done() { | |
console.log('\ndone', JSON.stringify(callCounts)); | |
} | |
} | |
const invoices = [ | |
{ | |
id: 1, | |
dueDate: new Date(2020, 2, 19) | |
}, | |
{ | |
id: 2, | |
dueDate: new Date(2020, 2, 19), | |
}, | |
{ | |
id: 3, | |
dueDate: new Date(2020, 2, 21), | |
}, | |
{ | |
id: 4, | |
dueDate: new Date(2020, 2, 21), | |
}, | |
{ | |
id: 5, | |
dueDate: new Date(2020, 2, 22), | |
} | |
]; | |
const p = new PaymentSettings(); | |
const updates = invoices.map(invoice => Object.assign({id: invoice.id}, p.process(invoice))); | |
p.done(); | |
console.log('\n', JSON.stringify(updates)); |
This file contains hidden or 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
/Users/mattcarter/.nvm/versions/node/v8.16.0/bin/node /Users/mattcarter/Library/Preferences/WebStorm2019.3/scratches/scratch_95.es6 | |
process invoice [id=1, dueDate=Thu Mar 19 2020 00:00:00 GMT+0000 (UTC)] | |
done -> [id=1, fromCache="2020-03-19T00:00:00.000Z"] | |
process invoice [id=2, dueDate=Thu Mar 19 2020 00:00:00 GMT+0000 (UTC)] | |
invoice dueDate in cache! [dueDate=2020-03-19, cache="2020-03-19T00:00:00.000Z"] | |
process invoice [id=3, dueDate=Sat Mar 21 2020 00:00:00 GMT+0000 (UTC)] | |
caching next valid due date | |
process invoice [id=4, dueDate=Sat Mar 21 2020 00:00:00 GMT+0000 (UTC)] | |
invoice dueDate in cache! [dueDate=2020-03-21, cache="2020-03-23T00:00:00.000Z"] | |
process invoice [id=5, dueDate=Sun Mar 22 2020 00:00:00 GMT+0000 (UTC)] | |
caching next valid due date | |
done {"set":3,"get":5,"has":5} | |
[{"id":1},{"id":2,"dueDate":"2020-03-19T00:00:00.000Z","updated":"2020-03-19T18:12:29.918Z"},{"id":3,"dueDate":"2020-03-23T00:00:00.000Z"},{"id":4,"dueDate":"2020-03-23T00:00:00.000Z","updated":"2020-03-19T18:12:29.918Z"},{"id":5,"dueDate":"2020-03-24T00:00:00.000Z"}] | |
Process finished with exit code 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment