open-index/open-npm huggingface dataset:
npm package: @gk-nzaytsev/fast-string-truncated-width
Description: A fast function for calculating where a string should be truncated, given an optional width limit and an ellipsis string.
Latest version: 1.1.0

# Fast String Truncated Width (Fork)

* This fork is only to allow commonJs imports

A fast function for calculating where a string should be truncated, given a width limit and an ellipsis string.

This is a low-level function that basically calculates the visual width of a string and the index at which it should be truncated once printed to the terminal, but taking into account an optional width limit and an optional ellipsis string, so that the string doesn't have to be processed multiple times to be truncated, and how long the part after the truncation point is doesn't cost us anything because we can just ignore it.

## Install

```sh
npm install --save fast-string-truncated-width
```

## Usage

```ts
import fastStringTruncatedWidth from 'fast-string-truncated-width';

// Retrieving the result for a string that fits within our width limit

const result1 = fastStringTruncatedWidth ( '\x1b[31mhello', { limit: Infinity, ellipsis: '…' } );

result1.truncated; // => false, the string fits within the width limit, it doesn't have to be truncated
result1.ellipsed; // => false, the ellipsis string doesn't need to be appended to the string
result1.width; // => 5, the visual width of the string once printed to the terminal
result1.index; // => 10, the end index at which the string should be sliced, equal to input.length in this case

// Retrieving the result for a string that doesn't fit within our width limit

const result2 = fastStringTruncatedWidth ( '\x1b[31mhello', { limit: 3, ellipsis: '…' } );

result2.truncated; // => true, the string doesn't fit within the width limit, it has to be truncated
result2.ellipsed; // => true, the ellipsis string should be appended to the string (this isn't always the case, for example if our limit is 0)
result2.width; // => 2, the visual width of the string once printed to the terminal (this doesn't account for the width of the ellipsis string itself)
result2.index; // => 7, the end index at which the string should be sliced to truncate it correctly

// Let's actually truncate a string
// If you are truncating a string that may contain ANSI escapes you'll probaly want to put a "reset" escape after the sliced portion of the input

const input = '\x1b[31mhello';
const options = { limit: 3, ellipsis: '…' };
const result3 = fastStringTruncatedWidth ( input, options );
const output = `${input.slice ( 0, result3.index )}${result3.ellipsed ? options.ellipsis : ''}`; // => '\x1b[31mhe…'
```

## License

MIT © Fabio Spampinato
npm package: @gk3000/use-previous
Description: React hook to get the previous value of a prop or state
Latest version: 1.0.2

# usePrevious custom hook for React 

A lightweight and simple custom React hook that helps you track the **previous value** of a state or prop in your functional components.

## Features

- **Track Previous State**: Easily keep track of the previous value of any piece of state or props in React.
- **No Re-renders**: Uses `useRef` to persist values across renders without triggering re-renders.
- **Simple API**: Provides an intuitive, easy-to-use API that seamlessly integrates with your existing React components.
- **Lightweight**: Minimal code that adds functionality without bloat.

## Installation

Install the hook via npm:

```bash
npm install @gk3000/use-previous
```
or via yarn:
```
yarn add @gk3000/use-previous
```

##U sage

### Basic Example
This hook is perfect for tracking the previous value of a piece of state or a prop in your component. Here's an example of how to use it in a functional component:
```
import React, { useState } from 'react';
import usePrevious from '@gk3000/use-previous';

function Counter() {
  const [count, setCount] = useState(0);
  
  // Track the previous value of the count state
  const previousCount = usePrevious(count);

  return (
    <div>
      <h1>Current Count: {count}</h1>
      <h2>Previous Count: {previousCount}</h2>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <button onClick={() => setCount(count - 1)}>Decrement</button>
    </div>
  );
}

export default Counter;
```
## Parameters
```
const previousValue = usePrevious(value);
```
`value` (`any`): The state or prop value you want to track.

## Returns
- `previousValue`: The previous value of the state or prop before the last update. On the initial render, it will return `undefined`.

### Advanced Example

This hook can also track previous values of more complex data types, such as objects or arrays:
```
import React, { useState } from 'react';
import usePrevious from '@gk3000/use-previous';

function UserInfo() {
  const [user, setUser] = useState({ name: 'John', age: 30 });

  const previousUser = usePrevious(user);

  return (
    <div>
      <h1>Current User: {user.name}, Age: {user.age}</h1>
      <h2>Previous User: {previousUser?.name}, Age: {previousUser?.age}</h2>
      <button onClick={() => setUser({ name: 'Jane', age: 25 })}>
        Change User
      </button>
    </div>
  );
}

export default UserInfo;
```

## Why Use usePrevious?

- Useful for Comparing State Changes: It helps in situations where you need to know the previous value to perform comparisons, animations, or transitions based on changes.
- Simple & Flexible: The hook is easy to use and works with any data type (primitives, arrays, objects).
- Optimized: Since usePrevious uses useRef, it stores the previous value without causing unnecessary re-renders, making it efficient for performance-sensitive components.

## License

This project is licensed under the MIT License.