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

react-middle-truncate

v1.0.3

Published

A React component for intelligently truncating text in the middle of the string.

Downloads

3,472

Readme

react-middle-truncate

github_forks_img github_stars_img

A React component for intelligently truncating text in the middle of the string with an ellipsis. You can see the component in action in this demo.

Installation

npm install react-middle-truncate --save

Usage

The truncation point is determined by taking into account the inherited font CSS properties of the element, and rendering the result to Canvas in order to dynamically calculate the width of the text. Should the width of the text not fit into the available space to render, truncation is applied. The result of which looks like this:

import MiddleTruncate from 'react-middle-truncate';

<MiddleTruncate
  text="Hello world this is a really long string"
  start={/Hello\sworld/}
  end={6} />

Result of Middle Truncation using the code sample above

You'll note from the code sample above we can use the start and end props to determine how much of the start and end of the string to preserve. These props can either be numeric, which indicates how many characters of the string from that direction to preserve, or you can provide a Regular Expression to dynamically match parts of the string.

In the case of using a Regular Expression for the end prop, remember to match from the end of the pattern using the $ flag.

Options

| Prop | Type | Description | Default | |----------------------|--------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------| | text | String | The input text to truncate if there isn't enough space to render the entire string. | '' | | ellipsis | String | The ellipsis to use when the text is truncated. | '...' | | start | Number RegExp | The number of characters or a Regular Expression match from the start of the text to preserve. | 0 | | end | Number RegExp | The number of characters or a Regular Expression match from the end of the text to preserve. | 0 | | smartCopy | Boolean String | Can be one of the following: false, 'partial' or 'all'. Allows the the full un-truncated text to be copied to the user's clipboard when they select and copy the truncated text. | 'all' | | onResizeDebounceMs | Number | A delay in milliseconds to debounce the window resize event listener. | 100 |

Why and when to middle truncate?

We have access to CSS text truncation with the text-overflow: ellipsis; which will truncate the text at the end of a string for us. So why do you need middle truncation, and when should you use it?

Ultimately it boils down to whether text at the end or in the middle of the string is more likely to differentiate the item.

In the case of a street address, the end of the string is likely to be less important in helping the user to differentiate items from one another, as most strings of that category are likely to end in road, street, avenue etc... So in that case truncating at the end of the string would be a better solution.

In the case of a connection string or a URL, the start and/or end of the string is likely to contain more valuable information to help the user differentiate items from one another, such as the protocol and sub domain parts at the start, and the port number at the end of the string are more likely to be different. In that case truncating in the middle of the string would be a better solution.

Contributing

Pull requests of any kind are welcome from the community. Please ensure you have read the guidelines for Contributing and this project's Code of Conduct before raising a pull request.

Maintainers

License

MIT License