arendjr / text-clipper

Fast and correct clip functions for HTML and plain text
MIT License
133 stars 13 forks source link

Clipped length is different from max length #4

Closed numscrl closed 5 years ago

numscrl commented 5 years ago

I am consistently seeing clipped text who length does not match the max length requested. For example,

var textClipper = require("text-clipper") var html = "<p>David</p>"; textClipper(html, 4, { indicator: "", html: true, breakWords: true} );

returns the clipped text "Dav". Tried with many other short-ish strings with consistent results. The clipped text is always one character less than requested.

Am I possibly misusing the API in some way?

arendjr commented 5 years ago

Thanks for the report! I think this is a genuine bug. Normally, it would make sense to have the clipped text be one character shorter, because space needs to be kept for the indicator, but seeing how you're not using any indicator, I agree the clipped text should just use that extra character.

I'm afraid currently it is hard-coded to reserve exactly one character, but I think it can easily be fixed by changing the initialization of numChars at lines 80 and 363. Be my guest to try it out, and if it works, I'd happily apply the PR!

arendjr commented 5 years ago

Sorry for the late response! This is now fixed in release 1.2.3!

numscrl commented 5 years ago

Thanks!


From: Arend van Beelen jr. notifications@github.com Sent: Wednesday, April 10, 2019 11:06 AM To: arendjr/text-clipper Cc: numscrl; Author Subject: Re: [arendjr/text-clipper] Clipped length is different from max length (#4)

Sorry for the late response! This is now fixed in release 1.2.3!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/arendjr/text-clipper/issues/4#issuecomment-481777527, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AeMpxv6npmzBf0zIImVUJk59QUXj3FbLks5vfhn8gaJpZM4XZrQT.