Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library > Formatting Text

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old October 5th, 2018, 07:33 AM
jpmiller jpmiller is offline
Junior Community Member
 
Join Date: Dec 2016
Location: Jacksonville, FL
Posts: 19
Smile Change Case tp Proper with Mary/ann in the record

When I use the toTitleCase function I would like the letter after / to be capitalized.

The data source contains names with /
i.e.: MARY/ANN to Mary/ann is what I get now
i.e.: MARY/ANN to Mary/Ann is the result expected

Also: How do I retain suffix capitalization when the name is one field?
i.e.: John Doe, III instead of John Doe, Iii

Thank you,
Jeff
Reply With Quote
  #2  
Old October 8th, 2018, 01:40 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,321
Default Re: Change Case tp Proper with Mary/ann in the record

First, I would ask to you to please refer to the many threads discussing the ToTitleCase function and its myriad limitations:
http://forums.pti.com/showthread.php?t=2731
http://forums.pti.com/showthread.php?t=2569
http://forums.pti.com/showthread.php?t=297

These are due to the fact that all the ways that humans might decide that names should be capitalized can't possibly be reduced to a computer algorithm, as capitalization itself is arbitrary, with more exceptions than hard-and-fast rules. No amount of code is going to be able to anticipate and "properly" capitalize names like these:
  • McDonald
  • d'Angelo
  • O'Neil
  • Mies van der Rohe
  • Leonardo di Caprio
  • Quincy Jones, MD
  • Director of Marketing
  • NE 1st Street
  • John Doe III
  • John Smith, CEO
Just to name a few of an uncountable number of examples. There's no complete list of exceptions to title casing. Like many "rules" about how text should be typeset and formatted, this is one of those things that's as much art as science.

You also have to be careful with non-ASCII (sometimes called "special") characters, with various umlauts and diacriticals and accents, such as "ń" or "ü", in non-English names, especially with JavaScript Regular Expressions, which do not make distinctions about such characters when identifying things like word boundaries.

So hopefully I've tempered your expectations about exactly what can be accomplished with a "title case" function or rule. Your best bet is to have the original name, in the original, proper case, on hand in your data, before it's gone through some kind of transformation, such as CASS processing which "normalizes" names by converting them to all caps, because such a transformation is "lossy" and can't be reliably reversed.

That said, if you look at the ToTitleCase function in Builtins.js, you can see that it has been through a few revisions. Please DO NOT edit Builtins.js, but if you want slightly different ToTitleCase functionality, you can copy the function from there and paste it into your own rules, with a different name. If you were to do this, and in your own version of the function, uncomment the line below the comment "This doesn't properly handle accented characters," then it will do what you want, for a name like "MARY/ANN":
Code:
function CustomToTitleCase(string, leaveAllCapsWords)
{
    //if((typeof string != "number") && (typeof string != "string"))
    //    ThrowError("ToTitleCase", "The input string is invalid.");

    // This doesn't properly handle accented characters.
    return String(string).toLowerCase().replace(/\b\w/g, function(w){return w.toUpperCase()});
}

return CustomToTitleCase("MARY/ANN");
// returns "Mary/Ann"
Though obviously the problem mentioned in that comment will occur with a name such as "Nińa", which is why that change was made to the rule in the first place. But that change obviously affected strings with things like slashes and other punctuation, or really anything other than spaces, delimiting different words in a name. Like I said, there isn't one master algorithm which can handle every possible way of doing capitalization, and every "fix" has its own trade-offs.

As for "suffix capitalization", such as "John Doe, III", that's also hard to reduce to an algorithm, but there was a lot of discussion about just that kind of thing in this thread:
http://forums.pti.com/showthread.php?t=2731
And I suggested this:
Code:
var result = ToTitleCase(Field("Full Name"));
var Exceptions = [ "I", "II", "III", "IV" ];
for (var i in Exceptions)
{
    var re = RegExp("\\b" + Exceptions[i] + "\\b", "gi");
    result = result.replace(re, Exceptions[i]);
}
return result;
Though again, there are many, many possible exceptions to title casing, including honorariums, abbreviations, and acronyms such as CEO, MD, POTUS, etc. Those could all be added to that Exceptions array, but it's a game you're never going to win.

I will add that, with FusionPro 10.1, you can specify as many instances of the "Preserve Case of Word or Phrase" callback XML rule as you want, which can be used to preserve the case of these kinds of abbreviations, even when ToTitleCase is called, as the FusionPro.Composition.AddTextReplacement function called in that rule works at a very low level in the typesetter. So you could have a series of rules like this:


Or, you could have a series of these "whole word" exceptions defined in OnJobStart, like so:
Code:
var Exceptions = [ "I", "II", "III", "IV", "MD", "CEO" ];
for (var i in Exceptions)
    FusionPro.Composition.AddTextReplacement(Exceptions[i], Exceptions[i], false, true);
Though again, the task of constantly adding "new" exceptions to this list as you encounter them in your data will be a never-ending one.
Attached Images
File Type: jpg PreserveCase.jpg (25.2 KB, 28 views)
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
Reply

Tags
case

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:49 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.