Skip to content

Commit bdf02d4

Browse files
committedNov 29, 2016
chore(util): add removeArrayItem fn
1 parent a4ab7ca commit bdf02d4

File tree

7 files changed

+336
-335
lines changed

7 files changed

+336
-335
lines changed
 

‎src/components/menu/menu-controller.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Menu } from './menu';
22
import { MenuType } from './menu-types';
33
import { Platform } from '../../platform/platform';
4+
import { removeArrayItem } from '../../util/util';
45

56

67
/**
@@ -291,10 +292,7 @@ export class MenuController {
291292
* @private
292293
*/
293294
unregister(menu: Menu) {
294-
let index = this._menus.indexOf(menu);
295-
if (index > -1) {
296-
this._menus.splice(index, 1);
297-
}
295+
removeArrayItem(this._menus, menu);
298296
}
299297

300298
/**

‎src/navigation/nav-controller-base.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { convertToView, convertToViews, NavOptions, DIRECTION_BACK, DIRECTION_FO
88
import { setZIndex } from './nav-util';
99
import { DeepLinker } from './deep-linker';
1010
import { GestureController } from '../gestures/gesture-controller';
11-
import { isBlank, isNumber, isPresent, assert } from '../util/util';
11+
import { isBlank, isNumber, isPresent, assert, removeArrayItem } from '../util/util';
1212
import { isViewController, ViewController } from './view-controller';
1313
import { Ion } from '../components/ion';
1414
import { Keyboard } from '../util/keyboard';
@@ -893,10 +893,7 @@ export class NavControllerBase extends Ion implements NavController {
893893
}
894894

895895
unregisterChildNav(nav: any) {
896-
const index = this._children.indexOf(nav);
897-
if (index > -1) {
898-
this._children.splice(index, 1);
899-
}
896+
removeArrayItem(this._children, nav);
900897
}
901898

902899
destroy() {

‎src/platform/platform.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EventEmitter, NgZone, OpaqueToken } from '@angular/core';
22

33
import { QueryParams } from './query-params';
44
import { ready, windowDimensions, flushDimensionCache } from '../util/dom';
5+
import { removeArrayItem } from '../util/util';
56

67

78
/**
@@ -373,10 +374,7 @@ export class Platform {
373374

374375
// return a function to unregister this back button action
375376
return () => {
376-
let index = this._bbActions.indexOf(action);
377-
if (index > -1) {
378-
this._bbActions.splice(index, 1);
379-
}
377+
removeArrayItem(this._bbActions, action);
380378
};
381379
}
382380

@@ -524,14 +522,11 @@ export class Platform {
524522
* @private
525523
*/
526524
onResize(cb: Function): Function {
527-
let self = this;
525+
const self = this;
528526
self._onResizes.push(cb);
529527

530528
return function() {
531-
const index = self._onResizes.indexOf(cb);
532-
if (index > -1) {
533-
self._onResizes.splice(index, 1);
534-
}
529+
removeArrayItem(self._onResizes, cb);
535530
};
536531
}
537532

‎src/util/dom.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function rafFrames(framesToWait: number, callback: Function) {
5151
if (framesToWait === 0) {
5252
callback();
5353

54-
}else if (framesToWait < 2) {
54+
} else if (framesToWait < 2) {
5555
rafId = nativeRaf(callback);
5656

5757
} else {

‎src/util/form.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Injectable } from '@angular/core';
2+
import { removeArrayItem } from './util';
23

34

45
/**
@@ -15,10 +16,7 @@ export class Form {
1516
}
1617

1718
deregister(input: any) {
18-
let index = this._inputs.indexOf(input);
19-
if (index > -1) {
20-
this._inputs.splice(index, 1);
21-
}
19+
removeArrayItem(this._inputs, input);
2220
if (input === this._focused) {
2321
this._focused = null;
2422
}

‎src/util/test/util.spec.ts

+315-311
Large diffs are not rendered by default.

‎src/util/util.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,21 @@ export const isCheckedProperty = function(a: any, b: any): boolean {
148148
* @private
149149
*/
150150
export function reorderArray(array: any[], indexes: {from: number, to: number}): any[] {
151-
let element = array[indexes.from];
151+
const element = array[indexes.from];
152152
array.splice(indexes.from, 1);
153153
array.splice(indexes.to, 0, element);
154154
return array;
155155
}
156156

157+
158+
/**
159+
* @private
160+
*/
161+
export function removeArrayItem(array: any[], item: any) {
162+
const index = array.indexOf(item);
163+
return !!~index && !!array.splice(index, 1);
164+
}
165+
157166
/**
158167
* @private
159168
*/

0 commit comments

Comments
 (0)
Please sign in to comment.