-
-
Save taroyanaka/8ca9e94456f66f8bd28b2ffef9fc1618 to your computer and use it in GitHub Desktop.
ramda: lens (lens, lensProp, lensPath, view, set, over)
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
import { lensProp, lensPath, view, set, over, toUpper } from "ramda" | |
const me = { | |
firstName: "Sébastien", | |
lastName: "Nicolaïdis", | |
birthDate: new Date("1979-09-13T15:00Z"), | |
age: 38, | |
hobbies: ["Rock climbing", "Horse riding", "Chess"], | |
parents: { | |
brother: { | |
firstName: "Frédéric", | |
lastName: "Nicolaïdis", | |
birthDate: new Date("1973-09-18T04:00Z"), | |
age: 44, | |
}, | |
sister: { | |
firstName: "Virginia", | |
lastName: "Nicolaïdis", | |
birthDate: "1975-03-23T09:32Z", | |
marks: [16, 20, 17, 18], | |
}, | |
}, | |
} | |
const myNameLens = lensProp("firstName") // equals lens(prop("firstName"), assoc("firstName")) | |
const viewMe = view(myNameLens, me) | |
console.log("viewMe", viewMe) | |
const setMe = set(myNameLens, "Patrick", me) | |
console.log("setMe", setMe) | |
const brotherNameLens = lensPath(["parents", "brother", "firstName"]) | |
const overBrother = over(brotherNameLens, toUpper, me) | |
console.log("overBrother", overBrother) | |
const twentyLens = lensPath(["parents", "sister", "marks", 1]) | |
console.log("view twentyLens", view(twentyLens, me)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment