Socket
Socket
Sign inDemoInstall

@yoomoney/pure-process

Package Overview
Dependencies
0
Maintainers
2
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @yoomoney/pure-process

Primitives for business logic structuring


Version published
Maintainers
2
Install size
25.1 kB
Created

Changelog

Source

0.2.0

  • this argument of the function created with pipeP or parallelMerge is propagated to argument functions
  • parallelMerge can be called without arguments
  • Update dev dependencies

Readme

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

FAQs

Last updated on 30 Jan 2022

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc