diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index 0ee06a9..8152ced 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index 0ee06a9..8152ced 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index 0ee06a9..8152ced 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index 0ee06a9..8152ced 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index 0ee06a9..8152ced 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 5298760..42ae05c 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -253,7 +253,7 @@ if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) { - chars.splice(lastSpace, i - lastSpace); + core.utils.removeItems(chars, lastSpace, i - lastSpace); i = lastSpace; lastSpace = -1; diff --git a/src/accessibility/AccessibilityManager.js b/src/accessibility/AccessibilityManager.js index 0ee06a9..8152ced 100644 --- a/src/accessibility/AccessibilityManager.js +++ b/src/accessibility/AccessibilityManager.js @@ -201,7 +201,7 @@ { child._accessibleActive = false; - this.children.splice(i, 1); + core.utils.removeItems(this.children, i, 1); this.div.removeChild( child._accessibleDiv ); this.pool.push(child._accessibleDiv); child._accessibleDiv = null; diff --git a/src/core/display/Container.js b/src/core/display/Container.js index ca03448..5968e9e 100644 --- a/src/core/display/Container.js +++ b/src/core/display/Container.js @@ -1,4 +1,5 @@ var math = require('../math'), + utils = require('../utils'), DisplayObject = require('./DisplayObject'), RenderTexture = require('../textures/RenderTexture'), _tempMatrix = new math.Matrix(); @@ -233,7 +234,7 @@ var currentIndex = this.getChildIndex(child); - this.children.splice(currentIndex, 1); //remove from old position + utils.removeItems(this.children, currentIndex, 1); // remove from old position this.children.splice(index, 0, child); //add at new position this.onChildrenChange(index); }; @@ -284,7 +285,7 @@ } child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); @@ -305,7 +306,7 @@ var child = this.getChildAt(index); child.parent = null; - this.children.splice(index, 1); + utils.removeItems(this.children, index, 1); // TODO - lets either do all callbacks or all events.. not both! this.onChildrenChange(index); diff --git a/src/core/renderers/webgl/WebGLRenderer.js b/src/core/renderers/webgl/WebGLRenderer.js index c37bab8..05258f6 100644 --- a/src/core/renderers/webgl/WebGLRenderer.js +++ b/src/core/renderers/webgl/WebGLRenderer.js @@ -449,7 +449,7 @@ { var i = this._managedTextures.indexOf(texture); if (i !== -1) { - this._managedTextures.splice(i, 1); + utils.removeItems(this._managedTextures, i, 1); } } } diff --git a/src/core/utils/index.js b/src/core/utils/index.js index 49ecd64..29cd7fc 100644 --- a/src/core/utils/index.js +++ b/src/core/utils/index.js @@ -231,11 +231,37 @@ * @param n {number} * @returns {number} 0 if n is 0, -1 if n is negative, 1 if n i positive */ - sign: function (n) { + sign: function (n) + { return n ? (n < 0 ? -1 : 1) : 0; }, /** + * removeItems + * + * @param {array} arr The target array + * @param {number} startIdx The index to begin removing from (inclusive) + * @param {number} removeCount How many items to remove + */ + removeItems: function (arr, startIdx, removeCount) + { + var length = arr.length; + + if (startIdx >= length || removeCount === 0) + { + return; + } + + removeCount = (startIdx+removeCount > length ? length-startIdx : removeCount); + for (var i = startIdx, len = length-removeCount; i < len; ++i) + { + arr[i] = arr[i + removeCount]; + } + + arr.length = len; + }, + + /** * @todo Describe property usage * @private */ diff --git a/src/extras/BitmapText.js b/src/extras/BitmapText.js index 5298760..42ae05c 100644 --- a/src/extras/BitmapText.js +++ b/src/extras/BitmapText.js @@ -253,7 +253,7 @@ if (lastSpace !== -1 && this.maxWidth > 0 && pos.x * scale > this.maxWidth) { - chars.splice(lastSpace, i - lastSpace); + core.utils.removeItems(chars, lastSpace, i - lastSpace); i = lastSpace; lastSpace = -1; diff --git a/test/unit/core/utils/util.test.js b/test/unit/core/utils/util.test.js index ee9e2cb..199a1a4 100644 --- a/test/unit/core/utils/util.test.js +++ b/test/unit/core/utils/util.test.js @@ -107,4 +107,34 @@ } }); }); + + describe('.removeItems', function () { + var arr; + + beforeEach(function () { + arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; + }); + + it('should return if the start index is greater than or equal to the length of the array', function () { + PIXI.utils.removeItems(arr, arr.length+1, 5); + expect(arr.length).to.be.equal(10); + }); + + it('should return if the remove count is 0', function () { + PIXI.utils.removeItems(arr, 2, 0); + expect(arr.length).to.be.equal(10); + }); + + it('should remove the number of elements specified from the array, starting from the start index', function () { + var res = [ 1, 2, 3, 8, 9, 10 ]; + PIXI.utils.removeItems(arr, 3, 4); + expect(arr).to.be.deep.equal(res); + }); + + it('should remove rest of elements if the delete count is > than the number of elements after start index', function () { + var res = [ 1, 2, 3, 4, 5, 6, 7 ]; + PIXI.utils.removeItems(arr, 7, 10); + expect(arr).to.be.deep.equal(res); + }); + }); });