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

maju-decorators

v1.0.5

Published

Decorators utils created mostly by [core-decorators](https://www.npmjs.com/package/core-decorators)

Downloads

2

Readme

Maju Decorators

This is a decorators utils created mostly by core-decorators, just fixed some features and implements of decorate, time decorator. & Removes deprecated decorators, since core-decorators is now set to read-only.

Install

$ npm install maju-decorators

Documentation

Mostly the same with core-decorators

decorate

Just let decorate can be connected as list, and the decorator function will receive a context of following content.

  • target: decorator target prototype
  • key: decorator method name
  • fn: decorator function(from the last decorator or the origin method)
import { decorate, time } from 'maju-decorators'

function mylog1({ fn }) {
  return function(...args) {
    console.log('log1');
    return fn.apply(this, args);
  }
}

function mylog2({ fn }) {
  return function(...args) {
    console.log('log2');
    return fn.apply(this, args);
  }
}

class A {
  @decorate(mylog1)
  @decorate(mylog2)
  @time()
  run() {
    console.log('test');
  }
}

const a = new A();
a.run();
// log1
// log2
// test
// A.run-0: 0.135ms

time

add async support for time decorate, we can now use as following:

class A {
  @decorate(mylog1)
  @decorate(mylog2)
  @time()
  async run() {
    console.log('test start');
    await delay(2000);
    console.log('test end');
  }
}

be careful when decorating async function, since the decorator wraps the function below, once there's a sync decorator without return the fn, this will interrupt the async wrapper afterward. So always put time decorator right above the target method should be great.

function mylog2({ fn }) {
  return function(...args) {
    console.log('log2');
    // if the decorator before `time` did not return, if will break the async
    fn.apply(this, args);
  }
}

// `time` decorator in middle
class A {
  @decorate(mylog1)
  @time()
  @decorate(mylog2)
  async run() {
    console.log('test start');
    await delay(2000);
    console.log('test end');
  }
}

const a = new A();
a.run();
// log1
// log2
// test start
// A.run-0: 1.014ms
// test end

getter/setter

add to getter, setter of property, both like the method decorate decorator, will receive a context to use.

  • getter: must return a value
  • setter: must pass a newValue to original setter
import { getter } from 'maju-decorators'

function getterDeco1({ fn: originalGet }) {
  return function() {
    console.log('getter deco1', this)
    // getter must return a value
    return originalGet.call(this);
  }
}

function setterDeco1({ fn: originalSet }) {
  return function(newValue) {
    console.log('setter deco1', this);
    // you can edit the original newValue here
    newValue += 1
    // setter must pass newValue
    return originalSet.call(this, newValue);
  }
}

class A {
  @getter(getterDeco1)
  get foo() {
    console.log('get foo');
    return 'hello'
  }

  @setter(setterDeco1)
  set foo(value) {
    console.log('set foo', value);
  }
}

const a = new A();
console.log(a.foo);
// getter deco1 A {}
// get foo
// hello

a.foo = 0;
// setter deco1 A {}
// set foo 1

about the getter decorator, we can also edit the getter value before return

function getterDeco1({ fn: originalGet }) {
  return function() {
    console.log('getter deco1', this)
    // edit value before return
    const value = originalGet.call(this)
    return `${value} bar`
  }
}