-
Notifications
You must be signed in to change notification settings - Fork 0
clamp param
awekrx edited this page May 29, 2026
·
1 revision
import { clampParam } from '@dev-suite/decorators/clamp-param'
parameter
Clamp numeric parameter to a min/max range before business logic.
- Manual
Math.min/Math.maxin every handler - Repeated range-normalization helpers
class GameService {
setVolume(level: number) {
const safe = Math.max(0, Math.min(100, level));
return this.audio.setVolume(safe);
}
}import { clampParam } from '@dev-suite/decorators/clamp-param';
class GameService {
setVolume(@clampParam({ min: 0, max: 100 }) level: number) {
return this.audio.setVolume(level);
}
}- Centralizes cross-cutting behavior.
- Method/class/property code stays focused on domain logic.
class ShippingApi {
estimate(weightKg: number) {
const safe = Math.max(0.1, Math.min(70, weightKg));
return this.rates.estimate(safe);
}
}import { clampParam } from '@dev-suite/decorators/clamp-param';
class ShippingApi {
estimate(@clampParam({ min: 0.1, max: 70 }) weightKg: number) {
return this.rates.estimate(weightKg);
}
}- Second scenario reuses same policy without duplication.
- Behavior is more consistent and easier to audit.