Remove Promise polyfill.
[pwl6.git] / src / yuu / pre.js
1 /* This is free and unencumbered software released into the public
2 domain. To the extent possible under law, the author of this file
3 waives all copyright and related or neighboring rights to it.
4 */
5
6 (function () {
7 "use strict";
8
9 /** Polyfills and cross-browser fixes */
10
11 if (!Math.sign)
12 Math.sign = function (a) { return a && (a > 0) - (a < 0); };
13
14 if (!String.prototype.repeat)
15 Object.defineProperty(String.prototype, "repeat", {
16 value: function (count) {
17 var string = this.toString();
18 var result = '';
19 var n = count | 0;
20 while (n) {
21 if (n % 2 === 1)
22 result += string;
23 if (n > 1)
24 string += string;
25 n >>= 1;
26 }
27 return result;
28 }
29 });
30
31 if (!String.prototype.startsWith)
32 Object.defineProperty(String.prototype, "startsWith", {
33 value: function (sub) {
34 return this.lastIndexOf(sub, 0) !== -1;
35 }
36 });
37
38 if (!String.prototype.endsWith)
39 Object.defineProperty(String.prototype, "endsWith", {
40 value: function (sub) {
41 return this.indexOf(sub, this.length - sub.length) !== -1;
42 }
43 });
44
45 function toObject (o) {
46 if (o === null || o === undefined)
47 throw new TypeError("invalid ToObject cast");
48 return Object(o);
49 }
50
51 if (!Object.assign)
52 Object.defineProperty(Object, "assign", {
53 value: function (target) {
54 target = toObject(target);
55 for (var i = 1; i < arguments.length; ++i) {
56 var source = toObject(arguments[i]);
57 var keys = Object.keys(source);
58 for (var j = 0; j < keys.length; ++j)
59 target[keys[j]] = source[keys[j]];
60 }
61 }
62 });
63
64 if (!Array.prototype.fill)
65 Object.defineProperty(Array.prototype, "fill", {
66 value: function (value) {
67 var beg = arguments.length > 1 ? +arguments[1] : 0;
68 var end = arguments.length > 2 ? +arguments[2] : this.length;
69 if (beg < 0) beg += this.length;
70 if (end < 0) end += this.length;
71 for (var i = beg; i < end; ++i)
72 this[i] = value;
73 return this;
74 }
75 });
76
77 if (typeof window !== "undefined") {
78 window.requestAnimationFrame = (
79 window.requestAnimationFrame
80 || window.mozRequestAnimationFrame
81 || window.webkitRequestAnimationFrame);
82 window.cancelAnimationFrame = (
83 window.cancelAnimationFrame
84 || window.mozCancelAnimationFrame
85 || window.webkitCancelAnimationFrame);
86
87 if (!window.AudioContext)
88 window.AudioContext = (
89 window.webkitAudioContext
90 || window.mozAudioContext);
91
92 if (window.AudioContext && !window.AudioContext.prototype.createGain)
93 window.AudioContext.prototype.createGain =
94 window.AudioContext.prototype.createGainNode;
95
96 /** Canonicalize fullscreen function names if available.
97
98 Based on http://fullscreen.spec.whatwg.org/, June 7th 2013.
99 */
100
101 if (!Element.prototype.requestFullscreen)
102 Element.prototype.requestFullscreen = (
103 Element.prototype.requestFullScreen
104 || Element.prototype.webkitRequestFullscreen
105 || Element.prototype.webkitRequestFullScreen
106 || Element.prototype.mozRequestFullScreen
107 || function () {});
108 if (!document.exitFullscreen)
109 document.exitFullscreen = (
110 document.webkitExitFullscreen
111 || document.webkitCancelFullScreen
112 || document.mozCancelFullScreen
113 || function () {});
114 if (!document.hasOwnProperty("fullscreenEnabled"))
115 Object.defineProperty(document, "fullscreenEnabled", {
116 enumerable: true,
117 get: function () {
118 return (this.webkitFullscreenEnabled
119 || this.mozFullScreenEnabled
120 || false);
121 }
122 });
123 if (!document.hasOwnProperty("fullscreenElement"))
124 Object.defineProperty(document, "fullscreenElement", {
125 enumerable: true,
126 get: function () {
127 return (this.webkitFullscreenElement
128 || this.webkitCurrentFullScreenElement
129 || this.mozFullScreenEleement
130 || null);
131 }
132 });
133 }
134
135 }).call(this);