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

@qiwi/decorator-utils

v4.5.10

Published

Universal decorator factories made from scratch

Downloads

169

Readme

decorator-utils

Universal decorator factories made from scratch

Maintainability Test Coverage

Install

yarn add @qiwi/decorator-utils

Notes

Usage

Method

import {constructDecorator} from '@qiwi/decorator-utils'

const decorator = constructDecorator((targetType, target, param) => {
  if (targetType === METHOD) {
    return value => param || 'qux'
  }
})

class Foo {
  @decorator()
  foo () { return 'bar' }
  @decorator('BAZ')
  baz () { return 'baz' }
}

Class

const decorator = constructDecorator((targetType, target) => {
  if (targetType === CLASS) {
    return class Bar extends target {
      constructor (name, age) {
        super(name)
        this.age = age
      }
    }
  }
})

@decorator()
class Foo {
  constructor (name) {
    this.name = name
  }
  foo () { return 'bar' }
}

Field & Param

import {createDecorator, FIELD, PARAM} from '@qiwi/decorator-utils'

const meta: any = {}
const decorator = constructDecorator(({
  propName,
  paramIndex,
  targetType,
  target,
  args: [param]
}: IDecoratorHandlerContext) => {
  if (targetType === PARAM) {
    if (propName && typeof paramIndex === 'number') {
      meta[propName] = meta[propName] || {}
      meta[propName][paramIndex] = target
    }
  }

  if (targetType === FIELD) {
    if (propName) {
      meta[propName] = param
    }
  }
})

class Foo {
  @decorator('arg')
  foo = 'bar'

  bar(one: any, @decorator() two: any) {
    return 'bar'
  }
}

/**
    Now `meta` is smth like:
    {
      foo: 'arg',
      bar: {
        1: Foo.prototype.bar,
      },
    }
*/

You may also apply the decorator to the class, but decorate its methods:

const decorator = constructDecorator((targetType, target) => {
  if (targetType === METHOD) {
    return () => {
      return target().toUpperCase()
    }
  }
})

@decorator()
class Foo {
  foo () { return 'bar' }
  baz () { return 'baz' }
}

Context

constructDecorator factory provides the handler access to the decorator context. This data describes the specifics of the decorated target, decorator arguments and so on.

type IDecoratorHandlerContext = {
  args: IDecoratorArgs
  kind: ITargetType | null        // targetType alias
  targetType: ITargetType | null
  target: ITarget
  proto: IProto
  ctor: Function
  name?: IPropName                // propName alias
  propName?: IPropName
  paramIndex?: IParamIndex
  descriptor?: IDescriptor
}

Options

You may set additional options to apply some decorator asserts: allowed target types, repeatable or not.

const plus = constructDecorator(
  ({ targetType, target, args: [param] }) => {
    return (value: number) => target(value) + param
  },
  {
    allowedTypes: METHOD, // string | string[]
    repeatable: true,
  },
)

class Foo {
  @plus(2)
  @plus(1)
  bar(v: number) {
    return v
  }
}

reflect-metadata

If the global Reflect provides getMetadataKeys, getMetadata and defineMetadata methods, they will be used to attach corresponding data to the decorated targets. To enable, just add reflect-metadata to your dependencies and load it.

import 'reflect-metadata'
import {constructDecorator} from '@qiwi/decorator-utils'
// ...

Refs

License

MIT