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: #150
  • Loading branch information
carlo-nomes committed Feb 28, 2019
1 parent e0a3afb commit 0d3d8e0
Show file tree
Hide file tree
Showing 22 changed files with 65 additions and 150 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
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
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 @@ -18,11 +18,7 @@ import { StarkComponentUtil, StarkFormUtil } from "../../../util";
import { FormControl, Validators } from "@angular/forms";
import { distinctUntilChanged, filter } from "rxjs/operators";
import { Subscription } from "rxjs";

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

/**
* Name of the component
Expand Down Expand Up @@ -265,7 +261,7 @@ export class StarkDropdownComponent extends AbstractStarkUiComponent implements
if (
!changes["value"].firstChange &&
StarkFormUtil.isFormControl(this.formControl) &&
!_isEqual(this.formControl.value, this.value)
!isEqual(this.formControl.value, this.value)
) {
this.formControl.setValue(this.value);
}
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";

export abstract class AbstractStarkFormComponent<CriteriaType> {
public originalCopy: CriteriaType;
Expand All @@ -22,21 +14,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 @@ -220,15 +216,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { StarkMessage, StarkMessageType } from "../../../common/message";
import { StarkMessageCollection } from "../entities";
import { StarkMessagePaneActions, StarkMessagePaneActionTypes } from "../actions";
import { StarkAddMessages, StarkRemoveMessages } from "../actions/message-pane.actions";

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

/**
* Initial state of the store
Expand Down Expand Up @@ -110,21 +106,21 @@ function removeMessages(state: Readonly<StarkMessageCollection>, action: Readonl

switch (messageToRemove.type) {
case StarkMessageType.INFO:
idx = _findIndex(mutableState.infoMessages, ["id", messageToRemove.id]);
idx = findIndex(mutableState.infoMessages, ["id", messageToRemove.id]);
if (idx !== -1) {
mutableState.infoMessages.splice(idx, 1);
newStateAfterRemoval.infoMessages = mutableState.infoMessages;
}
break;
case StarkMessageType.WARNING:
idx = _findIndex(mutableState.warningMessages, ["id", messageToRemove.id]);
idx = findIndex(mutableState.warningMessages, ["id", messageToRemove.id]);
if (idx !== -1) {
mutableState.warningMessages.splice(idx, 1);
newStateAfterRemoval.warningMessages = mutableState.warningMessages;
}
break;
case StarkMessageType.ERROR:
idx = _findIndex(mutableState.errorMessages, ["id", messageToRemove.id]);
idx = findIndex(mutableState.errorMessages, ["id", messageToRemove.id]);
if (idx !== -1) {
mutableState.errorMessages.splice(idx, 1);
newStateAfterRemoval.errorMessages = mutableState.errorMessages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ import { MatPaginator, MatPaginatorIntl, PageEvent } from "@angular/material/pag
import { STARK_LOGGING_SERVICE, StarkLoggingService } from "@nationalbankbelgium/stark-core";
import { StarkPaginationConfig } from "./pagination-config.intf";
import { StarkPaginateEvent } from "./paginate-event.intf";

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

/**
* Name of the component
Expand Down Expand Up @@ -151,7 +147,7 @@ export class StarkPaginationComponent extends MatPaginator implements OnInit, On
) {
this.onChangePagination();
} else if (
!_isEqual(paginationConfigOriginalChange, changesObj["paginationConfig"].previousValue) ||
!isEqual(paginationConfigOriginalChange, changesObj["paginationConfig"].previousValue) ||
paginationConfigOriginalChange.totalItems !== this.paginationConfig.totalItems ||
paginationConfigOriginalChange.itemsPerPageOptions !== this.paginationConfig.itemsPerPageOptions
) {
Expand Down
Loading

0 comments on commit 0d3d8e0

Please sign in to comment.