truncateToNChars

truncateToNChars

  • (n: number) => (aString: string) => string
  1. Truncate a string to n chars, replacing the last three characters with '...'.

    const truncateTo5Chars = truncateToNChars(5)
    
    truncateTo5Chars('12345') // is '12345'
    truncateTo5Chars('123456') // is '12...'
    const truncateTo5Chars = truncateToNChars(5)
    
    truncateTo5Chars('12345') // is '12345'
    truncateTo5Chars('123456') // is '12...'
    
    const truncateTo5Chars = truncateToNChars(5)
    
    truncateTo5Chars('12345') // is '12345'
    truncateTo5Chars('123456') // is '12...'
    const truncateTo5Chars = truncateToNChars(5)
    
    truncateTo5Chars('12345') // is '12345'
    truncateTo5Chars('123456') // is '12...'
    
  2. Sometimes we want to limit the number of characters while indicating there's more. Below, we are coding a text notification for our phone. Let's make sure the notification has at most 50 characters.

    const longText =
      "Hi, my name is Liz Brown from Volley Fun.  We're hosting\n" +
      'a weekend coed tournament where proceeds go to our local\n' +
      'animal shelter.  First prize also gets $200.  Would you\n' +
      'or anyone you know be interested in signing up?'
    
    const truncateTo50Chars = truncateToNChars(50)
    
    const notification = truncateTo50Chars(longText)
    console.log(notification)
    const longText =
      "Hi, my name is Liz Brown from Volley Fun.  We're hosting\n" +
      'a weekend coed tournament where proceeds go to our local\n' +
      'animal shelter.  First prize also gets $200.  Would you\n' +
      'or anyone you know be interested in signing up?'
    
    const truncateTo50Chars = truncateToNChars(50)
    
    const notification = truncateTo50Chars(longText)
    console.log(notification)
    
    const longText =
      "Hi, my name is Liz Brown from Volley Fun.  We're hosting\n" +
      'a weekend coed tournament where proceeds go to our local\n' +
      'animal shelter.  First prize also gets $200.  Would you\n' +
      'or anyone you know be interested in signing up?'
    
    const truncateTo50Chars = truncateToNChars(50)
    
    const notification = truncateTo50Chars(longText)
    console.log(notification)
    const longText =
      "Hi, my name is Liz Brown from Volley Fun.  We're hosting\n" +
      'a weekend coed tournament where proceeds go to our local\n' +
      'animal shelter.  First prize also gets $200.  Would you\n' +
      'or anyone you know be interested in signing up?'
    
    const truncateTo50Chars = truncateToNChars(50)
    
    const notification = truncateTo50Chars(longText)
    console.log(notification)