~tomasgroth/openlp/portable-path

« back to all changes in this revision

Viewing changes to tests/js/polyfill.js

  • Committer: Tomas Groth
  • Date: 2019-04-30 19:02:42 UTC
  • mfrom: (2829.2.32 openlp)
  • Revision ID: tomasgroth@yahoo.dk-20190430190242-6zwjk8724tyux70m
trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * This file contains polyfills, implementing functionality that exists in WebEngine,
 
3
 * but isn't yet supported by PhantomJS.
 
4
 *
 
5
 * These polyfills have been taken from Mozilla Developer Network.
 
6
 */
 
7
// Production steps of ECMA-262, Edition 6, 22.1.2.1
 
8
if (!Array.from) {
 
9
  Array.from = (function () {
 
10
    var toStr = Object.prototype.toString;
 
11
    var isCallable = function (fn) {
 
12
      return typeof fn === 'function' || toStr.call(fn) === '[object Function]';
 
13
    };
 
14
    var toInteger = function (value) {
 
15
      var number = Number(value);
 
16
      if (isNaN(number)) { return 0; }
 
17
      if (number === 0 || !isFinite(number)) { return number; }
 
18
      return (number > 0 ? 1 : -1) * Math.floor(Math.abs(number));
 
19
    };
 
20
    var maxSafeInteger = Math.pow(2, 53) - 1;
 
21
    var toLength = function (value) {
 
22
      var len = toInteger(value);
 
23
      return Math.min(Math.max(len, 0), maxSafeInteger);
 
24
    };
 
25
 
 
26
    // The length property of the from method is 1.
 
27
    return function from(arrayLike/*, mapFn, thisArg */) {
 
28
      // 1. Let C be the this value.
 
29
      var C = this;
 
30
 
 
31
      // 2. Let items be ToObject(arrayLike).
 
32
      var items = Object(arrayLike);
 
33
 
 
34
      // 3. ReturnIfAbrupt(items).
 
35
      if (arrayLike == null) {
 
36
        throw new TypeError('Array.from requires an array-like object - not null or undefined');
 
37
      }
 
38
 
 
39
      // 4. If mapfn is undefined, then let mapping be false.
 
40
      var mapFn = arguments.length > 1 ? arguments[1] : void undefined;
 
41
      var T;
 
42
      if (typeof mapFn !== 'undefined') {
 
43
        // 5. else
 
44
        // 5. a If IsCallable(mapfn) is false, throw a TypeError exception.
 
45
        if (!isCallable(mapFn)) {
 
46
          throw new TypeError('Array.from: when provided, the second argument must be a function');
 
47
        }
 
48
 
 
49
        // 5. b. If thisArg was supplied, let T be thisArg; else let T be undefined.
 
50
        if (arguments.length > 2) {
 
51
          T = arguments[2];
 
52
        }
 
53
      }
 
54
 
 
55
      // 10. Let lenValue be Get(items, "length").
 
56
      // 11. Let len be ToLength(lenValue).
 
57
      var len = toLength(items.length);
 
58
 
 
59
      // 13. If IsConstructor(C) is true, then
 
60
      // 13. a. Let A be the result of calling the [[Construct]] internal method 
 
61
      // of C with an argument list containing the single item len.
 
62
      // 14. a. Else, Let A be ArrayCreate(len).
 
63
      var A = isCallable(C) ? Object(new C(len)) : new Array(len);
 
64
 
 
65
      // 16. Let k be 0.
 
66
      var k = 0;
 
67
      // 17. Repeat, while k < len… (also steps a - h)
 
68
      var kValue;
 
69
      while (k < len) {
 
70
        kValue = items[k];
 
71
        if (mapFn) {
 
72
          A[k] = typeof T === 'undefined' ? mapFn(kValue, k) : mapFn.call(T, kValue, k);
 
73
        } else {
 
74
          A[k] = kValue;
 
75
        }
 
76
        k += 1;
 
77
      }
 
78
      // 18. Let putStatus be Put(A, "length", len, true).
 
79
      A.length = len;
 
80
      // 20. Return A.
 
81
      return A;
 
82
    };
 
83
  }());
 
84
}