expand
expand
(length: number, char: Character) => (aString: string) => string
Expand a string to
lengthwithcharconst 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-'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! | // |==============================|