Skip to content

Commit d85c4e5

Browse files
Merge pull request #27121 from xl1/26977-typedarray
Allow non-number source for TypedArray.from
2 parents 16b6706 + ecc2ba7 commit d85c4e5

File tree

5 files changed

+807
-221
lines changed

5 files changed

+807
-221
lines changed

src/lib/es5.d.ts

Lines changed: 64 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ interface TemplateStringsArray extends ReadonlyArray<string> {
587587

588588
/**
589589
* The type of `import.meta`.
590-
*
590+
*
591591
* If you need to declare that a given property exists on `import.meta`,
592592
* this type may be augmented via interface merging.
593593
*/
@@ -1913,13 +1913,19 @@ interface Int8ArrayConstructor {
19131913
*/
19141914
of(...items: number[]): Int8Array;
19151915

1916+
/**
1917+
* Creates an array from an array-like or iterable object.
1918+
* @param arrayLike An array-like or iterable object to convert to an array.
1919+
*/
1920+
from(arrayLike: ArrayLike<number>): Int8Array;
1921+
19161922
/**
19171923
* Creates an array from an array-like or iterable object.
19181924
* @param arrayLike An array-like or iterable object to convert to an array.
19191925
* @param mapfn A mapping function to call on every element of the array.
19201926
* @param thisArg Value of 'this' used to invoke the mapfn.
19211927
*/
1922-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int8Array;
1928+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int8Array;
19231929

19241930

19251931
}
@@ -2183,13 +2189,19 @@ interface Uint8ArrayConstructor {
21832189
*/
21842190
of(...items: number[]): Uint8Array;
21852191

2192+
/**
2193+
* Creates an array from an array-like or iterable object.
2194+
* @param arrayLike An array-like or iterable object to convert to an array.
2195+
*/
2196+
from(arrayLike: ArrayLike<number>): Uint8Array;
2197+
21862198
/**
21872199
* Creates an array from an array-like or iterable object.
21882200
* @param arrayLike An array-like or iterable object to convert to an array.
21892201
* @param mapfn A mapping function to call on every element of the array.
21902202
* @param thisArg Value of 'this' used to invoke the mapfn.
21912203
*/
2192-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8Array;
2204+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint8Array;
21932205

21942206
}
21952207
declare const Uint8Array: Uint8ArrayConstructor;
@@ -2452,13 +2464,19 @@ interface Uint8ClampedArrayConstructor {
24522464
*/
24532465
of(...items: number[]): Uint8ClampedArray;
24542466

2467+
/**
2468+
* Creates an array from an array-like or iterable object.
2469+
* @param arrayLike An array-like or iterable object to convert to an array.
2470+
*/
2471+
from(arrayLike: ArrayLike<number>): Uint8ClampedArray;
2472+
24552473
/**
24562474
* Creates an array from an array-like or iterable object.
24572475
* @param arrayLike An array-like or iterable object to convert to an array.
24582476
* @param mapfn A mapping function to call on every element of the array.
24592477
* @param thisArg Value of 'this' used to invoke the mapfn.
24602478
*/
2461-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint8ClampedArray;
2479+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint8ClampedArray;
24622480
}
24632481
declare const Uint8ClampedArray: Uint8ClampedArrayConstructor;
24642482

@@ -2719,13 +2737,19 @@ interface Int16ArrayConstructor {
27192737
*/
27202738
of(...items: number[]): Int16Array;
27212739

2740+
/**
2741+
* Creates an array from an array-like or iterable object.
2742+
* @param arrayLike An array-like or iterable object to convert to an array.
2743+
*/
2744+
from(arrayLike: ArrayLike<number>): Int16Array;
2745+
27222746
/**
27232747
* Creates an array from an array-like or iterable object.
27242748
* @param arrayLike An array-like or iterable object to convert to an array.
27252749
* @param mapfn A mapping function to call on every element of the array.
27262750
* @param thisArg Value of 'this' used to invoke the mapfn.
27272751
*/
2728-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int16Array;
2752+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int16Array;
27292753

27302754

27312755
}
@@ -2989,13 +3013,19 @@ interface Uint16ArrayConstructor {
29893013
*/
29903014
of(...items: number[]): Uint16Array;
29913015

3016+
/**
3017+
* Creates an array from an array-like or iterable object.
3018+
* @param arrayLike An array-like or iterable object to convert to an array.
3019+
*/
3020+
from(arrayLike: ArrayLike<number>): Uint16Array;
3021+
29923022
/**
29933023
* Creates an array from an array-like or iterable object.
29943024
* @param arrayLike An array-like or iterable object to convert to an array.
29953025
* @param mapfn A mapping function to call on every element of the array.
29963026
* @param thisArg Value of 'this' used to invoke the mapfn.
29973027
*/
2998-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint16Array;
3028+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint16Array;
29993029

30003030

30013031
}
@@ -3258,13 +3288,19 @@ interface Int32ArrayConstructor {
32583288
*/
32593289
of(...items: number[]): Int32Array;
32603290

3291+
/**
3292+
* Creates an array from an array-like or iterable object.
3293+
* @param arrayLike An array-like or iterable object to convert to an array.
3294+
*/
3295+
from(arrayLike: ArrayLike<number>): Int32Array;
3296+
32613297
/**
32623298
* Creates an array from an array-like or iterable object.
32633299
* @param arrayLike An array-like or iterable object to convert to an array.
32643300
* @param mapfn A mapping function to call on every element of the array.
32653301
* @param thisArg Value of 'this' used to invoke the mapfn.
32663302
*/
3267-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Int32Array;
3303+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Int32Array;
32683304

32693305
}
32703306
declare const Int32Array: Int32ArrayConstructor;
@@ -3526,13 +3562,19 @@ interface Uint32ArrayConstructor {
35263562
*/
35273563
of(...items: number[]): Uint32Array;
35283564

3565+
/**
3566+
* Creates an array from an array-like or iterable object.
3567+
* @param arrayLike An array-like or iterable object to convert to an array.
3568+
*/
3569+
from(arrayLike: ArrayLike<number>): Uint32Array;
3570+
35293571
/**
35303572
* Creates an array from an array-like or iterable object.
35313573
* @param arrayLike An array-like or iterable object to convert to an array.
35323574
* @param mapfn A mapping function to call on every element of the array.
35333575
* @param thisArg Value of 'this' used to invoke the mapfn.
35343576
*/
3535-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Uint32Array;
3577+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Uint32Array;
35363578

35373579
}
35383580
declare const Uint32Array: Uint32ArrayConstructor;
@@ -3795,13 +3837,19 @@ interface Float32ArrayConstructor {
37953837
*/
37963838
of(...items: number[]): Float32Array;
37973839

3840+
/**
3841+
* Creates an array from an array-like or iterable object.
3842+
* @param arrayLike An array-like or iterable object to convert to an array.
3843+
*/
3844+
from(arrayLike: ArrayLike<number>): Float32Array;
3845+
37983846
/**
37993847
* Creates an array from an array-like or iterable object.
38003848
* @param arrayLike An array-like or iterable object to convert to an array.
38013849
* @param mapfn A mapping function to call on every element of the array.
38023850
* @param thisArg Value of 'this' used to invoke the mapfn.
38033851
*/
3804-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float32Array;
3852+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float32Array;
38053853

38063854

38073855
}
@@ -4065,13 +4113,19 @@ interface Float64ArrayConstructor {
40654113
*/
40664114
of(...items: number[]): Float64Array;
40674115

4116+
/**
4117+
* Creates an array from an array-like or iterable object.
4118+
* @param arrayLike An array-like or iterable object to convert to an array.
4119+
*/
4120+
from(arrayLike: ArrayLike<number>): Float64Array;
4121+
40684122
/**
40694123
* Creates an array from an array-like or iterable object.
40704124
* @param arrayLike An array-like or iterable object to convert to an array.
40714125
* @param mapfn A mapping function to call on every element of the array.
40724126
* @param thisArg Value of 'this' used to invoke the mapfn.
40734127
*/
4074-
from(arrayLike: ArrayLike<number>, mapfn?: (v: number, k: number) => number, thisArg?: any): Float64Array;
4128+
from<T>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => number, thisArg?: any): Float64Array;
40754129

40764130
}
40774131
declare const Float64Array: Float64ArrayConstructor;

