Repository: NativeScript/NativeScript
File: packages/core/data/observable-array/index.ts (TypeScript)

Code before:
		return this._array.sort(compareFn);
	}

	/**
	 * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
	 * @param start The zero-based location in the array from which to start removing elements.
	 * @param deleteCount The number of elements to remove.
	 * @param items Elements to insert into the array in place of the deleted elements.
	 */
	splice(start: number, deleteCount?: number, ...items: any): T[] {
		const length = this._array.length;
		const result = this._array.splice.apply(this._array, arguments);

		this.notify(<ChangedData<T>>{
			eventName: CHANGE,
			object: this,
			action: ChangeType.Splice,
			index: Math.max(Math.min(start, this._array.length-1), 0),
			removed: result,
			addedCount: this._array.length + result.length - length,
		});
		if (this._array.length !== length) {
			this._notifyLengthChange();
		}

		return result;
	}

	/**
	 * Inserts new elements at the start of an array.
	 * @param items  Elements to insert at the start of the Array.
	 */
	unshift(...args: any): number {
		const length = this._array.length;
		const result = this._array.unshift.apply(this._array, arguments);

		this._addArgs.index = 0;
		this._addArgs.addedCount = result - length;

		this.notify(this._addArgs);
		this._notifyLengthChange();

		return result;
	}

	/**
	 * Returns the index of the first occurrence of a value in an array.
	 * @param searchElement The value to locate in the array.
	 * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
	 */
	indexOf(searchElement: T, fromIndex?: number): number {

Review comment (suggestion):
```suggestion
			//   the first value of the index, so this fixes it when you put a value too high in
```

Code after:
	/**
	 * Sorts an array.
	 * @param compareFn The name of the function used to determine the order of the elements. If omitted, the elements are sorted in ascending, ASCII character order.
	 */
	sort(compareFn?: (a: T, b: T) => number): T[] {
		return this._array.sort(compareFn);
	}

	/**
	 * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
	 * @param start The zero-based location in the array from which to start removing elements.
	 * @param deleteCount The number of elements to remove.
	 * @param items Elements to insert into the array in place of the deleted elements.
	 */
	splice(start: number, deleteCount?: number, ...items: any): T[] {
		const length = this._array.length;
		const result = this._array.splice.apply(this._array, arguments);

		this.notify(<ChangedData<T>>{
			eventName: CHANGE,
			object: this,
			action: ChangeType.Splice,

			// The logic here is a bit weird; so lets explain why it is written this way
			// First of all, if you ADD any items to the array, we want the index to point to
			//   the first value of the index, so this fixes it when you put a value to high in
			// If you remove items from the array, then the index needs to point to the INDEX
			//   where you removed the item.
			// If you add and remove items, the index will point to the remove location as that
			//   is the index you passed in.
			index: Math.max(Math.min(start, length - (result.length > 0 ? 1 : 0)), 0),
			removed: result,
			addedCount: this._array.length + result.length - length,
		});
		if (this._array.length !== length) {
			this._notifyLengthChange();
		}

		return result;
	}

	/**
	 * Inserts new elements at the start of an array.
	 * @param items  Elements to insert at the start of the Array.
	 */
	unshift(...args: any): number {
		const length = this._array.length;
		const result = this._array.unshift.apply(this._array, arguments);

		this._addArgs.index = 0;
		this._addArgs.addedCount = result - length;