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

@teamawesome/transform

v2.0.6

Published

Function composition

Downloads

309

Readme

Installation

npm i @teamawesome/transform

Usage

import Pipe from '@teamawesome/transform'

const p = new Pipe({
    first: () => {},
});
// Insert a function after another
p.after('first', 'afterFirst', () => {});

// Insert a function before another
p.before('first', 'beforeFirst', () => {});

// will run 'beforeFirst', 'first', 'afterFirst'.
const value = p.transform();

Class

Constructor

Can be given an iterable with entries, an object, or just a function.

new Pipe({
    first: () => {},
    second: () => {}
});

new Pipe(function main () {
});

new Pipe(new Map(...));

// Clone a pipe
new Pipe(otherPipe);

insert, before, after

const key = {};
const func = () => {};

// A function can be registered with any key.
pipe.insert('neighbour', key, func);

// You can also use the hook as the key. The following have the same result.
pipe.insert('neighbour', func, func);
pipe.insert('neighbour', func);

transform

import {Pipe, Arguments} from '@teamwesome/transform';

const pipe = new Pipe([
    (a, b) => a + b,
    (sum) => sum * 2, 
    (product) => product / 2, 
]);
// Each function is called with the result of the last function.
pipe.transform(2, 4); // 16

// If you want to return multiple args for the next hook, return a Arguments.
const pipe = new Pipe([
    (obj) => {
        const keys = Object.keys(obj);
        const values = Object.values(obj);
        return new Arguments(keys, values);
    },
    (keys, values) => {
        return new Arguments(
            keys.includes('hello'),
            values.includes('world')
        )
    }, 
]);

pipe.transform({
    hello: 'planet',
}); // [true, false]

Callback

A handy utility is pipe to easily generate callbacks. The following are roughly equivalant.

import {pipe} from '@teamwesome/transform';

// Signature is the same as Pipe constructor.
const callback = pipe();
const p = new Pipe();
const callback = p.transform.bind(pipe)

However, the callbacks from pipe are pipes themselves.

import {pipe} from '@teamwesome/transform';

const callback = pipe({
    main: () => {}
});
// Works
callback.insert('main', () => {});
callback.transform('sure', 'why not');

callback instanceof Pipe; // true
callback instanceof Function; // true
typeof callback === 'function' // true

All methods

export default class Pipe implements Iterable<[any, Hook]> {
    protected order: any[];
    protected readonly hooks: Map<any, Hook>;
    /**
     * Construct a new Pipe.
     * If entries is an Entries, each entry is added.
     * If entries is an object, each key value pair is added.
     * If entries is a function, it is added with itself as key.
     * @param entries
     */
    constructor(entries?: Entries | object | Hook);
    /**
     * Get a hook.
     */
    get(key: any): Hook | undefined;
    /**
     * Append or overwrite a hook.
     */
    set(key: any, value: Hook): this;
    /**
     * Check if a key is registered.
     */
    has(key: any): boolean;
    /**
     * Delete a hook.
     */
    delete(key: any): boolean;
    /**
     * Remove all hooks.
     */
    clear(): void;
    /**
     * Insert a hook.
     *
     * @param neighbour - The key to insert before or after.
     * @param key - The key to use.
     * @param value - The hook to insert.
     * @param insertAfter - true to insert after neighbour, false to insert before.
     */
    insert(neighbour: any, key: any, value?: Hook, insertAfter?: boolean): this;
    /**
     * Insert a hook before another.
     *
     * @param neighbour - The key to insert before or after.
     * @param key - The key to use.
     * @param value - The hook to insert.
     */
    before(neighbour: any, key: any, value?: Hook): this;
    /**
     * Insert a hook after another.
     *
     * @param neighbour - The key to insert before or after.
     * @param key - The key to use.
     * @param value - The hook to insert.
     */
    after(neighbour: any, key: any, value?: Hook): this;
    /**
     * Transform the pipe with the given args.
     *
     * @param args - Initial arguments call the first function with.
     */
    transform(...args: any[]): Promise<any> | any;
    /**
     * Execute the given callback once for each entry.
     */
    forEach(callback: ForEachCallback, thisArg?: any): void;
    /**
     * Get an iterable for the registered key-hook pairs.
     */
    [Symbol.iterator](): IterableIterator<[any, Hook]>;
    /**
     * Get an iterable for the registered key-hook pairs.
     */
    entries(): Iterable<[any, Hook]>;
    /**
     * Get an iterable for the registered keys.
     */
    keys(): Iterable<any>;
    /**
     * Get an iterable for the registered hooks.
     */
    values(): Iterable<Hook>;
    /**
     * Get the amount of entries.
     */
    get size(): number;
}

export interface Entries {
    entries(): IterableIterator<[any, Hook]>;
}
export declare type Hook = (...args: any[]) => any;
export declare type ForEachCallback = (key: any, hook: Hook, pipe: Pipe) => void;