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

Notices

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #9  
Old September 14th, 2017, 09:29 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Mass Entity Rule Possible?

Quote:
Originally Posted by lance View Post
Isn't this just re-adding (overwriting) and re-defining the variable each iteration through the for loop?
Yes. But, in doing so it updates the value returned by the "Field" function. Let's assume you have a field in your data called "Salutation" and that field contains "Dear Lance." Naturally, that is too formal and you'd instead like to be addressed as "The Lance-meister":
Code:
var salutation = ReplaceSubstring(
  Field("Salutation"), // Field("Salutation") will return "Dear Lance"
  "Lance",             // This is the value to be replaced
  "The Lance-meister"  // This is the new value
);

// This line will update the value returned by the "Field" function
// for the "Salutation" field.
FusionPro.Composition.AddVariable("Salutation", salutation);

// On second thought, "The Lance-meister" sounds a little too 1985.
// Let's update that to "The Lance-inator"
FusionPro.Composition.AddVariable("Salutation", ReplaceSubstring(
  Field("Salutation"), // Field("Salutation") returns "Dear The Lance-meister"
  "-meister",          // This is the value to be replaced
  "-inator"            // This is the new value
));

// "Dear?" That's no way to address The Lance-inator. Let's fix that.
FusionPro.Composition.AddVariable("Salutation", ReplaceSubstring(
  Field("Salutation"), // Field("Salutation") returns "Dear The Lance-inator"
  "Dear",              // This is the value to be replaced
  "Sup"                // This is the new value
));
Obviously, you wouldn't really write the code like that but I just wanted to illustrate what the for loop is doing in the original code. The above example could be re-written like so:
Code:
var charMap = {
// Find       : Replace
   "Lance"    : "The Lance-meister",
   "-meister" : "-inator",
   "Dear"     : "Sup",
}

for (var field in FusionPro.Fields) {
  for (var find in charMap) {
    var replace = charMap[find];
    FusionPro.Composition.AddVariable(field,
      ReplaceSubstring(Field(field), find, replace), true);
  }
}
That being said, if you're skeptical of overwriting the field value with AddVariable, you could assign the values to a variable and only update the field value one time once all of the necessary replacements have been made:
Code:
// An object of values to find as the keys,
// and the string to replace them with as the value.
var charMap = {
// Find      : Replace
   "’":"’",
   "’":"'",
   "¼":"¼",
   "½":"½",
   "¾":"¾",
   "—":"–",
   "—":"—",
}

// Loop through all of the fields.
for (var field in FusionPro.Fields) {
  var fieldVal = Field(field);
  // Loop through all of the find/replace values for current field.
  for (var find in charMap) {
    var replace = charMap[find];
    fieldVal = ReplaceSubstring(fieldVal, find, replace);
  }
  // Update the field's value
  FusionPro.Composition.AddVariable(field, fieldVal, true);
}
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
 

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 01:31 AM.


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