Trying to extract common functionality among these

I have below method where i am using reducer to set these dictionaries “earliestOptionByInitialRevision” and “latestOptionByInitialRevision” inside the reducer and the code is looks like as below

 const lookups = optionsInput?.reduce(
    (acc, option) => {
      const [optionById, earliestOptionByInitialRevision, latestOptionByInitialRevision] = acc;
      optionById[option.id] = option;
      const isCustomProject = option.initialRevisionId === null || option.initialRevisionId === undefined;
      if (
        isCustomProject ||
        !earliestOptionByInitialRevision[option.initialRevisionId] ||
        option.revision < earliestOptionByInitialRevision[option.initialRevisionId].revision
      ) {
          // trying to extract the below into common 
          //function because in  below if condition i have used the same and difference is
         // "earliestOptionByInitialRevision"

        if (isCustomProject) { 
          option = {...option, initialRevisionId:'customProjectOption'}
          if (!earliestOptionByInitialRevision[option.initialRevisionId]) 
          {
            earliestOptionByInitialRevision[option.initialRevisionId] = [option];
          } else {
            earliestOptionByInitialRevision[option.initialRevisionId].push(option);
          }
        } else {
          earliestOptionByInitialRevision[option.initialRevisionId] = option;
        }
      }
      if (
        isCustomProject ||
        !latestOptionByInitialRevision[option.initialRevisionId] ||
        option.revision > latestOptionByInitialRevision[option.initialRevisionId].revision
      ) {
        // the below if condition same as with above and the difference            
        // is "latestOptionByInitialRevision"

        if (isCustomProject) { 
          option = {...option, initialRevisionId:'customProjectOption'}
          if (!latestOptionByInitialRevision[option.initialRevisionId]) {
            latestOptionByInitialRevision[option.initialRevisionId] = [option];
          } else {
            latestOptionByInitialRevision[option.initialRevisionId].push(option);
          }
        } else {
          latestOptionByInitialRevision[option.initialRevisionId] = option;
        }
      }
      return acc;
    },
    [{}, {}, {}]
  ) ?? [{}, {}, {}];

  const [optionById, earliestOptionByInitialRevision, latestOptionByInitialRevision] = lookups;

i would like to extract the below common functionality but could not be able to get through on how to achieve the same. Could any one please help on this that would be very grateful to me

  if (isCustomProject) {
    option = {...option, initialRevisionId:'customProjectOption'}
    if (!earliestOptionByInitialRevision[option.initialRevisionId]) {
      earliestOptionByInitialRevision[option.initialRevisionId] = [option];
    } else {
      earliestOptionByInitialRevision[option.initialRevisionId].push(option);
    }
  } else {
    earliestOptionByInitialRevision[option.initialRevisionId] = option;
  }

Many thanks in advance update:

enter image description here

Answer

A quick fix could be to extract the common functionality into a function, parameterised by the dict (JavaScript object).

const optionsInput = [{
    id: 1,
    revision: 2
  },
  {
    id: 2,
    revision: 1
  },
];

  const lookups = optionsInput?.reduce(
    (acc, option) => {
      const [optionById, earliestOptionByInitialRevision, latestOptionByInitialRevision] = acc;
      optionById[option.id] = option;
      const isCustomProject = option.initialRevisionId === null || option.initialRevisionId === undefined;

      const updateDict = (dict) => {
        const newDict = {...dict};
        if (isCustomProject || !dict[option.initialRevisionId] || option.revision < dict[option.initialRevisionId].revision) {
          if (isCustomProject) {
            let newOption = {...option, initialRevisionId:'customProjectOption'}
            if (!dict[option.initialRevisionId]) {
              newDict[option.initialRevisionId] = [newOption];
            } else {
              newDict[option.initialRevisionId].push(newOption);
            }
          } else {
            newDict[option.initialRevisionId] = option;
          }
        }
        
        return newDict;
      };

      return [optionById, updateDict(earliestOptionByInitialRevision), updateDict(latestOptionByInitialRevision)];
    },
    [{}, {}, {}]
  ) ?? [{}, {}, {}];

console.log(lookups);