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

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old March 14th, 2012, 09:48 AM
traba5058 traba5058 is offline
Regular Contributor
 
Join Date: Jan 2012
Location: Atlanta, GA
Posts: 77
Question Write a case rule with exceptions

I am a new Fusion Pro & Marcomm user and just getting started with Javascript.

I have 8 direct mail templates setup for a client. The client provides the list in all uppercase. This works great for the mailing panel but not so great when the name is used elsewhere in the piece.

Right now, the list has 1 name field (Full Name) and that is all their system can provide. That field contains First, Middle Initial, Last and Suffix.

I need to know how to change the following rule

var Var1 = "Full Name";
var CaseSelection = "propercase";


if(CaseSelection == "propercase")
return ToTitleCase(Field(Var1));

so it leaves I, II and III etc. suffixes uppercase.

Can anyone help?

Thanks!
Reply With Quote
  #2  
Old March 14th, 2012, 11:04 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,671
Default Re: Write a case rule with exceptions

Unfortunately, there's no computerized algorithm which can accurately calculate all the somewhat arbitrary capitalization rules for proper names. The ToTitleCase function also doesn't know how to properly capitalize names like McDonald or Werner von Braun, nor acronyms which are parts of names and addresses, such as directional notations like NW. For that matter, not every proper name conforms to the Western ideal of first, middle, last, and suffix.

So, using the rather simplistic ToTitleCase string processing function to cleanse or "fix" your data is perilous, as there are many, many exception cases where proper names do not conform to "title case" logic". Here's one of my rants about this from the old email list:
http://www.mail-archive.com/fusionpr.../msg01161.html

You could theoretically apply the same kind of exception case logic as in that post, with a list of exceptions like I, II, III, etc., but I still don't recommend doing this, as you will undoubtedly find other kinds of names where forcing a particular mixed case style is simply not appropriate. This is why the USPS standardizes all mailing addresses to upper-case.

In short, there's really no reliable way to do programmatically restore the missing capitalization in the data, short of going back to the customer and getting the original data, before it was converted to all upper-case.
__________________
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
  #3  
Old March 14th, 2012, 11:18 AM
rpaterick rpaterick is offline
Senior Contributor
 
Join Date: Mar 2009
Posts: 427
Default Re: Write a case rule with exceptions

Here is an example of how this code could work. You would have to add all scenarios to meet your customer's needs, as Dan states.

Code:
var Name = Field("Full Name");
Name = Name.replace("Sr","SR").replace("Jr","JR").replace("iii","III").replace("ii","II");
return Name;
__________________
Windows 7 64BIT
Intel - I7 chip - 3.33Ghz - 6GB ram
8.0 Creator/8.0 Producer/CS5.5
Reply With Quote
  #4  
Old March 14th, 2012, 01:27 PM
traba5058 traba5058 is offline
Regular Contributor
 
Join Date: Jan 2012
Location: Atlanta, GA
Posts: 77
Default Re: Write a case rule with exceptions

Thanks so much for getting back to me! That code worked.
Reply With Quote
  #5  
Old March 14th, 2012, 01:33 PM
rpaterick rpaterick is offline
Senior Contributor
 
Join Date: Mar 2009
Posts: 427
Default Re: Write a case rule with exceptions

Quote:
Originally Posted by traba5058 View Post
Thanks so much for getting back to me! That code worked.
No problem. Just remember, if your record has "Iii" you will need to add that to the code to replace it with "III"(How your customer wants it) and other scenarios as well.
__________________
Windows 7 64BIT
Intel - I7 chip - 3.33Ghz - 6GB ram
8.0 Creator/8.0 Producer/CS5.5
Reply With Quote
  #6  
Old March 14th, 2012, 01:41 PM
traba5058 traba5058 is offline
Regular Contributor
 
Join Date: Jan 2012
Location: Atlanta, GA
Posts: 77
Default Re: Write a case rule with exceptions

I actually manipulated it to read

var Name = Rule("ProperCase_FullName_Rule");
Name = Name.replace("Ii","II").replace("Iii","III").repla ce("IIi","III").replace("Iv","IV");
return Name;

One thing I noticed is that the PropoerCase rule changed III to Iii, but the rule above changed it to IIi. So, I had to add the "replace("IIi","III")" portion. Logically that didn't make sense to me. Do you have any thoughts on why I had to add that?
Reply With Quote
  #7  
Old March 14th, 2012, 01:49 PM
rpaterick rpaterick is offline
Senior Contributor
 
