Giter VIP home page Giter VIP logo

node-truncate's Introduction

Truncate

CI Downloads available-for-advisory Twitter Follow Get help on Codementor Slack

Truncate text and keeps urls safe.

NPM

Install the module with: npm install truncate

❤️ Shameless plug

Usage

// Browser
String.truncate("1234 http://google.com hey :)", 2) === "12…"
// NodeJS
> truncate = require('truncate');
> truncate("1234 http://google.com hey :)", 4);
"1234…"
> truncate("1234 http://google.com hey :)", 4, {ellipsis:null}); // or ellipsis:''
"1234"
> truncate("1234 http://google.com hey :)", 6);
"1234 http://google.com…"
> truncate("1234 http://google.com hey :)", 100);
"1234 http://google.com hey :)"

Donate

I maintain this project in my free time, if it helped you please support my work via paypal or bitcoins, thanks a lot!

License

Copyright (c) 2014 Francois-Guillaume Ribreau Licensed under the MIT license.

node-truncate's People

Contributors

davisjam avatar fgribreau avatar greenkeeperio-bot avatar karlvr avatar mac-chaffee avatar mjlescano avatar stevenmhunt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

node-truncate's Issues

Support HTML suffix

It would be great to be able to append some HTML after the ellipsis. In this example, there's a link to continue reading.

image

Text cut short after URL

I am doing a call on a text where the result is cut off way too short and also the three dots at the end are missing:
The call
truncate(value, 330);

The text given to the function
<a href=\"https://www.example.org\">hello test</a>\n

The result of the call is
<a href=\"https://www.example.org/

Since the given string is only 65 characters long, I expect it to be not cut off at all.
Is there anything I can do to make it work or is it a bug?

Limit URL size when truncating

Hi there,

I noticed that there's no maximum to the size of a URL that could be at the end of a string someone intends to truncate:

URL_REGEX = /(((ftp|https?):\/\/)[\-\w@:%_\+.~#?,&\/\/=]+)|((mailto:)?[_.\w-]{1,300}@(.{1,300}\.)[a-zA-Z]{2,3})/g;

So for example:

const truncate = require('truncate')
input = "hello http://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
truncate(input, 7)
# 'hello http://aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

As far as I can tell, if someone fed a huge URL to any of the popular libraries that depend on truncate, nothing too bad would happen, but it also looks like they don't expect this URL-preserving-at-all-costs behavior:

I understand there's not really an RFC-defined maximum URL size, but could we implement one as a precaution? It could be something huge like 32k or something like 10 times the maxLength?

Alternatively, the URL-preserving feature could be moved into an option and disabled by default. Would be a breaking change for people other than those libraries I mentioned, but potentially safer.

Typing

Would you like to add types to the project? We whipped up these types and would be very happy if you included them in your library!

Also it looks like the keepImageTag option is documented but not implemented?

declare module 'truncate' {
	interface TruncateOptions {
		keepImageTag: boolean
		ellipsis?: string
	}

	export default function truncate(string: string, maxLength: number, options?: TruncateOptions): string
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.