Fix median sort, set-op duplicates, and Unescape Unicode range (#2239, #2241, #2242)
Summary
Fixes three independent bugs in a single PR.
#2239 — Median returns wrong result for unsorted odd-length inputs
src/core/lib/Arithmetic.mjs — median()
The .sort() call was inside if (even), so odd-length arrays were never sorted before picking the middle element. Median([10, 1, 2]) returned 1 instead of 2.
Fix: sort unconditionally before branching on even/odd length.
#2241 — Set Difference / Set Intersection preserve duplicates from first input
src/core/operations/SetDifference.mjs, src/core/operations/SetIntersection.mjs
Both operations used a plain .filter() on array a. If a contained duplicate entries that passed the filter, all copies appeared in the output, violating set semantics. SetDifference(["x","x","y"], ["y"]) returned "x,x".
Fix: deduplicate a via [...new Set(a)] before filtering.
#2242 — Unescape Unicode Characters rejects code points > 4 hex digits under U+
src/core/operations/UnescapeUnicodeCharacters.mjs — run()
The regex quantifier was hardcoded to {4} for all prefix types. The U+ notation supports 4–6 hex digits (e.g. U+1F600 for 😀, U+000041 zero-padded). \u and %u remain at {4} per their respective specs.
Fix: use {4,6} when the selected prefix is U+, {4} otherwise.
Test plan
- [ ]
npm test— all 2178 tests pass (243 Node API + 1935 operations), zero failures - [ ]
Medianwith input10,1,2(Comma delimiter) →2 - [ ]
Set Differencewith sample 1x,x,y, sample 2y→x - [ ]
Set Intersectionwith sample 1y,y,z, sample 2y→y - [ ]
Unescape Unicode Characters(prefixU+), inputU+1F600→ 😀 - [ ]
Unescape Unicode Characters(prefixU+), inputU+000041→A - [ ]
Unescape Unicode Characters(prefix\u), inputA→A(4-digit still works)
@vigneshrajan94, @GCHQDeveloper58, will say you what ai you used?
@vigneshrajan94 thank you for your pull request. In general, we would prefer minimally scoped individual pull requests, rather than pull requests incorporating multiple features/bug fixes. Would you mind splitting the pull request into one for each issue? This makes it easier for us to review and to track changes.