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

legofy-js

v0.0.3

Published

legofy is a library for writing interactive procedures

Downloads

2

Readme

legofy · GitHub license npm version

Legofy is a JavaScript library for building modular extendable functions.

  • Declarative: Legofy let you use your modules in a very declarative way, it makes your code more predictable, simpler to understand, and easier to debug.
  • Focused: It handles both Sync and Async modules, so that you only focus on the steps and how to connect different modules.
  • Promise-like syntax You'll notice that we used an Promise-like syntax, so that writing it feels like home. Learn how to use legofy in your own project.

Getting Started

Use npm install legofy-js --save to install the package then use import or require to get started

import {legofy} from 'legofy-js';

// ...
var legofy = require('legofy-js')

// ...

Examples

We have several examples on the website. Here are some to get you started:

var lego = legofy(num => num * 2)
  .then(num=> num +1)
 
 lego(4) // 9
var lego = legofy((arg) => arg * 2)
    .then(arg => {
        return arg + 2
    })
    .then(arg => {
        return arg * 3
    })
    .then(res => {
        return res - 8
    })
    
lego(2) //10
var lego = legofy(() => { throw new Error("no way") })
    .catch(e => {
        e; //"no way"
        throw new Error("yes way")
    })
    .then(num => num * 2)
    .catch(e => {
        e; //"yes way"
        return 5
    })
    .then(res => {
        return 4 * res; // 4 * 5
    })
    
lego() //20

Those examples show how you can declare legos and start using them in a promise like syntax, they also show how you can attach a catch statement after some step so that you can handle the error and resume the rest of your steps

Legos will act sync when no async code found ie (Functions that returns a promise etc...)

var fun = legofy((num) => Promise.resolve(num * 2))

fun(2)
    .then((res) => {
        res; //4
    })
var fun = legofy(() => 2)
    .then(num => Promise.resolve(num + 1))
    .then(num => num * 2)
    .then(num => num + 3)
fun()
    .then((res) => {
        res; //9
    })
var fun = legofy(() => Promise.reject("no way"))
    .catch(e => {
        e; //"no way"
        return "lol"
    })

fun()
    .then((res) => {
        res; //"lol"
    })

Notice how Legos resolve into promises when async blocks found

var legoPiece = legofy((num) => num * 2)

var legoPiecePlus1 = legoPiece
    .then(num => num + 1)
    
var legoPiecePlus2 = legoPiece
    .then(num => num * 5)

legoPiece(3) //6
legoPiecePlus1(3) //7
legoPiecePlus2(3) //30
var legoPiece = legofy((num) => num * 2)

var legoPiecePlus = legoPiece
    .then(num => num + 1)

var legoPiecePlusPlus = legoPiecePlus
    .then(num => num + 1)

legoPiece(3) //6
legoPiecePlus(3) //7
legoPiecePlusPlus(3) //8
var legoPiece = legofy((num) => Promise.resolve(num * 2))

var legoPiecePlus = legoPiece
    .then(num => num + 1)

var legoPiecePlusPlus = legoPiecePlus
    .then(num => num + 1)

legoPiece(3)
    .then((res) => {
        res; //6
    })

legoPiecePlus(3)
    .then((res) => {
        res; //7
    })

legoPiecePlusPlus(3)
    .then((res) => {
        res; //8
    })
var legoPiece = legofy((num) => Promise.resolve(num * 2))

var legoPiecePlus1 = legoPiece
    .then(num => num + 1)

var legoPiecePlus2 = legoPiece
    .then(num => num * 5)

legoPiece(3)
    .then((res) => {
        res; //6
    })

legoPiecePlus1(3)
    .then((res) => {
        res; //7
    })

legoPiecePlus2(3)
    .then((res) => {
        res; //30
    })
var legoPiece = legofy(() => { throw new Error("lol") })

var legoPiecePlus1 = legoPiece
    .catch(e => {
        e; //"lol"
        return "lol1"
    })

var legoPiecePlus2 = legoPiece
    .catch(e => {
        e; //"lol"
        return "lol2"
    })

