JavaScript Code to Change String to TitleCase

I’d like to enhance my Mailto Action so that the email subject is converted to TitleCase.

I’ve search here for “title case” but I just find Actions that change case within a Draft. I’m looking for coding suggestions to use within an Action.

Should I somehow incorporate a snippet from another Action, e.g., TAD-Title Case Selection from ThoughtAsylum - Writing?

Sorry if that’s rude or considered plagiarism. I really don’t know what I’m doing. :pleading_face:

Thank you.

The Title example action has a re-usable, open-source toTitleCase extension you could re-use.

That function extracted:

/*
  * To Title Case 2.1 – http://individed.com/code/to-title-case/
  * Copyright © 2008–2013 David Gouch. Licensed under the MIT License.
 */

String.prototype.toTitleCase = function(){
  var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

  return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
    if (index > 0 && index + match.length !== title.length &&
      match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
      (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
      title.charAt(index - 1).search(/[^\s-]/) < 0) {
      return match.toLowerCase();
    }

    if (match.substr(1).search(/[A-Z]|\../) > -1) {
      return match;
    }

    return match.charAt(0).toUpperCase() + match.substr(1);
  });
};

Include that in your script, then you will have a toTitleCase() function you can call on any string literal or variable.

Thank you. I searched but didn’t find it.

By the way, according to the GitHub History/Changelog, the function has been updated (in minor ways) from ver 2.1 to 2.2.1 as of 2018-09-07.

You could include the TAD action then use this TADpoLe string function. It is based on the same case function, but with normalisation included.

What does “normalization” mean/do?

Hopefully, this Wikipedia entry will explain far better than I could.

1 Like

Sorry to be so dense, but …

How do I include this?

Yes, I’ve found and read the Include Action documentation in the Drafts User Guide.

I meant just copy and paste it into the script step in your action, didn’t mean anything fancy.

With this test script:

console.log("enter script\n");

let strTemp = "this is the page title";
console.log("strTemp = " + strTemp );

let strPageTitle= "";
console.log("strPageTitle = " + strPageTitle + "\n");

strPageTitle = strTemp.toTitleCase();
console.log("strPageTitle = " + strPageTitle + "\n");

console.log("exit script");


/*
  * To Title Case 2.1 – http://individed.com/code/to-title-case/
  * Copyright © 2008–2013 David Gouch. Licensed under the MIT License.
 */

String.prototype.toTitleCase = function(){
  var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;

  return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
    if (index > 0 && index + match.length !== title.length &&
      match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
      (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
      title.charAt(index - 1).search(/[^\s-]/) < 0) {
      return match.toLowerCase();
    }

    if (match.substr(1).search(/[A-Z]|\../) > -1) {
      return match;
    }

    return match.charAt(0).toUpperCase() + match.substr(1);
  });
};

I get an error saying that toTitleCase() is not a function:

Script step completed.
enter script

strTemp = this is the page title
strPageTitle = 

Script Error: TypeError: strTemp.toTitleCase is not a function. (In 'strTemp.toTitleCase()', 'strTemp.toTitleCase' is undefined)
Line number: 9, Column 35

The problem must be staring me in the face, but I don’t see it.

Move the toTitleCase() prototype definition to the top of the script. You are attempting to use it before you define it.

1 Like