Add method overload for fflib_ArrayUtils.replaceValues (#70)

Adds compatibility with Boolean values
This commit is contained in:
William Velzeboer
2026-01-19 09:57:46 +00:00
committed by GitHub
parent 2fe75d9bd1
commit 57a9c3f503
4 changed files with 64 additions and 2 deletions
+3 -1
View File
@@ -16,4 +16,6 @@ package.xml
**/*.ts
**/tsconfig*.json
**/*.tsbuildinfo
**/eslint.config.mjs
**/eslint.config.mjs
**/graphql.config.yml
+3 -1
View File
@@ -59,4 +59,6 @@ sfdx-source/apex-extensions-examples
target/
/.illuminatedCloud/
**/tsconfig*.json
**/*.tsbuildinfo
**/*.tsbuildinfo
**/graphql.config.yml
@@ -400,6 +400,36 @@ public class fflib_ArrayUtils
return result;
}
/**
* Takes two maps and replaces the value of the source with the value of the replacement,
* where the source value matches the replacement key.
* The value will become null if the source value does not exists as key in replacement map
*
* @param source The map to replace its value
* @param replacement The map to use its value for replacement
*
* @return Returns a Map<{Source-key} Id, {Replacement-value} Id>
*/
public static Map<Id, Boolean> replaceValue(Map<Id, Id> source, Map<Id, Boolean> replacement)
{
Map<Id, Boolean> result = new Map<Id, Boolean>();
for (Id keyId : source.keySet())
{
Id sourceValue = source.get(keyId);
if (replacement.containsKey(sourceValue))
{
result.put(keyId, replacement.get(sourceValue));
}
else
{
// There is no replacement value for this keyId
result.put(keyId, null);
}
}
return result;
}
/**
* Takes two maps and replaces the value of the source with the values of the replacement,
* where the source value matches the replacement key.
@@ -322,6 +322,34 @@ private class fflib_ArrayUtilsTest
System.Assert.isNull(result.get(accountIdC));
}
@IsTest
static void itShouldReplaceValueInTheMap_boolean()
{
Id accountIdA = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id accountIdB = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id accountIdC = fflib_IDGenerator.generate(Schema.Account.SObjectType);
Id contactIdA = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Id contactIdB = fflib_IDGenerator.generate(Schema.Contact.SObjectType);
Map<Id, Id> source = new Map<Id, Id>
{
accountIdA => contactIdA,
accountIdB => contactIdB,
accountIdC => fflib_IDGenerator.generate(Schema.Contact.SObjectType)
};
Map<Id, Boolean> replacement = new Map<Id, Boolean>
{
contactIdA => true,
contactIdB => false
};
Map<Id, Boolean> result = fflib_ArrayUtils.replaceValue(source, replacement);
System.Assert.areEqual(3, result.size());
System.Assert.isTrue(result.get(accountIdA));
System.Assert.isFalse(result.get(accountIdB));
System.Assert.isNull(result.get(accountIdC));
}
@IsTest
static void itShouldReturnAllValuesForTheGivenIds()
{