Files
youle_cocos/cocoscreator_projects/scripts/ui-migration/frames.mjs
T
joywayerandClaude Opus 5 e1f67dde1c 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>
2026-08-28 00:53:42 +08:00

47 lines
1.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 帧矩形与命名。见 spec §0.3、§3。
* 帧号 1 基、行优先——已由读取 00014.png 的网格内容与 11 个按钮的 FrameIndex 对照实证。
*/
/** 帧号 → 源矩形。FrameIndex=0 按帧 1 处理(实测仅 2 个对象如此)。 */
export function frameRect(image, frameIndex) {
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},0 视同 1)`,
);
}
return {
x: (idx % image.w) * image.w1,
y: Math.floor(idx / image.w) * image.h1,
width: image.w1,
height: image.h1,
};
}
/** 补零位数:保证字典序 = 帧序。 */
export function padWidth(frameCount) {
return String(frameCount).length;
}
/** 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(normalized).padStart(padWidth(frameCount), '0');
return `${base}_${n}.png`;
}
/** 该图全部帧的文件名,下标 0 对应帧 1。 */
export function frameNames(image) {
return Array.from({ length: image.frame_all }, (_, i) =>
frameFileName(image.bmp, i + 1, image.frame_all));
}