Skip to content

Commit

Permalink
Merge pull request #1147 from cnomes/feature/lodash-types
Browse files Browse the repository at this point in the history
refactor(stark-all): replace 'lodash' by tree-shakeable 'lodash-es'. Add '@types/lodash-es' to get correct typings.
  • Loading branch information
SuperITMan committed Apr 1, 2019
2 parents 9b0a04c + 3a8a21b commit c5c34c6
Show file tree
Hide file tree
Showing 56 changed files with 7,521 additions and 7,061 deletions.
1,740 changes: 608 additions & 1,132 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion packages/rollup.config.common-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const globals = {
"@angular/material/autocomplete": "ngMaterial.autocomplete",
"@angular/material/button": "ngMaterial.button",
"@angular/material/button-toggle": "ngMaterial.buttonToggle",
"@angular/material/card":"ngMaterial.card",
"@angular/material/card": "ngMaterial.card",
"@angular/material/checkbox": "ngMaterial.checkbox",
"@angular/material/core": "ngMaterial.core",
"@angular/material/datepicker": "ngMaterial.datepicker",
Expand Down Expand Up @@ -59,6 +59,17 @@ const globals = {
cerialize: "cerialize",
ibantools: "ibantools",
moment: "moment",
"lodash-es/cloneDeep": "lodash-es.cloneDeep",
"lodash-es/findIndex": "lodash-es.findIndex",
"lodash-es/floor": "lodash-es.floor",
"lodash-es/get": "lodash-es.get",
"lodash-es/isEmpty": "lodash-es.isEmpty",
"lodash-es/isEqual": "lodash-es.isEqual",
"lodash-es/merge": "lodash-es.merge",
"lodash-es/noop": "lodash-es.noop",
"lodash-es/reduce": "lodash-es.reduce",
"lodash-es/sortBy": "lodash-es.sortBy",
"lodash-es/uniqueId": "lodash-es.uniqueId",
nouislider: "nouislider",
"prettier/standalone": "prettier.standalone",
"prettier/parser-babylon": "prettier.parserBabylon",
Expand Down
2 changes: 1 addition & 1 deletion packages/stark-build/config/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
}
],
"no-magic-numbers": false,
"import-blacklist": [true, "@angular/material", "lodash", "rxjs/internal", "rxjs/index", "rxjs/Rx"],
"import-blacklist": [true, "@angular/material", "lodash", "rxjs/internal", "rxjs/index", "rxjs/Rx", "lodash-es"],
"ordered-imports": false,
"max-classes-per-file": false, // [true, 1],
"prefer-for-of": true,
Expand Down
1,547 changes: 709 additions & 838 deletions packages/stark-build/package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions packages/stark-core/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import "core-js/es7/reflect";
import "core-js/stage/4";

/* tslint:disable:no-import-side-effect */
// FIXME: change when https://github.com/monounity/karma-typescript/issues/320 is resolved
// tslint:disable-next-line:import-blacklist
import "lodash-es"; // see https://github.com/monounity/karma-typescript/issues/150#issuecomment-318620280
import "zone.js/dist/zone";
import "zone.js/dist/long-stack-trace-zone";
import "zone.js/dist/proxy"; // since zone.js 0.6.15
Expand Down
15 changes: 14 additions & 1 deletion packages/stark-core/karma.conf.ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,20 @@ const starkCoreSpecificConfiguration = Object.assign({}, defaultKarmaCIConfig, {
dir: helpers.root("reports/coverage/packages")
}),
// add missing files due to "@nationalbankbelgium/stark-core" imports used in mock files of the testing sub-package
files: [...defaultKarmaCIConfig.files, ...karmaTypescriptFiles]
files: [...defaultKarmaCIConfig.files, ...karmaTypescriptFiles],
// Overwrite the karmaTypescriptConfig to pass the correct preset to karma-typescript-es6-transform
karmaTypescriptConfig: {
...defaultKarmaCIConfig.karmaTypescriptConfig,
bundlerOptions: {
...defaultKarmaCIConfig.karmaTypescriptConfig.bundlerOptions,
transforms: [
require("../stark-testing/node_modules/karma-typescript-angular2-transform"),
require("../stark-testing/node_modules/karma-typescript-es6-transform")({
presets: [helpers.root("../stark-testing/node_modules/babel-preset-env")] // add preset in a way that the package can find it
})
]
}
}
});

// export the configuration function that karma expects and simply return the stark configuration
Expand Down
15 changes: 14 additions & 1 deletion packages/stark-core/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ const karmaTypescriptFiles = [{ pattern: helpers.root("index.ts") }, { pattern:
// start customizing the Karma configuration from stark-testing
const starkCoreSpecificConfiguration = Object.assign({}, defaultKarmaConfig, {
// add missing files due to "@nationalbankbelgium/stark-core" imports used in mock files of the testing sub-package
files: [...defaultKarmaConfig.files, ...karmaTypescriptFiles]
files: [...defaultKarmaConfig.files, ...karmaTypescriptFiles],
// Overwrite the karmaTypescriptConfig to pass the correct preset to karma-typescript-es6-transform
karmaTypescriptConfig: {
...defaultKarmaConfig.karmaTypescriptConfig,
bundlerOptions: {
...defaultKarmaConfig.karmaTypescriptConfig.bundlerOptions,
transforms: [
require("../stark-testing/node_modules/karma-typescript-angular2-transform"),
require("../stark-testing/node_modules/karma-typescript-es6-transform")({
presets: [helpers.root("../stark-testing/node_modules/babel-preset-env")] // add preset in a way that the package can find it
})
]
}
}
});

// export the configuration function that karma expects and simply return the stark configuration
Expand Down
61 changes: 37 additions & 24 deletions packages/stark-core/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/stark-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
"@ngrx/store": "^7.1.0",
"@ngrx/store-devtools": "^7.1.0",
"@ngx-translate/core": "^11.0.1",
"@types/lodash-es": "^4.17.1",
"@types/node": "^8.10.37",
"@types/uuid": "^3.4.4",
"@uirouter/angular": "^3.0.0",
"cerialize": "^0.1.18",
"class-validator": "^0.7.3",
"deep-freeze-strict": "^1.1.1",
"ibantools": "^2.0.0",
"lodash": "^4.17.11",
"lodash-es": "^4.17.11",
"moment": "^2.22.2",
"uuid": "^3.3.2"
},
Expand Down
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
Loading

0 comments on commit c5c34c6

Please sign in to comment.