Skip to content

Commit 5849118

Browse files
loganrosenCopilot
andauthored
Bug 681635 - Sort and de-duplicate merged product values (#245)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> r=justdave
1 parent 67e2b76 commit 5849118

2 files changed

Lines changed: 111 additions & 46 deletions

File tree

‎js/productform.js‎

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -215,53 +215,24 @@ function fake_diff_array(a, b) {
215215
* @return Merged and sorted array.
216216
*/
217217
function merge_arrays(a, b, b_is_select) {
218-
var pos_a = 0;
219-
var pos_b = 0;
218+
var items = a.slice();
220219
var ret = new Array();
221-
var bitem, aitem;
222-
223-
// Iterate through both arrays and add the larger item to the return
224-
// list. Remove dupes, too. Use toLowerCase to provide
225-
// case-insensitivity.
226-
while ((pos_a < a.length) && (pos_b < b.length)) {
227-
aitem = a[pos_a];
228-
if (b_is_select)
229-
bitem = b[pos_b].value;
230-
else
231-
bitem = b[pos_b];
232-
233-
// Smaller item in list a.
234-
if (aitem.toLowerCase() < bitem.toLowerCase()) {
235-
ret[ret.length] = aitem;
236-
pos_a++;
237-
}
238-
else {
239-
// Smaller item in list b.
240-
if (aitem.toLowerCase() > bitem.toLowerCase()) {
241-
ret[ret.length] = bitem;
242-
pos_b++;
243-
}
244-
else {
245-
// List contents are equal, include both counters.
246-
ret[ret.length] = aitem;
247-
pos_a++;
248-
pos_b++;
249-
}
250-
}
251-
}
252-
253-
// Catch leftovers here. These sections are ugly code-copying.
254-
if (pos_a < a.length)
255-
for (; pos_a < a.length ; pos_a++)
256-
ret[ret.length] = a[pos_a];
257-
258-
if (pos_b < b.length) {
259-
for (; pos_b < b.length; pos_b++) {
260-
if (b_is_select)
261-
bitem = b[pos_b].value;
262-
else
263-
bitem = b[pos_b];
264-
ret[ret.length] = bitem;
220+
var i;
221+
222+
for (i = 0; i < b.length; i++)
223+
items[items.length] = b_is_select ? b[i].value : b[i];
224+
225+
items.sort(function(left, right) {
226+
left = left.toLowerCase();
227+
right = right.toLowerCase();
228+
return left < right ? -1 : left > right ? 1 : 0;
229+
});
230+
231+
for (i = 0; i < items.length; i++) {
232+
if (!ret.length
233+
|| items[i].toLowerCase() != ret[ret.length - 1].toLowerCase())
234+
{
235+
ret[ret.length] = items[i];
265236
}
266237
}
267238

‎t/015productform.t‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
#
5+
# This Source Code Form is "Incompatible With Secondary Licenses", as
6+
# defined by the Mozilla Public License, v. 2.0.
7+
8+
9+
##################
10+
#Bugzilla Test 15#
11+
####Productform###
12+
13+
use 5.10.1;
14+
use strict;
15+
use warnings;
16+
17+
use File::Spec;
18+
use Test::More;
19+
20+
my $node;
21+
foreach my $dir (File::Spec->path()) {
22+
foreach my $name (qw(node node.exe)) {
23+
my $path = File::Spec->catfile($dir, $name);
24+
if (-x $path && !-d $path) {
25+
$node = $path;
26+
last;
27+
}
28+
}
29+
last if $node;
30+
}
31+
32+
if (!$node) {
33+
plan tests => 1;
34+
fail('Node.js is required to test js/productform.js');
35+
exit;
36+
}
37+
38+
my $script = <<'JS';
39+
var fs = require('fs');
40+
var vm = require('vm');
41+
42+
vm.runInThisContext(
43+
fs.readFileSync('js/productform.js', 'utf8'),
44+
{ filename: 'js/productform.js' }
45+
);
46+
47+
console.log(
48+
merge_arrays(
49+
['Trunk', '2.0'],
50+
['unspecified', 'Trunk'],
51+
false
52+
).join('\t')
53+
);
54+
console.log(
55+
merge_arrays(
56+
['Beta', 'alpha'],
57+
[{ value: 'ALPHA' }, { value: 'Release' }],
58+
true
59+
).join('\t')
60+
);
61+
JS
62+
63+
my $pid = open(my $fh, '-|', $node, '-e', $script);
64+
if (!defined $pid) {
65+
plan tests => 1;
66+
fail("could not run $node: $!");
67+
exit;
68+
}
69+
70+
my @results = <$fh>;
71+
close($fh);
72+
my $status = $?;
73+
74+
if ($status || @results != 2) {
75+
plan tests => 1;
76+
fail('js/productform.js did not produce the expected test output');
77+
diag("Node.js exit status: $status");
78+
diag("Node.js output:\n" . join('', @results));
79+
exit;
80+
}
81+
82+
chomp(@results);
83+
plan tests => 2;
84+
85+
is_deeply(
86+
[split(/\t/, $results[0])],
87+
['2.0', 'Trunk', 'unspecified'],
88+
'unsorted product values are sorted and de-duplicated'
89+
);
90+
is_deeply(
91+
[split(/\t/, $results[1])],
92+
['alpha', 'Beta', 'Release'],
93+
'select options are merged, sorted, and de-duplicated'
94+
);

0 commit comments

Comments
 (0)