tests/baselines/reference/typedArrays.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ function CreateTypedArraysOf2() {
104104
return typedArrays;
105105
}
106106

107+
function CreateTypedArraysFromMapFn2<T>(obj:ArrayLike<T>, mapFn: (n:T, v:number)=> number) {
108+
var typedArrays = [];
109+
typedArrays[0] = Int8Array.from(obj, mapFn);
110+
typedArrays[1] = Uint8Array.from(obj, mapFn);
111+
typedArrays[2] = Int16Array.from(obj, mapFn);
112+
typedArrays[3] = Uint16Array.from(obj, mapFn);
113+
typedArrays[4] = Int32Array.from(obj, mapFn);
114+
typedArrays[5] = Uint32Array.from(obj, mapFn);
115+
typedArrays[6] = Float32Array.from(obj, mapFn);
116+
typedArrays[7] = Float64Array.from(obj, mapFn);
117+
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);
118+
119+
return typedArrays;
120+
}
121+
107122
function CreateTypedArraysFromMapFn(obj:ArrayLike<number>, mapFn: (n:number, v:number)=> number) {
108123
var typedArrays = [];
109124
typedArrays[0] = Int8Array.from(obj, mapFn);
@@ -131,6 +146,21 @@ function CreateTypedArraysFromThisObj(obj:ArrayLike<number>, mapFn: (n:number, v
131146
typedArrays[7] = Float64Array.from(obj, mapFn, thisArg);
132147
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);
133148

149+
return typedArrays;
150+
}
151+
152+
function CreateTypedArraysFromThisObj2<T>(obj:ArrayLike<T>, mapFn: (n:T, v:number)=> number, thisArg: {}) {
153+
var typedArrays = [];
154+
typedArrays[0] = Int8Array.from(obj, mapFn, thisArg);
155+
typedArrays[1] = Uint8Array.from(obj, mapFn, thisArg);
156+
typedArrays[2] = Int16Array.from(obj, mapFn, thisArg);
157+
typedArrays[3] = Uint16Array.from(obj, mapFn, thisArg);
158+
typedArrays[4] = Int32Array.from(obj, mapFn, thisArg);
159+
typedArrays[5] = Uint32Array.from(obj, mapFn, thisArg);
160+
typedArrays[6] = Float32Array.from(obj, mapFn, thisArg);
161+
typedArrays[7] = Float64Array.from(obj, mapFn, thisArg);
162+
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);
163+
134164
return typedArrays;
135165
}
136166

