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

wenn

v0.2.0

Published

A simple but powerful utility function, inspired by Kotlin's when.

Downloads

7

Readme

npm version Build Status Coverage Status

wenn.js

A simple but powerful utility function, inspired by Kotlin's when.

Installation

npm install wenn.js --save

Basic Usage

const value = "Foo";  

const result = wenn(value,  
  Case("Foo").Then(0),  
  Case("Bar").Then(1)
);  

// result == 0
const value = "Foo";  

wenn(value,  
  Case("Foo").Then(() => console.log("Value is 'Foo'")),  
  Case("Bar").Then(() => console.log("Value is 'Bar'"))
);  
const value = "Test";  

const result = wenn(value,  
  Case("Foo").Then(0),  
  Case("Bar").Then(1),  
  Else(-1)
);  

// result == -1

If an Else case would be required but not found, there will be an error. You can always add Else(undefined).

const value = "Test";  

const result = wenn(value,  
  Case("Foo").Then(0),  
  Case("Bar").Then(1)
);  

// ERROR: No case matched, but also no ELSE case given. You can add Else(undefined) to your cases to prevent an error.

Usage in TypeScript

Infer the types to prevent errors while compiling.

const value: string = "Test";

const result: string = wenn(value,
  Case("Foo").Then("A"),
  Case("Bar").Then("B"),
  Else("?"));

wennChain usage

wennChain allows you to propagate a value through all cases, until it doesn't match anymore or there's a Break().

const value = 4;

const result = wennChain(value,
    Case(isNegative).Then(0),
    Break(),
    Case(isPositive).Then(x => x + 1),
    Case(always).Then(x => x * 4),
    Break(),
    Case(always).Then(x => x * 3)
);

// result === 20

wennElvis usage

wennElvis builds on top of wennChain. It's basically chained isntUndefined cases, with your given thens. It allows a string with dot notation or even function calls, to access nested properties. If a property wasn't found or a function call returns undefined, the function will safely return undefined.

const value = {
    data: {
        persons: [
            {
                name: "A",
                age: 18
            },
            {
                name: "B",
                age: 21
            },
            {
                name: "C",
                age: 46
            }
        ]
    }
};

const result = wennElvis(value,
    "data.persons",
    arr => arr.find(v => v.age > 100),
    "name"
);

// result === undefined, and no error

You can look at some examples in the test cases.

Comparison with Kotlin's when

Simple number switch else case

Kotlin's when

when (x) {  
	1 -> print("x == 1")  
	2 -> print("x == 2") 
	else -> {  // Note the block  
		print("x is neither 1 nor 2")  
	}  
}

wenn.js

wenn(x,  
  Case(1).Then(() => console.log("x == 1")),  
  Case(2).Then(() => console.log("x == 2")),  
  Else(() => console.log("x is neither 1 nor 2"))  
);

Multi cases

Kotlin's when

when (x) {
    0, 1 -> print("x == 0 or x == 1")
    else -> print("otherwise")
}

wenn.js

wenn(x,  
  Case(0, 1).Then(() => console.log("x == 0 or x == 1")),  
  Else(() => console.log("otherwise"))  
);

Arbitrary expressions

Kotlin's when

when (x) {
    parseInt(s) -> print("s encodes x")
    else -> print("s does not encode x")
}

wenn.js

wenn(x,  
  Case(isNumeric(s)).Then(() => console.log("s encodes x")),  
  Else(() => console.log("s does not encode x"))  
);

Negate, range, in array

Kotlin's when

when (x) {
    in 1..10 -> print("x is in the range")
    in validNumbers -> print("x is valid")
    !in 10..20 -> print("x is outside the range")
    else -> print("none of the above")
}

wenn.js

wenn(x,  
  Case(inRange(1, 10)).Then(() => console.log("x is in the range")),
  Case(inArray(validNumbers)).Then(() => console.log("x is valid")),
  Case(not(inRange(10, 20))).Then(() => console.log("x is outside the range")),  
  Else(() => console.log("none of the above"))  
);

Functions of objects as cases

Kotlin's when

when {
    x.isOdd() -> print("x is odd")
    x.isEven() -> print("x is even")
    else -> print("x is funny")
}

wenn.js

wenn(true,
    Case(x.isOdd()).Then("x is odd"),
    Case(x.isEven()).Then("x is even"),
    Else("x is funny")
);

Test

npm run test

Special Thanks

Special thanks to @MakroCow for helping out on the syntax and beta testing.

License

MIT