1
- /*
2
- * Copyright 2012 The Polymer Authors. All rights reserved.
3
- * Use of this source code is governed by a BSD-style
4
- * license that can be found in the LICENSE file.
5
- */
6
-
7
- if ( typeof WeakMap === 'undefined' ) {
8
- ( function ( ) {
9
- var defineProperty = Object . defineProperty ;
10
- var counter = Date . now ( ) % 1e9 ;
11
-
12
- var WeakMap = function ( ) {
13
- this . name = '__st' + ( Math . random ( ) * 1e9 >>> 0 ) + ( counter ++ + '__' ) ;
14
- } ;
15
-
16
- WeakMap . prototype = {
17
- set : function ( key , value ) {
18
- var entry = key [ this . name ] ;
19
- if ( entry && entry [ 0 ] === key )
20
- entry [ 1 ] = value ;
21
- else
22
- defineProperty ( key , this . name , { value : [ key , value ] , writable : true } ) ;
23
- return this ;
24
- } ,
25
- get : function ( key ) {
26
- var entry ;
27
- return ( entry = key [ this . name ] ) && entry [ 0 ] === key ?
28
- entry [ 1 ] : undefined ;
29
- } ,
30
- delete : function ( key ) {
31
- var entry = key [ this . name ] ;
32
- if ( ! entry ) return false ;
33
- var hasValue = entry [ 0 ] === key ;
34
- entry [ 0 ] = entry [ 1 ] = undefined ;
35
- return hasValue ;
36
- } ,
37
- has : function ( key ) {
38
- var entry = key [ this . name ] ;
39
- if ( ! entry ) return false ;
40
- return entry [ 0 ] === key ;
41
- }
42
- } ;
43
-
44
- window . WeakMap = WeakMap ;
45
- } ) ( ) ;
46
- }
1
+ ( function ( exports ) { 'use strict' ;
2
+ //shared pointer
3
+ var i ;
4
+ //shortcuts
5
+ var defineProperty = Object . defineProperty , is = function ( a , b ) { return isNaN ( a ) ? isNaN ( b ) : a === b ; } ;
6
+
7
+
8
+ //Polyfill global objects
9
+ if ( typeof WeakMap == 'undefined' ) {
10
+ exports . WeakMap = createCollection ( {
11
+ // WeakMap#delete(key:void*):boolean
12
+ 'delete' : sharedDelete ,
13
+ // WeakMap#clear():
14
+ clear : sharedClear ,
15
+ // WeakMap#get(key:void*):void*
16
+ get : sharedGet ,
17
+ // WeakMap#has(key:void*):boolean
18
+ has : mapHas ,
19
+ // WeakMap#set(key:void*, value:void*):void
20
+ set : sharedSet
21
+ } , true ) ;
22
+ }
23
+
24
+ if ( typeof Map == 'undefined' || typeof ( ( new Map ) . values ) !== 'function' || ! ( new Map ) . values ( ) . next ) {
25
+ exports . Map = createCollection ( {
26
+ // WeakMap#delete(key:void*):boolean
27
+ 'delete' : sharedDelete ,
28
+ //:was Map#get(key:void*[, d3fault:void*]):void*
29
+ // Map#has(key:void*):boolean
30
+ has : mapHas ,
31
+ // Map#get(key:void*):boolean
32
+ get : sharedGet ,
33
+ // Map#set(key:void*, value:void*):void
34
+ set : sharedSet ,
35
+ // Map#keys(void):Iterator
36
+ keys : sharedKeys ,
37
+ // Map#values(void):Iterator
38
+ values : sharedValues ,
39
+ // Map#entries(void):Iterator
40
+ entries : mapEntries ,
41
+ // Map#forEach(callback:Function, context:void*):void ==> callback.call(context, key, value, mapObject) === not in specs`
42
+ forEach : sharedForEach ,
43
+ // Map#clear():
44
+ clear : sharedClear
45
+ } ) ;
46
+ }
47
+
48
+ if ( typeof Set == 'undefined' || typeof ( ( new Set ) . values ) !== 'function' || ! ( new Set ) . values ( ) . next ) {
49
+ exports . Set = createCollection ( {
50
+ // Set#has(value:void*):boolean
51
+ has : setHas ,
52
+ // Set#add(value:void*):boolean
53
+ add : sharedAdd ,
54
+ // Set#delete(key:void*):boolean
55
+ 'delete' : sharedDelete ,
56
+ // Set#clear():
57
+ clear : sharedClear ,
58
+ // Set#keys(void):Iterator
59
+ keys : sharedValues , // specs actually say "the same function object as the initial value of the values property"
60
+ // Set#values(void):Iterator
61
+ values : sharedValues ,
62
+ // Set#entries(void):Iterator
63
+ entries : setEntries ,
64
+ // Set#forEach(callback:Function, context:void*):void ==> callback.call(context, value, index) === not in specs
65
+ forEach : sharedForEach
66
+ } ) ;
67
+ }
68
+
69
+ if ( typeof WeakSet == 'undefined' ) {
70
+ exports . WeakSet = createCollection ( {
71
+ // WeakSet#delete(key:void*):boolean
72
+ 'delete' : sharedDelete ,
73
+ // WeakSet#add(value:void*):boolean
74
+ add : sharedAdd ,
75
+ // WeakSet#clear():
76
+ clear : sharedClear ,
77
+ // WeakSet#has(value:void*):boolean
78
+ has : setHas
79
+ } , true ) ;
80
+ }
81
+
82
+
83
+ /**
84
+ * ES6 collection constructor
85
+ * @return {Function } a collection class
86
+ */
87
+ function createCollection ( proto , objectOnly ) {
88
+ function Collection ( a ) {
89
+ if ( ! this || this . constructor !== Collection ) return new Collection ( a ) ;
90
+ this . _keys = [ ] ;
91
+ this . _values = [ ] ;
92
+ this . _itp = [ ] ; // iteration pointers
93
+ this . objectOnly = objectOnly ;
94
+
95
+ //parse initial iterable argument passed
96
+ if ( a ) init . call ( this , a ) ;
97
+ }
98
+
99
+ //define size for non object-only collections
100
+ if ( ! objectOnly ) {
101
+ defineProperty ( proto , 'size' , {
102
+ get : sharedSize
103
+ } ) ;
104
+ }
105
+
106
+ //set prototype
107
+ proto . constructor = Collection ;
108
+ Collection . prototype = proto ;
109
+
110
+ return Collection ;
111
+ }
112
+
113
+
114
+ /** parse initial iterable argument passed */
115
+ function init ( a ) {
116
+ var i ;
117
+ //init Set argument, like `[1,2,3,{}]`
118
+ if ( this . add )
119
+ a . forEach ( this . add , this ) ;
120
+ //init Map argument like `[[1,2], [{}, 4]]`
121
+ else
122
+ a . forEach ( function ( a ) { this . set ( a [ 0 ] , a [ 1 ] ) } , this ) ;
123
+ }
124
+
125
+
126
+ /** delete */
127
+ function sharedDelete ( key ) {
128
+ if ( this . has ( key ) ) {
129
+ this . _keys . splice ( i , 1 ) ;
130
+ this . _values . splice ( i , 1 ) ;
131
+ // update iteration pointers
132
+ this . _itp . forEach ( function ( p ) { if ( i < p [ 0 ] ) p [ 0 ] -- ; } ) ;
133
+ }
134
+ // Aurora here does it while Canary doesn't
135
+ return - 1 < i ;
136
+ } ;
137
+
138
+ function sharedGet ( key ) {
139
+ return this . has ( key ) ? this . _values [ i ] : undefined ;
140
+ }
141
+
142
+ function has ( list , key ) {
143
+ if ( this . objectOnly && key !== Object ( key ) )
144
+ throw new TypeError ( "Invalid value used as weak collection key" ) ;
145
+ //NaN or 0 passed
146
+ if ( key != key || key === 0 ) for ( i = list . length ; i -- && ! is ( list [ i ] , key ) ; ) { }
147
+ else i = list . indexOf ( key ) ;
148
+ return - 1 < i ;
149
+ }
150
+
151
+ function setHas ( value ) {
152
+ return has . call ( this , this . _values , value ) ;
153
+ }
154
+
155
+ function mapHas ( value ) {
156
+ return has . call ( this , this . _keys , value ) ;
157
+ }
158
+
159
+ /** @chainable */
160
+ function sharedSet ( key , value ) {
161
+ this . has ( key ) ?
162
+ this . _values [ i ] = value
163
+ :
164
+ this . _values [ this . _keys . push ( key ) - 1 ] = value
165
+ ;
166
+ return this ;
167
+ }
168
+
169
+ /** @chainable */
170
+ function sharedAdd ( value ) {
171
+ if ( ! this . has ( value ) ) this . _values . push ( value ) ;
172
+ return this ;
173
+ }
174
+
175
+ function sharedClear ( ) {
176
+ this . _values . length = 0 ;
177
+ }
178
+
179
+ /** keys, values, and iterate related methods */
180
+ function sharedKeys ( ) {
181
+ return sharedIterator ( this . _itp , this . _keys ) ;
182
+ }
183
+
184
+ function sharedValues ( ) {
185
+ return sharedIterator ( this . _itp , this . _values ) ;
186
+ }
187
+
188
+ function mapEntries ( ) {
189
+ return sharedIterator ( this . _itp , this . _keys , this . _values ) ;
190
+ }
191
+
192
+ function setEntries ( ) {
193
+ return sharedIterator ( this . _itp , this . _values , this . _values ) ;
194
+ }
195
+
196
+ function sharedIterator ( itp , array , array2 ) {
197
+ var p = [ 0 ] , done = false ;
198
+ itp . push ( p ) ;
199
+ return {
200
+ next : function ( ) {
201
+ var v , k = p [ 0 ] ;
202
+ if ( ! done && k < array . length ) {
203
+ v = array2 ? [ array [ k ] , array2 [ k ] ] : array [ k ] ;
204
+ p [ 0 ] ++ ;
205
+ } else {
206
+ done = true ;
207
+ itp . splice ( itp . indexOf ( p ) , 1 ) ;
208
+ }
209
+ return { done : done , value : v } ;
210
+ }
211
+ } ;
212
+ }
213
+
214
+ function sharedSize ( ) {
215
+ return this . _values . length ;
216
+ }
217
+
218
+ function sharedForEach ( callback , context ) {
219
+ var it = this . entries ( ) ;
220
+ for ( ; ; ) {
221
+ var r = it . next ( ) ;
222
+ if ( r . done ) break ;
223
+ callback . call ( context , r . value [ 1 ] , r . value [ 0 ] , this ) ;
224
+ }
225
+ }
226
+
227
+ } ) ( typeof exports != 'undefined' && typeof global != 'undefined' ? global : window ) ;
0 commit comments