
Product
Introducing Socket Firewall Enterprise: Flexible, Configurable Protection for Modern Package Ecosystems
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.
@qiwi/decorator-utils
Advanced tools
Universal decorator factories made from scratch
yarn add @qiwi/decorator-utils
@parentheses() and @plain.
Holy War thread: wycats/javascript-decorators/issues/23import {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' }
}
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' }
}
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' }
}
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
}
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
}
}
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'
// ...
FAQs
Universal decorator factories made from scratch
The npm package @qiwi/decorator-utils receives a total of 90 weekly downloads. As such, @qiwi/decorator-utils popularity was classified as not popular.
We found that @qiwi/decorator-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket Firewall Enterprise is now available with flexible deployment, configurable policies, and expanded language support.

Security News
Open source dashboard CNAPulse tracks CVE Numbering Authorities’ publishing activity, highlighting trends and transparency across the CVE ecosystem.

Product
Detect malware, unsafe data flows, and license issues in GitHub Actions with Socket’s new workflow scanning support.