Skip to content

clamp param

awekrx edited this page May 29, 2026 · 1 revision

clamp-param

Import

import { clampParam } from '@dev-suite/decorators/clamp-param'

Category

  • parameter

Use Case

Clamp numeric parameter to a min/max range before business logic.

Replaces

  • Manual Math.min/Math.max in every handler
  • Repeated range-normalization helpers

Example 1

Without decorator

class GameService {
  setVolume(level: number) {
    const safe = Math.max(0, Math.min(100, level));
    return this.audio.setVolume(safe);
  }
}

With decorator

import { clampParam } from '@dev-suite/decorators/clamp-param';

class GameService {
  setVolume(@clampParam({ min: 0, max: 100 }) level: number) {
    return this.audio.setVolume(level);
  }
}

Why better

  • Centralizes cross-cutting behavior.
  • Method/class/property code stays focused on domain logic.

Example 2

Without decorator

class ShippingApi {
  estimate(weightKg: number) {
    const safe = Math.max(0.1, Math.min(70, weightKg));
    return this.rates.estimate(safe);
  }
}

With decorator

import { clampParam } from '@dev-suite/decorators/clamp-param';

class ShippingApi {
  estimate(@clampParam({ min: 0.1, max: 70 }) weightKg: number) {
    return this.rates.estimate(weightKg);
  }
}

Why better

  • Second scenario reuses same policy without duplication.
  • Behavior is more consistent and easier to audit.

Clone this wiki locally