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

dataway

v3.1.0

Published

## Installation

Downloads

106

Readme

Dataway

Installation

npm install dataway
yarn add dataway

API documentation

Introduction

Dataway is a datastructure representing the four possible states of a remote datasource fetching result.

  • The remote datasource was NotAsked but eventually will be
  • The remote datasource is Loading
  • The remote datasource fetching has been a Success and a value was retrieved
  • The remote datasource fetching ended up in a Failure and some error information was collected

This aims to solve a classic data management issue often handled either through booleans or complex, unwanted and polluted states. With one entry point and only 4 strongly-typed states, remote data handling becomes much cleaner.

Dataway also provides a great api to manipulate, transform and aggregate Dataway values in a safe and optimistic way. This reduces bug and crash occurence while making your code simpler to read.

Example

Imagine that our application relies on a webservice that provides us with a list of elements, and that our job is to both store the number of elements in the application state for future usage and to display it.

Open in codesandbox.io

import { fold, notAsked, loading, failure, success } from "dataway";
import stateManager from "./statemanager";

const appElement = document.getElementById("list");
const loadButton = document.getElementById("load-button");

const view = state => {
  appElement.innerHTML = fold(
    () => "<p>Click on the load button</p>",
    () => "<p>Loading</p>",
    error => `<p>something wrong did happen : ${error}</p>`,
    success => `<ul>${success.map(post => `<li>${post.title}</li>`)}</ul>`
  )(state);
};

const setState = stateManager(view, notAsked);

loadButton.onclick = event => {
  setState(loading);
  setTimeout(
    () =>
      fetch("https://jsonplaceholder.typicode.com/posts")
        .then(response => {
          if (response.ok) {
            return response.json();
          } else {
            return Promise.reject(
              `Request rejected with status ${response.status}`
            );
          }
        })
        .then(json => setState(success(json)))
        .catch(error => setState(failure(error))),
    2000
  );
};

How to use

First you have to create some Dataway values using notAsked, loading, failure(error), success(value)

import { notAsked, failure, success } from 'dataway'

const foo = success('Mr Wilson');

const bar = failure('any suited error value');

const baz = notAsked;

test on runkit

Then we can use the provided map api to apply a function on any Success variance of Dataway, wrapping automatically the result in a new Success.

If the provided variance of Dataway is not a Success, it will be returned without change, and without executing the function.

As a developper it means you do not have to check for Dataway variance before applying a function to its Success value.

const { notAsked, failure, success, map } = require('dataway');

map(value => value.toUpperCase())(success('Mr Wilson'));
// => Success "MR WILSON"

map(value => value.toUpperCase())(failure('any suited error value'));
// => Failure "any suited error value"

map(value => value.toUpperCase())(notAsked);
// => NotAsked

test on runkit

Rewrapping the transformed value in a Success or returning the other variance untouched, allows to transform a Dataway value in multiple distinct step wihout risking runtime error due to unexistant values (null | undefined) while keeping the variance of Dataway intact.

const { notAsked, success, map } = require('dataway');

const upperCasedSuccess = map(value => value.toUpperCase())(success('Mr Wilson'));
map(value => value.split(' '))(upperCasedSuccess);
// => Success ['MR', 'WILSON']

const foo = map(value => value.toUpperCase())(notAsked);
map(value => value.split(' '))(foo);
// => NotAsked

test on runkit

To extract and use the Success value you must use the fold API. The following example illustrates how this forces you to consider the four different UIs each state implies.

const { success, failure, notAsked, loading, map, fold } = require('dataway');

// => Success ['MR', 'WILSON']
const render = dataway => fold(
  () => "<p>Click on the load button</p>",
  () => "<p>Loading</p>",
  error => `<p>something wrong did happen : ${error}</p>`,
  success => `<p>${success}</p>`
)(dataway);

render(success('Mr Wilson'));
// => <p>Mr Wilson</p>
render(failure('Ooops failed to fetch Mr Wilson data'));
// => <p>something wrong did happen : Ooops failed to fetch Mr Wilson data</p>
render(notAsked);
// => '<p>Click on the load button</p>'
render(loading);
// => '<p>Loading</p>'

test on runkit

This is really great to easily create consistent UIs.

TL;DR

Dataway offers a rich API to aggregate multiple "dataways" or to handle computation failure on your dataway values.

Dataway is written in typescript with thoughtful type description, enabling you to use it in a typescript environnement without hassle while keeping great type safety.

Dataway also offers compatibility with great libraries such as Ramda, and fp-ts

You can check and play with several examples

API docs