All files / src/functions updateSocketData.ts

88.88% Statements 16/18
85.71% Branches 6/7
100% Functions 3/3
88.88% Lines 16/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 382x     2x       3x 3x 3x   3x 2x     1x 3x       1x 2x 2x 1x   1x       1x             2x  
import {cloneDeep} from 'lodash';
import {ITicker} from '../interfaces';
 
const updateSocketData = (
  originalData: ITicker[],
  newData: ITicker[],
): ITicker[] => {
  try {
    const copyOriginal = cloneDeep(originalData);
    const copyNew = cloneDeep(newData);
 
    if (!copyOriginal.length || !copyNew.length) {
      return copyOriginal || copyNew;
    }
 
    const copyOriginalIndexed = copyOriginal.reduce(
      (acc, curr) => ({...acc, [curr.code]: curr}), // curr을 통해 copyOriginal 원소의 reference를 넘김
      {} as {[key: string]: ITicker},
    );
 
    copyNew.forEach(ele => {
      const target = copyOriginalIndexed[ele.code];
      if (target) {
        Object.assign(target, ele); // copyOriginal 원소의 reference인 target을 Object.assign하여 덮어씌움으로 copyOriginal까지 변경
      } else {
        copyOriginal.push(ele);
      }
    });
 
    return copyOriginal;
  } catch (error) {
    console.error(error);
    return originalData;
  }
};
 
export default updateSocketData;