Skip to content

Instantly share code, notes, and snippets.

@davedele
Created June 19, 2023 06:38
Show Gist options
  • Select an option

  • Save davedele/30de1c8ebd321898536ac6c733032c36 to your computer and use it in GitHub Desktop.

Select an option

Save davedele/30de1c8ebd321898536ac6c733032c36 to your computer and use it in GitHub Desktop.
function that uses regular expressions to split the email address at logical breakpoints (i.e., before a . or after a @). It then creates a string with HTML line breaks (<br>) where the email was split. This function assumes a maximum line length of 40 characters, which is typical for point-of-sale receipt printers.:
// The standard width of a typical receipt paper is 80mm, print density is about 40-48 characters per line depending on font size.
// function that uses regular expressions to split the email address at logical breakpoints (i.e., before a . or after a @, etc.).
// It then creates a string with HTML line breaks (<br>) where the email was split. This function assumes a maximum line length of 40 characters, which is typical for point-of-sale receipt printers.:
function formatEmailForReceipt(email: string): string {
const MAX_LINE_LENGTH: number = 40;
const MIN_BREAKPOINT_LENGTH: number = 10; // The minimum length before a breakpoint to consider breaking
let lines: string[] = [];
let currentLine: string = "";
// Split email into parts at breakpoints (@, ., -, _)
const parts: string[] = email.split(/(?=@)|(?=(\.))|(?<=(-))|(?<=(_))/g);
parts.forEach(part => {
if (currentLine.length + part.length <= MAX_LINE_LENGTH) {
currentLine += part;
} else if (currentLine.length >= MIN_BREAKPOINT_LENGTH) {
lines.push(currentLine);
currentLine = part;
} else {
currentLine += part;
}
});
lines.push(currentLine);
return lines.join("<br>");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment