gcanti/monocle-ts is one of the UI-focused repositories TopGit tracks, currently at 1.1k stars, written primarily in TypeScript. Functional optics: a (partial) porting of Scala monocle
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
Modifying immutable nested object in JavaScript is verbose which makes code difficult to understand and reason about.
Let's have a look at some examples:
interface Street {
num: number
name: string
}
interface Address {
city: string
street: Street
}
interface Company {
name: string
address: Address
}
interface Employee {
name: string
company: Company
}
Let’s say we have an employee and we need to upper case the first character of his company street name. Here is how we
could write it in vanilla JavaScript
As we can see copy is not convenient to update nested objects because we need to repeat ourselves. Let's see what could
we do with monocle-ts
import { Lens } from 'monocle-ts'
const company = Lens.fromProp<Employee>()('company')
const address = Lens.fromProp<Company>()('address')
const street = Lens.fromProp<Address>()('street')
const name = Lens.fromProp<Street>()('name')
compose takes two Lenses, one from A to B and another one from B to C and creates a third Lens from A to
C. Therefore, after composing company, address, street and name, we obtain a Lens from Employee to
string (the street name). Now we can use this Lens issued from the composition to modify the street name using the
function capitalize
Here modify lift a function string => string to a function Employee => Employee. It works but it would be clearer
if we could zoom into the first character of a string with a Lens. However, we cannot write such a Lens because
Lenses require the field they are directed at to be mandatory. In our case the first character of a string is
optional as a string can be empty. So we need another abstraction that would be a sort of partial Lens, in
monocle-ts it is called an Optional.
import { Optional } from 'monocle-ts'
import { some, none } from 'fp-ts/Option'
const firstLetterOptional = new Optional<string, string>(
(s) => (s.length > 0 ? some(s[0]) : none),
(a) => (s) => (s.length > 0 ? a + s.substring(1) : s)
)
const firstLetter = company.compose(address).compose(street).compose(name).asOptional().compose(firstLetterOptional)
assert.deepStrictEqual(firstLetter.modify((s) => s.toUpperCase())(employee), employeeCapitalized)
Similarly to compose for lenses, compose for optionals takes two Optionals, one from A to B and another from
B to C and creates a third Optional from A to C. All Lenses can be seen as Optionals where the optional
element to zoom into is always present, hence composing an Optional and a Lens always produces an Optional.
TypeScript compatibility
The stable version is tested against TypeScript 3.5.2, but should run with TypeScript 2.8.0+ too
monocle-ts version
required typescript version
2.0.x+
3.5+
1.x+
2.8.0+
Note. If you are running < [email protected] you have to polyfill unknown.
You can use unknown-ts as a polyfill.
Documentation
API Reference
Experimental modules (version 2.3+)
Experimental modules (*) are published in order to get early feedback from the community.
The experimental modules are independent and backward-incompatible with stable ones.
(*) A feature tagged as Experimental is in a high state of flux, you're at risk of it changing without notice.
From [email protected]+ you can use the following experimental modules:
Iso
Lens
Prism
Optional
Traversal
At
Ix
which implement the same features contained in index.ts but are pipe-based instead of class-based.
The most recent commit recorded on gcanti/monocle-ts was 4.0 years ago, based on the GitHub push timestamp. The repository has 55 forks — one of the better signals of community interest.
How does gcanti/monocle-ts compare to other Frontend projects?
gcanti/monocle-ts is tracked by TopGit in the Frontend category, with 1.1k GitHub stars and written in TypeScript. Browse the Frontend topic page on TopGit to compare it against similar projects by stars and activity.
How many stars does gcanti/monocle-ts have?
gcanti/monocle-ts has 1.1k GitHub stars — refresh the page for the live number, or check github.com/gcanti/monocle-ts. TopGit mirrors GitHub's count but does not claim minute-by-minute accuracy.
What is gcanti/monocle-ts?
gcanti/monocle-ts (gcanti/monocle-ts) is a TypeScript project on GitHub. From the project's own README: Functional optics: a (partial) porting of Scala monocle
What language is gcanti/monocle-ts written in?
gcanti/monocle-ts is written primarily in TypeScript. GitHub's language field is based on the largest share of bytes in the default branch.
What topics is gcanti/monocle-ts associated with?
GitHub's repository topics for gcanti/monocle-ts: "functional-programming", "lenses", "optics", "typescript". TopGit's editorial category is Frontend.
Why is gcanti/monocle-ts categorized under Frontend?
TopGit places gcanti/monocle-ts in the Frontend category based on its GitHub topics and description (tagged: "functional-programming", "lenses", "optics"). Categories are assigned from real repository metadata, not editorial guesswork.
Read full README in the tab above.
Want a second opinion on monocle-ts?
Ask an AI that can read this page — one click and you get its take on monocle-ts.