Skip to content

Commit 6edf7ff

Browse files
author
DavidQ
committed
Move Asteroids-local number sanitizers into src/shared/math/numberNormalization.js and replace local definitions with shared imports.
1 parent 77f9254 commit 6edf7ff

10 files changed

Lines changed: 88 additions & 20 deletions

docs/dev/CODEX_COMMANDS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ MODEL: GPT-5.4-codex
22
REASONING: high
33

44
COMMAND:
5-
Execute exactly docs/pr/BUILD_PR_SHARED_EXTRACTION_52_ASNUMBER_TO_SHARED_MATH.md.
5+
Execute exactly docs/pr/BUILD_PR_SHARED_EXTRACTION_53_ASTEROIDS_NUMBER_SANITIZERS_TO_SHARED_MATH.md.
66
Modify only the exact target files listed in the PR doc.
77
Do not expand scope.
88
Package the delta zip to:
9-
<project folder>/tmp/BUILD_PR_SHARED_EXTRACTION_52_ASNUMBER_TO_SHARED_MATH_delta.zip
9+
<project folder>/tmp/BUILD_PR_SHARED_EXTRACTION_53_ASTEROIDS_NUMBER_SANITIZERS_TO_SHARED_MATH_delta.zip

docs/dev/COMMIT_COMMENT.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Promote asNumber into src/shared/math/numberNormalization.js and migrate the two exact network debug utility duplicates.
1+
Move Asteroids-local number sanitizers into src/shared/math/numberNormalization.js and replace local definitions with shared imports.

docs/dev/NEXT_COMMAND.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
After commit, continue with BUILD_PR_SHARED_EXTRACTION_53_ASTEROIDS_NUMBER_SANITIZERS_TO_SHARED_MATH using exact files only.
1+
After commit, inspect remaining number-related helpers and continue with the next exact shared-math extraction cluster only if exact files and consumer replacement are known.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
- Promote duplicated asNumber helper into canonical shared math
2-
- Remove duplicate local asNumber definitions from the two exact network debug utility files
3-
- Preserve existing exports and caller-facing API
1+
- Promote Asteroids-local finite/positive number sanitizers into canonical shared math
2+
- Remove local numeric helper duplication from AsteroidsWorld.js
3+
- Preserve exact behavior and existing gameplay usage

docs/dev/reports/file_tree.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
src/shared/math/numberNormalization.js
2-
src/shared/utils/networkDebugUtils.js
3-
src/engine/debug/network/shared/networkDebugUtils.js
4-
docs/pr/BUILD_PR_SHARED_EXTRACTION_52_ASNUMBER_TO_SHARED_MATH.md
2+
games/Asteroids/game/AsteroidsWorld.js
3+
docs/pr/BUILD_PR_SHARED_EXTRACTION_53_ASTEROIDS_NUMBER_SANITIZERS_TO_SHARED_MATH.md
54
docs/dev/codex_commands.md
65
docs/dev/commit_comment.txt
76
docs/dev/next_command.txt

