UNPKG

flagsmith-nodejs

Version:

Flagsmith lets you manage features flags and remote config across web, mobile and server side applications. Deliver true Continuous Integration. Get builds out faster. Control who has access to new features.

26 lines (25 loc) 1.27 kB
import { createHash } from 'node:crypto'; const md5 = (data) => createHash('md5').update(data).digest('hex'); const makeRepeated = (arr, repeats) => Array.from({ length: repeats }, () => arr).flat(); // https://stackoverflow.com/questions/12532871/how-to-convert-a-very-large-hex-number-to-decimal-in-javascript /** * Given a list of object ids, get a floating point number between 0 and 1 based on * the hash of those ids. This should give the same value every time for any list of ids. * * @param {Array<any>} objectIds list of object ids to calculate the has for * @param {} iterations=1 num times to include each id in the generated string to hash * @returns number number between 0 (inclusive) and 100 (exclusive) */ export function getHashedPercentageForObjIds(objectIds, iterations = 1) { let toHash = makeRepeated(objectIds, iterations).join(','); const hashedValue = md5(toHash); const hashedInt = BigInt('0x' + hashedValue); const value = (Number(hashedInt % 9999n) / 9998.0) * 100; // we ignore this for it's nearly impossible use case to catch /* istanbul ignore next */ if (value === 100) { /* istanbul ignore next */ return getHashedPercentageForObjIds(objectIds, iterations + 1); } return value; }