import { entries } from "alcalzone-shared/objects" ; import { ZWaveError , ZWaveErrorCodes } from "../error/ZWaveError" ; /** Ensures that the values array is consecutive */ export function isConsecutiveArray ( values ) { return values . every ( ( v , i , arr ) => ( i === 0 ? true : v - 1 === arr [ i - 1 ] ) ) ; } /** Returns an object that includes all non-undefined properties from the original one */ export function stripUndefined < T > ( obj ) { const ret = { } as Record < string , T > ; for ( const [ key , value ] of entries ( obj ) ) { if ( value !== undefined ) ret [ key ] = value ; } return ret ; } param assertions type PayloadValidationFunction = ( ... assertions : unknown [ ] ) => void ; interface ValidatePayload extends PayloadValidationFunction { param reason withReason ( reason ) : PayloadValidationFunction ; param reason fail ( reason ) : never ; } function validatePayloadInternal ( reason , ... assertions ) { if ( ! assertions . every ( Boolean ) ) { throw new ZWaveError ( "The message payload is invalid!" , ZWaveErrorCodes . PacketFormat_InvalidPayload , reason ) ; } } export const validatePayload = validatePayloadInternal . bind ( undefined , undefined ) as ValidatePayload ; validatePayload . withReason = ( reason ) => validatePayloadInternal . bind ( undefined , reason ) ; validatePayload . fail = ( reason ) => validatePayload . withReason ( reason ) ( false ) as unknown as never ; /**
 * Determines how many bits a value must be shifted to be right-aligned with a given bit mask
 * Example:
 * ```txt
 *   Mask = 00110000
 *             ^---- => 4 bits
 *
 *   Mask = 00110001
 *                 ^ => 0 bits
 * ```
 */ export function getMinimumShiftForBitMask ( mask ) { let i = 0 ; while ( mask % 2 === 0 ) { mask >>>= 1 ; if ( mask === 0 ) break ; i ++ ; } return i ; } /**
 * Determines how many wide a given bit mask is
 * Example:
 * ```txt
 *   Mask = 00110000
 *            ^^---- => 2 bits
 *
 *   Mask = 00110001
 *            ^....^ => 6 bits
 * ```
 */ export function getBitMaskWidth ( mask ) { mask = mask >>> getMinimumShiftForBitMask ( mask ) ; let i = 0 ; while ( mask > 0 ) { mask >>>= 1 ; i ++ ; } return i ; }