export function shuffleAndTruncate()

in packages/synthetics-sdk-broken-links/src/link_utils.ts [267:285]


export function shuffleAndTruncate(
  links: LinkIntermediate[],
  link_limit: number,
  link_order: BrokenLinksResultV1_BrokenLinkCheckerOptions_LinkOrder
): LinkIntermediate[] {
  // shuffle links if link_order is `RANDOM` and truncate to link_limit

  // Shuffle the links if link_order is RANDOM, or copy the original array
  const linksToFollow =
    link_order === BrokenLinksResultV1_BrokenLinkCheckerOptions_LinkOrder.RANDOM
      ? [...links]
          .map((value) => ({ value, sort: Math.random() }))
          .sort((a, b) => a.sort - b.sort)
          .map(({ value }) => value)
      : [...links];

  // Truncate the processed array to match the link_limit
  return linksToFollow.slice(0, link_limit! - 1);
}