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

line-truncation

v1.3.9

Published

Line Truncation is a zero dependency tool that truncate text by user defined line number

Downloads

2,761

Readme

Line Truncation

Line Truncation is a zero dependency tool that truncate text by user defined line number.

The solution was inspired by line-clamp and shave. Line Truncation blends in their strengths, and being crafted to be faster and more capable.

Feature

  • Works with nest DOM element
  • Maintain original HTML element tags and styles
  • Lightening fast and capable
  • Custom ellipsis character
  • Callback provided

Installation

To install, simply run

npm install line-truncation

for front end

import { truncate } from 'line-truncation';

(browser support will be added in shortly)

There are some tool functions that come with this library, some of them provide you more control in whether you want to execute the truncation

  • truncateWhenNecessary(element, tries = 1, maxTries = 10) // execute truncation in safe manner, so that you don't have to check if there is line height. // it's going to use retry logic to wait until you have all the pre-requisition of truncation (when work with framework, you might want to put it in life cycle hook that the DOM render has initialized )

  • truncate(element, lines, ellipsis = ellipsisCharacter, callback = val => {}) // truncate function

  • getContentHeight(element) // get element's content height exclude padding,margin etc.

  • getLineHeight(element) // get element's line height, fallback to 1.2*fontSize when there is no line height

How to use

[line-truncation] works with any html tags that has text content as child such as div, p, span For example, you have some text in [p]

<p id="example">
  Lorem ipsum dolor sit, amet consectetur adipisicing elit. Nesciunt consequatur ipsum unde
  doloremque aliquid hic vitae iure necessitatibus, maiores repellendus, quos dignissimos Quis
  necessitatibus quos voluptas nesciunt facere mollitia cupiditate.
</p>

and then you could just simply get the element and call [LineTruncation.truncate()] with this element, your desired number of lines

import * as LineTruncation from 'line-truncation';
// import { lineTruncation } from 'line-truncation';also works;

var textElement = document.querySelector('p#example');
var lineHeight = text.clientHeight;

LineTruncation.lineTruncation(textElement, 2);

sometimes, above code won't work because the function might executes before DOM initialized you can do, or write your own logic

import * as LineTruncation from 'line-truncation';

var textElement = document.querySelector('p#example');
LineTruncation.truncateWhenNecessary(text);

Additionally, ou can also provide the ellipsis character you would like to see as third parameter, also a handler function you wish to execute when truncation finish as fourth parameter

var text = document.querySelector('p#example');
var lineHeight = text.clientHeight;
var handler = function(hasTruncated) {
  if(hasTruncated) (console.log('hello');
  }

LineTruncation.lineTruncation(text, 2, '✂', handler);

Contact me

If you have more idea about improving this package, feel free to make a contribution. And you could always reach out to me at [email protected]

License

The repository code is open-sourced software licensed under the MIT license.