fix(ui-migration): frameIndex 验证(拒绝非整数与 undefined/NaN)

问题:\`||\` 运算符接受任何 falsy 值(undefined/NaN/null)为帧 1,
违反「零兜底」原则。brief 仅明确规定 frameIndex=0 视同 1。

修法:
- 将 \`frameIndex || 1\` 改为 \`frameIndex === 0 ? 1 : frameIndex\`
- 在 frameRect 中加入 Number.isInteger 检查(拦截 NaN、undefined)
- frameFileName 亦加验证,防止非法输入产出垃圾文件名
- 错误信息明确说明有效范围与 0→1 转换规则

新增 4 条测试用例覆盖 undefined 与 NaN,确保非整数被正确拒绝。

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-28 00:53:42 +08:00
co-authored by Claude Opus 5
parent f1ab6e7fd9
commit e1f67dde1c
2 changed files with 29 additions and 4 deletions
@@ -5,10 +5,11 @@
/** 帧号 → 源矩形。FrameIndex=0 按帧 1 处理(实测仅 2 个对象如此)。 */
export function frameRect(image, frameIndex) {
const idx = (frameIndex || 1) - 1;
if (idx < 0 || idx >= image.frame_all) {
const normalized = frameIndex === 0 ? 1 : frameIndex;
const idx = normalized - 1;
if (!Number.isInteger(idx) || idx < 0 || idx >= image.frame_all) {
throw new RangeError(
`帧号越界: ${image.bmp} frameIndex=${frameIndex}(有效范围 1..${image.frame_all})`,
`帧号非法或越界: ${image.bmp} frameIndex=${frameIndex}(有效范围 1..${image.frame_all},0 视同 1)`,
);
}
return {
@@ -27,8 +28,14 @@ export function padWidth(frameCount) {
/** 00014.png + 帧 5/共 12 → 00014_05.png;单帧图原样返回。 */
export function frameFileName(bmp, frameIndex, frameCount) {
if (frameCount <= 1) return bmp;
const normalized = frameIndex === 0 ? 1 : frameIndex;
if (!Number.isInteger(normalized) || normalized <= 0) {
throw new RangeError(
`帧号非法: ${bmp} frameIndex=${frameIndex}(有效范围 >= 1,0 视同 1)`,
);
}
const base = bmp.replace(/\.png$/i, '');
const n = String(frameIndex || 1).padStart(padWidth(frameCount), '0');
const n = String(normalized).padStart(padWidth(frameCount), '0');
return `${base}_${n}.png`;
}