@@ -27,9 +27,12 @@ let already_patched = (typeof window !== 'undefined'
2727export const clone = obj => Object . assign ( Object . create ( Object . getPrototypeOf ( obj ) ) , obj ) ;
2828
2929if ( ! 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+ } ) ;
3336}
3437
3538export const type = element => (
@@ -72,115 +75,165 @@ if (!already_patched) {
7275 Math . HALF_PI = Math . PI * 0.5 ;
7376 Math . triangle = t => Math . abs ( ( ( t - 1 ) % 4 ) - 2 ) - 1 ;
7477
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+ } ) ;
8290
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+ } ) ;
103129 Object . defineProperty ( Array . prototype , 'x' , {
104130 get : function x ( ) {
105131 return this [ 0 ] ;
106132 } ,
107133 set : function x ( new_x ) {
108134 return this [ 0 ] = new_x ;
109- }
135+ } ,
136+ enumerable : false ,
110137 } ) ;
111138 Object . defineProperty ( Array . prototype , 'y' , {
112139 get : function y ( ) {
113140 return this [ 1 ] ;
114141 } ,
115142 set : function y ( new_y ) {
116143 return this [ 1 ] = new_y ;
117- }
144+ } ,
145+ enumerable : false ,
118146 } ) ;
119147 Object . defineProperty ( Array . prototype , 'z' , {
120148 get : function y ( ) {
121149 return this [ 2 ] ;
122150 } ,
123151 set : function z ( new_z ) {
124152 return this [ 2 ] = new_z ;
125- }
153+ } ,
154+ enumerable : false ,
126155 } ) ;
127156 Object . defineProperty ( Array . prototype , 'first' , {
128157 get : function first ( ) {
129158 return this [ 0 ] ;
130159 } ,
131160 set : function first ( other ) {
132161 return this [ 0 ] = other ;
133- }
162+ } ,
163+ enumerable : false ,
134164 } ) ;
135165 Object . defineProperty ( Array . prototype , 'last' , {
136166 get : function last ( ) {
137167 return this [ this . length - 1 ] ;
138168 } ,
139169 set : function last ( other ) {
140170 return this [ this . length - 1 ] = other ;
141- }
171+ } ,
172+ enumerable : false ,
142173 } ) ;
143174 Object . defineProperty ( Array . prototype , 'tail' , {
144175 get : function tail ( ) {
145176 return this . slice ( 1 ) ;
146- }
177+ } ,
178+ enumerable : false ,
147179 } ) ;
148180 Object . defineProperty ( Array . prototype , 'point' , {
149181 get : function point ( ) {
150182 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 ,
152194 } ) ;
153- Array . prototype . head = Array . prototype . first ;
154195
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+ } ) ;
158202
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+ } ) ;
165212
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+ }
170219 }
171- }
172- } ;
220+ } ,
221+ enumerable : false ,
222+ } ) ;
173223
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+ } ) ;
179231
180232 Object . defineProperty ( HTMLElement . prototype , 'elem' , {
181233 get : function elem ( ) {
182234 return this ;
183235 } ,
236+ enumerable : false ,
184237 } ) ;
185238}
186239
0 commit comments