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

@fp-ts/optic

Package Overview
Dependencies
Maintainers
3
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fp-ts/optic

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
135
decreased by-80.8%
Maintainers
3
Weekly downloads
 
Created
Source

A porting of zio-optics to TypeScript

npm downloads

Optics

flowchart TD
  Optic --> Iso
  Iso --> Lens
  Iso --> Prism
  Lens --> Optional
  Prism --> Optional
  Optional --> Getter
  Optional --> Setter

Example

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 TypeScript

const employee: Employee = {
  name: "john",
  company: {
    name: "awesome inc",
    address: {
      city: "london",
      street: {
        num: 23,
        name: "high street",
      },
    },
  },
};

const capitalize = (s: string): string =>
  s.substring(0, 1).toUpperCase() + s.substring(1);

const employeeCapitalized = {
  ...employee,
  company: {
    ...employee.company,
    address: {
      ...employee.company.address,
      street: {
        ...employee.company.address.street,
        name: capitalize(employee.company.address.street.name),
      },
    },
  },
};

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 @fp-ts/optic

import * as Optic from "@fp-ts/optic";

const _name = Optic.id<Employee>()
  .compose(Optic.key("company"))
  .compose(Optic.key("address"))
  .compose(Optic.key("street"))
  .compose(Optic.key("name"));

const capitalizeName = Optic.modify(_name)(capitalize);

expect(capitalizeName(employee)).toEqual(employeeCapitalized);

Installation

To install the alpha version:

npm install @fp-ts/optic

Documentation

License

The MIT License (MIT)

FAQs

Package last updated on 25 Nov 2022

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