Join Date: Mar 2009
Posts: 427
Default Re: Write a case rule with exceptions

Quote:
Originally Posted by traba5058 View Post
I actually manipulated it to read

var Name = Rule("ProperCase_FullName_Rule");
Name = Name.replace("Ii","II").replace("Iii","III").repla ce("IIi","III").replace("Iv","IV");
return Name;

Do you have any thoughts on why I had to add that?
Unfortunately I'm no code expert but I see "replace" has a space after the "a." I know one thing with code, if it isn't spelled correctly, it won't work.
__________________
Windows 7 64BIT
Intel - I7 chip - 3.33Ghz - 6GB ram
8.0 Creator/8.0 Producer/CS5.5
Reply With Quote
  #8  
Old March 14th, 2012, 02:11 PM
esmith esmith is offline
Senior Contributor
 
Join Date: Oct 2008
Location: Charlotte, NC
Posts: 869
Default Re: Write a case rule with exceptions

Quote:
Originally Posted by traba5058 View Post
I actually manipulated it to read

var Name = Rule("ProperCase_FullName_Rule");
Name = Name.replace("Ii","II").replace("Iii","III").repla ce("IIi","III").replace("Iv","IV");
return Name;

One thing I noticed is that the PropoerCase rule changed III to Iii, but the rule above changed it to IIi. So, I had to add the "replace("IIi","III")" portion. Logically that didn't make sense to me. Do you have any thoughts on why I had to add that?
That is because your first replace() checks for "Ii". The search will replace the first instance that matches that string whether it is "Ii" or "Iii". What you could do instead of adding another replace, would be to reverse the replace methods you already had. So replace("Iii", "III") would come before replace("Ii","II").

Keep in mind that if your field value were "John Iilian Ii", your result would be "John IIlian Ii" since the replace method only searches for the first match. There are ways around that, but they fall under JavaScript 301.

Last edited by esmith; March 14th, 2012 at 02:15 PM..
Reply With Quote
  #9  
Old March 14th, 2012, 02:32 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,671
Default Re: Write a case rule with exceptions

Quote:
Originally Posted by rpaterick View Post
Here is an example of how this code could work. You would have to add all scenarios to meet your customer's needs, as Dan states.

Code:
var Name = Field("Full Name");
Name = Name.replace("Sr","SR").replace("Jr","JR").replace("iii","III").replace("ii","II");
return Name;
Well, yes, although if you're going to do this, despite the caveats I offered, I think it's cleaner to create a simple array of exception-case strings and iterate through them, as in the example I cited, instead of chaining together a bunch of replace calls. Something like 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;
This makes it a lot easier to add more exceptions to the list. Note also that my version only replaces entire words, while the simpler call to replace without the regular expression could result in output such as "IVerson".
Quote:
Originally Posted by traba5058 View Post
I actually manipulated it to read

var Name = Rule("ProperCase_FullName_Rule");
Name = Name.replace("Ii","II").replace("Iii","III").repla ce("IIi","III").replace("Iv","IV");
return Name;

One thing I noticed is that the PropoerCase rule changed III to Iii, but the rule above changed it to IIi. So, I had to add the "replace("IIi","III")" portion. Logically that didn't make sense to me. Do you have any thoughts on why I had to add that?
Yes, well, that rule is very dependent on the order in which the replace calls are made, as it was doing case-sensitive searches. My solution does case-insensitive searches, as denoted by the "i" flag in the RegExp object. It also includes the "g" flag to replace all instances, not just the first, which probably doesn't matter much for a name with a single suffix, but it's technically more complete.
__________________
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)});

Last edited by Dan Korn; March 14th, 2012 at 04:59 PM.. Reason: fix typo
Reply With Quote
  #10  
Old March 14th, 2012, 02:44 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,671
Default Re: Write a case rule with exceptions

Quote:
Originally Posted by rpaterick View Post
Unfortunately I'm no code expert but I see "replace" has a space after the "a." I know one thing with code, if it isn't spelled correctly, it won't work.
This was probably a result of pasting the code in directly without using [CODE] and [/CODE] tags. If you're posting code to the forum, you can click the "Go Advanced" button, then select the code you pasted in and click the # button to add the appropriate tags, which will put the code in a special "Code" block as in my posts. You can also use the handy "Preview Post" button to make sure things look right before submitting. Okay, I'm taking my moderator hat off now.
__________________
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

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 06:29 AM.


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