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

intention

v2.3.0

Published

Create truly pure functional runtime envs

Downloads

2

Readme

intention

The road to functional programming is paved with pure intentions

Create truly pure functional runtime environments

The goals

  • Separate pure functions from impure functions in a clear, composable manner, using ES2015.
  • Create easily testable logics, even impure ones
  • Easy inter-op with "regular" JS

Installation

npm i -S intention

##Usage

// index.js
const {
  intent, // the most basic atom for `intention`
  impure,
  interpret,
} = require('intention');
const reality = require('./reality');

// all this function does is to create an intention.
// nothing actually gets executed here, this is truly pure.
const request = options => intent('write:net', { options });
const log = (...args) => intent('write:log', { args });

const main = impure(function* (url) {
  // yielding from an impure function
  // ACTUALLY executes the reality's interpretation of the 'write:net' intent, see below
  const body = yield request(url);
  yield log(body);
});

// calling an impure function simply returns another intent, though
const mainIntent = main('http://example.com');

// the `interpret` function converts an intent (intention) into a Promise (action),
// according to the reality's interpretation of the intent's type
interpret(mainIntent, reality)
  .then(() => console.log('DONE'))
  .catch(() => console.error('Boo!'));

// reality.js
const request = require('request');

// A reality object must handle every type of intent the program uses.
// the handler receives 3 parameters:
// - the object passed as the intent's second parameter
// - a resolver function that marks the intent as successful with an optional value
// - a rejecter function that marks the intent as failed with an optional value
// You might notice that we don't explicitly handle any intent type which starts with
// the `impure:` prefix. These are reserved for internal usage.
module.exports = {
  'write:net': ({ options }, resolve, reject) => request(options, (err, resp, body) => {
    if (err) return reject(err);
    if (resp.statusCode >= 400) return reject(resp);
    return resolve(body);
  }),
  'write:log': ({ args }, resolve) => resolve(console.log(...args)),
};

API

require('intention')

Requiring intention returns a functional environment, with the API below.

env.intent(intentType, [intentParameters])

This is the most basic part of intention. Every intent has a type, and an optional parameters object. Intentions are immutable, with a type property and a values property, corresponding to this function's parameters. Every intent type should be handled explicitly as part of a reality object (again, below).

env.isIntent(intent)

This function simply returns true if intent is an intent belonging to this env.

env.impure(generatorFunction)

This function accepts a generator function that can yield intent objects, and get back their resolved values. It returns a function, that when called, does nothing but return an intent object of type impure:call.

env.interpret(intent, reality)

This function converts an intent (which is a symbol for an intent) into a Promise (which is a symbol for an action), via the reality parameter's interpretation of the intent. Should the intent type not be handled by the reality, this function rejects immediately.

env.concurrent(intents)

This function returns an intent of the impure:concurrent type, which has a default interpretation of interpreting all intents in its intents parameter, according to the same reality that interprets itself, and resolves with an array of the return values in the same order as their respective intents. Basically, concurrent is to intent objects, as Promise.all is to Promise objects.

env.firstOf(intents)

This function returns an intent of the impure:firstOf type, which has a default interpretation of interpreting all intents in its intents parameter, according to the same reality that interprets itself, and resolves with the first resolved value of any of the intents. Basically, firstOf is to intent objects, as Promise.race is to Promise objects.

The reality object

The reality object passed to the interpret function is not magic, but a plain JS object. For every type of intent your env uses, you must include it as a property of the reality, with a value that looks like so: (intentParams, resolve, reject) => intentParams.shouldWork ? resolve(10) : reject(new Error('Meow')) Basically, use resolve to mark (with a possible value) a successful side-effect, and reject to mark a failed one - just like with a Promise.

Usage in testing

It is highly advised to test your entire program's logic with different reality objects to simulate as many possible scenarios as you feel appropriate, while testing your actual reality object separately, using Promises.