MCPcopy Create free account
hub / github.com/nodejs/node / shorten_array

Function shorten_array

test/fixtures/wpt/resources/testharness.js:1699–1724  ·  view source on GitHub ↗
(arr, offset = 0)

Source from the content-addressed store, hash-verified

1697 {
1698 const max_array_length = 20;
1699 function shorten_array(arr, offset = 0) {
1700 // Make ", …" only show up when it would likely reduce the length, not accounting for
1701 // fonts.
1702 if (arr.length < max_array_length + 2) {
1703 return arr;
1704 }
1705 // By default we want half the elements after the offset and half before
1706 // But if that takes us past the end of the array, we have more before, and
1707 // if it takes us before the start we have more after.
1708 const length_after_offset = Math.floor(max_array_length / 2);
1709 let upper_bound = Math.min(length_after_offset + offset, arr.length);
1710 const lower_bound = Math.max(upper_bound - max_array_length, 0);
1711
1712 if (lower_bound === 0) {
1713 upper_bound = max_array_length;
1714 }
1715
1716 const output = arr.slice(lower_bound, upper_bound);
1717 if (lower_bound > 0) {
1718 output.beginEllipsis = true;
1719 }
1720 if (upper_bound < arr.length) {
1721 output.endEllipsis = true;
1722 }
1723 return output;
1724 }
1725
1726 assert(typeof actual === "object" && actual !== null && "length" in actual,
1727 "assert_array_equals", description,

Callers 1

assert_array_equalsFunction · 0.70

Calls 3

sliceMethod · 0.65
minMethod · 0.45
maxMethod · 0.45

Tested by

no test coverage detected