Skip to content

mfellner/typelens

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TypeLens

Travis Codecov codebeat license

TypeLens is a typesafe lensing library for JavaScript and TypeScript.

Introduction

TypeLens is inspired by and borrows ideas from Ramda and Fantasy Land. It is made to safely access properties in unknown objects.

Usage

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

About

Work in progress.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published