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

javascript-decorators

v1.1.1

Published

Decorator Helpers

Downloads

713

Readme

javascript-decorators

Compatible with Node JS version >= 5

npm version Build Status Greenkeeper badge

Common helpers using es7 decorators.

You can see the Changelog for the version specific changes.

This project adheres to Semantic Versioning.

How to use

NPM

npm i --save javascript-decorators

And then to use a decorator just import it:

import { multiInherit } from 'javascript-decorators'

class Manager{
  render(){ return 42; }
  init(){ return 43; }
  t(){ return 44; }
};
class Employee{
  render(){ return 45; }
  mount(){ return 46; }
  t(){ return 47;}
};

@multiInherit( Manager, Employee )
class Person{
  t(){ return 3; }
};

const p = new Person();
p.render(); // 45
p.t(); // 3
p.init(); // 43

More examples here

Decorators

###Class decorators:

  • @abstract() : Make a class "abstract". It cannot be instantiated directly, but it can be inherrited.

  • @multiInherit( array of classes ) (alias: @multiExtend()) : Inherit all the methods of the passed classes. If two classes have method with the same name the last one is inheritted.

  • @partiallyInherit( class or array of classes, methodName or array of methodNames ) (alias: @partiallyExtend()) : Inherit only the specified methods.

###Method decorators:

  • @autobind: function calls always have this refer to the class instance

  • @compose( methods ) (alias: @rightCompose): Call each method passing the result of the previous as an argument. In mathematics (g º f)(x) = g(f(x)). The functions are executed from right-to-left with the class's method executed last. See more on examples.

  • @leftCompose( methods ): The left-to-right version of the previous.

  • @deprecated: Logs a warning message indicating that the method has been deprecated.

  • @before ( beforeFunc ): Executes the beforeFunc before the class" method.

  • @after ( afterFunc ): Executes the afterFunc after the class" method.

  • @valuesEqualToNumberOfArguments ( failSilent ): Executes the method only if the number of values pass to the function is equal with the number of function's parameters, otherwise it will through an exception unless failSilent = true.

  • @overridden() The method can be called from an instance of the base class, but cannot be called from an instance of a derived class. The derived class has to define its own version.

  • @forceoverridden() This is a more strict version of the previous. The method cannot be called from an instance of the base class or derived class. The derived class has to define its own version. Not particularly useful, mostly as a signal that the method has to be implemented on the subclass.

  • @once() : Executes the function only once. Repeat calls return the value of the first call.

  • @times(n) : Executes the function at most n times. Repeat calls return the value of the nth call. n: number

  • @timesCalled() : Attaches a timesCalled property to the function that indicates how many times the function has been called.

  • @trycatch( errorHandler ) : Wraps the function around a try/catch, requires a passed errorHandler function

  • @validateSchema( schema ) : Executes the function only if the schema is valid, schema: object

  • @acceptsObject( positions, failSilent ) : Executes the function only if the passed arg is an object positions: number|array (optional, default 0), failSilent: boolean(optional, default false)

  • @acceptsArray( positions, failSilent ) : Executes the function only if the passed arg is an array

  • @acceptsInteger( positions, failSilent ): Executes the function only if the passed arg is an integer

  • @acceptsNumber( positions, failSilent ): Executes the function only if the passed arg is a number

  • @acceptsBoolean( positions, failSilent ): Executes the function only if the passed arg is a boolean

  • @acceptsFunction( positions, failSilent ): Executes the function only if the passed arg is a function

  • @acceptsString( positions, failSilent ): Executes the function only if the passed arg is a string

  • @acceptsPromise( positions, failSilent ): Executes the function only if the passed arg is a promise

  • @immutable(): Makes a deepcopy of the passed arguments and executes the method with the copy to ensure that the initial parameters are not mutated

  • @doesNotMutate() : Executes the method only if it doesnt mutate the passed arguments. Useful when the class extends another class and/or calls methods from the parent.

  • @memoization() : Use the memoization technique to prevent expensive function calls.

  • @log(): Logs the passed values and the returned result.

  • @loglocalstorage() : Logs the size of the localstorage before and after the function call.

  • @donotlog() : Do not log messages, errors and warnings.

  • @donotlogmessages() : Do not log messages.

  • @donotlogwarnings() : Do not log warnings.

  • @donotlogerrors() : Do not log errors.

  • @timeout( ms ) (alias: @debounce( ms )) : Executes the function after the specified wait (default 300ms) ms: number

  • @defer() : Executes the function when the current call stack has cleared

  • @enumerable()

###Method & Property decorators:

  • @readony()

  • @nonconfigurable()

Property decorators:

  • @nonenumerable()

Contributing

Feel free to open issues, make suggestions or send PRs. This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code.

Contact:

Twitter: @avraamakis