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

objektiv

v0.5.0

Published

Functional lenses for JavaScript

Downloads

12

Readme

Objektiv Build Status

Functional lenses in JavaScript

Objektiv (German) Lens, optics, objective

Basics

Objektiv.attr('field') Object attribute accessor. Objektiv.at(index) Array element accessor. lens.then(otherLens, ...) Lens composition can take many arguments. lens.traversal([filter]) Returns traversal, optionally filtered.

Regular lenses

Throw TypeError unless element has been found

const data = { array: [1, 2, 3] };
const firstOfSome = Objektiv.attr("array").at(0);

firstOfSome.get(data); //=> 1
firstOfSome.set(data, 10); //=> { array: [10, 2, 3] }
firstOfSome.map(data, x => x + 1); //=> { array: [2, 2, 3] }

Partial lenses

Skip missing element

const data = { array: [1, 2, 3] };
const firstOfMissing = Objektiv.partialAttr("missing").partialAt(0);

firstOfMissing.get(data); //=> undefined
firstOfMissing.set(data, 10); //=> { array: [1, 2, 3] }
firstOfMissing.map(data, x => x + 1); //=> { array: [1, 2, 3] }

Traversals

Traversals make working with series of data easy:

const data = { array: [{ x: 0, y: 9 }, { x: 1, y: 8 }, { x: 2, y: 7 }] };
const traversal = Objektiv.attr("array")
  .traversal()
  .attr("x");

traversal.get(data); //=> [0, 1, 2]
traversal.map(data, x => x + 1); //=> {array: [{x: 1, y:9}, {x: 2, y: 8}, {x: 3, y: 7}]}
traversal.set(data, 6); //=> {array: [{x: 6, y:9}, {x: 6, y: 8}, {x: 6, y: 7}]}

// Nested traversals
const data = {
  users: [{ id: 1, friends: ["Alice", "Bob"] }, { id: 2, friends: ["Sam"] }]
};
const traversal = Objektiv.attr("users")
  .traversal()
  .attr("friends")
  .traversal();

traversal.get(data); //=> [['Alice', 'Bob'], ['Sam']]
traversal.map(data, name => name.toUpperCase()); //=> {users: [{id: 1, friends: ['ALICE', 'BOB']}, {id: 2, friends: ['SAM']}]};

Filtered traversals

Array elements can be traversed by calling .traversal([predicate]) on a lens that references an array. You can pass predicate in traversal to filter elements of an array or chain .traversal().filter(predicate) function. Several .filter() functions can be chained one after another.

const data = { array: [{ x: 0, y: 9 }, { x: 1, y: 8 }, { x: 2, y: 7 }] };
const xGreaterThanOne = el => el.x > 1;
const traversal = Objektiv.attr("array")
  .traversal(xGreaterThanOne)
  .attr("x");
// Same as:
Objektiv.attr("array")
  .traversal()
  .filter(xGreaterThanOne)
  .attr("x");

traversal.get(data); //=> [2]
traversal.map(data, x => x + 1); //=> {array: [{x: 0, y:9}, {x: 1, y: 8}, {x: 3, y: 7}]}
traversal.set(data, 6); //=> {array: [{x: 0, y:9}, {x: 0, y: 8}, {x: 6, y: 7}]}

// Nested traversals
const data = {
  users: [{ id: 1, friends: ["Alice", "Bob"] }, { id: 2, friends: ["Sam"] }]
};
const hasTwoFriends = user => user.friends.length == 2;
const threeLetter = str => str.length === 3;

const traversal = Objektiv.attr("users")
  .traversal(hasTwoFriends)
  .attr("friends")
  .traversal(threeLetter);

traversal.get(data); //=> [['Bob']]
traversal.map(data, name => name.toUpperCase()); //=> {users: [{id: 1, friends: ['Alice', 'BOB']}, {id: 2, friends: ['Sam']}]};

Cursors

Objektiv also provides cursors which are lenses enclosed over data or root accessors. Cursor is a handy self-contained object to pass around. Cursors can be composed and traversed as regular lenses.

const data = {some: deep: 1};
const full = Objektiv.dataCursor(data);
const deepLens = Objektiv.attr('some').attr('deep');
const cursor = full.then(deepLens);

cursor.get() //=> 1
cursor.set(42)
cursor.get() //=> 42
full.get() //=> {some: deep: 42}
// All data are handled as immutable so original data is still:
data   //=> {some: deep: 1}

Cursor can notify about each change via callback:

const full = Objektiv.dataCursor(data, (newState, oldState) => {
  // ... deal with it
});

This way it can be used with ReactJS. Inside a component:

const that = this;
const full = Objektiv.dataCursor(this.state, function(state) {
  that.setState(state);
});
const childCursor = full.attr("child"); // ... and pass it to a child component

Outside of a root component:

const root = <Root ... />;
Objektiv.dataCursor({/** Initial data **/}, function (state) {
  root.setProps(state);
});
React.renderComponent(root, ...);