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

vitrarius

v1.5.1

Published

Functional optics implemented by a mad scientist to solve an engineering problem.

Downloads

31

Readme

Vitrarius

vitrarius: a glassblower; one who works with glass

Vitrarius is a raw optics library for modern JavaScript. In the context of Redux, optics are useful as reducers. In general, optics are a utility to facilitate manipulating and interpretating nested data. They hail from strict, functional languages, so optics are declarative and respect immutability. There are several formal implementations of optics in JavaScript already; in fact, if you are interested in optics from a pure perspective, I recommend checking them out here. By comparison, Vitrarius is mathematically uncouth; it emphasizes flexibility, performance, and traceability over purity.

Out of the box, the optics of Vitrarius can handle Objects, Arrays, Maps, and primitive values. To operate on other types (like those of ImmutableJS), vitrarius makes use of a container protocol. This protocol is similar to the iterator protocol, and allows vitrarius to gracefully handle any type of data. Documentation for the container protocol is coming soon.

Vitrarius is not intended to replace more formal implementations, but rather to provide an idiomatic JavaScript alternative.

Fundamental Usage


Vitrarius is a single module exporting several pure functions.

import { view, pluck, inject, compose } from 'vitrarius'

Optics are executed using the view function once created.

let object = {};

// create an optic to add
// a name property to targets
let exampleOptic = inject('name', {
    first: 'Haskell',
    last: 'Curry',
});

// execute the optic using view
let person = view(exampleOptic, object);

console.log(person);
/* > { name: { first: 'Haskell', last: 'Curry' } } */

Some things worth noting:

// default optics treat objects as immutables...
console.log(object);
/* > { } */

// ...so shallow comparisons are safe!
console.log(person !== object);
/* > true */

// unchanged objects are preserved
console.log(person === view(exampleOptic, person));
/* > true */

Working with nested data is a matter of composing multiple optics into one.

let nestedObject = { person: { } };

// combine a pluck optic with our old optic
let nestedOptic = compose(pluck('person'), exampleOptic);

// execute the nested optic as normal
let nestedPerson = view(nestedOptic, nestedObject);

console.log(nestedPerson);
/* > { person: { name: { first: 'Haskell', last: 'Curry' } } } */

There are several built in optics. pluck peers into containers as demonstrated above. inject adds information to targets, while remove deletes information. There are also each and where optics for use on collections and ill-formatted data respectively.

The constructive power of Vitrarius comes from the ability to define custom optics using the compose, chain, and cycle optics. Vitrarius also supports a range of short-hands for working with otherwise cumbersome optics.

// most values in JavaScript can be interpreted as optics
let printName = compose('person', 'name', 'first', console.log);

view(printName, nestedPerson);
/* > 'Haskell' */

Advanced features include the infinite cycle, convenient parallel, and magic phantom optics.

// An optic which recursively freezes
// a value (without polluting the call stack).
// Deep clones and traversals can be 
// similarly defined.
let deepFreeze = cycle(Object.freeze, each);

// Take a function which accepts a value
// and performs operations on it- return 
// an optic which performs that function 
// immutably using a proxy. 
let asImmutable = fun => compose(phantom, fun); 

Examples and formal documentation coming soon! For now, refer to the knarly use case vitrarius was built for.