Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

optics-ts

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

optics-ts

Type-safe, ergonomic, polymorphic optics for TypeScript

  • 2.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
138K
decreased by-3.1%
Maintainers
1
Weekly downloads
 
Created

What is optics-ts?

The optics-ts package provides a powerful and type-safe way to work with immutable data structures in TypeScript. It allows you to create and compose lenses, prisms, and other optics to focus on specific parts of your data structures, making it easier to read, update, and transform data in a functional programming style.

What are optics-ts's main functionalities?

Lenses

Lenses allow you to focus on a specific part of a data structure. In this example, we create a lens to focus on the city within the user's address and demonstrate how to get and set the city value.

const { lens } = require('optics-ts');

const user = { name: 'Alice', address: { city: 'Wonderland' } };
const addressLens = lens(user => user.address, (user, address) => ({ ...user, address }));
const cityLens = addressLens.compose(lens(address => address.city, (address, city) => ({ ...address, city })));

const city = cityLens.get(user); // 'Wonderland'
const updatedUser = cityLens.set(user, 'New Wonderland'); // { name: 'Alice', address: { city: 'New Wonderland' } }

Prisms

Prisms are used to focus on a part of a data structure that may or may not be present. In this example, we create a prism to focus on the value inside an option type and demonstrate how to get and set the value.

const { prism } = require('optics-ts');

const some = value => ({ type: 'Some', value });
const none = { type: 'None' };

const optionPrism = prism(
  option => (option.type === 'Some' ? option.value : undefined),
  value => some(value)
);

const option = some(42);
const value = optionPrism.getOption(option); // 42
const updatedOption = optionPrism.set(option, 100); // { type: 'Some', value: 100 }

Traversal

Traversal allows you to focus on multiple parts of a data structure. In this example, we create a traversal to focus on even numbers in an array and demonstrate how to get all even numbers and modify them.

const { traversal } = require('optics-ts');

const numbers = [1, 2, 3, 4, 5];
const evenTraversal = traversal(
  numbers => numbers.filter(n => n % 2 === 0),
  (numbers, evens) => numbers.map(n => (n % 2 === 0 ? evens.shift() : n))
);

const evens = evenTraversal.getAll(numbers); // [2, 4]
const updatedNumbers = evenTraversal.modify(numbers, n => n * 2); // [1, 4, 3, 8, 5]

Other packages similar to optics-ts

Keywords

FAQs

Package last updated on 10 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc