Skip to content

Instantly share code, notes, and snippets.

@sebabouche
Created June 22, 2018 20:22
Show Gist options
  • Save sebabouche/c66321bd305464279d06e87ef4e272e5 to your computer and use it in GitHub Desktop.
Save sebabouche/c66321bd305464279d06e87ef4e272e5 to your computer and use it in GitHub Desktop.
ramda: lens (lens, lensProp, lensPath, view, set, over)
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