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:
_.bind
This function explicitly sets the context (this) for the given function.
//Příklad
var boundFunction = _.bind(func, context);
Š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 acceptthisArg
. 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
Updated
by Anna Gernát