Jump to content

Recommended Posts

Posted (edited)

So I have this rule that strings together 3 variable fields. Trying to modify it so if all three of my fields are not populated it will change accordingly:

 

So right now the rule returns "Store1, Store2, and Store3".

 

First I want to change the "and" to "&".

 

But then if I only have two stores I want it to return "Store1 & Store 2".

 

And finally if I only have one store to return "Store1"

 

This rule I found in a different post so if it can be shorten up that is fine. The most I will have is three stores. I should mention this is in the middle of a large paragraph. TIA

 

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;
 };
}
var contact = [[Field("Store1")],[Field("Store2")],[Field("Store3")]];
var result = [];
 for(var i=0; i<3; i++){
       if (contact[i][1] != "") { 
           result.push(contact[i].join(" "));
           }

       }

return (result.length>2) ? result.join(", ").replace(/^(.*)(, )(.*?)$/, "$1, and $3") : result.join(", ").replace(/^(.*)(, )(.*?)$/, "$1 and $3");

Edited by dreimer
Posted

If you're wanting to make your current code work, you should make these edits (delete what's in red and add what's in green)

var contact = [[color="red"][[/color]Field("Store1")[color="red"]][/color],[color="red"][[/color]Field("Store2")[color="red"]][/color],[color="red"][[/color]Field("Store3")[color="red"]][/color]];
var result = [];
 for(var i=0; i<3; i++){
       if (contact[i][color="red"][1][/color] != "") { 
           result.push(contact[i][color="red"].join(" ")[/color]);
           }

       }

return (result.length>2) ? result.join(", ").replace(/^(.*)(, )(.*?)$/, "$1, and $3") : result.join(", ").replace(/^(.*)(, )(.*?)$/, "$1 [color="Lime"]&[/color] $3");

 

Since you're using FP9, you don't need the filter prototype at the beginning of that code or the for loop. I think this simplified code would work for you:

var stores = [Field("Store1"),Field("Store2"),Field("Store3")]; // Array of stores
stores = stores.filter(String); // Filter array to remove empties

return (stores.length > 2 ) ? stores.join(", ").replace(/,\s([^,]*)$/,', and $1') : stores.join(" & ");

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...