maybe bind
https://github.com/eu81273/jsfiddle-console
by dimitrs_papadimitriou
HTML
<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>
JavaScript
const some = (v) => ({
map: (f) => some(f(v)),
bind: (f) => f(v),
matchWith: pattern => pattern.some(v),
});
const none = () => ({
map: (f) => none(),
bind: (f) => none(),
matchWith: pattern => pattern.none(),
});
Array.prototype.firstOrNone = function (predicate) {
var result = this.find(predicate);
if (result)
return some(result)
else
return none()
}
var clientRepository = ({
getById: (id) =>
[{ id: 1, name: 'jim', age: 29, employeeId:1 },
{ id: 2, name: 'jane', age: 25 , employeeId:3}]
.firstOrNone(c => c.id == id)
});
var employeesRepository = ({
getById: (id) =>
[{ id: 1, name: 'jim', age: 29 },
{ id: 2, name: 'jane', age: 25 }]
.firstOrNone(e => e.id == id)
});
clientRepository
.getById(2)
.bind(client=>employeesRepository.getById(client.employeeId))
.map(client => client.name)
.matchWith({
some: value => console.log(`the client name is ${value}`),
none: () => console.log(`no employee found`)
})