mwg API
    Preparing search index...

    Function bisectShareLink

    • The longest prefix of items whose link fits, found by bisection: build makes the payload for a count, and the answer is the largest count that fits (0 is allowed, for a payload with no items). Counts that do not fit are never tried twice.

      Type Parameters

      • T

      Parameters

      • type: string
      • id: string
      • items: readonly T[]
      • build: (kept: readonly T[], truncated: boolean) => unknown
      • options: ShareEncodeOptions = {}

      Returns Promise<{ kept: number; link: string }>

      import { bisectShareLink } from '@datamoc/mwg/core';

      const notes = Array.from({ length: 500 }, (_, i) => [60 + (i % 12), i]);
      const { link, kept } = await bisectShareLink('music', 'game@1.0.0', notes, (items, cut) => ({ cut, items }), {
      maxChars: 800,
      });
      console.log(link.length <= 800, kept < notes.length); // true true