UNPKG

@jk-core/components

Version:
25 lines (23 loc) 681 B
import { isDefined } from '@jk-core/utils'; /** * unit으로 나누어떨어지면서, * a가 양수라면, a보다 크되 가장 작은 정수. * a가 음수라면, a보다 작되 가장 큰 정수를 반환. * 절댓값이 threshold에 미치지못한다면, 그대로 반환. * @param {*} value * @param {*} unit */ export const calculateMax = ( value?: number, threshold: number = 10, unit: number = 10, ) => { if (!isDefined(value)) return value; if (unit === 0 || Math.abs(value) < threshold) return value; if (value > 0) { return Math.ceil(value / unit) * unit; } if (value < 0) { return Math.floor(value / unit) * unit; } return value; };