npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@optiqs/projections

v1.4.0

Published

Projection library for optics

Downloads

17

Readme

Projections

Projections is a thin abstraction, inspired by reselect and monocle-ts, designed to solve a set of problems regarding the manipulation of large and nested objects such as Redux stores.

Use case

While Lenses solve problems regarding basic CRUD operations on data objects, Projections are read-only views into the data, allowing for transformation (map) and merging (combine) of projections and lenses.

Image of data transformation through Lenses and Projections

The main struggle that this project aspires to resolve is the verbosity required by Redux. Redux is simple and yet a robust solution for state management. When reducers are just anemic CRUD operations, it minimizes data indirection and increases maintainability of the app.

A summarized version of the Redux data flow

Lenses are viable alternative to catalyze the Redux by removing verbosity, allowing the write operations to the state to handled by a single reducer.

Furthermore, Lenses play a fair share part in the selectors space. The same lenses created for modification of state can be used to read data(get). However, lenses may not suffice all requirements related to the read operations.

Operations that combine different parts of the store(combine) and different modifications of its shape(map) are quite common. Besides, some transformation can be computational expensive and especially when run repeatedly, they can become bottlenecks. Projections solve both of these problems:

  1. They are memoized by default; and
  2. They can combine(merge) multiple projections or lenses into new projections

Documentation

Given the following sample type definitions:

// Given this structure
type A = {foo: string}
type B = {bar: number}
type C = {baz: Date}
type S = {a: A; b: B; c: C}

Creating Projections

You can create projections directly:

const p1: Projection<S, A> = Projection.fromProp<S>()('a')
const p2: Projection<S, B> = Projection.fromProp<S>()('b')

Or you can create them from a Lens (or anything else with a matching get method):

import {Lens, lens} from 'monocle-ts'
import {pipe} from 'fp-ts/function'

const lens1 = Lens.fromProp<S>()('a')
const lens2 = pipe(lens.id<S>(), lens.prop('b'))
const custom = {
  get: (s: S) => s.c
}

const p1: Projection<S, A> = Projection.from(lens1)
const p2: Projection<S, B> = Projection.from(lens2)
const p3: Projection<S, C> = Projection.from(custom)

Or you can create them directly from a function:

const p1: Projection<S, A> = Projection.of((s: S) => s.a)
const p2: Projection<S, B> = Projection.of((s: S) => s.b)

Mapping

You can directly map a projection to create a new projection

const pFoo: Projection<S, string> = p1.map(a => a.foo)

Or you can combine projections to map the resulting values together into a new projection

const combined: Projection<S, {c: string}> = p1.combine(p2, (a, b) => ({
  c: `${a.foo}-${b.bar}`
}))

Combining multiple projections

You can combine an arbitrary number of projections together to create new projections.

const combined = Projection.mapN([p1, p2, p3], (a, b, c) => ({
  c: `${a.foo}-${b.bar}-${c.baz}`
}))

// Or
const combined = p1.combine([p2, p3], (a, b, c) => ({
  c: `${a.foo}-${b.bar}-${c.baz}`
}))

If you prefer using the fp-ts-based pipe approach, you can also do the following (Note: using as const or Projection.createTuple is required in this approach due to type widening):

const combined = pipe(
  [p1, p2] as const,
  Projection.mapF((a, b) => ({c: `${a.foo}-${b.bar}`}))
)

// Or
const combined = pipe(
  Projection.createTuple(p1, p2),
  Projection.mapF((a, b) => ({c: `${a.foo}-${b.bar}`}))
)

Memoization

Enabled by default

Memoization is enabled by default. The default implementation has a cache size of 1, based on referential equality. This means that only the last result is stored.

For example, if you have the following projection:

import {S, A} from './my-types'

declare function expensiveComputation(s: S): A
declare const state: S

const p = Projection.from(expensiveComputation)
const a = p.get(state)
const b = p.get(state)
const c = p.get(state)
const d = p.get({...state})

Calling p.get(state) multiple times will not recalculate the value if state is the same reference, so a, b, and c above will all be the references to the exact same object.

However, while p.get({...state}) will evaluate to the same value, since the argument is a different reference, the value of d will be recalculated and not be the same reference as a, b, and c.

This implementation works similarly to how memoization in react works. This approach helps avoid creating an ever-growing cache of inputs to outputs.

With a custom memoize function

If the default approach is insufficient for your needs (for example if you really need to only calculate the projection map one time per input), you may provide a custom memoization function as well.

Projection.customMemoization(myMemoizationFunction)

Other memoization options

If that still doesn't suit your needs, you can always use another existing memoization solution directly (e.g. lodash's memoize function):

import {memoize} from 'lodash'
import {State, A} from './my-types'

declare function expensiveComputation(s: S): A

const p = Projection.from(memoize(expensiveComputation))