Browse Source

refactor: use unique function rather than duplicate implementation

Michael Bromley 7 years ago
parent
commit
7d6415b27c

+ 2 - 4
admin-ui/src/app/catalog/components/product-category-detail/product-category-detail.component.ts

@@ -21,6 +21,7 @@ import {
     UpdateProductCategoryInput,
 } from 'shared/generated-types';
 import { CustomFieldConfig } from 'shared/shared-types';
+import { unique } from 'shared/unique';
 
 import { BaseDetailComponent } from '../../../common/base-detail.component';
 import { createUpdatedTranslatable } from '../../../common/utilities/create-updated-translatable';
@@ -117,11 +118,8 @@ export class ProductCategoryDetailComponent extends BaseDetailComponent<ProductC
             .subscribe(([facetValueIds, category]) => {
                 if (facetValueIds) {
                     const existingFacetValueIds = this.categoryForm.value.facetValueIds;
-                    const uniqueFacetValueIds = Array.from(
-                        new Set([...existingFacetValueIds, ...facetValueIds]),
-                    );
                     this.categoryForm.patchValue({
-                        facetValueIds: uniqueFacetValueIds,
+                        facetValueIds: unique([...existingFacetValueIds, ...facetValueIds]),
                     });
                     this.categoryForm.markAsDirty();
                     this.changeDetector.markForCheck();

+ 2 - 2
server/src/data-import/providers/import-parser/import-parser.ts

@@ -3,6 +3,7 @@ import * as parse from 'csv-parse';
 import { Stream } from 'stream';
 
 import { normalizeString } from '../../../../../shared/normalize-string';
+import { unique } from '../../../../../shared/unique';
 
 export type BaseProductRecord = {
     name?: string;
@@ -172,8 +173,7 @@ export class ImportParser {
 function populateOptionGroupValues(currentRow: ParsedProductWithVariants) {
     const values = currentRow.variants.map(v => v.optionValues);
     currentRow.product.optionGroups.forEach((og, i) => {
-        const uniqueValues = Array.from(new Set(values.map(v => v[i])));
-        og.values = uniqueValues;
+        og.values = unique(values.map(v => v[i]));
     });
 }
 

+ 2 - 1
server/src/service/helpers/list-query-builder/parse-sort-params.ts

@@ -2,6 +2,7 @@ import { Connection, OrderByCondition } from 'typeorm';
 import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
 
 import { Type } from '../../../../../shared/shared-types';
+import { unique } from '../../../../../shared/unique';
 import { UserInputError } from '../../../common/error/errors';
 import { NullOptionals, SortParameter } from '../../../common/types/common-types';
 import { VendureEntity } from '../../../entity/base/base.entity';
@@ -52,5 +53,5 @@ export function parseSortParams<T extends VendureEntity>(
 }
 
 function getValidSortFields(columns: ColumnMetadata[]): string {
-    return Array.from(new Set(columns.map(c => c.propertyName))).join(', ');
+    return unique(columns.map(c => c.propertyName)).join(', ');
 }

+ 2 - 1
server/src/service/services/channel.service.ts

@@ -5,6 +5,7 @@ import { Connection } from 'typeorm';
 import { CreateChannelInput, UpdateChannelInput } from '../../../../shared/generated-types';
 import { DEFAULT_CHANNEL_CODE } from '../../../../shared/shared-constants';
 import { ID } from '../../../../shared/shared-types';
+import { unique } from '../../../../shared/unique';
 import { RequestContext } from '../../api/common/request-context';
 import { DEFAULT_LANGUAGE_CODE } from '../../common/constants';
 import { EntityNotFoundError, InternalServerError } from '../../common/error/errors';
@@ -36,7 +37,7 @@ export class ChannelService {
      * specified in the RequestContext.
      */
     assignToChannels<T extends ChannelAware>(entity: T, ctx: RequestContext): T {
-        const channelIds = [...new Set([ctx.channelId, this.getDefaultChannel().id])];
+        const channelIds = unique([ctx.channelId, this.getDefaultChannel().id]);
         entity.channels = channelIds.map(id => ({ id })) as any;
         return entity;
     }