legoPiecePlus1() //"lol1"
legoPiecePlus2() //"lol2"
var legoPiece = legofy(() => { throw new Error("lol") })

var legoPiecePlus1 = legoPiece
    .catch(e => {
        e; //"lol"
        return 1
    })

var legoPiecePlus2 = legoPiece
    .catch(e => {
        e; //"lol"
        return 2
    })

try {
    legoPiece()
} catch (e) {
    e.message //"lol"
}

legoPiecePlus1() //1
legoPiecePlus2() //2
var legoPiece = legofy(() => { throw new Error("lol") })

var legoPiecePlus1 = legoPiece
    .catch(e => {
        e; //"lol"
        throw new Error("nope")
    })
    .catch(e => {
        e; //"nope"
        return 1
    })
    .then(num => num * 2)

var legoPiecePlus2 = legoPiece
    .catch(e => {
        e; //"lol"
        return 2
    })
    .then(() => { throw new Error("nope") })
    .catch(e => {
        e; //"nope"
    })

try {
    legoPiece()
} catch (e) {
    e.message //"lol"
}

legoPiecePlus1() //2
legoPiecePlus2() //undefined

Notice how legos can be easily extended and exchanged

var legoPiece = legofy((num) => num * 2)

var legoPiecePlus = legoPiece
    .then(num => Promise.resolve(num + 1))

//no async steps found so it will execute sync
legoPiece(3) //6

//an async step found so it will return a promise
legoPiecePlus(3)
    .then((res) => {
        res //7
    })
var legoPiece = legofy((num) => num * 2)

var legoPiecePlus = legoPiece
    .then(num => Promise.resolve(num + 1))

var legoPiecePlusPlus = legoPiecePlus
    .then(num => Promise.resolve(num + 1))

//no async steps found so it will execute sync
legoPiece(3) //6

//an async step found so it will return a promise
legoPiecePlus(3)
    .then((res) => {
        res //7
    })
    
//an async step found so it will return a promise
legoPiecePlusPlus(3)
    .then((res) => {
        res //8
    })

Notice how legos executes sync till an async step interrupts

var lego1 = legofy((num) => num * 2)

var lego2 = legofy((num) => num + 1)

var legoCon1 = lego1.concat(lego2)
var legoCon2 = lego2.concat(lego1)

lego1(2) //4
lego2(2) //3
legoCon1(2) //5
legoCon2(2) //6
var lego1 = legofy((num) => num * 2)

var lego2 = legofy((num) => Promise.resolve(num + 1))

var legoCon1 = lego1.concat(lego2)
var legoCon2 = lego2.concat(lego1)

lego1(2) //4

lego2(2).then((res) => {
    res; //3
})

legoCon1(2).then((res) => {
    res; //5
})

legoCon2(2).then((res) => {
    res; //6
})
var lego1 = legofy(() => { throw new Error("lol") })

var lego2 = legofy((num) => num + 1)
    .catch(e => {
        e; //"lol"
        return 5
    })
    .then(num => num * 2)

var legoCon1 = lego1.concat(lego2)
var legoCon2 = lego2.concat(lego1)

try {
    lego1()
} catch (e) {
    e.message; //"lol"
}

try {
    legoCon2()
} catch (e) {
    e.message; //"lol"
}

lego2(2); //6
legoCon1(2); //10
var lego1 = legofy(() => Promise.reject("no way"))

var lego2 = legofy((num) => num + 1)
    .catch(e => {
        e; //"no way"
        return 5
    })
    .then(num => num * 2)

var legoCon1 = lego1.concat(lego2)
var legoCon2 = lego2.concat(lego1)

lego2(2); //6

lego1()
    .catch((res) => {
        res; //"no way"
    })

legoCon1()
    .then((res) => {
        res; //10
    })

legoCon2()
    .catch((res) => {
        res; //"no way"
    })
  • legofy offers you a new pattern of writing modular reusable code that handles both sync and async callbacks to let you focus on building your logic and declare your steps.
  • Legos can be easily extended and you can literally build your app out of small little legos
  • Legos make your code very declarative beacause each legoPiece kind of represents a step, so it's alot easier for anyone to reason about your logic

License

legofy is MIT licensed.