Jump to content

multiple names rule


Recommended Posts

Hi I'm not familiar with javascripts but I've edited a few pre-written scripts from the FusionPro rules library.

 

Currently I'm working on an announcement card that involves four names. The text always have two names but can possibly extend to four names. Between each name will have a comma or the word "and" depending on how many names.

 

I am not sure how to write the rule or what function to use. Below are examples of what I am trying to do. Any help would be appreciated.

 

Example 1:

John Doe, Head of Private Banking USA,

is pleased to announce <Name1> and <Name2> have joined Bank of America.

 

Example 2:

John Doe, Head of Private Banking USA,

is pleased to announce <Name1>, <Name2> and <Name3> have joined Bank of America.

 

Example 3:

John Doe, Head of Private Banking USA,

is pleased to announce <Name1>, <Name2>, <Name3> and <Name4> have joined Bank of America.

Link to comment
Share on other sites

Thanks Dan,

I tried your script and it generated an error. It reads "names,line2: TypeError: names.filter is not a function"

 

This is what I plugged in, they are the actual field names I used.

var names = [Field("RM1Name"), Field("RM2Name"), Field("RM3Name"), Field("RM4Name")];

return names.filter(Boolean).join(", ").replace(/^(.*)(, )(.*?)$/, "$1, and $3");

Link to comment
Share on other sites

Thanks Dan,

I tried your script and it generated an error. It reads "names,line2: TypeError: names.filter is not a function"

In the absence of any specific information about what version of FusionPro you're running, or on what platform, I assumed that you were using the latest 8.1 version of FusionPro, or the 8.0 version. If you're using an older version, then you need to add this function to your JavaScript Globals:

if (!Array.prototype.filter)
{
 Array.prototype.filter = function(fun /*, thisp */)
 {
   "use strict";

   if (this == null)
     throw new TypeError();

   var t = Object(this);
   var len = t.length >>> 0;
   if (typeof fun != "function")
     throw new TypeError();

   var res = [];
   var thisp = arguments[1];
   for (var i = 0; i < len; i++)
   {
     if (i in t)
     {
       var val = t[i]; // in case fun mutates this
       if (fun.call(thisp, val, i, t))
         res.push(val);
     }
   }

   return res;
 };
}

The above is from:

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/filter#Compatibility

Link to comment
Share on other sites

Inputting the code into JavaScript Globals eliminated the error. The script is functioning now but is there a way to break the text after the second name? This will keep the names reading together without breaking up.

 

Below is an example of how it looks with the script on. With the third name in red text, the first and last name is separated because there is not enough room to fit. If I can make a permanent break after the second name, this should solve it.

 

John Doe, Head of Coca Cola Production North Region,

is pleased to announce John Smith, Larry Jackson, Louis

McDonald and Todd Butler have joined the corporation.

 

Thanks for the reminder, I've updated the list of software I am using on my profile and my signature.

Link to comment
Share on other sites

Inputting the code into JavaScript Globals eliminated the error. The script is functioning now but is there a way to break the text after the second name? This will keep the names reading together without breaking up.

 

Below is an example of how it looks with the script on. With the third name in red text, the first and last name is separated because there is not enough room to fit. If I can make a permanent break after the second name, this should solve it.

 

John Doe, Head of Coca Cola Production North Region,

is pleased to announce John Smith, Larry Jackson, Louis

McDonald and Todd Butler have joined the corporation.

I would think you would want to make it so that each name doesn't break. You could do something like this:

var names = [Field("RM1Name"), Field("RM2Name"), Field("RM3Name"), Field("RM4Name")];[color=Blue]
[/color]return names.filter(Boolean).map(function(s){return TaggedTextFromRaw(s).replace(/\s/g, " ");}).join(", ").replace(/^(.*)(, )(.*?)$/, "$1 and $3");

Although, since you're using the older version of FusionPro with the older JavaScript engine, you can't use Array.map either, so you can do this instead:

var names = [Field("RM1Name"), Field("RM2Name"), Field("RM3Name"), Field("RM4Name")];[color=Blue]
[/color]var taggedNames = [];
for (var i in names)
{
   if (names[i])
       taggedNames.push(TaggedTextFromRaw(names[i]).replace(/\s/g, " "));
}
return taggedNames.join(", ").replace(/^(.*)(, )(.*?)$/, "$1 and $3");

Note that you'll need to check "Treat returned strings as tagged text" as well.

Thanks for the reminder, I've updated the list of software I am using on my profile and my signature.

Great, thanks. Although your signature doesn't actually show up on your posts unless you check the box under Miscellaneous Options.

Link to comment
Share on other sites

Dan, your JavaScript is working great with keeping the names together but my client think it would look best if the text can break after second name. This will make the 3rd line look better filled with more words since this is a short note. See example 2. Can you please adjust the script again?

 

 

Example 1:

John Doe, Head of Coca Cola Production North Region,

is pleased to announce John Smith and Larry Jackson, have joined Pepsi

Cola.

 

Example 2:

John Doe, Head of Coca Cola Production North Region,

is pleased to announce John Smith and Larry Jackson,

have joined Pepsi Cola.

 

I did not make any adjustment to my profile but it appears my signature is showing up today. Cheers!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...