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

@tsutil/lenses

v1.0.5

Published

TypeScript data lenses for extracting and validating data properties.

Downloads

4

Readme

@tsutil/lenses Build Status

TypeScript data lenses for extracting and validating data.

Overview

This package provides a get function that can run 1-n lense functions against a given data object and return a promise to an array of values containing the result of each lense.

const [a, b, c] = await get(
  lenseA,
  lenseB,
  lenseC,
  data
);

Each lense is a function that takes will receive the data object passed to get and return either a value or a Promise to a value.

NOTE: The implementation details of each lense is really up to the provider. They might throw exceptions, set defaults, etc. It is really up to the caller of get to be familiar with the particular lenses that are used. There are also a few lense implementations provided with this library.

Installation

npm install --save @tsutil/lenses

Usage

Lenses are functions that take a data object and return either a value or a Promise to a value.

type Lense<V> = (data: any) => V | Promise<V>;

Here's an example using the required lense factory provided with this lib:

// setup 2 lense factories for required fields
const string = required('string');
const number = required('number');

// create our actual lense functions
const nameLense = string('name');
const ageLense = number('age');

const validData = {
  name: 'jane doe',
  age: 23
};

// extract data from our fields
const [name, age]: [string, number] = await get(
  nameLense,
  ageLense,
  validData
);

Each lense will enforce the corresponding required field, so the following will throw an exception:

const invalidData = {
  name: 'jane doe'
};

await get(
  nameLense,
  ageLense, // this will throw an exception since no age field exists
  invalidData
)

Lenses can also return promises, and they will be resolved into the result of get.

// 2 lenses returning promises to propery values
const getName = (data: any) => Promise.resolve(data['name']);
const getAge = (data: any) => Promise.resolve(data['age']);

const data = {
  name: 'john doe',
  age: 23
};

const [name, age]: [string, number] = await get(
  getName,
  getAge,
  data
);