https://github.com/mfellner/typelens
Work in progress.
https://github.com/mfellner/typelens
Last synced: 3 months ago
JSON representation
Work in progress.
- Host: GitHub
- URL: https://github.com/mfellner/typelens
- Owner: mfellner
- License: mit
- Created: 2018-03-08T16:38:59.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-04-05T14:55:20.000Z (about 7 years ago)
- Last Synced: 2024-10-12T00:24:14.604Z (8 months ago)
- Language: TypeScript
- Homepage:
- Size: 86.9 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# TypeLens
[](travis-ci.org/mfellner/typelens)
[](https://codecov.io/gh/mfellner/typelens)
[](https://codebeat.co/projects/github-com-mfellner-typelens-master)
[](https://choosealicense.com/licenses/mit)**TypeLens** is a typesafe lensing library for JavaScript and TypeScript.
### Introduction
TypeLens is inspired by and borrows ideas from [Ramda](http://ramdajs.com/docs/#lens) and [Fantasy Land](https://github.com/fantasyland/fantasy-land). It is made to **safely access** properties in **unknown objects**.
### Usage
```typescript
import { lensKey } from 'typelens';// Fetch some unsafe data.
const data = await fetch(url).then(r => t.json());// Define a lens for a nested property.
const nameLens = lensKey(['user', 'profile', 'name']);// Read the value at the focused property using `view`.
// Accessing a value with a lens returns a `Maybe` type.
const nameMaybe = lensKey.view(data);// Safely resolve the value to a string. If the value is
// undefined, 'anonymous' used used as fallback.
const name: string = nameMaybe.getString('anonymous');
```### Articles on functional lenses
* [Basic Lensing](https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/basic-lensing)
* [Taking a Closer Look at Lenses](https://hackernoon.com/taking-a-closer-look-at-lenses-c0304851d54c)
* [Functional Lenses, How Do They Work](https://medium.com/@dtipson/functional-lenses-d1aba9e52254)