From 99429c875128214d2af9d88e705e9a756da90a24 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Mon, 15 Jun 2026 12:29:38 -0400 Subject: [PATCH] Make union() deduplicate regardless of array size union() used a Set for large inputs but a plain copy of the first array for small ones, so duplicates in the first array survived only on the small-array path. union([1, 1, 2], [2, 3]) returned [1, 1, 2, 3] instead of [1, 2, 3]. Dedupe the first array on the small path too. --- lib/utils/objectUtils.js | 14 +++++++++----- tests/unit/utils.js | 8 +++++++- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/utils/objectUtils.js b/lib/utils/objectUtils.js index 4de3c2891..664a1d1b4 100644 --- a/lib/utils/objectUtils.js +++ b/lib/utils/objectUtils.js @@ -123,13 +123,17 @@ function union(arr1, arr2) { } function unionSmall(arr1, arr2) { - const all = arr1.slice(); + const all = []; - for (let i = 0, l = arr2.length; i < l; ++i) { - const item = arr2[i]; + for (let i = 0, l = arr1.length; i < l; ++i) { + if (all.indexOf(arr1[i]) === -1) { + all.push(arr1[i]); + } + } - if (all.indexOf(item) === -1) { - all.push(item); + for (let i = 0, l = arr2.length; i < l; ++i) { + if (all.indexOf(arr2[i]) === -1) { + all.push(arr2[i]); } } diff --git a/tests/unit/utils.js b/tests/unit/utils.js index b0fb68864..426561b7d 100644 --- a/tests/unit/utils.js +++ b/tests/unit/utils.js @@ -12,7 +12,7 @@ const { const { range } = require('lodash'); const { compose, mixin } = require('../../lib/utils/mixin'); const { map } = require('../../lib/utils/promiseUtils'); -const { jsonEquals, uniqBy } = require('../../lib/utils/objectUtils'); +const { jsonEquals, uniqBy, union } = require('../../lib/utils/objectUtils'); describe('utils', () => { describe('mixin', () => { @@ -450,4 +450,10 @@ describe('utils', () => { ).to.eql(items); }); }); + + describe('union', () => { + it('does not keep duplicates from the first array', () => { + expect(union([1, 1, 2], [2, 3])).to.eql([1, 2, 3]); + }); + }); });