@@ -27,9 +27,12 @@ let already_patched = (typeof window !== 'undefined'
27
27
export const clone = obj => Object . assign ( Object . create ( Object . getPrototypeOf ( obj ) ) , obj ) ;
28
28
29
29
if ( ! already_patched ) {
30
- Object . prototype . clone = function ( ) {
31
- return clone ( this ) ;
32
- } ;
30
+ Object . defineProperty ( Object . prototype , 'clone' , {
31
+ value : function ( ) { return clone ( this ) } ,
32
+ writable : true ,
33
+ configurable : true ,
34
+ enumerable : false
35
+ } ) ;
33
36
}
34
37
35
38
export const type = element => (
@@ -72,115 +75,165 @@ if (!already_patched) {
72
75
Math . HALF_PI = Math . PI * 0.5 ;
73
76
Math . triangle = t => Math . abs ( ( ( t - 1 ) % 4 ) - 2 ) - 1 ;
74
77
75
- Number . prototype . roundTo = function ( dp ) {
76
- return parseFloat ( ( this ) . toFixed ( dp ) ) ;
77
- } ;
78
- Number . prototype . times = function ( fn ) {
79
- for ( let i = 1 ; i <= this . valueOf ( ) ; ++ i )
80
- fn ( i ) ;
81
- } ;
78
+ Object . defineProperty ( Number . prototype , 'roundTo' , {
79
+ value : function ( dp ) {
80
+ return parseFloat ( ( this ) . toFixed ( dp ) ) ;
81
+ } ,
82
+ enumerable : false ,
83
+ } ) ;
84
+ Object . defineProperty ( Number . prototype , 'times' , {
85
+ value : function ( fn ) {
86
+ for ( let i = 1 ; i <= this . valueOf ( ) ; ++ i ) fn ( i ) ;
87
+ } ,
88
+ enumerable : false ,
89
+ } ) ;
82
90
83
- Array . prototype . each = Array . prototype . forEach ;
84
- Array . prototype . select = Array . prototype . filter ;
85
- Array . prototype . reject = function ( lambda , array ) {
86
- return this . filter ( ( e ) => ! lambda ( e ) , array )
87
- } ;
88
- Array . prototype . mag = function ( ) {
89
- return Math . sqrt ( this . reduce ( ( i , j ) => i + j ** 2 , 0 ) ) ;
90
- } ;
91
- Array . prototype . normalize = function ( ) {
92
- if ( this . every ( e => e === 0 ) ) {
93
- return this ;
94
- }
95
- return this . map ( e => e / this . mag ( ) ) ;
96
- } ;
97
- Array . prototype . rotate = function ( theta , origin = [ 0 , 0 ] ) {
98
- return [ // Only 2D
99
- origin [ 0 ] + ( this [ 0 ] - origin [ 0 ] ) * Math . cos ( theta ) - ( this [ 1 ] - origin [ 1 ] ) * Math . sin ( theta ) ,
100
- origin [ 1 ] + ( this [ 0 ] - origin [ 0 ] ) * Math . sin ( theta ) + ( this [ 1 ] - origin [ 1 ] ) * Math . cos ( theta )
101
- ] ;
102
- } ;
91
+ Object . defineProperty ( Array . prototype , 'each' , {
92
+ value : Array . prototype . forEach ,
93
+ enumerable : false ,
94
+ } ) ;
95
+ Object . defineProperty ( Array . prototype , 'select' , {
96
+ value : Array . prototype . filter ,
97
+ enumerable : false ,
98
+ } ) ;
99
+ Object . defineProperty ( Array . prototype , 'reject' , {
100
+ value : function ( lambda , array ) {
101
+ return this . filter ( ( e ) => ! lambda ( e ) , array ) ;
102
+ } ,
103
+ enumerable : false ,
104
+ } ) ;
105
+ Object . defineProperty ( Array . prototype , 'mag' , {
106
+ value : function ( ) {
107
+ return Math . sqrt ( this . reduce ( ( i , j ) => i + j ** 2 , 0 ) ) ;
108
+ } ,
109
+ enumerable : false ,
110
+ } ) ;
111
+ Object . defineProperty ( Array . prototype , 'normalize' , {
112
+ value : function ( ) {
113
+ if ( this . every ( e => e === 0 ) ) {
114
+ return this ;
115
+ }
116
+ return this . map ( e => e / this . mag ( ) ) ;
117
+ } ,
118
+ enumerable : false ,
119
+ } ) ;
120
+ Object . defineProperty ( Array . prototype , 'rotate' , {
121
+ value : function ( theta , origin = [ 0 , 0 ] ) {
122
+ return [ // Only 2D
123
+ origin [ 0 ] + ( this [ 0 ] - origin [ 0 ] ) * Math . cos ( theta ) - ( this [ 1 ] - origin [ 1 ] ) * Math . sin ( theta ) ,
124
+ origin [ 1 ] + ( this [ 0 ] - origin [ 0 ] ) * Math . sin ( theta ) + ( this [ 1 ] - origin [ 1 ] ) * Math . cos ( theta )
125
+ ] ;
126
+ } ,
127
+ enumerable : false ,
128
+ } ) ;
103
129
Object . defineProperty ( Array . prototype , 'x' , {
104
130
get : function x ( ) {
105
131
return this [ 0 ] ;
106
132
} ,
107
133
set : function x ( new_x ) {
108
134
return this [ 0 ] = new_x ;
109
- }
135
+ } ,
136
+ enumerable : false ,
110
137
} ) ;
111
138
Object . defineProperty ( Array . prototype , 'y' , {
112
139
get : function y ( ) {
113
140
return this [ 1 ] ;
114
141
} ,
115
142
set : function y ( new_y ) {
116
143
return this [ 1 ] = new_y ;
117
- }
144
+ } ,
145
+ enumerable : false ,
118
146
} ) ;
119
147
Object . defineProperty ( Array . prototype , 'z' , {
120
148
get : function y ( ) {
121
149
return this [ 2 ] ;
122
150
} ,
123
151
set : function z ( new_z ) {
124
152
return this [ 2 ] = new_z ;
125
- }
153
+ } ,
154
+ enumerable : false ,
126
155
} ) ;
127
156
Object . defineProperty ( Array . prototype , 'first' , {
128
157
get : function first ( ) {
129
158
return this [ 0 ] ;
130
159
} ,
131
160
set : function first ( other ) {
132
161
return this [ 0 ] = other ;
133
- }
162
+ } ,
163
+ enumerable : false ,
134
164
} ) ;
135
165
Object . defineProperty ( Array . prototype , 'last' , {
136
166
get : function last ( ) {
137
167
return this [ this . length - 1 ] ;
138
168
} ,
139
169
set : function last ( other ) {
140
170
return this [ this . length - 1 ] = other ;
141
- }
171
+ } ,
172
+ enumerable : false ,
142
173
} ) ;
143
174
Object . defineProperty ( Array . prototype , 'tail' , {
144
175
get : function tail ( ) {
145
176
return this . slice ( 1 ) ;
146
- }
177
+ } ,
178
+ enumerable : false ,
147
179
} ) ;
148
180
Object . defineProperty ( Array . prototype , 'point' , {
149
181
get : function point ( ) {
150
182
return Point ( this [ 0 ] , this [ 1 ] ) ;
151
- }
183
+ } ,
184
+ enumerable : false ,
185
+ } ) ;
186
+ Object . defineProperty ( Array . prototype , 'head' , {
187
+ get : function first ( ) {
188
+ return this [ 0 ] ;
189
+ } ,
190
+ set : function first ( other ) {
191
+ return this [ 0 ] = other ;
192
+ } ,
193
+ enumerable : false ,
152
194
} ) ;
153
- Array . prototype . head = Array . prototype . first ;
154
195
155
- String . prototype . replaceAll = function ( search , replacement ) {
156
- return this . replace ( new RegExp ( search , 'g' ) , replacement ) ;
157
- } ;
196
+ Object . defineProperty ( String . prototype , 'replaceAll' , {
197
+ value : function ( search , replacement ) {
198
+ return this . replace ( new RegExp ( search , 'g' ) , replacement ) ;
199
+ } ,
200
+ enumerable : false ,
201
+ } ) ;
158
202
159
- HTMLElement . prototype . html = function ( s , ...exps ) {
160
- const contain = document . createElement ( 'del' ) ;
161
- contain . style . textDecoration = 'none' ;
162
- contain . innerHTML = String . raw ( s , ...exps ) ;
163
- this . appendChild ( contain ) ;
164
- } ;
203
+ Object . defineProperty ( HTMLElement . prototype , 'html' , {
204
+ value : function ( s , ...exps ) {
205
+ const contain = document . createElement ( 'del' ) ;
206
+ contain . style . textDecoration = 'none' ;
207
+ contain . innerHTML = String . raw ( s , ...exps ) ;
208
+ this . appendChild ( contain ) ;
209
+ } ,
210
+ enumerable : false ,
211
+ } ) ;
165
212
166
- HTMLElement . prototype . css = function ( properties ) {
167
- for ( const property in properties ) {
168
- if ( Object . prototype . hasOwnProperty . call ( properties , property ) ) {
169
- this . style [ property ] = properties [ property ] ;
213
+ Object . defineProperty ( HTMLElement . prototype , 'css' , {
214
+ value : function ( properties ) {
215
+ for ( const property in properties ) {
216
+ if ( Object . prototype . hasOwnProperty . call ( properties , property ) ) {
217
+ this . style [ property ] = properties [ property ] ;
218
+ }
170
219
}
171
- }
172
- } ;
220
+ } ,
221
+ enumerable : false ,
222
+ } ) ;
173
223
174
- Object . prototype . omap = function ( lambda ) {
175
- return Object . assign ( { } ,
176
- ...Object . keys ( this ) . map ( k =>
177
- ( { [ k ] : lambda ( this [ k ] ) } ) ) ) ;
178
- } ;
224
+ Object . defineProperty ( Object . prototype , 'omap' , {
225
+ value : function ( lambda ) {
226
+ return Object . assign ( { } ,
227
+ ...Object . keys ( this ) . map ( k => ( { [ k ] : lambda ( this [ k ] ) } ) ) ) ;
228
+ } ,
229
+ enumerable : false
230
+ } ) ;
179
231
180
232
Object . defineProperty ( HTMLElement . prototype , 'elem' , {
181
233
get : function elem ( ) {
182
234
return this ;
183
235
} ,
236
+ enumerable : false ,
184
237
} ) ;
185
238
}
186
239
0 commit comments