isOdd
The isOdd utility is a type guard that checks if a given number is an odd integer.
Implementation
View Source Code
ts
/**
* Checks if a number is odd.
*
* @param {number} arg - The number to check.
*
* @returns {boolean} - Returns true if the number is odd, otherwise false.
*/
export function isOdd(arg: unknown): arg is number {
return typeof arg === 'number' && Number.isFinite(arg) && Number.isInteger(arg) && arg % 2 !== 0;
}Features
- Isomorphic: Works in both Browser and Node.js.
- Strict Check: Only returns
truefor valid numbers that are not divisible by 2. - Type-safe: Properly typed for numeric inputs.
API
ts
function isOdd(value: number): boolean;Parameters
value: The number to check.
Returns
trueif the number is odd; otherwise,false.
Examples
Basic Usage
ts
import { isOdd } from '@vielzeug/toolkit';
isOdd(3); // true
isOdd(41); // true
isOdd(2); // false
isOdd(0); // falseImplementation Notes
- Returns
trueifvalue % 2 !== 0. - Throws nothing; returns
falsefor non-numeric inputs if TypeScript checks are bypassed.