diff --git a/README.md b/README.md index 87b5208f4..303f34e39 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,7 @@ * [Sum of array of numbers](#sum-of-array-of-numbers) * [Swap values of two variables](#swap-values-of-two-variables) * [Tail of list](#tail-of-list) +* [Truncate_a_string](#truncate_a_string) * [Unique values of array](#unique-values-of-array) * [URL parameters](#url-parameters) * [UUID generator](#uuid-generator) @@ -591,6 +592,17 @@ const tail = arr => arr.length > 1 ? arr.slice(1) : arr; // tail([1]) -> [1] ``` +### Truncate a String + +Determine if the string's `length` is greater than `num`. +Return the string truncated to the desired length, with `...` appended to the end or the original string. + +``` +const truncate = (str, num) => + str.length > num ? str.slice(0, num > 3 ? num-3 : num) + '...' : str; +// truncate('boomerang', 7) -> 'boom...' +``` + ### Unique values of array Use ES6 `Set` and the `...rest` operator to discard all duplicated values.