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

@piplup/utils

v2.8.1

Published

A collection of utility hooks and functions crafted to power up the packages within Piplup.

Downloads

561

Readme

@piplup/utils

npm bundle size

This package contains a set of utility functions and React hooks designed to simplify common tasks and enhance your development experience. Below is an overview of each helper function and hook, along with usage examples.

Installation

npm install @piplup/utils

or

yarn add @piplup/utils

or

pnpm add @piplup/utils

Table of Contents

Helpers

compact

Removes all falsey values from an array.

Example

import { compact } from '@piplup/utils';

const result = compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]

execSequentially

Executes a series of functions sequentially with the same arguments.

Example

import { execSequentially } from '@piplup/utils';

const log1 = (message: string) => console.log(`Log1: ${message}`);
const log2 = (message: string) => console.log(`Log2: ${message}`);

const executeLogs = execSequentially(log1, log2);
executeLogs('Hello');
// Output:
// Log1: Hello
// Log2: Hello

hasOwnProperty

Checks if an object has a property with the specified key.

Example

import { hasOwnProperty } from '@piplup/utils';

const obj = { key: 'value' };

if (hasOwnProperty(obj, 'key')) {
  console.log(obj.key); // Output: 'value'
}

setRef

Sets a React ref or callback ref to a specified value.

Example

import { setRef } from '@piplup/utils';

const ref = React.createRef<HTMLDivElement>();

setRef(ref, document.createElement('div'));

forkRef

Creates a function that forwards an instance to multiple refs.

Example

import * as React from 'react';
import { forkRef } from '@piplup/utils';

type TextInputProps = React.InputHTMLAttributes<HTMLInputElement>;

function TextInputComponent(props: TextInputProps, ref?: React.Ref<HTMLInputElement>) {
  const internalRef = React.useRef<HTMLInputElement>(null);

  // Combine internalRef and ref received from parent component
  const handleInputRef = React.useMemo(() => forkRef(internalRef, ref), []);

  return (
    <input
      placeholder="Type something here"
      {...props}
      ref={handleInputRef} // Pass the combined ref to the input
    />
  );
}

const TextFieldInput = React.forwardRef(TextInputComponent);

export default TextFieldInput;

Hooks

useEventCallback

Returns a stable callback function that always refers to the latest version of the passed function.

Example

import { useEventCallback } from '@piplup/utils';

const MyComponent = () => {
  const handleClick = useEventCallback(() => {
    console.log('Clicked!');
  });

  return <button onClick={handleClick}>Click me</button>;
};

useEventListener

A hook for listening to events in react.

Example

import { useEventListener } from '@piplup/utils';

const MyComponent = () => {
  useEventListener('resize', () => {
    console.log('Window resized');
  });

  return <div>Resize the window</div>;
};

useForkRef

Combines multiple refs into a single ref callback.

Example

import { useForkRef } from '@piplup/utils';

const MyComponent = () => {
  const ref1 = React.useRef<HTMLDivElement>(null);
  const ref2 = React.useRef<HTMLDivElement>(null);

  const forkedRef = useForkRef(ref1, ref2);

  return <div ref={forkedRef}>Combined refs</div>;
};

useIsomorphicEffect

A hook that uses useLayoutEffect on the client side and useEffect on the server side.

Example

import { useIsomorphicEffect } from '@piplup/utils';

const MyComponent = () => {
  useIsomorphicEffect(() => {
    console.log('Effect runs on the client side');
  }, []);

  return <div>Check the console</div>;
};

useLocalStorage

A hook for reading value stored in localStorage

Example

import { useLocalStorage, setItem, removeItem } from '@piplup/utils';

const MyComponent = () => {
  const [value, setValue] = useLocalStorage('myKey', 'initialValue');

  return (
    <div>
      <p>Stored value: {value}</p>
      <button onClick={() => setItem('myKey', 'newValue')}>Set New Value</button>
      <button onClick={() => removeItem('myKey')}>Remove Value</button>
    </div>
  );
};