1+ /*!
2+ * ECMAScript 5 Polyfill v0.1
3+ * http://nuget.org/packages/ES5
4+ *
5+ * Copyright 2011, Douglas Crockford, Damian Edwards
6+ */
7+ // Function.prototype.bind
8+ if ( ! Function . prototype . hasOwnProperty ( 'bind' ) ) {
9+ Function . prototype . bind = function ( object ) {
10+ var slice = Array . prototype . slice ,
11+ func = this ,
12+ args = slice . call ( arguments , 1 ) ;
13+ return function ( ) {
14+ return func . apply ( object ,
15+ args . concat ( slice . call ( arguments , 0 ) ) ) ;
16+ } ;
17+ } ;
18+ }
19+
20+ // String.prototype.trim
21+ if ( ! String . prototype . hasOwnProperty ( 'trim' ) ) {
22+ String . prototype . trim = ( function ( re ) {
23+ return function ( ) {
24+ return this . replace ( re , "$1" ) ;
25+ } ;
26+ } ( / ^ \s * ( \S * ( \s + \S + ) * ) \s * $ / ) ) ;
27+ }
28+
29+ // Array.prototype.every
30+ if ( ! Array . prototype . hasOwnProperty ( 'every' ) ) {
31+ Array . prototype . every = function ( fun , thisp ) {
32+ var arr = this ,
33+ i ,
34+ length = arr . length ;
35+ for ( i = 0 ; i < length ; i += 1 ) {
36+ if ( arr . hasOwnProperty ( i ) &&
37+ ! fun . call ( thisp ,
38+ arr [ i ] , i , arr ) ) {
39+ return false ;
40+ }
41+ }
42+ return true ;
43+ } ;
44+ }
45+
46+ // Array.prototype.some
47+ if ( ! Array . prototype . hasOwnProperty ( 'some' ) ) {
48+ Array . prototype . some = function ( fun , thisp ) {
49+ var arr = this ,
50+ i ,
51+ length = arr . length ;
52+ for ( i = 0 ; i < length ; i += 1 ) {
53+ if ( arr . hasOwnProperty ( i ) &&
54+ fun . call ( thisp ,
55+ arr [ i ] , i , arr ) ) {
56+ return true ;
57+ }
58+ }
59+ return false ;
60+ } ;
61+ }
62+
63+ // Array.prototype.filter
64+ if ( ! Array . prototype . hasOwnProperty ( 'filter' ) ) {
65+ Array . prototype . filter = function ( fun , thisp ) {
66+ var arr = this ,
67+ i ,
68+ length = arr . length ,
69+ result = [ ] ,
70+ value ;
71+ for ( i = 0 ; i < length ; i += 1 ) {
72+ if ( arr . hasOwnProperty ( i ) ) {
73+ value = arr [ i ] ;
74+ if ( fun . call ( thisp , value , i , arr ) ) {
75+ result . push ( value ) ;
76+ }
77+ }
78+ }
79+ return result ;
80+ } ;
81+ }
82+
83+ // Array.prototype.forEach
84+ if ( ! Array . prototype . hasOwnProperty ( 'forEach' ) ) {
85+ Array . prototype . forEach = function ( fun , thisp ) {
86+ var arr = this ,
87+ i ,
88+ length = arr . length ;
89+ for ( i = 0 ; i < length ; i += 1 ) {
90+ if ( arr . hasOwnProperty ( i ) ) {
91+ fun . call ( thisp , arr [ i ] , i , arr ) ;
92+ }
93+ }
94+ } ;
95+ }
96+
97+ // Array.prototype.indexOf
98+ if ( ! Array . prototype . hasOwnProperty ( 'indexOf' ) ) {
99+ Array . prototype . indexOf = function ( searchElement , fromIndex ) {
100+ var arr = this ,
101+ i = fromIndex || 0 ,
102+ length = arr . length ;
103+ while ( i < length ) {
104+ if ( arr . hasOwnProperty ( i ) &&
105+ arr [ i ] === searchElement ) {
106+ return i ;
107+ }
108+ i += 1 ;
109+ }
110+ return - 1 ;
111+ } ;
112+ }
113+
114+ // Array.prototype.lastIndexOf
115+ if ( ! Array . prototype . hasOwnProperty ( 'lastIndexOf' ) ) {
116+ Array . prototype . lastIndexOf = function ( searchElement , fromIndex ) {
117+ var arr = this ,
118+ i = fromIndex ;
119+ if ( typeof i !== 'number' ) {
120+ i = arr . length - 1 ;
121+ }
122+ while ( i >= 0 ) {
123+ if ( arr . hasOwnProperty ( i ) &&
124+ arr [ i ] === searchElement ) {
125+ return i ;
126+ }
127+ i -= 1 ;
128+ }
129+ return - 1 ;
130+ } ;
131+ }
132+
133+ // Array.prototype.map
134+ if ( ! Array . prototype . hasOwnProperty ( 'map' ) ) {
135+ Array . prototype . map = function ( fun , thisp ) {
136+ var arr = this ,
137+ i ,
138+ length = arr . length ,
139+ result = [ ] ;
140+ for ( i = 0 ; i < length ; i += 1 ) {
141+ if ( arr . hasOwnProperty ( i ) ) {
142+ result [ i ] = fun . call ( thisp , arr [ i ] , i , arr ) ;
143+ }
144+ }
145+ return result ;
146+ } ;
147+ }
148+
149+ // Array.prototype.reduce
150+ if ( ! Array . prototype . hasOwnProperty ( 'reduce' ) ) {
151+ Array . prototype . reduce = function ( fun , initialValue ) {
152+ var arr = this ,
153+ i ,
154+ length = arr . length ;
155+ for ( i = 0 ; i < length ; i += 1 ) {
156+ if ( arr . hasOwnProperty ( i ) ) {
157+ initialValue = fun . call ( undefined ,
158+ initialValue , arr [ i ] , i , arr ) ;
159+ }
160+ }
161+ return initialValue ;
162+ } ;
163+ }
164+
165+ // Array.prototype.reduceRight
166+ if ( ! Array . prototype . hasOwnProperty ( 'reduceRight' ) ) {
167+ Array . prototype . reduceRight = function ( fun , initialValue ) {
168+ var arr = this ,
169+ i = arr . length - 1 ;
170+ while ( i >= 0 ) {
171+ if ( arr . hasOwnProperty ( i ) ) {
172+ initialValue = fun . call ( undefined ,
173+ initialValue , arr [ i ] , i , arr ) ;
174+ }
175+ i -= 1 ;
176+ }
177+ return initialValue ;
178+ } ;
179+ }
180+
181+ // Date.now()
182+ if ( ! Date . hasOwnProperty ( 'now' ) ) {
183+ Date . now = function ( ) {
184+ return ( new Date ( ) ) . getTime ( ) ;
185+ } ;
186+ }
187+
188+ // Date.prototype.toISOString
189+ if ( ! Date . prototype . hasOwnProperty ( 'toISOString' ) ) {
190+ Date . prototype . toISOString = function ( ) {
191+ function f ( n ) {
192+ return n < 10 ? '0' + n : n ;
193+ }
194+
195+ return this . getUTCFullYear ( ) + '-' +
196+ f ( this . getUTCMonth ( ) + 1 ) + '-' +
197+ f ( this . getUTCDate ( ) ) + 'T' +
198+ f ( this . getUTCHours ( ) ) + ':' +
199+ f ( this . getUTCMinutes ( ) ) + ':' +
200+ f ( this . getUTCSeconds ( ) ) + 'Z' ;
201+ } ;
202+ }
203+
204+ // Array.isArray
205+ if ( ! Array . hasOwnProperty ( 'isArray' ) ) {
206+ Array . isArray = function ( value ) {
207+ return Object . prototype
208+ . toString . apply ( value ) === '[object Array]' ;
209+ } ;
210+ }
211+
212+ // Object.keys
213+ if ( ! Object . hasOwnProperty ( 'keys' ) ) {
214+ Object . keys = function ( object ) {
215+ var name , result = [ ] ;
216+ for ( name in object ) {
217+ if ( Object . prototype
218+ . hasOwnProperty
219+ . call ( object , name ) ) {
220+ result . push ( name ) ;
221+ }
222+ }
223+ return result ;
224+ } ;
225+ }
226+
227+ // Object.create
228+ if ( ! Object . hasOwnProperty ( 'create' ) ) {
229+ Object . create = function ( object ) {
230+ function F ( ) { }
231+ F . prototype = object ;
232+ return new F ( ) ;
233+ } ;
234+ }
0 commit comments