Swap Name (Doe, John → John Doe)

This extension idea I received by email is to swap around a person’s name, e.g. if it currently says “John Smith” it would flip it to “Smith, John” or vice versa.

How it might work for more complex names

  • John Herbert Walker Smith <=> Smith, John Herbert Walker
  • John H. W. Smith, <=> Smith, John H. W.
  • J. Herbert Smith <=> Smith, J. Herbert

Basically it should assume the surname is one word an everything else is the forenames.

A quick snippet for this, it goes from Doe, Jon to John Doe but not the other way round.

#popclip
name: Swap Name
icon: symbol:arrow.triangle.swap
after: paste-result
regex: '\w+, .+'
javascript: |
  const match = popclip.input.text.match(/([\w\-]+), (.+)/)
  if (match) return match[2] + ' ' + match[1]; else return null

(The above block is an extension snippet - select it to install the extension with PopClip)

Test inputs:
Bush, George
Bush, George Herbert Walker
Bush, George H. W.
Smith-Smythe-Smith, Tarquin