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

cor-ts

v1.0.3

Published

CoR is a simple TypeScript implementation of chain of responsability design pattern

Downloads

6

Readme

CoR - Chain of Responsibility

CoR is a TypeScript library implementing the Chain of Responsibility pattern. This library allows you to define a series of business rules that process an object in sequence, where each rule can modify the object or validate it.

Installation

You can install the package via npm:

npm install cor-ts

Usage

Defining Business Rules

First, define your business rules by implementing the BusinessRule interface. Each rule should define getName, getId, validate, and process methods. Note that the process method is asynchronous and should return a Promise.

import { BusinessRule } from 'cor-ts';

export class ToUpperCaseBR implements BusinessRule {
    getName(): string {
        return 'ToUpperCaseBR';
    }

    getId(): string {
        return 'to_upper_case';
    }

    validate(object: any): boolean {
        return typeof object === 'string';
    }

    async process(object: any): Promise<object> {
        return object.toUpperCase();
    }
}

export class TrimBR implements BusinessRule {
    getName(): string {
        return 'TrimBR';
    }

    getId(): string {
        return 'trim';
    }

    validate(object: any): boolean {
        return typeof object === 'string';
    }

    async process(object: any): Promise<object> {
        return object.trim();
    }
}

Creating the Chain of Responsibility

Create an instance of the CoR class and add your business rules to it. Remember to use await when calling the execute method.

import { CoR, ToUpperCaseBR, TrimBR } from 'cor-ts';

async function run() {
    const chain = new CoR();
    chain.addRule(new ToUpperCaseBR());
    chain.addRule(new TrimBR());

    const input = '   hello world   ';
    const result = await chain.execute(input);
    console.log(result); // Output: 'HELLO WORLD'
}

run().catch(console.error);

Handling Validation Errors

If an object does not pass validation for a rule, the CoR class will throw an error:

import { CoR, ToUpperCaseBR, TrimBR } from 'cor-ts';

const chain = new CoR();
chain.addRule(new ToUpperCaseBR());
chain.addRule(new TrimBR());

try {
    const input = 12345;
    const result = await chain.execute(input);
} catch (error) {
    console.error(error.message); // Output: 'Validation failed for rule to_upper_case - ToUpperCaseBR'
}

Testing

The project uses Jest for testing. You can run the tests with the following command:

npm test

Example test cases are provided in src/CoR.test.ts.

import { CoR, ToUpperCaseBR, TrimBR } from 'cor-ts';

test('Chain of Responsibility with ToUpperCaseBR and TrimBR', async () => {
    const chain = new CoR();
    const toUpperCaseRule = new ToUpperCaseBR();
    const trimRule = new TrimBR();

    chain.addRule(toUpperCaseRule);
    chain.addRule(trimRule);

    let testString = '   hello world   ';
    const expectedString = 'HELLO WORLD';

    const result = await chain.execute(testString);

    expect(result).toBe(expectedString);
});

test('Chain of Responsibility throws error for non-string input', async () => {
    const chain = new CoR();
    const toUpperCaseRule = new ToUpperCaseBR();
    const name = toUpperCaseRule.getName();
    const id = toUpperCaseRule.getId();
    const trimRule = new TrimBR();

    chain.addRule(toUpperCaseRule);
    chain.addRule(trimRule);

    let testNumber = 12345;

    await expect(chain.execute(testNumber)).rejects.toThrowError(new Error(`Validation failed for rule ${id} - ${name}`));
});

License

This project is licensed under the MIT License.