Repository: mozilla/pdf.js
File: src/display/display_utils.js (JavaScript)

Code before:

    for (const [id, pageNumbers] of idToPageNumber) {
      for (let i = 0, ii = pageNumbers.length; i < ii; i++) {
        extractParams[i].includePages.push([id - 1, pageNumbers[i] - 1]);
      }
    }

    for (const { includePages, pageIndices } of extractParams) {
      includePages.sort((a, b) => a[0] - b[0]);
      for (let i = 0, ii = includePages.length; i < ii; i++) {
        pageIndices.push(includePages[i][1]);
        includePages[i] = includePages[i][0];
      }
    }

    return extractParams;
  }

  extractPages(extractedPageNumbers) {
    extractedPageNumbers = Array.from(extractedPageNumbers).sort(
      (a, b) => a - b
    );
    const usedIds = new Map();
    for (let i = 0, ii = extractedPageNumbers.length; i < ii; i++) {
      const id = this.getPageId(extractedPageNumbers[i]);
      const usedPageNumbers = usedIds.getOrInsertComputed(id, () => []);
      usedPageNumbers.push(i + 1);
    }
    return this.getPageMappingForSaving(usedIds);
  }

  /**
   * Gets the previous page number for a given page number.
   * @param {number} pageNumber
   * @returns {number} The previous page number for the given page number, or 0
   *   if no mapping exists.
   */
  getPrevPageNumber(pageNumber) {
    return this.#prevPageNumbers[pageNumber - 1] ?? 0;
  }

  /**
   * Gets the page number for a given page ID.
   * @param {number} id - The page ID (1-indexed).
   * @returns {number} The page number, or 0 if no mapping exists.
   */
  getPageNumber(id) {
    return this.#idToPageNumber ? (this.#idToPageNumber.get(id)?.[0] ?? 0) : id;
  }

  /**

Review comment (suggestion):
This should use `makeArr` instead after https://github.com/mozilla/pdf.js/commit/0d4e587a5fe782f8baddecbaf7138f39d96ef446.

Code after:
    }

    for (const [id, pageNumbers] of idToPageNumber) {
      for (let i = 0, ii = pageNumbers.length; i < ii; i++) {
        extractParams[i].includePages.push([id - 1, pageNumbers[i] - 1]);
      }
    }

    for (const { includePages, pageIndices } of extractParams) {
      includePages.sort((a, b) => a[0] - b[0]);
      for (let i = 0, ii = includePages.length; i < ii; i++) {
        pageIndices.push(includePages[i][1]);
        includePages[i] = includePages[i][0];
      }
    }

    return extractParams;
  }

  extractPages(extractedPageNumbers) {
    extractedPageNumbers = Array.from(extractedPageNumbers).sort(
      (a, b) => a - b
    );
    const usedIds = new Map();
    for (let i = 0, ii = extractedPageNumbers.length; i < ii; i++) {
      const id = this.getPageId(extractedPageNumbers[i]);
      const usedPageNumbers = usedIds.getOrInsertComputed(id, makeArr);
      usedPageNumbers.push(i + 1);
    }
    return this.getPageMappingForSaving(usedIds);
  }

  /**
   * Gets the previous page number for a given page number.
   * @param {number} pageNumber
   * @returns {number} The previous page number for the given page number, or 0
   *   if no mapping exists.
   */
  getPrevPageNumber(pageNumber) {
    return this.#prevPageNumbers[pageNumber - 1] ?? 0;
  }

  /**
   * Gets the page number for a given page ID.
   * @param {number} id - The page ID (1-indexed).
   * @returns {number} The page number, or 0 if no mapping exists.
   */
  getPageNumber(id) {
    return this.#idToPageNumber ? (this.#idToPageNumber.get(id)?.[0] ?? 0) : id;
  }