From 4712c5b7ff9c6c9af60ec7e5cbe9db51604953e1 Mon Sep 17 00:00:00 2001 From: Nicholas Jamieson Date: Sat, 10 Dec 2016 15:49:44 +1000 Subject: [PATCH] refactor(utils): replace isPresent with isNil isPresent is a misleading name (see #704) as it considers options specified as null to not be present. --- src/auth/auth.ts | 4 ++-- src/auth/firebase_sdk_auth_backend.ts | 2 +- src/database/firebase_list_factory.ts | 18 +++++++++--------- src/database/query_observable.ts | 23 +++++++++-------------- src/utils.ts | 8 ++++---- src/worker/auth/worker/auth.ts | 4 ++-- 6 files changed, 27 insertions(+), 32 deletions(-) diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 40b52b399..7b3c998a3 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -79,12 +79,12 @@ export class AngularFireAuth extends ReplaySubject { } config = this._mergeConfigs(config); - if (!utils.isPresent(config.method)) { + if (utils.isNil(config.method)) { return this._reject('You must provide a login method'); } let providerMethods = [AuthMethods.Popup, AuthMethods.Redirect, AuthMethods.OAuthToken]; if (providerMethods.indexOf(config.method) != -1) { - if (!utils.isPresent(config.provider)) { + if (utils.isNil(config.provider)) { return this._reject('You must include a provider to use this auth method.'); } } diff --git a/src/auth/firebase_sdk_auth_backend.ts b/src/auth/firebase_sdk_auth_backend.ts index 04485c37e..99878766e 100644 --- a/src/auth/firebase_sdk_auth_backend.ts +++ b/src/auth/firebase_sdk_auth_backend.ts @@ -3,7 +3,7 @@ import { Injectable, Inject } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { Observer } from 'rxjs/Observer'; import { FirebaseApp } from '../tokens'; -import { isPresent, ZoneScheduler } from '../utils'; +import { ZoneScheduler } from '../utils'; import { authDataToAuthState, AuthBackend, diff --git a/src/database/firebase_list_factory.ts b/src/database/firebase_list_factory.ts index 723525180..68094cad4 100644 --- a/src/database/firebase_list_factory.ts +++ b/src/database/firebase_list_factory.ts @@ -49,19 +49,19 @@ export function FirebaseListFactory ( } // check equalTo - if (utils.isPresent(query.equalTo)) { + if (!utils.isNil(query.equalTo)) { queried = queried.equalTo(query.equalTo); - if (utils.isPresent(query.startAt) || query.endAt) { + if (!utils.isNil(query.startAt) || query.endAt) { throw new Error('Query Error: Cannot use startAt or endAt with equalTo.'); } // apply limitTos - if (utils.isPresent(query.limitToFirst)) { + if (!utils.isNil(query.limitToFirst)) { queried = queried.limitToFirst(query.limitToFirst); } - if (utils.isPresent(query.limitToLast)) { + if (!utils.isNil(query.limitToLast)) { queried = queried.limitToLast(query.limitToLast); } @@ -69,24 +69,24 @@ export function FirebaseListFactory ( } // check startAt - if (utils.isPresent(query.startAt)) { + if (!utils.isNil(query.startAt)) { queried = queried.startAt(query.startAt); } - if (utils.isPresent(query.endAt)) { + if (!utils.isNil(query.endAt)) { queried = queried.endAt(query.endAt); } - if (utils.isPresent(query.limitToFirst) && query.limitToLast) { + if (!utils.isNil(query.limitToFirst) && query.limitToLast) { throw new Error('Query Error: Cannot use limitToFirst with limitToLast.'); } // apply limitTos - if (utils.isPresent(query.limitToFirst)) { + if (!utils.isNil(query.limitToFirst)) { queried = queried.limitToFirst(query.limitToFirst); } - if (utils.isPresent(query.limitToLast)) { + if (!utils.isNil(query.limitToLast)) { queried = queried.limitToLast(query.limitToLast); } diff --git a/src/database/query_observable.ts b/src/database/query_observable.ts index 4f5aec90b..a95f1a868 100644 --- a/src/database/query_observable.ts +++ b/src/database/query_observable.ts @@ -14,10 +14,10 @@ import { LimitToSelection, Primitive } from '../interfaces'; - +import { isNil } from '../utils'; export function observeQuery(query: Query): Observable { - if (!isPresent(query)) { + if (isNil(query)) { return observableOf(null); } @@ -34,7 +34,7 @@ export function observeQuery(query: Query): Observable { : [OrderBySelection, Primitive, Primitive, Primitive, LimitToSelection]) => { let serializedOrder: any = {}; - if (isPresent(orderBy) && isPresent(orderBy.value)) { + if (!isNil(orderBy) && !isNil(orderBy.value)) { switch (orderBy.key) { case OrderByOptions.Key: serializedOrder = { orderByKey: orderBy.value }; @@ -51,7 +51,7 @@ export function observeQuery(query: Query): Observable { } } - if (isPresent(limitTo) && isPresent(limitTo.value)) { + if (!isNil(limitTo) && !isNil(limitTo.value)) { switch (limitTo.key) { case LimitToOptions.First: serializedOrder.limitToFirst = limitTo.value; @@ -63,15 +63,15 @@ export function observeQuery(query: Query): Observable { } } - if (isPresent(startAt)) { + if (!isNil(startAt)) { serializedOrder.startAt = startAt; } - if (isPresent(endAt)) { + if (!isNil(endAt)) { serializedOrder.endAt = endAt; } - if (isPresent(equalTo)) { + if (!isNil(equalTo)) { serializedOrder.equalTo = equalTo; } @@ -86,7 +86,7 @@ export function getOrderObservables(query: Query): Observable return ({ key, option }) }) .filter(({key, option}: { key: string, option: OrderByOptions }) => { - return isPresent(query[key]); + return !isNil(query[key]); }) .map(({key, option}) => mapToOrderBySelection(query[key], option)); @@ -104,7 +104,7 @@ export function getOrderObservables(query: Query): Observable export function getLimitToObservables(query: Query): Observable | Observable> { var observables = ['limitToFirst', 'limitToLast'] .map((key: string, option: LimitToOptions) => ({ key, option })) - .filter(({key, option}: { key: string, option: LimitToOptions }) => isPresent(query[key])) + .filter(({key, option}: { key: string, option: LimitToOptions }) => !isNil(query[key])) .map(({key, option}) => mapToLimitToSelection(query[key], option)); if (observables.length === 1) { @@ -191,8 +191,3 @@ function hasObservableProperties(query: Query): boolean { if (query.orderByKey instanceof Observable) return true; return false; } - -function isPresent(val: any): boolean { - return val !== undefined && val !== null; -} - diff --git a/src/utils.ts b/src/utils.ts index be08ff875..5fccc1b35 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,8 +4,8 @@ import { Scheduler } from 'rxjs/Scheduler'; import { queue } from 'rxjs/scheduler/queue'; import { AFUnwrappedDataSnapshot} from './interfaces'; -export function isPresent(obj: any): boolean { - return obj !== undefined && obj !== null; +export function isNil(obj: any): boolean { + return obj === undefined || obj === null; } export function isString(value: any): boolean { @@ -29,7 +29,7 @@ export function isFirebaseQuery(value: any): boolean { } export function isEmptyObject(obj: Object): boolean { - if (!isPresent(obj)) { return false; } + if (isNil(obj)) { return false; } return Object.keys(obj).length === 0 && JSON.stringify(obj) === JSON.stringify({}); } @@ -47,7 +47,7 @@ export interface CheckUrlRef { * unwrapMapFn(snapshot) => { name: 'David', $key: 'david', $exists: Function } */ export function unwrapMapFn (snapshot:firebase.database.DataSnapshot): AFUnwrappedDataSnapshot { - var unwrapped = isPresent(snapshot.val()) ? snapshot.val() : { $value: null }; + var unwrapped = !isNil(snapshot.val()) ? snapshot.val() : { $value: null }; if ((/string|number|boolean/).test(typeof unwrapped)) { unwrapped = { $value: unwrapped diff --git a/src/worker/auth/worker/auth.ts b/src/worker/auth/worker/auth.ts index 103d52adc..13074c09f 100644 --- a/src/worker/auth/worker/auth.ts +++ b/src/worker/auth/worker/auth.ts @@ -22,7 +22,7 @@ import { OAuth2Credentials, OAuthCredentials } from '../../auth_backend'; -import {isPresent} from '../../../utils'; +import {isNil} from '../../../utils'; import * as firebase from 'firebase'; @Injectable() @@ -48,7 +48,7 @@ export class WebWorkerFirebaseAuth extends AuthBackend { onAuth (onComplete: (authData: FirebaseAuthData) => void): void { this._fbApp.onAuth((authData) => { if (!this._gotAuth) return false; - if (isPresent(authData) && isPresent(this._authMetadata[authData.token])) { + if (!isNil(authData) && !isNil(this._authMetadata[authData.token])) { authData = this._authMetadata[authData.token]; } onComplete(authData);