@@ -226,6 +256,19 @@ function CreateTypedArraysOf2() {
226256
typedArrays[8] = Uint8ClampedArray.of(1, 2, 3, 4);
227257
return typedArrays;
228258
}
259+
function CreateTypedArraysFromMapFn2(obj, mapFn) {
260+
var typedArrays = [];
261+
typedArrays[0] = Int8Array.from(obj, mapFn);
262+
typedArrays[1] = Uint8Array.from(obj, mapFn);
263+
typedArrays[2] = Int16Array.from(obj, mapFn);
264+
typedArrays[3] = Uint16Array.from(obj, mapFn);
265+
typedArrays[4] = Int32Array.from(obj, mapFn);
266+
typedArrays[5] = Uint32Array.from(obj, mapFn);
267+
typedArrays[6] = Float32Array.from(obj, mapFn);
268+
typedArrays[7] = Float64Array.from(obj, mapFn);
269+
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn);
270+
return typedArrays;
271+
}
229272
function CreateTypedArraysFromMapFn(obj, mapFn) {
230273
var typedArrays = [];
231274
typedArrays[0] = Int8Array.from(obj, mapFn);
@@ -252,3 +295,16 @@ function CreateTypedArraysFromThisObj(obj, mapFn, thisArg) {
252295
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);
253296
return typedArrays;
254297
}
298+
function CreateTypedArraysFromThisObj2(obj, mapFn, thisArg) {
299+
var typedArrays = [];
300+
typedArrays[0] = Int8Array.from(obj, mapFn, thisArg);
301+
typedArrays[1] = Uint8Array.from(obj, mapFn, thisArg);
302+
typedArrays[2] = Int16Array.from(obj, mapFn, thisArg);
303+
typedArrays[3] = Uint16Array.from(obj, mapFn, thisArg);
304+
typedArrays[4] = Int32Array.from(obj, mapFn, thisArg);
305+
typedArrays[5] = Uint32Array.from(obj, mapFn, thisArg);
306+
typedArrays[6] = Float32Array.from(obj, mapFn, thisArg);
307+
typedArrays[7] = Float64Array.from(obj, mapFn, thisArg);
308+
typedArrays[8] = Uint8ClampedArray.from(obj, mapFn, thisArg);
309+
return typedArrays;
310+
}

0 commit comments

Comments
 (0)