From ebcc38f6b64d17140a5a07628aa9b3510d1b4deb Mon Sep 17 00:00:00 2001 From: Art Lowel Date: Thu, 10 Aug 2017 15:50:58 +0200 Subject: [PATCH] added is(Not)Null and Undefined methods to empty.util because they're deprecated in the node core --- .../collection-page.component.ts | 5 +- src/app/shared/empty.util.spec.ts | 180 +++++++++++++++++- src/app/shared/empty.util.ts | 62 +++++- .../shared/pagination/pagination.component.ts | 2 - 4 files changed, 241 insertions(+), 8 deletions(-) diff --git a/src/app/collection-page/collection-page.component.ts b/src/app/collection-page/collection-page.component.ts index e08d078ddc..a29ecf94ef 100644 --- a/src/app/collection-page/collection-page.component.ts +++ b/src/app/collection-page/collection-page.component.ts @@ -1,10 +1,9 @@ import { - ChangeDetectionStrategy, ChangeDetectorRef, Component, DoCheck, OnChanges, OnDestroy, - OnInit, SimpleChanges + ChangeDetectionStrategy, ChangeDetectorRef, Component, OnDestroy, + OnInit } from '@angular/core'; import { ActivatedRoute, Params } from '@angular/router'; -import { Observable } from 'rxjs/Observable'; import { Subscription } from 'rxjs/Subscription'; import { Collection } from '../core/shared/collection.model'; diff --git a/src/app/shared/empty.util.spec.ts b/src/app/shared/empty.util.spec.ts index f9dd549adf..a9da472fef 100644 --- a/src/app/shared/empty.util.spec.ts +++ b/src/app/shared/empty.util.spec.ts @@ -1,4 +1,8 @@ -import { isEmpty, hasNoValue, hasValue, isNotEmpty } from './empty.util'; +import { + isEmpty, hasNoValue, hasValue, isNotEmpty, isNull, isNotNull, + isUndefined + isNotUndefined, +} from './empty.util'; describe('Empty Utils', () => { const strng = 'string'; @@ -10,6 +14,180 @@ describe('Empty Utils', () => { const fullMap: Map = new Map(); fullMap.set('foo', 'bar'); + describe('isNull', () => { + it('should return true for null', () => { + expect(isNull(null)).toBe(true); + }); + + it('should return false for undefined', () => { + expect(isNull(undefined)).toBe(false); + }); + + it('should return false for an empty String', () => { + expect(isNull('')).toBe(false); + }); + + it('should return false for true', () => { + expect(isNull(true)).toBe(false); + }); + + it('should return false for false', () => { + expect(isNull(false)).toBe(false); + }); + + it('should return false for a String', () => { + expect(isNull(strng)).toBe(false); + }); + + it('should return false for a Function', () => { + expect(isNull(fn)).toBe(false); + }); + + it('should return false for 0', () => { + expect(isNull(0)).toBe(false); + }); + + it('should return false for an empty Array', () => { + expect(isNull([])).toBe(false); + }); + + it('should return false for an empty Object', () => { + expect(isNull({})).toBe(false); + }); + + }); + + describe('isNotNull', () => { + + it('should return false for null', () => { + expect(isNotNull(null)).toBe(false); + }); + + it('should return true for undefined', () => { + expect(isNotNull(undefined)).toBe(true); + }); + + it('should return true for an empty String', () => { + expect(isNotNull('')).toBe(true); + }); + + it('should return true for false', () => { + expect(isNotNull(false)).toBe(true); + }); + + it('should return true for true', () => { + expect(isNotNull(true)).toBe(true); + }); + + it('should return true for a String', () => { + expect(isNotNull(strng)).toBe(true); + }); + + it('should return true for a Function', () => { + expect(isNotNull(fn)).toBe(true); + }); + + it('should return true for 0', () => { + expect(isNotNull(0)).toBe(true); + }); + + it('should return true for an empty Array', () => { + expect(isNotNull([])).toBe(true); + }); + + it('should return true for an empty Object', () => { + expect(isNotNull({})).toBe(true); + }); + + }); + + describe('isUndefined', () => { + it('should return false for null', () => { + expect(isUndefined(null)).toBe(false); + }); + + it('should return true for undefined', () => { + expect(isUndefined(undefined)).toBe(true); + }); + + it('should return false for an empty String', () => { + expect(isUndefined('')).toBe(false); + }); + + it('should return false for true', () => { + expect(isUndefined(true)).toBe(false); + }); + + it('should return false for false', () => { + expect(isUndefined(false)).toBe(false); + }); + + it('should return false for a String', () => { + expect(isUndefined(strng)).toBe(false); + }); + + it('should return false for a Function', () => { + expect(isUndefined(fn)).toBe(false); + }); + + it('should return false for 0', () => { + expect(isUndefined(0)).toBe(false); + }); + + it('should return false for an empty Array', () => { + expect(isUndefined([])).toBe(false); + }); + + it('should return false for an empty Object', () => { + expect(isUndefined({})).toBe(false); + }); + + }); + + describe('isNotUndefined', () => { + + it('should return true for null', () => { + expect(isNotUndefined(null)).toBe(true); + }); + + it('should return false for undefined', () => { + expect(isNotUndefined(undefined)).toBe(false); + }); + + it('should return true for an empty String', () => { + expect(isNotUndefined('')).toBe(true); + }); + + it('should return true for false', () => { + expect(isNotUndefined(false)).toBe(true); + }); + + it('should return true for true', () => { + expect(isNotUndefined(true)).toBe(true); + }); + + it('should return true for a String', () => { + expect(isNotUndefined(strng)).toBe(true); + }); + + it('should return true for a Function', () => { + expect(isNotUndefined(fn)).toBe(true); + }); + + it('should return true for 0', () => { + expect(isNotUndefined(0)).toBe(true); + }); + + it('should return true for an empty Array', () => { + expect(isNotUndefined([])).toBe(true); + }); + + it('should return true for an empty Object', () => { + expect(isNotUndefined({})).toBe(true); + }); + + }); + describe('hasNoValue', () => { it('should return true for null', () => { expect(hasNoValue(null)).toBe(true); diff --git a/src/app/shared/empty.util.ts b/src/app/shared/empty.util.ts index 4ed2b2b560..6346e7d042 100644 --- a/src/app/shared/empty.util.ts +++ b/src/app/shared/empty.util.ts @@ -1,14 +1,71 @@ +/** + * Returns true if the passed value is null. + * isNull(); // false + * isNull(null); // true + * isNull(undefined); // false + * isNull(''); // false + * isNull({}); // false + * isNull([]); // false + * isNull(function() {}); // false + */ +export function isNull(obj?: any): boolean { + return obj === null; +} + +/** + * Returns true if the passed value is not null. + * isNotNull(); // true + * isNotNull(null); // false + * isNotNull(undefined); // true + * isNotNull(''); // true + * isNotNull({}); // true + * isNotNull([]); // true + * isNotNull(function() {}); // true + */ +export function isNotNull(obj?: any): boolean { + return obj !== null; +} + +/** + * Returns true if the passed value is undefined. + * isUndefined(); // true + * isUndefined(null); // false + * isUndefined(undefined); // true + * isUndefined(''); // false + * isUndefined({}); // false + * isUndefined([]); // false + * isUndefined(function() {}); // false + */ +export function isUndefined(obj?: any): boolean { + return obj === undefined; +} + +/** + * Returns true if the passed value is not undefined. + * isNotUndefined(); // false + * isNotUndefined(null); // true + * isNotUndefined(undefined); // false + * isNotUndefined(''); // true + * isNotUndefined({}); // true + * isNotUndefined([]); // true + * isNotUndefined(function() {}); // true + */ +export function isNotUndefined(obj?: any): boolean { + return obj !== undefined; +} + /** * Returns true if the passed value is null or undefined. * hasNoValue(); // true * hasNoValue(null); // true * hasNoValue(undefined); // true * hasNoValue(''); // false + * hasNoValue({}); // false * hasNoValue([]); // false * hasNoValue(function() {}); // false */ export function hasNoValue(obj?: any): boolean { - return obj === null || obj === undefined; + return isUndefined(obj) || isNull(obj); } /** @@ -17,11 +74,12 @@ export function hasNoValue(obj?: any): boolean { * hasValue(null); // false * hasValue(undefined); // false * hasValue(''); // true + * hasValue({}); // true * hasValue([]); // true * hasValue(function() {}); // true */ export function hasValue(obj?: any): boolean { - return !hasNoValue(obj); + return isNotUndefined(obj) && isNotNull(obj); } /** diff --git a/src/app/shared/pagination/pagination.component.ts b/src/app/shared/pagination/pagination.component.ts index e62863fa73..a03590f22d 100644 --- a/src/app/shared/pagination/pagination.component.ts +++ b/src/app/shared/pagination/pagination.component.ts @@ -24,8 +24,6 @@ import { SortDirection, SortOptions } from '../../core/cache/models/sort-options import { hasValue } from '../empty.util'; import { PageInfo } from '../../core/shared/page-info.model'; import { isUndefined } from 'util'; -import { Store } from '@ngrx/store'; -import { ObjectCacheState } from '../../core/cache/object-cache.reducer'; /** * The default pagination controls component.