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 🙏

© 2026 – Pkg Stats / Ryan Hefner

segal-decorators

v0.3.1

Published

Bunch of useful decorators

Readme

Segal Decorators

:dizzy: Bunch of highly useful decorators, helps in writing a more concise code while improving readability.

Table of contents

Installation

Use npm to install the package

$ npm install segal-decorators --save 

Decorators

Memoize

Memoize the function by using the cached result. If hashFn is passed it is used to compute the hash key. default hash function is using JSON.stringify on the original function arguments.

Declaration

function Memoize(hashFn?: (...args: any[]) => string)

Alias

@memoize

Usage
import { Memoize } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Memoize()
  count(...args: any[]) {
    return ++this.counter;
  }
}

console.log(test.count(1)); // Outputs: 1
console.log(test.count(2)); // Outputs: 2
console.log(test.count(1)); // Outputs: 1
console.log(test.count(1, 2)); // Outputs: 3
console.log(test.count(2)); // Outputs: 2

Time Memoize

Time memoization, similar to Memoize, requires an additional parameter timeoutMs to determine the amount of time in milliseconds to cache the result.

Declaration

function TimeMemoize(timeoutMs: number, hashFn?: (...args: any[]) => string)

Alias

@timeMemoize

Usage
import { TimeMemoize } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @TimeMemoize(2000)
  count(...args: any[]) {
    return ++this.counter;
  }
}

console.log(test.count(1)); // Outputs: 1
console.log(test.count(2)); // Outputs: 2
console.log(test.count(1)); // Outputs: 1
console.log(test.count(2)); // Outputs: 2

setTimeout(() => {
  console.log(test.count(1)); // Outputs: 3
  console.log(test.count(2)); // Outputs: 4
}, 3000);

Lru Memoize

Least-recently-used memoization, similar to Memoize, requires an additional parameter maxRecords of the number of records to keep in cache.

Declaration

function LruMemoize(maxRecords: number, hashFn?: (...args: any[]) => string)

Alias

@lruMemoize

Usage
import { LruMemoize } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @LruMemoize(2)
  count(...args: any[]) {
    return ++this.counter;
  }
}

console.log(test.count(1)); // State of LRU Cache=[1], Outputs: 1 
console.log(test.count(2)); // State of LRU Cache=[2, 1], Outputs: 2
console.log(test.count(3)); // State of LRU Cache=[3, 2], Outputs: 3
console.log(test.count(2)); // State of LRU Cache=[2, 3], Outputs: 2
console.log(test.count(1)); // State of LRU Cache=[1, 2], Outputs: 4

Debounce

Postpone its execution until after timeMs have elapsed since the last time it was invoked.

Declaration

function Debounce(timeMs: number)

Alias

@debounce

Usage
import { Debounce } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Debounce(2000)
  call() {
    return ++this.counter;
  }
}

console.log(test.call()); // Does not output anything since it's called again with-in the 2 second time-frame.
console.log(test.call()); // Outputs: 1 (AFTER 2 SECONDS)

setTimeout(() => {
  console.log(test.call()); // Outputs: 1 (AFTER ADDITIONAL 2 SECONDS)
}, 3000)

Throttle

When invoked repeatedly, will only call the original function at most once per every timeMs.

Declaration

function Throttle(timeMs: number)

Alias

@throttle

Usage
import { Throttle } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Throttle(2000)
  call() {
    return ++this.counter;
  }
}

console.log(test.call()); // Outputs: 1
console.log(test.call()); // Does not output anything since it's called again with-in the 2 second time-frame.

setTimeout(() => {
  console.log(test.call()); // Outputs: 1 (AFTER ADDITIONAL 1 SECOND) 
}, 1000)

Async Retry

In-case an exception has been thrown, it will retry the action up to retries amount of times with a restMs delay between attempts.

Declaration

function AsyncRetry(retries: number, restMs: number = 100)

Alias

@asyncRetry

Usage
import { AsyncRetry } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @AsyncRetry(2)
  doSomething() {
    return requestServerSomething()
  }
}

// in-case of a failure will try twice before throwing an exception
test.doSomething().then((response) => {
  // Reach here if succeed in one of the 2 attempts
}).catch(() => {
  // Reach here if failed after 2 attempts
})

Bind

Binding methods to instance, making sure this is always set properly.

Declaration

function Bind()

Alias

@bind

Usage
import { bind } from 'segal-decorators';

class Foo {
  counter = 0;
  
  countWithout() {
    console.log(++this.counter)
  }
  
  @bind
  count() {
    console.log(++this.counter)
  }
}

const foo = new Foo();

setTimeout(foo.countWithout); // throws an error ('this' is undefined)
setTimeout(foo.count); // Outputs: 1

Once

Called exactly once. Repeated calls will have no effect, returning the value from the first call.

Declaration

function Once()

Alias

@once

Usage
import { Once } from 'segal-decorators';

class Foo {
  counter = 0;
  
  @Once()
  count(...args: string[]) {
    return ++this.counter;
  }
}

console.log(test.count()); // Outputs: 1
console.log(test.count()); // Outputs: 1
console.log(test.count(1)); // Outputs: 1
console.log(test.count(1, 2)); // Outputs: 1