Skip to content

Fix median sort, set-op duplicates, and Unescape Unicode range (#2239, #2241, #2242)#2457

Open
vigneshrajan94 wants to merge 1 commit into
gchq:masterfrom
vigneshrajan94:fix/median-set-unescape-bugs
Open

Fix median sort, set-op duplicates, and Unescape Unicode range (#2239, #2241, #2242)#2457
vigneshrajan94 wants to merge 1 commit into
gchq:masterfrom
vigneshrajan94:fix/median-set-unescape-bugs

Conversation

@vigneshrajan94

Copy link
Copy Markdown

Summary

Fixes three independent bugs in a single PR.

#2239 — Median returns wrong result for unsorted odd-length inputs

src/core/lib/Arithmetic.mjsmedian()

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.mjsrun()

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
  • Median with input 10,1,2 (Comma delimiter) → 2
  • Set Difference with sample 1 x,x,y, sample 2 yx
  • Set Intersection with sample 1 y,y,z, sample 2 yy
  • Unescape Unicode Characters (prefix U+), input U+1F600 → 😀
  • Unescape Unicode Characters (prefix U+), input U+000041A
  • Unescape Unicode Characters (prefix \u), input AA (4-digit still works)

Fixes three independent bugs:

- Arithmetic.mjs (median): sort was only applied for even-length arrays;
  odd-length inputs returned the middle element of the unsorted array.
  Sort is now unconditional. Fixes gchq#2239.

- SetDifference / SetIntersection: plain .filter() on array a preserved
  duplicate entries from the first input, violating set semantics.
  Deduplicate a via [...new Set(a)] before filtering. Fixes gchq#2241.

- UnescapeUnicodeCharacters: regex quantifier was hardcoded to {4} for
  all prefixes. U+ notation allows 4-6 hex digits (e.g. U+1F600 for 😀);
  \u and %u remain at exactly {4} per their respective specs. Fixes gchq#2242.
@HackingRepo

Copy link
Copy Markdown

@vigneshrajan94, @GCHQDeveloper58, will say you what ai you used?

@C85297

C85297 commented Jun 15, 2026

Copy link
Copy Markdown
Member

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants