GSBjay Posted August 28, 2012 Share Posted August 28, 2012 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 More sharing options...
Dan Korn Posted August 28, 2012 Share Posted August 28, 2012 Try this: var names = [Field("Name1"), Field("Name2"), Field("Name3"), Field("Name4")]; return names.filter(Boolean).join(", ").replace(/^(.*)(, )(.*?)$/, "$1, and $3");If you don't want the serial comma before the word "and", remove it after the "$1" above. Link to comment Share on other sites More sharing options...
GSBjay Posted August 28, 2012 Author Share Posted August 28, 2012 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 More sharing options...
Dan Korn Posted August 28, 2012 Share Posted August 28, 2012 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 More sharing options...
GSBjay Posted August 29, 2012 Author Share Posted August 29, 2012 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 More sharing options...
Dan Korn Posted August 29, 2012 Share Posted August 29, 2012 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 More sharing options...
GSBjay Posted August 30, 2012 Author Share Posted August 30, 2012 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.