expand

expand

  • (length: number, char: Character) => (aString: string) => string
  1. Expand a string to length with char

    const expandTo3 = expand(3, '-')
    const expandTo5 = expand(5, '-')
    
    expandTo3('ab') // is 'ab-'
    expandTo3('abc') // is 'abc'
    
    expandTo5('ab') // is '-ab--'
    expandTo5('abc') // is '-abc-'
    const expandTo3 = expand(3, '-')
    const expandTo5 = expand(5, '-')
    
    expandTo3('ab') // is 'ab-'
    expandTo3('abc') // is 'abc'
    
    expandTo5('ab') // is '-ab--'
    expandTo5('abc') // is '-abc-'
    
    const expandTo3 = expand(3, '-')
    const expandTo5 = expand(5, '-')
    
    expandTo3('ab') // is 'ab-'
    expandTo3('abc') // is 'abc'
    
    expandTo5('ab') // is '-ab--'
    expandTo5('abc') // is '-abc-'
    const expandTo3 = expand(3, '-')
    const expandTo5 = expand(5, '-')
    
    expandTo3('ab') // is 'ab-'
    expandTo3('abc') // is 'abc'
    
    expandTo5('ab') // is '-ab--'
    expandTo5('abc') // is '-abc-'
    
  2. Sometimes we want a string to have a certain length. Below, we format a comment header stating that the code should not be modified.

    const headerWidth = 30
    const border = '='.repeat(headerWidth)
    
    const headerLines = [border, 'Autogenerated code', 'Do not modify!', border]
    
    const expandLine = expand(headerWidth, ' ')
    
    const header = headerLines.map(line => `// |${expandLine(line)}|`).join('\n')
    console.log(header)
    // prints
    // |==============================|
    // |      Autogenerated code      |
    // |        Do not modify!        |
    // |==============================|
    const headerWidth = 30
    const border = '='.repeat(headerWidth)
    
    const headerLines = [border, 'Autogenerated code', 'Do not modify!', border]
    
    const expandLine = expand(headerWidth, ' ')
    
    const header = headerLines.map(line => `// |${expandLine(line)}|`).join('\n')
    console.log(header)
    // prints
    // |==============================|
    // |      Autogenerated code      |
    // |        Do not modify!        |
    // |==============================|
    
    const headerWidth = 30
    const border = '='.repeat(headerWidth)
    
    const headerLines = [
      border,
      'Autogenerated code',
      'Do not modify!',
      border,
    ]
    
    const expandLine = expand(headerWidth, ' ')
    
    const header = headerLines
      .map(line => `// |${expandLine(line)}|`)
      .join('\n')
    console.log(header)
    // prints
    // |==============================|
    // |      Autogenerated code      |
    // |        Do not modify!        |
    // |==============================|
    const headerWidth = 30
    const border = '='.repeat(headerWidth)
    
    const headerLines = [
      border,
      'Autogenerated code',
      'Do not modify!',
      border,
    ]
    
    const expandLine = expand(headerWidth, ' ')
    
    const header = headerLines
      .map(line => `// |${expandLine(line)}|`)
      .join('\n')
    console.log(header)
    // prints
    // |==============================|
    // |      Autogenerated code      |
    // |        Do not modify!        |
    // |==============================|