Skip to content

Allow non-number source for TypedArray.from #27121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 64 additions & 10 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ interface TemplateStringsArray extends ReadonlyArray<string> {

/**
* The type of `import.meta`.
*
*
* If you need to declare that a given property exists on `import.meta`,
* this type may be augmented via interface merging.
*/
Expand Down Expand Up @@ -1843,13 +1843,19 @@ interface Int8ArrayConstructor {
*/
of(...items: number[]): Int8Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Int8Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int8Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int8Array;


}
Expand Down Expand Up @@ -2113,13 +2119,19 @@ interface Uint8ArrayConstructor {
*/
of(...items: number[]): Uint8Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Uint8Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint8Array;

}
declare const Uint8Array: Uint8ArrayConstructor;
Expand Down Expand Up @@ -2382,13 +2394,19 @@ interface Uint8ClampedArrayConstructor {
*/
of(...items: number[]): Uint8ClampedArray;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Uint8ClampedArray;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8ClampedArray;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint8ClampedArray;
}
declare const Uint8ClampedArray: Uint8ClampedArrayConstructor;

Expand Down Expand Up @@ -2649,13 +2667,19 @@ interface Int16ArrayConstructor {
*/
of(...items: number[]): Int16Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Int16Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int16Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int16Array;


}
Expand Down Expand Up @@ -2919,13 +2943,19 @@ interface Uint16ArrayConstructor {
*/
of(...items: number[]): Uint16Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Uint16Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint16Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint16Array;


}
Expand Down Expand Up @@ -3188,13 +3218,19 @@ interface Int32ArrayConstructor {
*/
of(...items: number[]): Int32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Int32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int32Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int32Array;

}
declare const Int32Array: Int32ArrayConstructor;
Expand Down Expand Up @@ -3456,13 +3492,19 @@ interface Uint32ArrayConstructor {
*/
of(...items: number[]): Uint32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Uint32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint32Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint32Array;

}
declare const Uint32Array: Uint32ArrayConstructor;
Expand Down Expand Up @@ -3725,13 +3767,19 @@ interface Float32ArrayConstructor {
*/
of(...items: number[]): Float32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Float32Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float32Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float32Array;


}
Expand Down Expand Up @@ -3995,13 +4043,19 @@ interface Float64ArrayConstructor {
*/
of(...items: number[]): Float64Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
*/
from(arrayLike: ArrayLike<number>): Float64Array;

/**
* Creates an array from an array-like or iterable object.
* @param arrayLike An array-like or iterable object to convert to an array.
* @param mapfn A mapping function to call on every element of the array.
* @param thisArg Value of 'this' used to invoke the mapfn.
*/
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float64Array;
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float64Array;

}
declare const Float64Array: Float64ArrayConstructor;
Expand Down
56 changes: 56 additions & 0 deletions tests/baselines/reference/typedArrays.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ function CreateTypedArraysOf2() {
return typedArrays;
}

function CreateTypedArraysFromMapFn2<T>(obj:ArrayLike<T>, mapFn: (n:T, v:number)=> number) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
typedArrays[1] = Uint8Array.from(obj, mapFn);
typedArrays[2] = Int16Array.from(obj, mapFn);
typedArrays[3] = Uint16Array.from(obj, mapFn);
typedArrays[4] = Int32Array.from(obj, mapFn);
typedArrays[5] = Uint32Array.from(obj, mapFn);
typedArrays[6] = Float32Array.from(obj, mapFn);
typedArrays[7] = Float64Array.from(obj, mapFn);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);

return typedArrays;
}

function CreateTypedArraysFromMapFn(obj:ArrayLike<number>, mapFn: (n:number, v:number)=> number) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
Expand Down Expand Up @@ -131,6 +146,21 @@ function CreateTypedArraysFromThisObj(obj:ArrayLike<number>, mapFn: (n:number, v
typedArrays[7] = Float64Array.from(obj, mapFn, thisArg);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);

return typedArrays;
}

function CreateTypedArraysFromThisObj2<T>(obj:ArrayLike<T>, mapFn: (n:T, v:number)=> number, thisArg: {}) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn, thisArg);
typedArrays[1] = Uint8Array.from(obj, mapFn, thisArg);
typedArrays[2] = Int16Array.from(obj, mapFn, thisArg);
typedArrays[3] = Uint16Array.from(obj, mapFn, thisArg);
typedArrays[4] = Int32Array.from(obj, mapFn, thisArg);
typedArrays[5] = Uint32Array.from(obj, mapFn, thisArg);
typedArrays[6] = Float32Array.from(obj, mapFn, thisArg);
typedArrays[7] = Float64Array.from(obj, mapFn, thisArg);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);

return typedArrays;
}

Expand Down Expand Up @@ -226,6 +256,19 @@ function CreateTypedArraysOf2() {
typedArrays[8] = Uint8ClampedArray.of(1, 2, 3, 4);
return typedArrays;
}
function CreateTypedArraysFromMapFn2(obj, mapFn) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
typedArrays[1] = Uint8Array.from(obj, mapFn);
typedArrays[2] = Int16Array.from(obj, mapFn);
typedArrays[3] = Uint16Array.from(obj, mapFn);
typedArrays[4] = Int32Array.from(obj, mapFn);
typedArrays[5] = Uint32Array.from(obj, mapFn);
typedArrays[6] = Float32Array.from(obj, mapFn);
typedArrays[7] = Float64Array.from(obj, mapFn);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);
return typedArrays;
}
function CreateTypedArraysFromMapFn(obj, mapFn) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn);
Expand All @@ -252,3 +295,16 @@ function CreateTypedArraysFromThisObj(obj, mapFn, thisArg) {
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);
return typedArrays;
}
function CreateTypedArraysFromThisObj2(obj, mapFn, thisArg) {
var typedArrays = [];
typedArrays[0] = Int8Array.from(obj, mapFn, thisArg);
typedArrays[1] = Uint8Array.from(obj, mapFn, thisArg);
typedArrays[2] = Int16Array.from(obj, mapFn, thisArg);
typedArrays[3] = Uint16Array.from(obj, mapFn, thisArg);
typedArrays[4] = Int32Array.from(obj, mapFn, thisArg);
typedArrays[5] = Uint32Array.from(obj, mapFn, thisArg);
typedArrays[6] = Float32Array.from(obj, mapFn, thisArg);
typedArrays[7] = Float64Array.from(obj, mapFn, thisArg);
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);
return typedArrays;
}
Loading