Skip to content

Commit

Permalink
refactor(stark-all): update lodash usage
Browse files Browse the repository at this point in the history
This commit will refactor all used instances of `const (.*) = require("lodash/(.*)")` to `import (.*) from "lodash/(.*)"`

ISSUES CLOSED: NationalBankBelgium#150
  • Loading branch information
carlo-nomes committed Mar 27, 2019
1 parent e12315f commit 7a53c70
Show file tree
Hide file tree
Showing 28 changed files with 103 additions and 199 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { TranslateService } from "@ngx-translate/core";
import { StarkLocale } from "./locale.intf";
import { commonCoreTranslations } from "./common-translations";

/**
* @ignore
*/
const _cloneDeep: Function = require("lodash/cloneDeep");
import cloneDeep from "lodash-es/cloneDeep";

/**
* This function can be used by Stark modules to merge their translations into existing translations,
Expand Down Expand Up @@ -36,7 +32,7 @@ const _cloneDeep: Function = require("lodash/cloneDeep");
* mergeTranslations(this.translateService, english, french, dutch);
*/
export function mergeTranslations(translateService: TranslateService, ...localesToMerge: StarkLocale[]): void {
const currentTranslations: object = _cloneDeep(translateService.translations);
const currentTranslations: object = cloneDeep(translateService.translations);

for (const locale of localesToMerge) {
translateService.setTranslation(locale.languageCode, commonCoreTranslations[locale.languageCode], false);
Expand Down
14 changes: 5 additions & 9 deletions packages/stark-core/src/modules/http/services/http.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ import {
import { convertMapIntoObject } from "../../../util/util-helpers";
import { StarkHttpUtil } from "../../../util/http.util";
import { StarkHttpService, starkHttpServiceName } from "./http.service.intf";

/**
* @ignore
*/
const _cloneDeep: Function = require("lodash/cloneDeep");
import cloneDeep from "lodash-es/cloneDeep";

/**
* @ignore
Expand Down Expand Up @@ -140,8 +136,8 @@ export class StarkHttpServiceImpl<P extends StarkResource> implements StarkHttpS
let requestCopy: StarkHttpRequest<P> = request;

if (request.item) {
requestCopy = _cloneDeep(request);
const itemWithoutETag: P = _cloneDeep(<P>requestCopy.item);
requestCopy = cloneDeep(request);
const itemWithoutETag: P = cloneDeep(<P>requestCopy.item);
delete itemWithoutETag.etag;
requestCopy.item = itemWithoutETag;
}
Expand All @@ -157,7 +153,7 @@ export class StarkHttpServiceImpl<P extends StarkResource> implements StarkHttpS
public addDevAuthenticationHeaders(request: StarkHttpRequest<P>): StarkHttpRequest<P> {
this.logger.debug(starkHttpServiceName + ": Adding dev-authentication headers");

const requestCopy: StarkHttpRequest<P> = _cloneDeep(request);
const requestCopy: StarkHttpRequest<P> = cloneDeep(request);

// add the preAuthentication headers to the request headers
this.sessionService.devAuthenticationHeaders.forEach((value: string | string[], header: string) => {
Expand All @@ -175,7 +171,7 @@ export class StarkHttpServiceImpl<P extends StarkResource> implements StarkHttpS
public addCorrelationIdentifierHeader(request: StarkHttpRequest<P>): StarkHttpRequest<P> {
if (this.logger.correlationIdHttpHeaderName && this.logger.correlationIdHttpHeaderName.length > 0 && this.logger.correlationId) {
this.logger.debug(starkHttpServiceName + ": Adding correlation identifier header");
const requestCopy: StarkHttpRequest<P> = _cloneDeep(request);
const requestCopy: StarkHttpRequest<P> = cloneDeep(request);
requestCopy.headers.set(this.logger.correlationIdHttpHeaderName, this.logger.correlationId);
return requestCopy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ import { StarkFlushLogMessages, StarkLogMessageAction } from "../actions";
import { selectStarkLogging } from "../reducers";
import { StarkError, StarkErrorImpl } from "../../../common/error";
import { StarkConfigurationUtil } from "../../../util/configuration.util";

/**
* @ignore
*/
const _noop: Function = require("lodash/noop");
import noop from "lodash-es/noop";

const xsrfServiceNotFound: "not provided" = "not provided";

Expand Down Expand Up @@ -246,7 +242,7 @@ export class StarkLoggingServiceImpl implements StarkLoggingService {
*/
protected getConsole(type: string): Function {
const console: any = window && window.console ? window.console : {};
const logFn: Function = console[type] || console.log || _noop;
const logFn: Function = console[type] || console.log || noop;

return (...args: any[]): any => {
const consoleArgs: any[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ import { StarkStateConfigWithParams } from "./state-config-with-params.intf";
import { StarkCoreApplicationState } from "../../../common/store";
import { StarkConfigurationUtil } from "../../../util/configuration.util";
import { starkAppExitStateName, starkAppInitStateName } from "../../session/constants";

/**
* @ignore
*/
const _isEmpty: Function = require("lodash/isEmpty");
import isEmpty from "lodash-es/isEmpty";

/**
* @ignore
Expand Down Expand Up @@ -515,7 +511,7 @@ export class StarkRoutingServiceImpl implements StarkRoutingService {
(pathNode.state === this.getCurrentState() || this.isParentState(pathNode.state))
) {
const resolvablesData: { [key: string]: any } = this.extractResolvablesData(pathNode.resolvables);
const stateResolves: any = _isEmpty(resolvablesData) ? undefined : resolvablesData;
const stateResolves: any = isEmpty(resolvablesData) ? undefined : resolvablesData;
stateTreeResolves.set(pathNode.state.name, stateResolves);
}
}
Expand Down Expand Up @@ -548,7 +544,7 @@ export class StarkRoutingServiceImpl implements StarkRoutingService {
pathNode.state !== pathNode.state.root() &&
(pathNode.state === this.getCurrentState() || this.isParentState(pathNode.state))
) {
const stateData: any = _isEmpty(pathNode.state.data) ? undefined : pathNode.state.data;
const stateData: any = isEmpty(pathNode.state.data) ? undefined : pathNode.state.data;
stateTreeData.set(pathNode.state.name, stateData);
}
}
Expand Down
10 changes: 3 additions & 7 deletions packages/stark-core/src/util/http.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { HttpParameterCodec, HttpParams } from "@angular/common/http";
import { StarkQueryParam } from "../modules/http/entities/http-request.entity.intf";
import { StarkHttpParameterCodec } from "../modules/http/entities/http-parameter-codec";
import { convertMapIntoObject } from "./util-helpers";

/**
* @ignore
*/
const _reduce: Function = require("lodash/reduce");
import reduce from "lodash-es/reduce";

/**
* A custom implementation of HttpParameterCodec to correctly encode query parameters.
Expand All @@ -23,15 +19,15 @@ export class StarkHttpUtil {
* @param starkQueryParam - params to convert
*/
public static convertStarkQueryParamsIntoHttpParams(starkQueryParam: Map<string, StarkQueryParam>): HttpParams {
return _reduce(
return reduce(
convertMapIntoObject(starkQueryParam), // convert to object
(httpParams: HttpParams, value: StarkQueryParam, key: string) =>
typeof value === "undefined"
? // set key to empty string when not defined
httpParams.set(key, "")
: Array.isArray(value)
? // append each string to the key when set to an array
_reduce(value, (acc: HttpParams, entry: string) => acc.append(key, entry), httpParams)
reduce(value, (acc: HttpParams, entry: string) => acc.append(key, entry), httpParams)
: httpParams.set(key, value),
new HttpParams({ encoder: STARK_HTTP_PARAM_ENCODER })
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* @ignore
*/
const _floor: Function = require("lodash/floor");
import floor from "lodash-es/floor";

import { isValidBBAN } from "ibantools";

Expand Down Expand Up @@ -42,7 +39,7 @@ export function starkIsBBAN(bban: string, countryCode: string = ""): boolean {
*/
function calculateCheckDigit(bbanNumber: string): number {
const firstPart: number = parseInt(bbanNumber.substring(0, bbanNumber.length - 2), 10);
let checkDigit: number = _floor(firstPart % 97);
let checkDigit: number = floor(firstPart % 97);
if (checkDigit === 0) {
checkDigit = 97;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* @ignore
*/
const _floor: Function = require("lodash/floor");
import floor from "lodash-es/floor";

/**
* @ignore
Expand Down Expand Up @@ -36,7 +33,7 @@ export function starkIsEstablishmentUnitNumber(establishmentNumber: string): boo
const numberToCheck: number = parseInt(enterpriseNumber.substring(0, controlNumberBeginIndex), 10);

// We validate 8 first digits with a mod-97 checksum algorithm
isValid = 97 - _floor(numberToCheck % 97) === controlNumber;
isValid = 97 - floor(numberToCheck % 97) === controlNumber;
}

return isValid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* @ignore
*/
const _floor: Function = require("lodash/floor");
import floor from "lodash-es/floor";

/**
* @ignore
Expand Down Expand Up @@ -45,12 +42,12 @@ export function starkIsISIN(isin: string): boolean {
digit *= 2;
}

sum += _floor(digit / modulo);
sum += floor(digit / modulo);
sum += digit % modulo;
}

const currentCheckDigit: number = parseInt(isin[lengthWithoutCheckDigit], base);
const expectedCheckDigit: number = sum % modulo === 0 ? 0 : (_floor(sum / modulo) + 1) * modulo - sum;
const expectedCheckDigit: number = sum % modulo === 0 ? 0 : (floor(sum / modulo) + 1) * modulo - sum;
isValid = currentCheckDigit === expectedCheckDigit;
}
return isValid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* @ignore
*/
const _floor: Function = require("lodash/floor");
import floor from "lodash-es/floor";

/**
* @ignore
Expand Down Expand Up @@ -79,7 +76,7 @@ function isValidBelgianNin(nin: string): boolean {
const numberToCheck19thCentury: number = parseInt(getBirthDate(nin) + getOrder(nin), 10);
const numberToCheck20thCentury: number = parseInt("2" + getBirthDate(nin) + getOrder(nin), 10);

return 97 - _floor(numberToCheck19thCentury % 97) === checkDigits || 97 - _floor(numberToCheck20thCentury % 97) === checkDigits;
return 97 - floor(numberToCheck19thCentury % 97) === checkDigits || 97 - floor(numberToCheck20thCentury % 97) === checkDigits;
}
}

Expand Down
8 changes: 2 additions & 6 deletions packages/stark-ui/src/common/message/message.entity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { StarkMessage } from "./message.intf";
import { StarkMessageType } from "./message-type.intf";

/**
* @ignore
*/
const _uniqueId: any = require("lodash/uniqueId");
import uniqueId from "lodash-es/uniqueId";

/**
* @ignore
Expand All @@ -18,6 +14,6 @@ export class StarkMessageImpl implements StarkMessage {

public constructor() {
// set random id
this.id = _uniqueId();
this.id = uniqueId();
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import merge from "lodash-es/merge";
import { TranslateService } from "@ngx-translate/core";
import { mergeTranslations, StarkLocale } from "@nationalbankbelgium/stark-core";
import { commonUiTranslations } from "./common-translations";

/**
* @ignore
*/
const _merge: Function = require("lodash/merge");

/**
* This function can be used by Stark UI modules to merge their translations into existing translations,
* without losing any existing translations.
Expand All @@ -26,7 +22,7 @@ export function mergeUiTranslations(translateService: TranslateService, ...local
for (const locale of localesToMerge) {
allLocalesToMerge.push({
languageCode: locale.languageCode,
translations: _merge({}, commonUiTranslations[locale.languageCode], locale.translations)
translations: merge({}, commonUiTranslations[locale.languageCode], locale.translations)
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Subject, Subscription } from "rxjs";
import { FocusMonitor, FocusOrigin } from "@angular/cdk/a11y";
import { coerceBooleanProperty } from "@angular/cdk/coercion";
import { TranslateService } from "@ngx-translate/core";
import isEqual from "lodash-es/isEqual";

/**
* Type expected by `dateFilter` @Input.
Expand All @@ -40,11 +41,6 @@ export type StarkDatePickerFilter = "OnlyWeekends" | "OnlyWeekdays" | ((date: Da
*/
export type StarkDatePickerMaskConfig = StarkTimestampMaskConfig | boolean | undefined;

/**
* @ignore
*/
const _isEqual: Function = require("lodash/isEqual");

/**
* Default DateMask configuration
*/
Expand Down Expand Up @@ -182,7 +178,7 @@ export class StarkDatePickerComponent extends AbstractStarkUiComponent
}

public set value(value: Date | null) {
if (!_isEqual(this._value, value)) {
if (!isEqual(this._value, value)) {
this._value = value;
this.stateChanges.next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ import { FocusMonitor, FocusOrigin } from "@angular/cdk/a11y";
import { MatSelectChange } from "@angular/material/select";
import { coerceBooleanProperty } from "@angular/cdk/coercion";
import { TranslateService } from "@ngx-translate/core";
import isEqual from "lodash-es/isEqual";

/**
* @ignore
*/
const _isEqual: Function = require("lodash/isEqual");

/**
* Name of the component
Expand Down Expand Up @@ -160,7 +157,7 @@ export class StarkDropdownComponent extends AbstractStarkUiComponent
}

public set value(value: any | any[]) {
if (!_isEqual(this._value, value)) {
if (!isEqual(this._value, value)) {
this._value = value;
this.stateChanges.next();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { StarkLoggingService } from "@nationalbankbelgium/stark-core";

/**
* @ignore
*/
const _cloneDeep: Function = require("lodash/cloneDeep");

/**
* @ignore
*/
const _isEqual: Function = require("lodash/isEqual");
import cloneDeep from "lodash-es/cloneDeep";
import isEqual from "lodash-es/isEqual";

/**
* Abstract class defining the source model to bind to form components in Stark (i.e. {@link AbstractStarkSearchComponent})
Expand Down Expand Up @@ -37,21 +29,21 @@ export abstract class AbstractStarkFormComponent<CriteriaType> {
*/
protected setOriginalCopy(originalCopy: CriteriaType = <any>{}): void {
this.originalCopy = originalCopy;
this.workingCopy = _cloneDeep(this.originalCopy);
this.workingCopy = cloneDeep(this.originalCopy);
}

/**
* Revert the form's working copy back to the original copy (a deep clone copy)
*/
protected reset(): void {
this.workingCopy = _cloneDeep(this.originalCopy);
this.workingCopy = cloneDeep(this.originalCopy);
}

/**
* Check whether the working copy is exactly the same as the original copy.
* Performs a deep comparison between the two objects to determine if they are equivalent.
*/
protected isDirty(): boolean {
return !_isEqual(this.workingCopy, this.originalCopy);
return !isEqual(this.workingCopy, this.originalCopy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ import { STARK_LOGGING_SERVICE, StarkLoggingService } from "@nationalbankbelgium
import { FormGroup } from "@angular/forms";
import { animate, AnimationTriggerMetadata, state, style, transition, trigger } from "@angular/animations";
import { AbstractStarkUiComponent } from "../../../../common/classes/abstract-component";

/**
* @ignore
*/
const _isEqual: Function = require("lodash/isEqual");
import isEqual from "lodash-es/isEqual";

/**
* Name of the component
Expand Down Expand Up @@ -262,15 +258,15 @@ export class StarkGenericSearchComponent extends AbstractStarkUiComponent implem
if (
changesObj["formButtonsConfig"] &&
!changesObj["formButtonsConfig"].isFirstChange() &&
!_isEqual(this.formButtonsConfig, this.normalizedFormButtonsConfig)
!isEqual(this.formButtonsConfig, this.normalizedFormButtonsConfig)
) {
this.normalizedFormButtonsConfig = this.normalizeFormButtonsConfig(this.formButtonsConfig);
}

if (
changesObj["formActionBarConfig"] &&
!changesObj["formActionBarConfig"].isFirstChange() &&
!_isEqual(this.formActionBarConfig, this.normalizedFormActionBarConfig)
!isEqual(this.formActionBarConfig, this.normalizedFormActionBarConfig)
) {
this.normalizedFormActionBarConfig = this.normalizeFormActionBarConfig(this.formActionBarConfig);
this.actionBarConfig = this.buildActionBarConfig(this.normalizedFormActionBarConfig);
Expand Down
Loading

0 comments on commit 7a53c70

Please sign in to comment.