chore(util): add removeArrayItem fn

This commit is contained in:
Adam Bradley
2016-11-29 10:21:47 -06:00
parent a4ab7cae8f
commit bdf02d4280
7 changed files with 379 additions and 378 deletions

View File

@ -1,6 +1,7 @@
import { Menu } from './menu';
import { MenuType } from './menu-types';
import { Platform } from '../../platform/platform';
import { removeArrayItem } from '../../util/util';
/**
@ -291,10 +292,7 @@ export class MenuController {
* @private
*/
unregister(menu: Menu) {
let index = this._menus.indexOf(menu);
if (index > -1) {
this._menus.splice(index, 1);
}
removeArrayItem(this._menus, menu);
}
/**

View File

@ -8,7 +8,7 @@ import { convertToView, convertToViews, NavOptions, DIRECTION_BACK, DIRECTION_FO
import { setZIndex } from './nav-util';
import { DeepLinker } from './deep-linker';
import { GestureController } from '../gestures/gesture-controller';
import { isBlank, isNumber, isPresent, assert } from '../util/util';
import { isBlank, isNumber, isPresent, assert, removeArrayItem } from '../util/util';
import { isViewController, ViewController } from './view-controller';
import { Ion } from '../components/ion';
import { Keyboard } from '../util/keyboard';
@ -893,10 +893,7 @@ export class NavControllerBase extends Ion implements NavController {
}
unregisterChildNav(nav: any) {
const index = this._children.indexOf(nav);
if (index > -1) {
this._children.splice(index, 1);
}
removeArrayItem(this._children, nav);
}
destroy() {

View File

@ -2,6 +2,7 @@ import { EventEmitter, NgZone, OpaqueToken } from '@angular/core';
import { QueryParams } from './query-params';
import { ready, windowDimensions, flushDimensionCache } from '../util/dom';
import { removeArrayItem } from '../util/util';
/**
@ -373,10 +374,7 @@ export class Platform {
// return a function to unregister this back button action
return () => {
let index = this._bbActions.indexOf(action);
if (index > -1) {
this._bbActions.splice(index, 1);
}
removeArrayItem(this._bbActions, action);
};
}
@ -524,14 +522,11 @@ export class Platform {
* @private
*/
onResize(cb: Function): Function {
let self = this;
const self = this;
self._onResizes.push(cb);
return function() {
const index = self._onResizes.indexOf(cb);
if (index > -1) {
self._onResizes.splice(index, 1);
}
removeArrayItem(self._onResizes, cb);
};
}

View File

@ -1,4 +1,5 @@
import { Injectable } from '@angular/core';
import { removeArrayItem } from './util';
/**
@ -15,10 +16,7 @@ export class Form {
}
deregister(input: any) {
let index = this._inputs.indexOf(input);
if (index > -1) {
this._inputs.splice(index, 1);
}
removeArrayItem(this._inputs, input);
if (input === this._focused) {
this._focused = null;
}

View File

@ -1,5 +1,7 @@
import * as util from '../util';
describe('util', () => {
describe('isPrimitive', () => {
it('should be false for array/object values', () => {
@ -360,3 +362,5 @@ describe('defaults', () => {
});
});
});

View File

@ -148,12 +148,21 @@ export const isCheckedProperty = function(a: any, b: any): boolean {
* @private
*/
export function reorderArray(array: any[], indexes: {from: number, to: number}): any[] {
let element = array[indexes.from];
const element = array[indexes.from];
array.splice(indexes.from, 1);
array.splice(indexes.to, 0, element);
return array;
}
/**
* @private
*/
export function removeArrayItem(array: any[], item: any) {
const index = array.indexOf(item);
return !!~index && !!array.splice(index, 1);
}
/**
* @private
*/