```
23.2.3.27 %TypedArray%.prototype.slice ( start, end )
The interpretation and use of the arguments of this method are the same as for Array.prototype.slice as defined in 23.1.3.28.
This method performs the following steps when called:
1. Let O be the this value.
2. Perform ? ValidateTypedArray(O).
3. Let len be O.[[ArrayLength]].
4. Let relativeStart be ? ToIntegerOrInfinity(start).
5. If relativeStart = -∞, let k be 0.
6. Else if relativeStart < 0, let k be max(len + relativeStart, 0).
7. Else, let k be min(relativeStart, len).
8. If end is undefined, let relativeEnd be len; else let relativeEnd be ? ToIntegerOrInfinity(end).
9. If relativeEnd = -∞, let final be 0.
10. Else if relativeEnd < 0, let final be max(len + relativeEnd, 0).
11. Else, let final be min(relativeEnd, len).
12. Let count be max(final - k, 0).
13. Let A be ? TypedArraySpeciesCreate(O, « 𝔽(count) »).
14. If count > 0, then
a. If IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
b. Let srcType be TypedArrayElementType(O).
c. Let targetType be TypedArrayElementType(A).
d. If srcType is targetType, then
i. NOTE: The transfer must be performed in a manner that preserves the bit-level encoding of the source data.
ii. Let srcBuffer be O.[[ViewedArrayBuffer]].
iii. Let targetBuffer be A.[[ViewedArrayBuffer]].
iv. Let elementSize be TypedArrayElementSize(O).
v. Let srcByteOffset be O.[[ByteOffset]].
vi. Let srcByteIndex be (k × elementSize) + srcByteOffset.
vii. Let targetByteIndex be A.[[ByteOffset]].
viii. Let limit be targetByteIndex + count × elementSize.
ix. Repeat, while targetByteIndex < limit,
1. Let value be GetValueFromBuffer(srcBuffer, srcByteIndex, Uint8, true, Unordered).
2. Perform SetValueInBuffer(targetBuffer, targetByteIndex, Uint8, value, true, Unordered).
3. Set srcByteIndex to srcByteIndex + 1.
4. Set targetByteIndex to targetByteIndex + 1.
e. Else,
i. Let n be 0.
ii. Repeat, while k < final,
1.Let Pk be ! ToString(𝔽(k)).
2.Let kValue be ! Get(O, Pk).
3.Perform ! Set(A, ! ToString(𝔽(n)), kValue, true).
4.Set k to k + 1.
5.Set n to n + 1.
15. Return A.
This method is not generic. The this value must be an object with a [[TypedArrayName]] internal slot.
```
```
/**
* Creates a slice of current Uint8Array using range [begin, end)
*
* @param begin start index to be taken into slice
*
* @param end last index to be taken into slice
*
* @returns a new Uint8Array with elements of current Uint8Array[begin;end) where end index is excluded
*
* @link https://developer.mozilla.org/en ... ts/TypedArray/slice
*/
public slice(begin?: Number, end?: Number): Uint8Array {
return this.slice(asIntOrDefault(begin, 0 as int), asIntOrDefault(end, this.lengthInt))
}
/**
* Creates a slice of current Uint8Array using range [begin, end)
*
* @param begin start index to be taken into slice
*
* @param end last index to be taken into slice
*
* @returns a new Uint8Array with elements of current Uint8Array[begin;end) where end index is excluded
*
* @link https://developer.mozilla.org/en ... ts/TypedArray/slice
*/
public slice(begin: number, end: number): Uint8Array {
return this.slice(begin as int, end as int)
}
/**
* Creates a slice of current Uint8Array using range [begin, end)
*
* @param begin start index to be taken into slice
*
* @param end last index to be taken into slice
*
* @returns a new Uint8Array with elements of current Uint8Array[begin;end) where end index is excluded
*
* @link https://developer.mozilla.org/en ... ts/TypedArray/slice
*/
public slice(begin: number, end: int): Uint8Array {
return this.slice(begin as int, end as int)
}
/**
* Creates a slice of current Uint8Array using range [begin, end)
*
* @param begin start index to be taken into slice
*
* @param end last index to be taken into slice
*
* @returns a new Uint8Array with elements of current Uint8Array[begin;end) where end index is excluded
*
* @link https://developer.mozilla.org/en ... ts/TypedArray/slice
*/
public slice(begin: int, end: number): Uint8Array {
return this.slice(begin as int, end as int)
}
/**
* Creates a slice of current Uint8Array using range [begin, end)
*
* @param begin start index to be taken into slice
*
* @param end last index to be taken into slice
*
* @returns a new Uint8Array with elements of current Uint8Array[begin;end) where end index is excluded
*
* @link https://developer.mozilla.org/en ... ts/TypedArray/slice
*/
public slice(begin: int, end: int): Uint8Array {
const len: int = this.lengthInt
const relStart = normalizeIndex(begin, len)
const relEnd = normalizeIndex(end, len)
let count = relEnd - relStart
if (count < 0) {
count = 0
}
if (this.buffer instanceof ArrayBuffer) {
let buf = (this.buffer as ArrayBuffer).slice(relStart * Uint8Array.BYTES_PER_ELEMENT as int, relEnd * Uint8Array.BYTES_PER_ELEMENT as int) as ArrayBuffer
return new Uint8Array(buf)
} else if (this.buffer instanceof SharedArrayBuffer) {
let buf = (this.buffer as SharedArrayBuffer).slice(relStart * Uint8Array.BYTES_PER_ELEMENT as int, relEnd * Uint8Array.BYTES_PER_ELEMENT as int) as SharedArrayBuffer
return new Uint8Array(buf)
} else {
throw new Error("unexpected type of buffer")
}
}
/**
* Creates a slice of current Uint8Array using range [begin, this.lengthInt).
*
* @param begin start index to be taken into slice
*
* @returns a new Uint8Array with elements of current Uint8Array[begin, this.lengthInt)
*/
public slice(begin: number): Uint8Array {
return this.slice(begin as int)
}
/**
* Creates a slice of current Uint8Array using range [begin, this.lengthInt).
*
* @param begin start index to be taken into slice
*
* @returns a new Uint8Array with elements of current Uint8Array[begin, this.lengthInt)
*/
public slice(begin: int): Uint8Array {
return this.slice(begin, this.lengthInt)
}
```
const success = 0;
const fail = 1;
function testSliceWithOutParam(): int {
let source: number[] = [10, 20, 30, 40, 50, 60];
let ss = new ArrayBuffer(source.length as int * 1);
if(target.length as int != origin.length as int) {
console.log("Array length mismatch on slice");
return fail;
}
//Check all the data copied;
for(let i:int = 0; i< origin.length as int; i++) {
let tv = target as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
origin= new Uint8Array(0);
if(origin.length as int != 0){
return fail;
}
if(target.length as int != origin.length as int - sliceStart) {
console.log("Array length mismatch on slice One Params" + target.length);
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
let tv = target[i - sliceStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if(target.length as int != origin.length as int) {
console.log("Array length mismatch on slice One Params" + target.length);
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
let tv = target[i - sliceStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
return success;
}
function testSliceTwoParams(): int {
let source: number[] = [10, 20, 30, 40, 50, 60, 70, 80];
let ss = new ArrayBuffer(source.length as int * 1);
if(target.length as int != sliceEnd - sliceStart) {
console.log("Array length mismatch on slice2");
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
let tv = target[i - sliceStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if(target.length as int != sliceEnd - sliceStart) {
console.log("Array length mismatch on slice2");
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
let tv = target[i - sliceStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if(target.length as int != sliceEnd - sliceStart) {
console.log("Array length mismatch on slice2");
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
let tv = target[i - sliceStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if(target.length as int != sliceEnd - sliceStart) {
console.log("Array length mismatch on slice2");
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
let tv = target[i - sliceStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if(target.length as int != sliceEnd - sliceStart) {
console.log("Array length mismatch on slice2");
return fail;
}
//Check all the data copied;
for(let i:int = sliceStart; i< sliceEnd; i++) {
let tv = target[i - sliceStart] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}
if(target.length as int != 0) {
console.log("Array length mismatch on slice2");
return fail;
}
return success;
}
function testSliceTwoParamsWithOtherNumber(): int {
let source: number[] = [10, 20, 30, 40, 50, 60, 70, 80];
let ss = new ArrayBuffer(source.length as int * 1);
if(target.length as int != sliceEnd - (origin.length + sliceStart)) {
console.log("Array length mismatch on slice2");
return fail;
}
//Check all the data copied;
for(let i:int = (origin.length + sliceStart) as int; i< sliceEnd; i++) {
let tv = target[i - (origin.length + sliceStart)] as number;
let ov = origin as number;
console.log(source + "->" + tv + "->" + ov);
if(tv != ov) {
console.log("Array data mismatch");
return fail;
}
}