New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

@forivall/decorator

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@forivall/decorator

Simple function decorator/wrapping util, in typescript

latest
Source
npmnpm
Version
1.2.0
Version published
Maintainers
1
Created
Source

@forivall/decorator

Simple function decorator/wrapping util, in typescript

build status dependency status dependency status coverage status npm version bundle size

Installation

npm install --save @forivall/decorator

Usage

Using the following wrapper function for our examples

function log<F extends (...args: any[]): any>(fn: F, label: string): F {
  return (...args: Parameters<F>) => {
    console.log(`enter ${label}`)
    const out: ReturnType<F> = fn(...args)
    console.log(`exit ${label}`)
    return out
  } as F
}

wrap()

import {wrap} from '@forivall/decorator'
const doStuff = wrap(log, () => console.log('stuff'), 'doStuff')
doStuff()
// Output:
// enter doStuff
// stuff
// exit doStuff

decorator()

Create a decorator function

import decorator from '@forivall/decorator'
const logDecorator = decorator(log)
class Stuff {
  @logDecorator('doStuff')
  doIt() {
    console.log('stuff')
  }
}
new Stuff().doIt()
// Output:
// enter doStuff
// stuff
// exit doStuff

proxied()

Add metadata to a function that proxies another. Useful for the proxy pattern.

class Stuff {
  doIt() {
    console.log('stuff')
  }
  somethingElse() {
    console.log('more stuff')
  }
}

import {proxied} from '@forivall/decorator'
class StuffWrapper {
  stuff = new Stuff()
}
interface StuffWrapper implements Stuff {}
Object.keys(Stuff.prototype).forEach((k) => {
  StuffWrapper.prototype[k] = proxied(Stuff, k, function(this: StuffWrapper, ...args: unknown[]) {
    return this.stuff[k](...args)
  })
})

Credits

Emily Marigold Klassen

License

ISC

FAQs

Package last updated on 03 May 2023

Did you know?

Socket

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.

Install

Related posts