Num.Natural
Num / Natural
Import
import { Num } from '@wollybeard/kit'
// Access via namespace
Num.Natural.someFunction()
import * as Num from '@wollybeard/kit/num'
// Access via namespace
Num.Natural.someFunction()
Functions
[F]
is
(value: unknown): boolean
Parameters:
value
- The value to check
Returns: True if value is a natural number
Type predicate to check if value is a natural number. Returns true for positive integers (1, 2, 3, ...).
[F]
from
(value: number): number & { [NaturalBrand]: true; } & { [IntBrand]: true; } & { [PositiveBrand]: true; }
Parameters:
value
- The number to convert to Natural
Returns: The value as a Natural number
Throws:
- Error if value is not a positive integer
Construct a Natural number. Throws if the value is not a positive integer.
[F]
tryFrom
(value: number): (number & { [NaturalBrand]: true; } & { [IntBrand]: true; } & { [PositiveBrand]: true; }) | null
Parameters:
value
- The number to try converting
Returns: The Natural number or null
Try to construct a Natural number. Returns null if the value is not a positive integer.
[F]
parseAsNatural
(value: string): (number & { [NaturalBrand]: true; } & { [IntBrand]: true; } & { [PositiveBrand]: true; }) | null
Parameters:
value
- The string to parse
Returns: The parsed natural number or null
Parse a string as a natural number. Returns null if the string doesn't represent a positive integer. Note: parseInt("1.5") returns 1, but we check if the original string represents an integer by comparing with the parsed result.
[F]
next
(value: number): number & { [NaturalBrand]: true; } & { [IntBrand]: true; } & { [PositiveBrand]: true; }
Parameters:
value
- The number to get the next natural from
Returns: The next natural number
Get the next natural number. For any number, returns the smallest natural number greater than the input.
[F]
prev
(value: number): (number & { [NaturalBrand]: true; } & { [IntBrand]: true; } & { [PositiveBrand]: true; }) | null
Parameters:
value
- The number to get the previous natural from
Returns: The previous natural number or null
Get the previous natural number. Returns null if there is no previous natural (i.e., for values = 1).
Types
[∩]
Natural
type Natural = number & { [NaturalBrand]: true }
Natural number (positive integer: 1, 2, 3, ...). These are the counting numbers used in everyday life.
Natural numbers are both integers and positive, so they combine both brands for maximum type safety.