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 🙏

© 2025 – Pkg Stats / Ryan Hefner

fn-ref

v1.3.0

Published

utility to handle dynamic functions with refs

Downloads

12

Readme

Utility Functions: ref and fnRef

This utility provides two powerful functions, ref and fnRef, designed for managing state and function references in JavaScript and TypeScript. They are especially useful in scenarios where dynamic updates to values or callbacks are needed while maintaining clean and efficient code.


Function: fnRef

Purpose

The fnRef function manages a reference to a function, allowing dynamic updates to the function logic while maintaining a consistent interface for callers. This is particularly useful for handling callbacks that need to be updated at runtime.

Syntax

function fnRef<T extends (...args: any[]) => any>(
  fn: T
): T & { set: (fn: T) => void };

Parameters

  • fn (T): The initial function to store in the reference.

Returns

A function with:

  • The same signature as the input function.
  • An additional set method to update the stored function.

Use Cases

  1. Dynamic Callbacks: Use fnRef to update callbacks without requiring the consuming code to rebind or reinitialize the callback.

  2. Middleware-like Functionality: Dynamically inject or change behavior at runtime without altering the caller.

  3. Decoupling Logic: Separate the function definition from its execution context, enabling modular and reusable designs.

Example

const logRef = fnRef((message: string) => {
  console.log(`Initial log: \${message}`);
});

// Call the function
logRef("Hello, world!"); // Output: Initial log: Hello, world!

// Update the function logic
logRef.set((message: string) => {
  console.log(`Updated log: \${message}`);
});

// Call the updated function
logRef("Hello again!"); // Output: Updated log: Hello again!

Function: ref

Purpose

The ref function acts as a simple reference holder for any value. It provides a getter and setter interface that allows for dynamically managing a value during runtime.

Syntax

function ref<T>(value: T | null | undefined): (newValue?: T | null) => T | void;

Parameters

  • value (T | null | undefined): The initial value to store in the reference.

Returns

A function that:

  • Returns the current value when called without arguments.
  • Updates the stored value when called with a new value.

Use Cases

  1. Dynamic State Management: Use ref to hold a value that needs to be dynamically updated or accessed during runtime.

  2. Lightweight Alternative to React’s useRef: For non-reactive contexts, ref provides a similar utility without requiring React.

  3. Encapsulation: Hide internal state management logic while exposing a clean API for getting or setting values.

Example

const valueRef = ref<number>(42);

// Get the current value
console.log(valueRef()); // Output: 42

// Set a new value
valueRef(100);

// Get the updated value
console.log(valueRef()); // Output: 100

Why Use These Utilities?

Benefits of ref:

  • Simple, lightweight state holder.
  • Decouples state management logic from its consumers.
  • Works with any data type.

Benefits of fnRef:

  • Allows dynamic updates to function logic while keeping a stable reference.
  • Avoids boilerplate for reassigning functions.
  • Ensures type safety with full TypeScript support.

Installation and Usage

Adding to Your Project

Since these are custom utilities, you can copy them directly into your project or include them as part of a shared utility library.

TypeScript Compatibility

Both functions are fully typed, ensuring type safety and autocompletion in TypeScript projects.


Feel free to explore these functions and incorporate them into your JavaScript or TypeScript projects for enhanced flexibility and cleaner code management!