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

Function shorten_array

deps/v8/test/wasm-js/third_party/testharness.js:1234–1259  ·  view source on GitHub ↗
(arr, offset = 0)

Source from the content-addressed store, hash-verified

1232 {
1233 const max_array_length = 20;
1234 function shorten_array(arr, offset = 0) {
1235 // Make ", …" only show up when it would likely reduce the length, not accounting for
1236 // fonts.
1237 if (arr.length < max_array_length + 2) {
1238 return arr;
1239 }
1240 // By default we want half the elements after the offset and half before
1241 // But if that takes us past the end of the array, we have more before, and
1242 // if it takes us before the start we have more after.
1243 const length_after_offset = Math.floor(max_array_length / 2);
1244 let upper_bound = Math.min(length_after_offset + offset, arr.length);
1245 const lower_bound = Math.max(upper_bound - max_array_length, 0);
1246
1247 if (lower_bound === 0) {
1248 upper_bound = max_array_length;
1249 }
1250
1251 const output = arr.slice(lower_bound, upper_bound);
1252 if (lower_bound > 0) {
1253 output.beginEllipsis = true;
1254 }
1255 if (upper_bound < arr.length) {
1256 output.endEllipsis = true;
1257 }
1258 return output;
1259 }
1260
1261 assert(typeof actual === "object" && actual !== null && "length" in actual,
1262 "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