panzerdp / voca

The ultimate JavaScript string library
https://vocajs.pages.dev
MIT License
3.6k stars 137 forks source link

issue w/ prune #45

Closed mattiaerre closed 4 years ago

mattiaerre commented 4 years ago

Welcome to Voca's GitHub repo!

Expected behavior :smile_cat:

expect(v.prune('YATTA, ', 6, '')).toEqual('YATTA,');

Actual behavior :crying_cat_face:

expect(v.prune('YATTA, ', 6, '')).toEqual('YATTA'); // the string is missing the `,` at the end

Steps to reproduce :construction_worker:

https://github.com/mattiaerre/china-musk/blob/master/src/store/prune.test.js

Technical details: :wrench:

Browser/OS type: macOS Mojave Node version: v10

thanks so much, -Mattia

panzerdp commented 4 years ago

Prune doesn't allow having non-word characters (like punctuation characters) after the last word. This increases the readability of the truncated phrase.

For example, this is better in sense of truncation:

v.prune('Good day. Little Red Riding Hood', 16, '...');
// => 'Good day...'

Compared to:

v.prune('Good day. Little Red Riding Hood', 16, '...');
// => 'Good day. ...'
mattiaerre commented 4 years ago

thanks @panzerdp for the quick answer, really appreciated; my point is that this is not always the case as this is what's happening now:

expect(v.prune('YATTA,', 6, '')).toEqual('YATTA,');
expect(v.prune('YATTA, ', 6, '')).toEqual('YATTA');

the 1st string w/o a final empty space returns the comma, the second one w/ space doesn't; using your example:

  expect(v.prune('Good day. Little Red Riding Hood,', 12, '')).toEqual(
    'Good day'
  );
  expect(v.prune('Good day. Little Red Riding Hood,', 33, '')).toEqual(
    'Good day. Little Red Riding Hood,'
  );

the 1st string does not return the punctuation. thanks again, -Mattia

panzerdp commented 4 years ago

Prune is applied (including the removal of non-word chars) only when the string length is bigger than prune length. Otherwise the string is not modified, because there's nothing to remove.

mattiaerre commented 4 years ago

oh I see @panzerdp although I still think that

v.prune('YATTA, ', 6, '')

should return

YATTA, w/ the comma

and not

YATTA