You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.3 KiB
38 lines
1.3 KiB
// Extends the String type and other string helper functions |
|
import wrapAnsi from 'wrap-ansi' |
|
|
|
// Adds a .toTitleCase() function to every string |
|
String.prototype.toTitleCase = function () { |
|
return this.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()}) |
|
} |
|
|
|
// Wraps the string to the console (or specified) width, ignoring any ansi formatting codes |
|
String.prototype.wordWrap = function (width = 80) { |
|
return wrapAnsi(this, width) |
|
} |
|
|
|
export const repeatString = (str, n) => { |
|
return new Array(1 + (n || 0)).join(str) |
|
} |
|
|
|
String.prototype.repeat = repeatString |
|
|
|
String.prototype.centerInConsole = function (width = 80) { |
|
const consoleWidth = process.stdout.columns |
|
const padding = Math.floor((consoleWidth - width) / 2) |
|
const lines = this.split('\n') |
|
const centered = lines.map(line => repeatString(" ", padding) + line) |
|
return centered.join('\n') |
|
} |
|
|
|
// Centers a one-line string within the given number of characters by adding padding to the left |
|
String.prototype.centerInLine = function (lineWidth, width = 80) { |
|
const padding = Math.floor((width - lineWidth) / 2) |
|
return repeatString(" ", padding) + this |
|
} |
|
|
|
export const centerLines = (str) => { |
|
const lines = str.split('\n') |
|
const centered = lines.map(line => line.centerInLine(line.length, process.stdout.columns)) |
|
return centered.join('\n') |
|
}
|
|
|