Simple ES6 Truncate method for character, word, and sentence limits Simple truncating method for characters, words, and sentences for a personal utility library I've been working on.

Improvements &amp; criticism welcomed! Especially around default param usage, as I've not utilized it much to date.



const truncate = ((
  value = '',
  int = 0,
  elipsis = '\u2026', /* ... */
  prune = (val, num, at) =&gt; val.split(at).splice(0, num).join(at)
) =&gt; {
  const sentences = (val = value, num = int, end = elipsis) =&gt; prune(val, '.', num) + end;
  const words = (val = value, num = int, end = elipsis) =&gt; prune(val, ' ', num) + end;
  const characters = (val = value, num = int, end = elipsis) =&gt; val.substring(0, num) + end;
  return {sentences, words, characters};
})();

console.clear();
console.log([
  truncate.sentences("This is a sentence. There are many like it but you won't see the 2nd one", 1, '.'),
  truncate.words("These are words but you won't see what's after this because it'll be hidden", 10),
  truncate.characters("This won't exceed 31 characters so we cutting off the ending", 31, '!')
]);
The parameters of sentences and words appear to be out of order when passed to prune(). Not sure why the parameters passed to prune are hard-coded at the example, though still passed to prune(). Since words does not pass a parameter for at, 10 is used, which is not the expected result, though can be remedied by defining the parameter at at words and sentences functions, given that each function call expects all three parameters, and at is not defined at any of the functions at the code at the question; instead num is set to int. 

Adjusted body of prune to include an immediately invoked arrow function to handle the resulting array of .split() where delimiter character ! is not contained in string by checking if the element at index 0 of array is equal to val, if true spread val to array else use existing array to chain .splice() to, then concatenate an empty string or at following .join() call. At which point the same function can be substituted for sentences, words and characters.



const truncate = ((
  value = '',
  int = 0,
  elipsis = '\u2026', /* ... */
  prune = (val, at, num) =&gt; 
            ((s, k = s[0] === val) =&gt; 
              `${(k ? [...val] : s)
                 .splice(0, num)
                 .join(k ? '' : at)}${k ? at : ''}`)
            (val.split(at))
) =&gt; {
  const sentences = (val = value, at, num = int, end = elipsis) =&gt; prune(val, at, num) + end;
  const words = (val = value, at, num = int, end = elipsis) =&gt; prune(val, at, num) + end;
  const characters = (val = value, at, num = int, end = elipsis) =&gt; prune(val, at, num) + end;
  return {sentences, words, characters};
})();

console.clear();
console.log([
  truncate.sentences("This is a sentence. There are many like it but you won't see the 2nd one", '.', 1),
  truncate.words("These are words but you won't see what's after this because it'll be hidden", ' ', 10),
  truncate.characters("This won't exceed 31 characters so we cutting off the ending", '!', 31)
]);




Alternatively, though not consistent with the sentences, words can be composed with at being set as a default parameter

const words = ({val = value, at = ' ', num = int, end = elipsis} = {}) =&gt; prune(val, at, num) + end;


which would be called with

truncate.words({val: "These are words but you won't see what's after this because it'll be hidden", num: 10})


providing the ability to call the function without explicitly setting and passing at as a parameter.

Substituting a single function for sentences, words and characters



const truncate = (
  value = '',
  at = '',
  num = 0,
  elipsis = '\u2026', /* ... */
) =&gt; ((val, k = val[0] === value) =&gt; 
       `${(k ? [...value] : val).splice(0, num)
          .join(k ? '' : at)}${k ? at : ''}${elipsis}`)
     (value.split(at));

console.clear();
console.log([
  truncate("This is a sentence. There are many like it but you won't see the 2nd one", '.', 1),
  truncate("These are words but you won't see what's after this because it'll be hidden", ' ', 10),
  truncate("This won't exceed 31 characters so we cutting off the ending", '!', 31)
]);