@@ -48,22 +48,6 @@ function awaitAjax(url:string, method?:string):Promise<string> {
4848}
4949
5050
51- // implement functors on promises so that .then isnt so gross.
52- //
53- // exchanging one evil (duckwrapping) for another (monkeypatching a core type)
54- if ( typeof Promise . of == "undefined" ) {
55- Promise . of = function ( v ) {
56- return new Promise ( ( resolve , _ ) => resolve ( v ) ) ;
57- } ;
58- }
59-
60- if ( typeof Promise . prototype . map == "undefined" ) {
61- Promise . prototype . map = function ( fn ) {
62- return this . then ( ( data ) => Promise . of ( fn ( data ) ) ) ;
63- } ;
64- }
65-
66-
6751
6852// array polyfils taken from developer.mozilla.org
6953
@@ -240,3 +224,89 @@ if (!Array.prototype.forEach) {
240224 } ;
241225}
242226
227+
228+ if ( ! Array . of ) {
229+ Array . of = function ( ) {
230+ return Array . prototype . slice . call ( arguments ) ;
231+ } ;
232+ }
233+
234+
235+ // Production steps of ECMA-262, Edition 6, 22.1.2.1
236+ // Reference: https://people.mozilla.org/~jorendorff/es6-draft.html#sec-array.from
237+ if ( ! Array . from ) {
238+ Array . from = ( function ( ) {
239+ var toStr = Object . prototype . toString ;
240+ var isCallable = function ( fn ) {
241+ return typeof fn === 'function' || toStr . call ( fn ) === '[object Function]' ;
242+ } ;
243+ var toInteger = function ( value ) {
244+ var number = Number ( value ) ;
245+ if ( isNaN ( number ) ) { return 0 ; }
246+ if ( number === 0 || ! isFinite ( number ) ) { return number ; }
247+ return ( number > 0 ? 1 : - 1 ) * Math . floor ( Math . abs ( number ) ) ;
248+ } ;
249+ var maxSafeInteger = Math . pow ( 2 , 53 ) - 1 ;
250+ var toLength = function ( value ) {
251+ var len = toInteger ( value ) ;
252+ return Math . min ( Math . max ( len , 0 ) , maxSafeInteger ) ;
253+ } ;
254+
255+ // The length property of the from method is 1.
256+ return function from ( arrayLike /*, mapFn, thisArg */ ) {
257+ // 1. Let C be the this value.
258+ var C = this ;
259+
260+ // 2. Let items be ToObject(arrayLike).
261+ var items = Object ( arrayLike ) ;
262+
263+ // 3. ReturnIfAbrupt(items).
264+ if ( arrayLike == null ) {
265+ throw new TypeError ( "Array.from requires an array-like object - not null or undefined" ) ;
266+ }
267+
268+ // 4. If mapfn is undefined, then let mapping be false.
269+ var mapFn = arguments . length > 1 ? arguments [ 1 ] : void undefined ;
270+ var T ;
271+ if ( typeof mapFn !== 'undefined' ) {
272+ // 5. else
273+ // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
274+ if ( ! isCallable ( mapFn ) ) {
275+ throw new TypeError ( 'Array.from: when provided, the second argument must be a function' ) ;
276+ }
277+
278+ // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
279+ if ( arguments . length > 2 ) {
280+ T = arguments [ 2 ] ;
281+ }
282+ }
283+
284+ // 10. Let lenValue be Get(items, "length").
285+ // 11. Let len be ToLength(lenValue).
286+ var len = toLength ( items . length ) ;
287+
288+ // 13. If IsConstructor(C) is true, then
289+ // 13. a. Let A be the result of calling the [[Construct]] internal method of C with an argument list containing the single item len.
290+ // 14. a. Else, Let A be ArrayCreate(len).
291+ var A = isCallable ( C ) ? Object ( new C ( len ) ) : new Array ( len ) ;
292+
293+ // 16. Let k be 0.
294+ var k = 0 ;
295+ // 17. Repeat, while k < len… (also steps a - h)
296+ var kValue ;
297+ while ( k < len ) {
298+ kValue = items [ k ] ;
299+ if ( mapFn ) {
300+ A [ k ] = typeof T === 'undefined' ? mapFn ( kValue , k ) : mapFn . call ( T , kValue , k ) ;
301+ } else {
302+ A [ k ] = kValue ;
303+ }
304+ k += 1 ;
305+ }
306+ // 18. Let putStatus be Put(A, "length", len, true).
307+ A . length = len ;
308+ // 20. Return A.
309+ return A ;
310+ } ;
311+ } ( ) ) ;
312+ }
0 commit comments