SylarLong/lunar-lite

View on GitHub
src/utils.ts

Summary

Maintainability
A
0 mins
Test Coverage
/**
 * 用于处理索引,将索引锁定在 0~max 范围内
 *
 * @param index 当前索引
 * @param max 最大循环数,默认为12【因为12用得最多,宫位数量以及十二地支数量都为12,所以将12作为默认值】
 * @returns {number} 处理后的索引
 */
export const fixIndex = (index: number, max: number = 12): number => {
  if (index < 0) {
    return fixIndex(index + max, max);
  }

  if (index > max - 1) {
    return fixIndex(index - max, max);
  }

  const res = 1 / index === -Infinity ? 0 : index;

  return res;
};