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' .
//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:
_.bindThis function explicitly sets the context (this) for the given function.
//Příklad
var boundFunction = _.bind(func, context);
Šipkové funkceThese 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
thisArgparameter : Methods no longer acceptthisArg. Use_.bindor arrow functions instead.
Removal Method:
_.pluck: Use_.mapwith iteratee shorthand._.whereand_.findWhere: Use_.filterand_.findwith iteratee shorthand._.support: Removed.
Method Renames:
_.firstto_.head_.indexByto_.keyBy_.invoketo_.invokeMap_.padLeftand_.padRightto_.padStartand_.padEnd_.pairsto_.toPairs_.restto_.tail_.sortByOrderto_.orderBy_.trimLeftand_.trimRightto_.trimStartand_.trimEnd
Split Methods:
_.max&_.mininto_.maxBy&_.minBy_.assign&_.assignIninto_.assignWith&_.assignInWith_.clone&_.cloneDeepinto_.cloneWith&_.cloneDeepWith_.indexOf&_.lastIndexOfinto_.sortedIndexOf&_.sortedLastIndexOf_.isEqualinto_.isEqualWith_.isMatchinto_.isMatchWith_.mergeinto_.mergeWith_.omit&_.pickinto_.omitBy&_.pickBy_.sampleinto_.sampleSize_.sortedIndexinto_.sortedIndexBy_.sortedLastIndexinto_.sortedLastIndexBy_.suminto_.sumBy_.uniqinto_.sortedUniq,_.sortedUniqBy, &_.uniqBy_.zipObjectinto_.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->_.fromPairsor_.zipObject_.run->_.value_.select->_.filter_.unique->_.uniq
Updated
by Anna Gernát