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

View File

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

View File

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

View File

@ -51,7 +51,7 @@ export function rafFrames(framesToWait: number, callback: Function) {
if (framesToWait === 0) { if (framesToWait === 0) {
callback(); callback();
}else if (framesToWait < 2) { } else if (framesToWait < 2) {
rafId = nativeRaf(callback); rafId = nativeRaf(callback);
} else { } else {

View File

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

View File

@ -1,6 +1,8 @@
import * as util from '../util'; import * as util from '../util';
describe('isPrimitive', () => { describe('util', () => {
describe('isPrimitive', () => {
it('should be false for array/object values', () => { it('should be false for array/object values', () => {
expect(util.isPrimitive({})).toEqual(false); expect(util.isPrimitive({})).toEqual(false);
@ -31,9 +33,9 @@ describe('isPrimitive', () => {
expect(util.isPrimitive('hi')).toEqual(true); expect(util.isPrimitive('hi')).toEqual(true);
}); });
}); });
describe('isCheckedProperty', () => { describe('isCheckedProperty', () => {
it('should test a=undefined', () => { it('should test a=undefined', () => {
expect(util.isCheckedProperty(undefined, undefined)).toBe(true); expect(util.isCheckedProperty(undefined, undefined)).toBe(true);
@ -260,9 +262,9 @@ describe('isCheckedProperty', () => {
expect(util.isCheckedProperty('string', 'false')).toBe(false); expect(util.isCheckedProperty('string', 'false')).toBe(false);
}); });
}); });
describe('isTrueProperty', () => { describe('isTrueProperty', () => {
it('should be true from boolean true', () => { it('should be true from boolean true', () => {
expect(util.isTrueProperty(true)).toBe(true); expect(util.isTrueProperty(true)).toBe(true);
@ -310,9 +312,9 @@ describe('isTrueProperty', () => {
expect(util.isTrueProperty('doesnt actually matter')).toBe(false); expect(util.isTrueProperty('doesnt actually matter')).toBe(false);
}); });
}); });
describe('extend', () => { describe('extend', () => {
it('should extend simple', () => { it('should extend simple', () => {
var obj = { a: '0', c: '0' }; var obj = { a: '0', c: '0' };
@ -333,9 +335,9 @@ describe('extend', () => {
}); });
}); });
}); });
describe('defaults', () => { describe('defaults', () => {
it('should simple defaults', () => { it('should simple defaults', () => {
var obj = { a: '1' }; var obj = { a: '1' };
@ -359,4 +361,6 @@ describe('defaults', () => {
}); });
}); });
});
}); });

View File

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