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

kdo

v1.8.1

Published

Kdo easily organizes and executes small functions or files, more better than plain functions, makes the code clear, easy to read and maintain. Kdo requires Node.js 7.6+ for async/await.

Downloads

121

Readme

Kdo

Kdo easily organizes and executes small functions or files (as "NodeJS Best Practices" recommended), more better than plain functions (why), makes the code clear, easy to read and maintain. Kdo requires Node.js 7.6+ for async/await.

Kdo itself spreads the code into multiple small functions and files too. Noapi (a light API framework for Node.js, easily define, I/O and test) is a good usage of Kdo, it is recommended to read its source code.

Installation

npm i kdo --save

Test:

git clone https://github.com/hiowenluke/kdo
cd kdo
npm install
npm test

Examples

See examples to learn more.

Why kdo

There are many benefits to using kdo + flow (object or files) instead of plain functions.

1. Easily pass data

Left (not good)

The code in main function is complicated, pass the same parameters to multiple tasks, return new parameters from the task, and pass the new parameters to the next tasks, these make the code look uncomfortable and not easy to read.

And, the main problem is that the main function performs data dis-assembly and transfer. When we change the input and output parameters of the sub-function, we have to modify the related code in the main function at the same time, which means that the sub-function are not completely encapsulated.

Right (good)

Each of the functions is very concise and the logic is clear and easy to understand.

Examples

2. Clear process control

Left (not good)

The code that handles flag === 1 is spread across two places, f2 and main. When we change the condition of flag === 1, we need to modify these two places. If the code is long, or these functions spread across many files, then we may miss something.

And, the code in main function will not elegant (yes, writing elegant code is one of my goals).

Right (good)

The condition flag ===1 is met in f2, so the f3 will be ignored. We do not need to add redundant code in main function.

Examples

3. Flexible return value

Sometimes, in order to make the code structure clear, we classify the flow code into multiple files in a multi-level directory.

In the task flow, after a file is processed, if a non-undefined value is returned, kdo will terminate the subsequent processing and return it to the main function.

We do not need to write additional complex code. Yes, if we use plain functions instead of kdo, there must be a lot of redundant code to handle the same logic.

Examples

4. Require directory as an object

We can use kdo to easily organize small files in multi-levels directories, require them as an object, and access the functions or any other properties of it.

Examples

5. Require directory as a flow object

When we spread our business flows across multiple files in multi-levels directories, managing these flows is a big problem. This problem can be easily solved with kdo.

Kdo can easily requires the entire directory (including sub-directories) as flow object. So, we can split the long code into multiple files in multi-levels directories, without any restrictions.

// require the directory (including sub-directories) as a flow object
const flow = kdo('./01-flow');

const main = async () => {
    // execute all functions in flow object one by one
    // according to the order of the directory name and file name.
    const result = await kdo.do(flow);
    return result;
};

module.exports = main;

If our directories and files are as following:

/f1
    /f12
        f121.js
        f122.js
    f11.js
    f13.js

/f2   
    f21.js
    f22.js
    
f3.js

Then the order of execution will be like following (cool, right?):

f11, f121, f122, f21, f22, f3

See below examples to learn more.

Further more, we can specify the order of execution in index.js under directory, like below:

// index.js

const options = {
    first: 'f3', // execute these functions at first
    last: ['f7', 'f4'], // execute these functions at last
    exclude: 'f5', // do not execute these functions
};

module.exports = kdo.flow(options);

See below examples to learn more.

6. Require directory as an independent module

Kdo can easily requires the entire directory (including sub-directories) as a independent module, the main function does not needs to care about the details of it, just call it.

// index.js

const kdo = require('kdo');

// Kdo.flow() returns a function which does the following:
//     1. Requires the current directory (including sub-directories) as a flow object
//     2. Executes all functions in the flow object one by one
module.exports = kdo.flow();

Then require it in main function, and execute it.

// Require the sub-directory as a function which wrapped by kdo.
// Means, the sub-directory is a fully independent module, the main
// function does not needs to care about the details of it, just call it.
const flow = require('./01-flow');

const fn = async () => {
    const args = {a1: 1, a2: 2, a3: 3};

    // Execute the flow function, get the result.
    const result = await flow(args);
    return result;
};

module.exports = fn;

See below examples to learn more.

Why small functions

Why we should split long code into small functions or files?

As "Node.js Best Practices" said:

Otherwise: When developers who code new features struggle to realize the impact of their change and fear to break other dependent components - deployments become slower and riskier. It's also considered harder to scale-out when all the business units are not separated.

MURDER rule

Simply put, this can leads to MURDER which is a good thing (the answer comes from stackOverflow, by John Dibling):

  • M - Maintainability. Smaller, simpler functions are easier to maintain.
  • U - Understandability. Simpler functions are easier to understand.
  • R - Reuseability. Encourages code reuse by moving common operations to a separate function.
  • D - Debugability. It's easier to debug simple functions than complex ones.
  • E - Extensibility. Code reuse and maintainability lead to functions that are easier to refactor in 6 months.
  • R - Regression. Reuse and modularization lead to more effective regression testing.

More good articles

License

MIT

Copyright (c) 2019, Owen Luke