prune
prune
(pruneArg: Character | Character[]) => (aString: string) => string
Prune characters off the ends of a string
const pruneDashes = prune('-') pruneDashes('--abc--') // is 'abc'
const pruneDashes = prune('-') pruneDashes('--abc--') // is 'abc'
const pruneDashes = prune('-') pruneDashes('--abc--') // is 'abc'
const pruneDashes = prune('-') pruneDashes('--abc--') // is 'abc'
Sometimes we want to remove characters from the ends of strings. Below, we run a radio station and get ad scripts from our partner. Unfortunately they are poorly formatted, so let's clean up what we can.
const adScript = `The Avett Brothers are playing tonight! Come see them \n\n \tlive at Red Rocks. \n\n\n\n \tThe first caller who names the lead singer's debut\t \n\n solo album gets 2 free tickets.` const pruneWhitespace = prune([' ', '\t']) const removeStrayNewlines = replaceAllMatches('\n', '') const format = compose([ split('\n\n'), mapValues(removeStrayNewlines), mapValues(pruneWhitespace), joinValues('\n'), ]) const formattedScript = format(adScript) console.log(formattedScript) // prints // The Avett Brothers are playing tonight! Come see them // live at Red Rocks. // // The first caller who names the lead singer's debut // solo album gets 2 free tickets.
const adScript = `The Avett Brothers are playing tonight! Come see them \n\n \tlive at Red Rocks. \n\n\n\n \tThe first caller who names the lead singer's debut\t \n\n solo album gets 2 free tickets.` const pruneWhitespace = prune([' ', '\t']) const removeStrayNewlines = replaceAllMatches('\n', '') const format = compose([ split('\n\n'), mapValues(removeStrayNewlines), mapValues(pruneWhitespace), joinValues('\n'), ]) const formattedScript = format(adScript) console.log(formattedScript) // prints // The Avett Brothers are playing tonight! Come see them // live at Red Rocks. // // The first caller who names the lead singer's debut // solo album gets 2 free tickets.
const adScript = `The Avett Brothers are playing tonight! Come see them \n\n \tlive at Red Rocks. \n\n\n\n \tThe first caller who names the lead singer's debut\t \n\n solo album gets 2 free tickets.` const pruneWhitespace = prune([' ', '\t']) const removeStrayNewlines = replaceAllMatches( '\n', '' ) const format = compose([ split('\n\n'), mapValues(removeStrayNewlines), mapValues(pruneWhitespace), joinValues('\n'), ]) const formattedScript = format(adScript) console.log(formattedScript) // prints // The Avett Brothers are playing tonight! Come see them // live at Red Rocks. // // The first caller who names the lead singer's debut // solo album gets 2 free tickets.
const adScript = `The Avett Brothers are playing tonight! Come see them \n\n \tlive at Red Rocks. \n\n\n\n \tThe first caller who names the lead singer's debut\t \n\n solo album gets 2 free tickets.` const pruneWhitespace = prune([' ', '\t']) const removeStrayNewlines = replaceAllMatches( '\n', '' ) const format = compose([ split('\n\n'), mapValues(removeStrayNewlines), mapValues(pruneWhitespace), joinValues('\n'), ]) const formattedScript = format(adScript) console.log(formattedScript) // prints // The Avett Brothers are playing tonight! Come see them // live at Red Rocks. // // The first caller who names the lead singer's debut // solo album gets 2 free tickets.