Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

@yoomoney/pure-process

Package Overview
Dependencies
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yoomoney/pure-process

Primitives for business logic structuring

latest
Source
npmnpm
Version
0.2.0
Version published
Maintainers
2
Created
Source

Build Status Codecov License: MIT npm

Pure Process

Primitives for business logic structuring

Install

npm install @yoomoney/pure-process

Promise chain exits

JavaScript
const {exit} = require('@yoomoney/pure-process');

const EXIT_CODE = {
    needLogin: 'needLogin'
};

const getUser = async() => {
    const {user, latency} = await fetchUser();
    if (!user) {
        exit(EXIT_CODE.needLogin, {latency});
    }

    return user;
};

const getPosts = async(user) => ({
    posts: await fetchPosts(user.id)
});

const process = () => Promise.resolve()
    .then(getUser)
    .then(getPosts)
    .then(...exit());

const output = await process();

if (output.exitCode === EXIT_CODE.needLogin) {
    output.latency;
} else {
    output.exitCode; // main (default)
    output.posts;
}
TypeScript
const {createExit} = require('@yoomoney/pure-process');

enum ExitCode {
    NeedLogin = 'NEED_LOGIN',
    NoPosts = 'NO_POSTS'
};

const exit = createExit<{
    exitCode: ExitCode.NeedLogin;
    latency: number;
} | {
    exitCode: ExitCode.NoPosts
}>();

const getUser = async() => {
    const {user, latency} = await fetchUser();
    if (!user) {
        exit(ExitCode.NeedLogin, {latency});
    }

    return user;
};

const getPosts = async(user: User) => ({
    posts: await fetchPosts(user.id)
});

const process = () => Promise.resolve()
    .then(getUser)
    .then(getPosts)
    .then(...exit());

const output = await process();

if (output.exitCode === ExitCode.NeedLogin) {
    output.latency;
} else {
    output.exitCode; // main (default)
    output.posts;
}

Sequential and parallel execution

const {pipeP, parallelMerge} = require('@yoomoney/pure-process');

const stepA = () => ({
    resultA: 1
});

const stepB = ({resultA}) => ({
    resultB: resultA + 1
});

const stepC = ({resultA}) => ({
    resultC: resultA + 2
});

const process = () => Promise.resolve()
    .then(stepA)
    .then((data) => Promise.all([
            stepB(data),
            stepC(data)
        ]).then(([result1, result2]) => ({
            ...data,
            ...result1,
            ...result2
        }))
    );

// Equivalent
const process = pipeP(
    stepA,
    parallelMerge(
        stepB,
        stepC
    )
);

expect(await process()).toEqual({
    resultA: 1,
    resultB: 2,
    resultC: 3
});

this argument of the created function is propagated to argument functions.

Sequences with exits

const process = pipeP(
    stepA,
    stepB,
    exit.pipe
);

Skip errors

const {skipErrors} = require('@yoomoney/pure-process');

const stepA = async(data) => ({
    ...data,
    resultA: await fetchCritical()
});

const stepB = async(data) => ({
    ...data,
    resultB: await fetchNonCritical()
});

const process = (data) => Promise.resolve(data)
    .then(stepA)
    .then(skipErrors(stepB));

await process({
    // Optional
    logError: console.log
});

Keywords

typescript

FAQs

Package last updated on 30 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts