Value Helpers
Small helpers that operate on single values: testing for emptiness, bounding a number, and piping a value through a function.
import { blank, clamp, tap } from '@0x26e/utils'blank
Determines whether the given value is "blank". A value is blank when it is an empty string, a
string of only whitespace, null, undefined, an empty object, or an empty array.
function blank(value: any): booleanParameters
| Parameter | Type | Description |
|---|---|---|
value | any | The value to test. |
Returns boolean — true if the value is blank, false otherwise.
Truth table
| Input | Result |
|---|---|
blank('') | true |
blank(' ') | true |
blank(null) | true |
blank(undefined) | true |
blank({}) | true |
blank([]) | true |
blank(0) | false |
blank(true) | false |
blank(false) | false |
Example
blank('') // true
blank(' ') // true (whitespace only)
blank(null) // true
blank({}) // true
blank([]) // true
blank(0) // false
blank(false) // false
blank('hello') // false
blank([1, 2]) // falseBlank is not the same as falsy
blank is about emptiness, not falsiness. 0, false, and NaN are falsy in JavaScript but
are not blank — they are real values. Whitespace-only strings, on the other hand, are treated as
blank.
clamp
Returns a value clamped to the inclusive range of min and max. If the value is below min
it returns min; if it is above max it returns max; otherwise it returns the value unchanged.
function clamp(value: number, min: number, max: number): numberParameters
| Parameter | Type | Description |
|---|---|---|
value | number | The number to clamp. |
min | number | The lower bound of the range. |
max | number | The upper bound of the range. |
Returns number — the value bounded to [min, max].
Throws Error — when min is greater than max.
Examples
clamp(5, 1, 10) // 5 (already in range)
clamp(15, 1, 10) // 10 (above max → max)
clamp(-5, 1, 10) // 1 (below min → min)
clamp(5, 5, 5) // 5 (min and max equal)Throws when min is greater than max
clamp validates its bounds. If min > max, it throws an Error with a descriptive message rather
than returning a nonsensical value.
try {
clamp(5, 10, 1)
} catch (error) {
console.error(error.message) // 'Minimum (10) is not less than maximum (1).'
}Note that min === max is allowed — it simply pins the result to that single value.
tap
Invokes an interceptor function with the provided value and returns the interceptor's result.
It's a tiny way to pipe a value through a transformation inline.
function tap<T>(value: any, interceptor: (v: any) => T): TParameters
| Parameter | Type | Description |
|---|---|---|
value | any | The value passed to the interceptor. |
interceptor | (v: any) => T | A function that receives the value and returns a result. |
Returns T — whatever the interceptor returns.
Example
const tappedValue = tap(5, (n) => n * 2)
console.log(tappedValue) // 10It returns the interceptor's result
Unlike Laravel's tap, which returns the original value, this tap returns whatever the
interceptor returns. Use it when you want to transform a value in a single expression — the return
type T is inferred from the interceptor.
const name = tap({ first: 'Foysal', last: 'Ahmed' }, (u) => `${u.first} ${u.last}`)
// name === 'Foysal Ahmed'