Made some improvements to my Keyboard Maestro macro which converts selected text from camel case to upper-snake case. Made a small fix to deal with common abbreviations like URL or API, which usually appear in all uppercase. The previous version was treating each letter as the start of a separate word, so that URL would be changed to U_R_L.

Here’s the JavaScript portion of the macro:

'use scrict';

const commonAbbrevs = ['URL', 'API', 'SQS', 'DB'];

(function() {
  let app = Application.currentApplication()
  app.includeStandardAdditions = true;

  let text = app.theClipboard();

  // Capital case the common abbreviations so that they'll be properly cased
  for (let abbrev of commonAbbrevs) {
    let capitalCasedVersion = abbrev[0] + abbrev.substring(1).toLowerCase();
    text = text.replaceAll(abbrev, capitalCasedVersion);

  // Convert to upper snake case
  let newText = text.replaceAll(/\B[A-Z]/g, "_$&").toUpperCase();


Side-note: when I first heard that the let keyword was going to be coming to JavaScript, I heard others say that they wished let would effectively make a constant, much like how let works in Swift. I don’t hold a strong opinion on this, but I kinda wish that ECMA went through with that, since I see a lot of JavaScript code use const in place of let within a function block.

I must say, I’m not a huge fan of this practice. I mean, I can appreciate why they do it, since it reduces the chances of accidentally overriding a variable value. But being someone use to languages where const is reserved for constants values at the top-level, it doesn’t look aesthetically pleasing to me. I think I’ll keep using my lets.