Skip to content

Commit

Permalink
refactor(utils): replace isPresent with isNil
Browse files Browse the repository at this point in the history
isPresent is a misleading name (see #704) as it considers options
specified as null to not be present.
  • Loading branch information
cartant authored and katowulf committed Dec 12, 2016
1 parent 74d1a4e commit 4712c5b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ export class AngularFireAuth extends ReplaySubject<FirebaseAuthState> {
}
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.');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/auth/firebase_sdk_auth_backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 9 additions & 9 deletions src/database/firebase_list_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,44 +49,44 @@ 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);
}

return queried;
}

// 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);
}

Expand Down
23 changes: 9 additions & 14 deletions src/database/query_observable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import {
LimitToSelection,
Primitive
} from '../interfaces';

import { isNil } from '../utils';

export function observeQuery(query: Query): Observable<ScalarQuery> {
if (!isPresent(query)) {
if (isNil(query)) {
return observableOf(null);
}

Expand All @@ -34,7 +34,7 @@ export function observeQuery(query: Query): Observable<ScalarQuery> {
: [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: <boolean>orderBy.value };
Expand All @@ -51,7 +51,7 @@ export function observeQuery(query: Query): Observable<ScalarQuery> {
}
}

if (isPresent(limitTo) && isPresent(limitTo.value)) {
if (!isNil(limitTo) && !isNil(limitTo.value)) {
switch (limitTo.key) {
case LimitToOptions.First:
serializedOrder.limitToFirst = limitTo.value;
Expand All @@ -63,15 +63,15 @@ export function observeQuery(query: Query): Observable<ScalarQuery> {
}
}

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;
}

Expand All @@ -86,7 +86,7 @@ export function getOrderObservables(query: Query): Observable<OrderBySelection>
return ({ key, option })
})
.filter(({key, option}: { key: string, option: OrderByOptions }) => {
return isPresent(query[key]);
return !isNil(query[key]);
})
.map(({key, option}) => mapToOrderBySelection(<any>query[key], option));

Expand All @@ -104,7 +104,7 @@ export function getOrderObservables(query: Query): Observable<OrderBySelection>
export function getLimitToObservables(query: Query): Observable<LimitToSelection> | Observable<LimitToSelection | Observable<LimitToSelection>> {
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(<any>query[key], option));

if (observables.length === 1) {
Expand Down Expand Up @@ -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;
}

8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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({});
}

Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/worker/auth/worker/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
OAuth2Credentials,
OAuthCredentials
} from '../../auth_backend';
import {isPresent} from '../../../utils';
import {isNil} from '../../../utils';
import * as firebase from 'firebase';

@Injectable()
Expand All @@ -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);
Expand Down

0 comments on commit 4712c5b

Please sign in to comment.