Skip to content

Commit

Permalink
refactor: replace lodash flatten with built-in Array.flat function (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dhhyi authored Apr 15, 2024
1 parent a324675 commit 0b24445
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
"message": "use star imports only for aggregation of deeper lying imports"
},
{
"importNamePattern": "^(?!(range|uniq|memoize|once|groupBy|countBy|flatten|isEqual|intersection|pick|differenceBy|unionBy|mergeWith|snakeCase|capitalize)$).*",
"importNamePattern": "^(?!(range|uniq|memoize|once|groupBy|countBy|isEqual|intersection|pick|differenceBy|unionBy|mergeWith|snakeCase|capitalize)$).*",
"name": "lodash.*",
"filePattern": "^.*/src/app/(?!.*\\.spec\\.ts$).*\\.ts$",
"message": "importing this operator from lodash is forbidden"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { flatten, groupBy } from 'lodash-es';
import { groupBy } from 'lodash-es';

import { FilterNavigation } from 'ish-core/models/filter-navigation/filter-navigation.model';
import { ProductView } from 'ish-core/models/product-view/product-view.model';
Expand Down Expand Up @@ -99,9 +99,10 @@ export class ProductVariationHelper {
return product.variations?.length;
}

const selectedFacets = flatten(
filters.filter.map(filter => filter.facets.filter(facet => facet.selected).map(facet => facet.name))
).map(selected => selected.split('='));
const selectedFacets = filters.filter
.map(filter => filter.facets.filter(facet => facet.selected).map(facet => facet.name))
.flat()
.map(selected => selected.split('='));

if (!selectedFacets.length) {
return product.variations?.length;
Expand Down
13 changes: 8 additions & 5 deletions src/app/core/models/product/product.mapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Injectable } from '@angular/core';
import { flatten } from 'lodash-es';

import { AttachmentMapper } from 'ish-core/models/attachment/attachment.mapper';
import { AttributeGroup } from 'ish-core/models/attribute-group/attribute-group.model';
Expand Down Expand Up @@ -72,10 +71,14 @@ export class ProductMapper {
type: 'VariationProductMaster',
sku,
completenessLevel: 0,
variationAttributeValues: flatten(variations.map(v => v.variableVariationAttributes)).filter(
(val, idx, arr) =>
arr.findIndex(el => el.variationAttributeId === val.variationAttributeId && el.value === val.value) === idx
),
variationAttributeValues: variations
.map(v => v.variableVariationAttributes)
.flat()
.filter(
(val, idx, arr) =>
arr.findIndex(el => el.variationAttributeId === val.variationAttributeId && el.value === val.value) ===
idx
),
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions src/app/core/services/products/products.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { flatten, range } from 'lodash-es';
import { range } from 'lodash-es';
import { Observable, from, identity, of, throwError } from 'rxjs';
import { defaultIfEmpty, map, mergeMap, switchMap, toArray, withLatestFrom } from 'rxjs/operators';

Expand Down Expand Up @@ -289,7 +289,7 @@ export class ProductsService {
}),
mergeMap(identity, 2),
toArray(),
map(resp2 => [...resp.elements, ...flatten(resp2)])
map(resp2 => [...resp.elements, ...resp2.flat()])
)
),
map((links: ProductVariationLink[]) => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dictionary } from '@ngrx/entity';
import { createSelector, createSelectorFactory, resultMemoize } from '@ngrx/store';
import { flatten, isEqual, memoize, once, range } from 'lodash-es';
import { isEqual, memoize, once, range } from 'lodash-es';
import { identity } from 'rxjs';

import {
Expand Down Expand Up @@ -28,7 +28,7 @@ const getProductListingSettings = createSelector(getProductListingState, state =
const { selectEntities: getProductListingEntities } = adapter.getSelectors(getProductListingState);

function mergeAllPages(data: ProductListingType) {
return flatten(data.pages.map(page => data[page]));
return data.pages.map(page => data[page]).flat();
}

function calculatePageIndices(currentPage: number, itemCount: number, itemsPerPage: number) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { flatten, pick } from 'lodash-es';
import { pick } from 'lodash-es';
import { EMPTY, Observable, concat, defer, forkJoin, iif, of, throwError } from 'rxjs';
import { concatMap, defaultIfEmpty, expand, filter, last, map, take } from 'rxjs/operators';

Expand Down Expand Up @@ -48,7 +48,7 @@ export class QuotingService {
map(qrs => qrs.reverse()),
map((quotes: QuoteData[]) => quotes.map(data => this.quoteMapper.fromData(data, 'Quote')))
),
]).pipe(map(flatten));
]).pipe(map(quotes => quotes.flat()));
}

getQuoteDetails(id: string, type: 'Quote' | 'QuoteRequest', completenessLevel: QuoteCompletenessLevel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createSelector, createSelectorFactory, resultMemoize } from '@ngrx/store';
import { flatten, uniq } from 'lodash-es';
import { uniq } from 'lodash-es';

import { isArrayEqual } from 'ish-core/utils/functions';

Expand Down Expand Up @@ -37,5 +37,5 @@ export const getPreferredWishlist = createSelector(getAllWishlists, entities =>
export const getAllWishlistsItemsSkus = createSelectorFactory<object, string[]>(projector =>
resultMemoize(projector, isArrayEqual)
)(getAllWishlists, (wishlists: Wishlist[]): string[] =>
uniq(flatten(wishlists.map(wishlist => wishlist.items.map(items => items.sku))))
uniq(wishlists.map(wishlist => wishlist.items.map(items => items.sku)).flat())
);

0 comments on commit 0b24445

Please sign in to comment.