Link to home
Start Free TrialLog in
Avatar of rvfowler2
rvfowler2Flag for United States of America

asked on

FM - Problem with TitleCase Function

Using the following Let statement as a TitleCase custom function.  However, it renders the following Business Name on the left incorrectly on the right.  I got rid of the alwaysUpper list below but the rendering is still incorrect.  I must be missing something obvious.  Any suggestions?

AMY CARD & GIFT, INC dba                             AmY CaRd & GiFt, InC Dba
THE CARE CENTER d/b/a SOUNDVIEW          ThE CaRe CeNtEr D/B/A SoUnDvIeW


Let ( [

prefixList = "Mc";
alwaysLower = ""
/****
"a¶an¶and¶aka¶at¶by¶for¶ie¶in¶is¶it¶nor¶of¶on¶or¶the¶to¶vs¶vs.¶w/¶with"
****/
;
//  alwaysUpper= "AC¶EMI¶RCA¶ABC¶INXS¶REM¶UB40¶UK¶USA¶OK¶USSR¶MTV¶NRG¶DJ¶MC¶H2O¶CD¶NY¶NYC¶ID¶EP¶TV¶CVS¶CPA" ;
 alwaysUpper = "" ;

prevText = LeaveEmpty;
nextText = Case(IsEmpty(prevText) and Exact( Text; Upper(Text) ); Lower(Text); Text);

prevChar = Right( prevText; 1);
prevChar_preSpace = Right( Trim(prevText); 1);
prevChar_isUpper = Exact( prevChar_preSpace; Upper(prevChar_preSpace));
prevChar_isLower = Exact( prevChar_preSpace; Lower(prevChar_preSpace));
 
prevCaseType = Case(
   prevChar = " "; Case(prevChar_isUpper and prevChar_isLower; "symbol"; "space");
   PatternCount("0123456789"; prevChar); "number";
   prevChar = "'"; "apos";
   prevChar_isUpper and prevChar_isLower; "symbol";
   prevChar_isLower; "lower";
   prevChar_isUpper; "upper");
   
currWord = RightWords(prevText; 1) & Left(nextText; Position(nextText & " "; " "; 1; 1)-1);
currChar = Left(nextText; 1);

/************************ PLEASE READ THE COMMENTS BELOW ***********************/
/******** THEY MAY HELP YOU WITH TWEAKS THAT BETTER FIT YOUR SITUATION *********/

currCase = Case (
      // First character of the title is always uppercase
   IsEmpty ( prevText ); "upper";
      // Always capitalize the first letter after any symbol
      //   except an apostrophe (which is handled separately), even if that symbol
      //   was followed by spaces (avoids an alwaysLower word from being lower if
      //   it is really the beginning of a sentence)
   prevCaseType = "symbol"; "upper";
      // Make it lowercase if it is in your alwaysLower value list
   WordCount( nextText ) > 1 and prevCaseType <> "symbol"
        and not IsEmpty( FilterValues( LeftWords ( nextText; 1); alwaysLower)); "lower";
      // First letter after a space is always capitalized (unless it
      //   was one of the previous arguments, like alwaysLower)
   prevCaseType = "space"; "upper";
      // Make it uppercase if it is the first character after an item in your prefixList
   not IsEmpty ( FilterValues( RightWords(prevText; 1); prefixList ) ); "upper";
      // The letter after an apostrophe is uppercase if there are more than a couple characters
   prevCaseType = "apos" and Length ( LeftWords(nextText; 1) ) > 2; "upper";
      // Make it uppercase if it is in your alwaysUpper value list
   not IsEmpty( FilterValues( currWord; alwaysUpper)); "upper";
      // The character after a number is always lowercase (to handle something like "19th" - changed to handle suite numbers.)
   prevCaseType = "number"; "upper";
      // You may or may not want this, as it helps but is imperfect.  
      //   It makes a "word" without vowels into all caps.
      //   This will result in converting 'lcd' to 'LCD', which is probably desirable.
      //   But it won't convert 'led' to 'LED' because 'Led' could also be a word.
      //   It also tests for a period so that 'mrs' will convert to 'Mrs.'
  IsEmpty( Filter( currWord; "aeiouyAEIOUY")) and not PatternCount(currWord; ".")
       and prevCaseType <> "lower"; "upper";
      // You may or may not want this, as it may or may not help your situation.
      //   It makes it possible to enter 'LaFond' and keep it that way (as opposed
      //   to 'Lafond').  The down side of this is that you may prefer to just
      //   overwrite your users' potential inconsistencies.  
      //   Either way, it is imperfect for all situations.
    prevChar_isLower and Exact ( currChar; Upper ( currChar ) );
                    "upper";  "lower" );

/************** TWEAKING NOT RECOMMENDED BELOW THIS LINE ****************/

currChar = Case(
   currCase = "upper"; Upper(currChar);
   currCase = "lower"; Lower(currChar);
   "lower") ;

nextText = Right ( nextText; Length ( nextText ) - 1 )

] ;    // end Let() parameters


Case (
IsEmpty (Trim(nextText)); prevText & currChar;
NameTitle_cf(nextText; prevText & currChar)
)

)  // end Let() statement
Avatar of Will Loving
Will Loving
Flag of United States of America image

I didn't dig into the function deeply but testing reveals that in your examples, where the text is ALL CAPS except for "dba" or "d/b/a" you get the weird formatting. If you remove the lower case text ("dba" or "d/b/a") or change it to ALL CAPS then the function works as expected. Also, if you make the initial text normal upper and lower, e.g. "Amy Card & Gifts, Inc dba" then it also works correct. I'm guessing there is something in the calculation that is detecting ALL CAPS and applying different rules.

You could just add and UPPER( ) to the calculation before the initial text but then you would loose all local capitalization, e.g.  Title( Upper( Organization ) ;"" )  Or, you could try either FileMaker's Proper() function or the SmartProper() Custom Function which is designed a little differently.
Title-Test.fmp12
Avatar of rvfowler2

ASKER

Tried the SmartProper() Custom Function, but couldn't get it even the function to save as I kept getting errors.
ASKER CERTIFIED SOLUTION
Avatar of Will Loving
Will Loving
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, Will.  Yes, these is from a DOS db that is supposed to have data that is only all caps.  Simply used the Upper function first, and then the custom function above worked.  Thanks.
Thanks, Will.  Yes, these is from a DOS db that is supposed to have data that is only all caps.  Simply used the Upper function first, and then the custom function above worked.  Thanks.
Thanks, Will.