January 2020
Intermediate to advanced
470 pages
11h 13m
English
How do we implement prisms? We will take our cue from our lenses implementation and make a few changes. When getting an attribute, we must check whether the object we are processing is not null or undefined and whether the attribute we want is in the object. We can make do by making small changes to our original getField() function:
const getFieldP = curry((attr, obj) => obj && attr in obj ? obj[attr] : undefined);
Here, we're checking for the existence of the object and the attribute: if everything's fine, we return obj[attr]; otherwise, we return undefined otherwise. The changes for setField() are very similar:
const setFieldP = curry((attr, value, obj) => obj && attr in obj ? { ...obj, [attr]: value } : { ...obj }