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

decraft

v1.0.1

Published

Crafting TypeScript decorators easily

Downloads

3

Readme

Decraft - Smart TypeScript Decorators

Decraft is a user-friendly npm package that simplifies the creation of TypeScript decorators, significantly enhancing their power and readability. With Decraft, crafting intuitive and efficient decorators is a streamlined and enjoyable experience.

Starting from the end - How does you use the decorators?

Lets say that we created a decorator named @myDecorator. Those are all of the ways that we can use the same decorator.

We don't need to create a new decorator for each case, we can just use the same one for all of those cases.

@myDecorator

The classic way to use decorators. Just put it above a class method.

class MyClass {
	@myDecorator
	func(...params) {
		// code
	}
}

@myDecorator(...args)

We can also pass arguments to our decorator.

class MyClass {
	@myDecorator(...args)
	func(...params) {
		// code
	}
}

myDecorator(func, args)

Whooho, This is not a decorator!

You are right. But one of the issues with decorators is that we can't use them in some cases. For example for callbacks or functions outside of a class.

So with decraft, we allow you to use some hacks.

const func = myDecorator((...params) => {
	// code
}, ...args);

or another example:

function _func(...params) {
  // code
}

const func = myDecorator(_func, ...args)

myDecorator(...args)(func)

We allow also a different approach for readability.

const func = myDecorator(...args)((...params) => {
	// code
});

How to create a decorator?

It's pretty straightforward. We call the decorator function and pass as a generic a list of types that will be used for the decorator arguments.

Then we pass a callback which will be called each time we create the function (or the object it exists in).

It may also return a function, and this function will replace the function that the decorator is used on.

const  myDecorator  =  decorator<[...DecoratorParamsTypes]>((func, decoratorParams) => {
	// The function that we return here will replace the function that it's being used on
	return (...args:  any[]) => {
		// We're doing nothing, just calling the function
		return  func(...args);
	};

	// If we prefer, we can also return nothing and the decorator function will not replace the function that it's being used on
});

Our best practices

  1. We still have several type issues, so we recommend to check that you really got the params you wanted to get.
  2. Don't return a function with different type that the one you got. TypeScript not updating the function types right now when using this decorator.
  3. When using decorator as a function like this: myDecorator(func, ...args) or myDecorator(...args)(func) you should set a type to the variable it's assigned to. We return any right now as a type.