Lodash upgrade v4.17.x (>v5.5)

Since the old Lodash from 2015 was used, after the upgrade some features will no longer be supported and new ones will have to be used. Here is a list of the most important ones:

Attention, a major update is in the _.find function!

Important change in _.find functionality in Lodash

Attention! In a newer version of Lodash, there has been a major change in the _.find function that affects the way parameters are passed for key-value searches.

This function is widely used in dynamic rows script!

Original entry:

In Lodash version 3.x, the following notation could be used:

var dtCC = _.find(currentTask.processVars, 'tvar_name', '_dtCostCenters').ivar_value;

This method allowed you to directly pass the key ( 'tvar_name' ) and value ( '_dtCostCenters' ) as additional function parameters.

New entry:

In newer versions of Lodash (4.x and above), you need to use an object predicate for key-value searches. The new notation looks like this:

var dtCC = _.find(currentTask.processVars, { tvar_name: '_dtCostCenters' }).ivar_value;

Instead of separate parameters, we now use the object { tvar_name: '_dtCostCenters' } , which specifies that _.find should find the first object with tvar_name property equal to the value '_dtCostCenters' .

An easier way to rewrite it is to put only the parameters in square brackets.
//Old
var dtCC = _.find(currentTask.processVars, 'tvar_name', '_dtCostCenters').ivar_value;

//New
var dtCC = _.find(currentTask.processVars, ['tvar_name', '_dtCostCenters']).ivar_value;

Recommendation:

All occurrences _.find in the old format must be modified to the new format for the code to work correctly with the new version of Lodash.

Other examples:

//Old
rowIndex = (_.find(dynTable[_nazevDynTable1], 'col_1', formLang) || {}).dlv_index;
//New
rowIndex = (_.find(dynTable[_nazevDynTable1], { col_1: formLang }) || {}).dlv_index;
//Old
rowText = _.find(self.dynTable[_nazevDynTable1], 'dlv_index', rowIndex);
//New
rowText = _.find(self.dynTable[_nazevDynTable1], { dlv_index: rowIndex });

How to upgrade to the new lodash

To find out which lodash functions are used and no longer supported:

Within the administration (since TAS version 5.3.16), it is possible to run the following commands in the Console, which will list the no longer supported lodash functions.

sys.validateTemplate(tprocId: number);

This function validates the specific template you selected and lists the occurrence of unsupported functions. The script looks at:

  • Calculations
  • Dynamic conditions
  • Dynamic rows
  • Printing templates

sys.validateTemplates();

This function validates all templates and lists the occurrence of unsupported functions. The script looks at:

  • Calculations
  • Dynamic conditions
  • Dynamic rows
  • Printing templates

sys.validateGlobalScripts();

This function will perform a check in global scripts (Calculations, CO React, CO)

Replacing unsupported features with new ones:

  • Replace the found unsupported functions with new ones.

Updating method signatures:

  • Update method calls that used thisArg to use _.bind or arrow functions.

In Lodash 4, support for the thisArg parameter was removed from some functions, meaning that code that relied on this argument may stop working correctly.

In previous versions of Lodash (3.x and older), it was common to use thisArg to set the context (this) in some methods such as _.map, _.forEach, or _.reduce.

In Lodash 4, it is recommended to use either thisArg instead of:

  1. _.bind This function explicitly sets the context (this) for the given function.
//Příklad
var boundFunction = _.bind(func, context);
  1. Šipkové funkce These functions in JavaScript automatically preserve the context (this) from the parent lexical environment, eliminating the need for thisArg.
//Příklad
array.map(item => this.someMethod(item));

Using new methods:

  • New functionalities can also be used within the new lodash.

Testing changes:

  • Test the changes.

Example 1: Using _.pluck

Old Code :

var users = [{ 'user': 'barney' }, { 'user': 'fred' }];
var names = _.pluck(users, 'user');

New Code :

var users = [{ 'user': 'barney' }, { 'user': 'fred' }];
var names = _.map(users, 'user');

Example 2: Using _.where

Old Code :

var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
var activeUsers = _.where(users, { 'active': true });

New Code :

var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
var activeUsers = _.filter(users, { 'active': true });

Example 3: Using thisArg

Old Code :

var obj = { 'value': 1 };
function callback(item) {
return item.value + this.value;
}
var result = _.map([{ 'value': 2 }, { 'value': 3 }], callback, obj);

New Code :

var obj = { 'value': 1 };
function callback(item) {
return item.value + this.value;
}
var boundCallback = _.bind(callback, obj);
var result = _.map([{ 'value': 2 }, { 'value': 3 }], boundCallback);

🔥 Breaking Changes

  • Removed thisArg parameter : Methods no longer accept thisArg . Use _.bind or arrow functions instead.

Removal Method:

  • _.pluck : Use _.map with iteratee shorthand.
  • _.where and _.findWhere : Use _.filter and _.find with iteratee shorthand.
  • _.support : Removed.

Method Renames:

  • _.first to _.head
  • _.indexBy to _.keyBy
  • _.invoke to _.invokeMap
  • _.padLeft and _.padRight to _.padStart and _.padEnd
  • _.pairs to _.toPairs
  • _.rest to _.tail
  • _.sortByOrder to _.orderBy
  • _.trimLeft and _.trimRight to _.trimStart and _.trimEnd

Split Methods:

  • _.max & _.min into _.maxBy & _.minBy
  • _.assign & _.assignIn into _.assignWith & _.assignInWith
  • _.clone & _.cloneDeep into _.cloneWith & _.cloneDeepWith
  • _.indexOf & _.lastIndexOf into _.sortedIndexOf & _.sortedLastIndexOf
  • _.isEqual into _.isEqualWith
  • _.isMatch into _.isMatchWith
  • _.merge into _.mergeWith
  • _.omit & _.pick into _.omitBy & _.pickBy
  • _.sample into _.sampleSize
  • _.sortedIndex into _.sortedIndexBy
  • _.sortedLastIndex into _.sortedLastIndexBy
  • _.sum into _.sumBy
  • _.uniq into _.sortedUniq , _.sortedUniqBy , & _.uniqBy
  • _.zipObject into _.fromPairs

Removed Aliases:

  • _.all -> _.every
  • _.any -> _.some
  • _.backflow -> _.flowRight
  • _.callback -> _.iteratee
  • _.collect -> _.map
  • _.compose -> _.flowRight
  • _.contains -> _.includes
  • _.detect -> _.find
  • _.foldl -> _.reduce
  • _.foldr -> _.reduceRight
  • _.include -> _.includes
  • _.inject -> _.reduce
  • _.methods -> _.functions
  • _.object -> _.fromPairs or _.zipObject
  • _.run -> _.value
  • _.select -> _.filter
  • _.unique -> _.uniq

Anna Gernát Updated by Anna Gernát

Contact

Syca (opens in a new tab)

Powered by HelpDocs (opens in a new tab)