(rate: unknown)
| 262 | * @returns {boolean} |
| 263 | */ |
| 264 | export function isValidSampleRate(rate: unknown): boolean { |
| 265 | // we need to check NaN explicitly because it's of type 'number' and therefore wouldn't get caught by this typecheck |
| 266 | if ((typeof rate !== 'number' && typeof rate !== 'boolean') || (typeof rate === 'number' && isNaN(rate))) { |
| 267 | DEBUG_BUILD && |
| 268 | debug.warn( |
| 269 | `[Profiling] Invalid sample rate. Sample rate must be a boolean or a number between 0 and 1. Got ${JSON.stringify( |
| 270 | rate, |
| 271 | )} of type ${JSON.stringify(typeof rate)}.`, |
| 272 | ); |
| 273 | return false; |
| 274 | } |
| 275 | |
| 276 | // Boolean sample rates are always valid |
| 277 | if (rate === true || rate === false) { |
| 278 | return true; |
| 279 | } |
| 280 | |
| 281 | // in case sampleRate is a boolean, it will get automatically cast to 1 if it's true and 0 if it's false |
| 282 | if (rate < 0 || rate > 1) { |
| 283 | DEBUG_BUILD && debug.warn(`[Profiling] Invalid sample rate. Sample rate must be between 0 and 1. Got ${rate}.`); |
| 284 | return false; |
| 285 | } |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Checks if the profile is valid and can be sent to Sentry. |
no test coverage detected