All files / lib utils.ts

97.29% Statements 36/37
88.88% Branches 8/9
100% Functions 5/5
97.22% Lines 35/36

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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 772x   2x 4x 4x 2x 1x   1x   3x 3x         2x 8x 8x             6x 1x   5x   3x 3x       2x       3x 3x   3x 3x   3x   3x       3x     2x         5x 9x 9x 2x         3x 8x 6x 1x       2x    
import CryptoJS from 'crypto-js';
import { Property } from './types';
export async function getRequest(url: string): Promise<any> {
  try {
    const response = await fetch(url);
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return await response.json();
  } catch (error) {
    console.error('There was a problem with the fetch operation:', error);
    throw error;
  }
}
 
// POST 요청
export async function postRequest(url: string, data?: any): Promise<any> {
  try {
    const response = await fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(data),
    });
    if (!response.ok) {
      throw new Error('Network response was not ok');
    }
    return await response.json();
  } catch (error) {
    console.error('There was a problem with the fetch operation:', error);
    throw error;
  }
}
 
export function getHashedPercentageForObjectIds(
  objectIds: string[],
  iterations: number,
): number {
  let toHash = objectIds.join(',');
  toHash = toHash.repeat(Math.max(0, iterations));
 
  const hashedBytes = CryptoJS.MD5(toHash).toString();
  const no = BigInt('0x' + hashedBytes);
 
  let value = (Number(no % BigInt(9999)) / 9998) * 100;
 
  Iif (value === 100) {
    return getHashedPercentageForObjectIds(objectIds, iterations + 1);
  }
 
  return value;
}
 
export function compareObjectsAndMaps(
  obj: Property[],
  map: Map<string, string>,
): boolean {
  // Object의 키-값 쌍 비교
  for (const item of obj) {
    const { property, data } = item;
    if (!map.has(property) || map.get(property) !== data) {
      return false;
    }
  }
 
  // Map의 키-값 쌍 비교
  for (const [key, value] of map.entries()) {
    const found = obj.find((item) => item.property === key);
    if (!found || found.data !== value) {
      return false;
    }
  }
 
  return true;
}