UNPKG

@nacelle/compatibility-connector

Version:

Connect @nacelle/client-js-sdk to Nacelle's v2 back end with minimal code changes

31 lines (29 loc) 958 B
/** * Sanitize integers from user inputs. If the `input` is: * - not an integer, * - below an optional `minimum` value, or * - above an optional `maximum` value * * , we fall back to a `defaultValue`. * @param input could be anything...hopefully a valid integer 🤞 * @param defaultValue the value that should be used if `input` is not an integer, less than the `minimum`, or more than the `maximum` * @param minimum the minimum allowed value of `input` * @param maximum the maximum allowed value of `input` * @returns an integer within the `minimum`-`maximum` bounds, or the `defaultValue` */ export function sanitizeIntegerValue( input: unknown, defaultValue: number, minimum: number = Number.MIN_SAFE_INTEGER, maximum: number = Number.MAX_SAFE_INTEGER ): number { if ( typeof input === 'number' && Number.isInteger(input) && minimum <= input && input <= maximum ) { return input; } return defaultValue; }