Fix median sort, set-op duplicates, and unescape Unicode range
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 #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 #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 #2242.
This commit is contained in:
parent
6a3a370bb1
commit
618863ecf1
@ -108,10 +108,11 @@ export function mean(data) {
|
|||||||
* @returns {BigNumber}
|
* @returns {BigNumber}
|
||||||
*/
|
*/
|
||||||
export function median(data) {
|
export function median(data) {
|
||||||
if ((data.length % 2) === 0 && data.length > 0) {
|
if (data.length === 0) return data[0];
|
||||||
data.sort(function(a, b) {
|
data.sort(function(a, b) {
|
||||||
return a.minus(b);
|
return a.minus(b);
|
||||||
});
|
});
|
||||||
|
if ((data.length % 2) === 0) {
|
||||||
const first = data[Math.floor(data.length / 2)];
|
const first = data[Math.floor(data.length / 2)];
|
||||||
const second = data[Math.floor(data.length / 2) - 1];
|
const second = data[Math.floor(data.length / 2) - 1];
|
||||||
return mean([first, second]);
|
return mean([first, second]);
|
||||||
|
|||||||
@ -75,7 +75,7 @@ class SetDifference extends Operation {
|
|||||||
* @returns {Object[]}
|
* @returns {Object[]}
|
||||||
*/
|
*/
|
||||||
runSetDifference(a, b) {
|
runSetDifference(a, b) {
|
||||||
return a
|
return [...new Set(a)]
|
||||||
.filter((item) => {
|
.filter((item) => {
|
||||||
return b.indexOf(item) === -1;
|
return b.indexOf(item) === -1;
|
||||||
})
|
})
|
||||||
|
|||||||
@ -75,7 +75,7 @@ class SetIntersection extends Operation {
|
|||||||
* @returns {Object[]}
|
* @returns {Object[]}
|
||||||
*/
|
*/
|
||||||
runIntersect(a, b) {
|
runIntersect(a, b) {
|
||||||
return a
|
return [...new Set(a)]
|
||||||
.filter((item) => {
|
.filter((item) => {
|
||||||
return b.indexOf(item) > -1;
|
return b.indexOf(item) > -1;
|
||||||
})
|
})
|
||||||
|
|||||||
@ -56,7 +56,8 @@ class UnescapeUnicodeCharacters extends Operation {
|
|||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const prefix = prefixToRegex[args[0]],
|
const prefix = prefixToRegex[args[0]],
|
||||||
regex = new RegExp(prefix+"([a-f\\d]{4})", "ig");
|
quantifier = args[0] === "U+" ? "{4,6}" : "{4}",
|
||||||
|
regex = new RegExp(prefix+"([a-f\\d]"+quantifier+")", "ig");
|
||||||
let output = "",
|
let output = "",
|
||||||
m,
|
m,
|
||||||
i = 0;
|
i = 0;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user