Skip to content

Commit

Permalink
Merge pull request nestjs#1206 from jbpionnier/refactor_simplified_if
Browse files Browse the repository at this point in the history
refactor() simplified if (less checks)
  • Loading branch information
kamilmysliwiec authored Nov 30, 2018
2 parents db94dc5 + 3a29753 commit c6ec887
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 14 deletions.
3 changes: 1 addition & 2 deletions packages/common/services/logger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ export class Logger implements LoggerService {
if (Logger.contextEnvironment === NestEnvironment.TEST) {
return;
}
const output =
message && isObject(message) ? JSON.stringify(message, null, 2) : message;
const output = isObject(message) ? JSON.stringify(message, null, 2) : message;
process.stdout.write(color(`[Nest] ${process.pid} - `));
process.stdout.write(`${new Date(Date.now()).toLocaleString()} `);

Expand Down
11 changes: 11 additions & 0 deletions packages/common/test/utils/shared.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ describe('Shared utils', () => {
});
it('should returns false when object is not function', () => {
expect(isFunction(null)).to.be.false;
expect(isFunction(undefined)).to.be.false;
});
});
describe('isObject', () => {
Expand All @@ -33,6 +34,8 @@ describe('Shared utils', () => {
});
it('should returns false when object is not object', () => {
expect(isObject(3)).to.be.false;
expect(isObject(null)).to.be.false;
expect(isObject(undefined)).to.be.false;
});
});
describe('isString', () => {
Expand All @@ -41,6 +44,8 @@ describe('Shared utils', () => {
});
it('should returns false when object is not string', () => {
expect(isString(false)).to.be.false;
expect(isString(null)).to.be.false;
expect(isString(undefined)).to.be.false;
});
});
describe('isConstructor', () => {
Expand All @@ -58,6 +63,11 @@ describe('Shared utils', () => {
it('should returns same path', () => {
expect(validatePath('/nope')).to.be.eql('/nope');
});
it('should returns empty path', () => {
expect(validatePath('')).to.be.eql('');
expect(validatePath(null)).to.be.eql('');
expect(validatePath(undefined)).to.be.eql('');
});
});
describe('isNil', () => {
it('should returns true when obj is undefined or null', () => {
Expand All @@ -72,6 +82,7 @@ describe('Shared utils', () => {
it('should returns true when array is empty or not exists', () => {
expect(isEmpty([])).to.be.true;
expect(isEmpty(null)).to.be.true;
expect(isEmpty(undefined)).to.be.true;
});
it('should returns false when array is not empty', () => {
expect(isEmpty([1, 2])).to.be.false;
Expand Down
8 changes: 5 additions & 3 deletions packages/common/utils/shared.utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
export const isUndefined = (obj): obj is undefined =>
typeof obj === 'undefined';
export const isFunction = (fn): boolean => typeof fn === 'function';
export const isObject = (fn): fn is object => typeof fn === 'object';
export const isObject = (fn): fn is object => !isNil(fn) && typeof fn === 'object';
export const isString = (fn): fn is string => typeof fn === 'string';
export const isConstructor = (fn): boolean => fn === 'constructor';
export const validatePath = (path): string =>
path.charAt(0) !== '/' ? '/' + path : path;
export const validatePath = (path?: string): string =>
path
? path.charAt(0) !== '/' ? '/' + path : path
: '';
export const isNil = (obj): boolean => isUndefined(obj) || obj === null;
export const isEmpty = (array): boolean => !(array && array.length > 0);
export const isSymbol = (fn): fn is symbol => typeof fn === 'symbol';
2 changes: 1 addition & 1 deletion packages/core/exceptions/base-exception-filter-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class BaseExceptionFilterContext extends ContextCreator {
public createConcreteContext<T extends any[], R extends any[]>(
metadata: T,
): R {
if (isUndefined(metadata) || isEmpty(metadata)) {
if (isEmpty(metadata)) {
return [] as R;
}
return iterate(metadata)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/guards/guards-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class GuardsContextCreator extends ContextCreator {
public createConcreteContext<T extends any[], R extends any[]>(
metadata: T,
): R {
if (isUndefined(metadata) || isEmpty(metadata)) {
if (isEmpty(metadata)) {
return [] as R;
}
return iterate(metadata)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/helpers/external-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ export class ExternalContextCreator {
}

public getCustomFactory(factory: (...args) => void, data): (...args) => any {
return !isUndefined(factory) && isFunction(factory)
return isFunction(factory)
? (...args) => factory(data, args)
: () => null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/interceptors/interceptors-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class InterceptorsContextCreator extends ContextCreator {
public createConcreteContext<T extends any[], R extends any[]>(
metadata: T,
): R {
if (isUndefined(metadata) || isEmpty(metadata)) {
if (isEmpty(metadata)) {
return [] as R;
}
return iterate(metadata)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/middleware/middleware-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export class MiddlewareModule {
) {
const proxy = this.routerProxy.createProxy(middleware, exceptionsHandler);
const prefix = this.config.getGlobalPrefix();
const basePath = prefix ? validatePath(prefix) : '';
const basePath = validatePath(prefix);
router(basePath + path, proxy);
}
}
2 changes: 1 addition & 1 deletion packages/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class NestApplication extends NestApplicationContext
public async registerRouter() {
await this.registerMiddleware(this.httpAdapter);
const prefix = this.config.getGlobalPrefix();
const basePath = prefix ? validatePath(prefix) : '';
const basePath = validatePath(prefix);
this.routesResolver.resolve(this.httpAdapter, basePath);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/pipes/pipes-context-creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class PipesContextCreator extends ContextCreator {
public createConcreteContext<T extends any[], R extends any[]>(
metadata: T,
): R {
if (isUndefined(metadata) || isEmpty(metadata)) {
if (isEmpty(metadata)) {
return [] as R;
}
return iterate(metadata)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/router/router-execution-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ export class RouterExecutionContext {
}

public getCustomFactory(factory: (...args) => void, data): (...args) => any {
return !isUndefined(factory) && isFunction(factory)
return isFunction(factory)
? (req, res, next) => factory(data, req)
: () => null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/microservices/client/client-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ export abstract class ClientProxy {
}

protected normalizePattern<T = any>(pattern: T): string {
return pattern && isString(pattern) ? pattern : JSON.stringify(pattern);
return isString(pattern) ? pattern : JSON.stringify(pattern);
}
}

0 comments on commit c6ec887

Please sign in to comment.