All files / src/app/core/models/product-listing product-listing.mapper.ts

100% Statements 14/14
100% Branches 8/8
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 4818x 18x             18x                           29x 30x   29x 29x             30x     29x 2x     29x 2x     29x      
import { Injectable } from '@angular/core';
import { range } from 'lodash-es';
 
import { URLFormParams } from 'ish-core/utils/url-form-params';
 
import { ProductListingType, SortableAttributesType } from './product-listing.model';
 
@Injectable({ providedIn: 'root' })
export class ProductListingMapper {
  createPages(
    skus: string[],
    productListingType: string,
    productListingValue: string,
    itemsPerPage: number,
    extras?: {
      startPage?: number;
      sortableAttributes?: SortableAttributesType[];
      itemCount?: number;
      sorting?: string;
      filters?: URLFormParams;
    }
  ): ProductListingType {
    const pages = range(0, Math.ceil(skus.length / itemsPerPage)).map(n =>
      skus.slice(n * itemsPerPage, (n + 1) * itemsPerPage)
    );
    const startPage = extras?.startPage || 1;
    const view: ProductListingType = {
      id: {
        type: productListingType,
        value: productListingValue,
      },
      itemCount: extras?.itemCount || skus.length,
      sortableAttributes: extras?.sortableAttributes || [],
      ...pages.reduce((acc, val, idx) => ({ ...acc, [idx + startPage]: val }), {}),
    };
 
    if (extras?.sorting) {
      view.id.sorting = extras.sorting;
    }
 
    if (extras?.filters) {
      view.id.filters = extras.filters;
    }
 
    return view;
  }
}