This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
/// Truncates the string to length number of characters and | |
/// doesn't truncate within a word. | |
/// appends optional trailing string if longer | |
func truncate(_ length: Int, wordSeparator: String = " ", trailing: String = "…") -> String { | |
// only truncate if needed | |
if self.characters.count > length { | |
// count the trailing characters first | |
var cumulativeCharacters = trailing.characters.count |