docs/dev/reports/validation_checklist.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
- Confirm imports resolve correctly after the change
55
- Confirm no dead local helper remains in the touched files for this PR purpose
66
- Confirm no unrelated refactor or formatting-only churn was introduced
7-
- Confirm `asObject` and `asArray` were not changed in this PR
7+
- Confirm no non-number helper in AsteroidsWorld.js was changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# BUILD PR — Move Asteroids number sanitizers into shared math
2+
3+
## Purpose
4+
Normalize Asteroids-local numeric sanitizers into the canonical numeric home.
5+
6+
## Exact Target Files
7+
- `src/shared/math/numberNormalization.js`
8+
- `games/Asteroids/game/AsteroidsWorld.js`
9+
10+
## Required Code Changes
11+
1. In `src/shared/math/numberNormalization.js`
12+
- add/export exact-behavior shared equivalents for:
13+
- Asteroids finite-number sanitizer
14+
- Asteroids positive-number sanitizer
15+
- preserve current Asteroids runtime behavior exactly
16+
17+
2. In `games/Asteroids/game/AsteroidsWorld.js`
18+
- stop defining the local number sanitizer helpers
19+
- import the new shared math helper(s)
20+
- preserve all existing call-site behavior
21+
22+
## Hard Constraints
23+
- exact files only
24+
- do not touch any non-number helper in `AsteroidsWorld.js`
25+
- do not modify other Asteroids files
26+
- do not modify `src/shared/utils/numberUtils.js` in this PR
27+
- do not refactor unrelated gameplay/state logic
28+
- preserve behavior exactly; this is extraction, not semantic change
29+
30+
## Acceptance Criteria
31+
- Asteroids number sanitizers live in `src/shared/math/numberNormalization.js`
32+
- `games/Asteroids/game/AsteroidsWorld.js` uses shared math imports instead of local helper definitions
33+
- no local duplicate sanitizer implementation remains in `AsteroidsWorld.js`
34+
- no gameplay behavior change
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# BUILD PR — Promote asObject / asArray into shared utils
2+
3+
## Purpose
4+
Remove duplication of object/array helpers across engine + shared debug utilities.
5+
6+
## Exact Target Files
7+
- src/shared/utils/objectUtils.js
8+
- src/shared/utils/networkDebugUtils.js
9+
- src/engine/debug/network/shared/networkDebugUtils.js
10+
11+
## Required Code Changes
12+
1. In src/shared/utils/objectUtils.js
13+
- ensure/export:
14+
- asObject(value)
15+
- asArray(value)
16+
17+
2. In src/shared/utils/networkDebugUtils.js
18+
- remove local asObject / asArray
19+
- import from objectUtils
20+
21+
3. In src/engine/debug/network/shared/networkDebugUtils.js
22+
- remove local asObject / asArray
23+
- import from shared utils
24+
25+
## Constraints
26+
- exact files only
27+
- do not touch asNumber (already handled)
28+
- no repo scan
29+
- no refactor
30+
31+
## Acceptance
32+
- one canonical implementation
33+
- both files import shared
34+
- no duplicates remain

games/Asteroids/game/AsteroidsWorld.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Ufo from '../entities/Ufo.js';
1111
import { arePolygonsColliding } from '../../../src/engine/collision/index.js';
1212
import { distance } from '../../../src/engine/utils/index.js';
1313
import { randomRange } from '../utils/math.js';
14+
import { sanitizeFiniteNumber, sanitizePositiveNumber } from '../../../src/shared/math/numberNormalization.js';
1415

1516
const WAVE_ASTEROID_COUNTS = [4, 6, 8];
1617
const RESPAWN_SAFE_DISTANCE = 100;
@@ -31,10 +32,6 @@ const WAVE_SPAWN_ATTEMPTS = 60;
3132
const ASTEROID_SPAWN_SAFE_PADDING = 24;
3233
const MAX_UPDATE_STEP_SECONDS = 1 / 60;
3334

34-
function sanitizeFiniteNumber(value, fallback) {
35-
return Number.isFinite(value) ? value : fallback;
36-
}
37-
3835
function sanitizeBoolean(value, fallback = false) {
3936
return typeof value === 'boolean' ? value : fallback;
4037
}
@@ -46,11 +43,6 @@ function sanitizeInteger(value, fallback, { min = -Infinity, max = Infinity } =
4643
return Math.max(min, Math.min(max, Math.trunc(value)));
4744
}
4845

49-
function sanitizePositiveNumber(value, fallback) {
50-
const number = sanitizeFiniteNumber(value, fallback);
51-
return number > 0 ? number : fallback;
52-
}
53-
5446
function sanitizeArray(value) {
5547
return Array.isArray(value) ? value : [];
5648
}

src/shared/math/numberNormalization.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ export function asNumber(value, fallback = 0) {
88
return Number.isFinite(numeric) ? numeric : fallback;
99
}
1010

11+
export function sanitizeFiniteNumber(value, fallback) {
12+
return Number.isFinite(value) ? value : fallback;
13+
}
14+
15+
export function sanitizePositiveNumber(value, fallback) {
16+
const number = sanitizeFiniteNumber(value, fallback);
17+
return number > 0 ? number : fallback;
18+
}
19+
1120
export function roundNumber(value) {
1221
if (!Number.isFinite(value)) {
1322
return value;

0 commit comments

Comments
 (0)