@embrace-io/web-sdk
Version:
1 lines • 1.65 kB
Source Map (JSON)
{"version":3,"file":"isDeviceIdEnabled.cjs","names":[],"sources":["../../src/utils/isDeviceIdEnabled.ts"],"sourcesContent":["const DIGITS = 6;\n\n/**\n * Determines whether a deviceId is enabled for the given pctEnabled. This is achieved\n * by taking a normalized hex value from the last 6 digits of the device ID, and comparing\n * it against the enabled percentage. This ensures that devices are consistently in a given\n * group for beta functionality.\n *\n * This can be used to:\n * - Select sample devices for telemetry collection\n * - Enable/disable features for a percentage of users\n *\n * The normalized device ID has 16^6 possibilities (roughly 1.6m) which should be sufficient\n * granularity for our needs.\n */\nexport const isDeviceIdEnabled = (deviceId: string, pctEnabled?: number) => {\n if (!pctEnabled || pctEnabled <= 0 || pctEnabled > 100) {\n return false;\n }\n\n const normalizedDeviceId = getNormalizedDeviceId(deviceId);\n\n return pctEnabled >= normalizedDeviceId;\n};\n\nexport const getNormalizedDeviceId = (deviceId: string): number => {\n if (deviceId.length < DIGITS) {\n return 0;\n }\n\n const finalChars = deviceId.slice(-DIGITS); // last 6 chars\n const radix = 16;\n const space = radix ** DIGITS - 1;\n const value = parseInt(finalChars, radix);\n\n return (value / space) * 100;\n};\n"],"mappings":";;AAAA,MAAM,SAAS;;;;;;;;;;;;;;AAef,MAAa,qBAAqB,UAAkB,eAAwB;CAC1E,IAAI,CAAC,cAAc,cAAc,KAAK,aAAa,KACjD,OAAO;CAKT,OAAO,cAFoB,sBAAsB,QAEX;AACxC;AAEA,MAAa,yBAAyB,aAA6B;CACjE,IAAI,SAAS,SAAS,QACpB,OAAO;CAGT,MAAM,aAAa,SAAS,MAAM,EAAO;CACzC,MAAM,QAAQ;CAId,OAFc,SAAS,YAAY,KAEvB,KAHE,SAAS,SAAS,KAGP;AAC3B"}