UNPKG

lightswitch-js-sdk

Version:

light switch javascript sdk

63 lines (62 loc) 1.98 kB
import CryptoJS from 'crypto-js'; export async function getRequest(url) { 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, data) { 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, iterations) { 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; if (value === 100) { return getHashedPercentageForObjectIds(objectIds, iterations + 1); } return value; } export function compareObjectsAndMaps(obj, map) { // 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; }