Skip to content

Commit

Permalink
replace a couple regex with slice
Browse files Browse the repository at this point in the history
- performance and security
  • Loading branch information
msimerson committed Feb 23, 2024
1 parent 9a1e151 commit 98c30d4
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,32 @@ function _extract_name (name) {
// Using encodings, too hard. See Mail::Message::Field::Full.
if (/=?.*?\?=/.test(name)) return '';

// trim whitespace
name = name.trim();
name = name.replace(/\s+/, ' ');
// trim & condense whitespace
name = name.trim().replace(/\s+/, ' ');

// Disregard numeric names (e.g. [email protected])
if (/^[\d ]+$/.test(name)) return '';

name = name.replace(/^\((.*)\)$/, '$1') // remove outermost parenthesis
.replace(/^"(.*)"$/, '$1') // remove outer quotation marks
.replace(/\(.*?\)/g, '') // remove minimal embedded comments
.replace(/\\/g, '') // remove all escapes
.replace(/^"(.*)"$/, '$1') // remove internal quotation marks
.replace(/^([^\s]+) ?, ?(.*)$/, '$2 $1') // reverse "Last, First M." if applicable
// remove outermost parenthesis
if (name.slice(0,1) === '(' && name.slice(-1) === ')') name = name.slice(1,name.length-1)

// remove outer quotation marks
if (name.slice(0,1) === '"' && name.slice(-1) === '"') name = name.slice(1,name.length-1)

name = name.replace(/\(.*?\)/g, '') // remove minimal embedded comments
.replace(/\\/g, ''); // remove all escapes

// remove internal quotation marks
if (name.slice(0,1) === '"' && name.slice(-1) === '"') name = name.slice(1,name.length-1)

name = name.replace(/^([^\s]+) ?, ?(.*)$/, '$2 $1') // reverse "Last, First M." if applicable
.replace(/,.*/, '');

// Change casing only when the name contains only upper or only
// lower cased characters.
if ( exports.isAllUpper(name) || exports.isAllLower(name) ) {
// console.log("Changing case of: " + name);
// console.log(`Changing case: ${name} to ${exports.nameCase(name)}`);
name = exports.nameCase(name);
// console.log("Now: " + name);
}

// some cleanup
Expand Down

0 comments on commit 98c30d4

Please sign in to comment.