1- window . ls . container . set ( 'router' , function ( window ) {
1+ window . ls . container . set ( 'router' , function ( window ) {
22
33 /**
44 * Takes a valid URL and return a JSON based object with all params.
@@ -13,42 +13,42 @@ window.ls.container.set('router', function(window) {
1313 let getJsonFromUrl = function ( URL ) {
1414 let query ;
1515
16- if ( URL ) {
16+ if ( URL ) {
1717 let pos = location . search . indexOf ( '?' ) ;
18- if ( pos === - 1 ) return [ ] ;
19- query = location . search . substr ( pos + 1 ) ;
18+ if ( pos === - 1 ) return [ ] ;
19+ query = location . search . substr ( pos + 1 ) ;
2020 } else {
2121 query = location . search . substr ( 1 ) ;
2222 }
2323
24- let result = { } ;
24+ let result = Object . create ( null ) ;
2525
26- query . split ( '&' ) . forEach ( function ( part ) {
27- if ( ! part ) {
26+ query . split ( '&' ) . forEach ( function ( part ) {
27+ if ( ! part ) {
2828 return ;
2929 }
3030
3131 part = part . split ( '+' ) . join ( ' ' ) ; // replace every + with space, regexp-free version
3232
33- let eq = part . indexOf ( '=' ) ;
34- let key = eq > - 1 ? part . substr ( 0 , eq ) : part ;
35- let val = eq > - 1 ? decodeURIComponent ( part . substr ( eq + 1 ) ) : '' ;
36- let from = key . indexOf ( '[' ) ;
33+ let eq = part . indexOf ( '=' ) ;
34+ let key = eq > - 1 ? part . substr ( 0 , eq ) : part ;
35+ let val = eq > - 1 ? decodeURIComponent ( part . substr ( eq + 1 ) ) : '' ;
36+ let from = key . indexOf ( '[' ) ;
3737
38- if ( from === - 1 ) {
38+ if ( from === - 1 ) {
3939 result [ decodeURIComponent ( key ) ] = val ;
4040 }
4141 else {
4242 let to = key . indexOf ( ']' ) ;
43- let index = decodeURIComponent ( key . substring ( from + 1 , to ) ) ;
43+ let index = decodeURIComponent ( key . substring ( from + 1 , to ) ) ;
4444
45- key = decodeURIComponent ( key . substring ( 0 , from ) ) ;
45+ key = decodeURIComponent ( key . substring ( 0 , from ) ) ;
4646
47- if ( ! result [ key ] ) {
47+ if ( ! result [ key ] ) {
4848 result [ key ] = [ ] ;
4949 }
5050
51- if ( ! index ) {
51+ if ( ! index ) {
5252 result [ key ] . push ( val ) ;
5353 }
5454 else {
@@ -60,11 +60,11 @@ window.ls.container.set('router', function(window) {
6060 return result ;
6161 } ;
6262
63- let states = [ ] ;
64- let params = getJsonFromUrl ( window . location . search ) ;
65- let hash = window . location . hash ;
66- let current = null ;
67- let previous = null ;
63+ let states = [ ] ;
64+ let params = getJsonFromUrl ( window . location . search ) ;
65+ let hash = window . location . hash ;
66+ let current = null ;
67+ let previous = null ;
6868
6969 /**
7070 * Get previous state scope
@@ -78,7 +78,7 @@ window.ls.container.set('router', function(window) {
7878 *
7979 * @returns {* }
8080 */
81- let getCurrent = ( ) => current ;
81+ let getCurrent = ( ) => current ;
8282
8383 /**
8484 * Set previous state scope
@@ -111,7 +111,7 @@ window.ls.container.set('router', function(window) {
111111 * @param value
112112 * @returns {setParam }
113113 */
114- let setParam = function ( key , value ) {
114+ let setParam = function ( key , value ) {
115115 params [ key ] = value ;
116116 return this ;
117117 } ;
@@ -161,20 +161,20 @@ window.ls.container.set('router', function(window) {
161161 * @param view object
162162 * @returns this
163163 */
164- let add = function ( path , view ) {
164+ let add = function ( path , view ) {
165165
166166 /**
167167 * Validation
168168 */
169- if ( typeof path !== 'string' ) {
169+ if ( typeof path !== 'string' ) {
170170 throw new Error ( 'path must be of type string' ) ;
171171 }
172172
173- if ( typeof view !== 'object' ) {
173+ if ( typeof view !== 'object' ) {
174174 throw new Error ( 'view must be of type object' ) ;
175175 }
176176
177- states [ states . length ++ ] = { /* string */ path : path , /* object */ view : view } ;
177+ states [ states . length ++ ] = { /* string */ path : path , /* object */ view : view } ;
178178
179179 return this ;
180180 } ;
@@ -196,32 +196,32 @@ window.ls.container.set('router', function(window) {
196196 * @param location object
197197 * @return value object|null
198198 */
199- let match = function ( location ) {
199+ let match = function ( location ) {
200200 let url = location . pathname ;
201201
202- if ( url . endsWith ( '/' ) ) {
202+ if ( url . endsWith ( '/' ) ) {
203203 url = url . slice ( 0 , - 1 ) ;
204204 }
205205
206- states . sort ( function ( a , b ) { return b . path . length - a . path . length ; } ) ; // order by length
206+ states . sort ( function ( a , b ) { return b . path . length - a . path . length ; } ) ; // order by length
207207
208- states . sort ( function ( a , b ) {
208+ states . sort ( function ( a , b ) {
209209 let n = b . path . split ( '/' ) . length - a . path . split ( '/' ) . length ;
210210
211- if ( n !== 0 ) {
211+ if ( n !== 0 ) {
212212 return n ;
213213 }
214214
215215 return b . path . length - a . path . length ;
216216 } ) ; // order by number of paths parts
217217
218218 for ( let i = 0 ; i < states . length ; i ++ ) {
219- let value = states [ i ] ;
220- value . path = ( value . path . substring ( 0 , 1 ) !== '/' ) ? location . pathname + value . path : value . path ; // Support for relative paths
221- let match = new RegExp ( "^" + value . path . replace ( / : [ ^ \s / ] + / g, '([\\w-]+)' ) + "$" ) ;
222- let found = url . match ( match ) ;
219+ let value = states [ i ] ;
220+ value . path = ( value . path . substring ( 0 , 1 ) !== '/' ) ? location . pathname + value . path : value . path ; // Support for relative paths
221+ let match = new RegExp ( "^" + value . path . replace ( / : [ ^ \s / ] + / g, '([\\w-]+)' ) + "$" ) ;
222+ let found = url . match ( match ) ;
223223
224- if ( found ) {
224+ if ( found ) {
225225 previous = current ;
226226 current = value ;
227227
@@ -238,17 +238,17 @@ window.ls.container.set('router', function(window) {
238238 * @param URL string
239239 * @param replace bool
240240 */
241- let change = function ( URL , replace ) {
241+ let change = function ( URL , replace ) {
242242
243- if ( ! replace ) {
243+ if ( ! replace ) {
244244 window . history . pushState ( { } , '' , URL ) ;
245245 }
246246 else {
247247 window . history . replaceState ( { } , '' , URL ) ;
248248 }
249-
249+
250250 window . dispatchEvent ( new PopStateEvent ( 'popstate' , { } ) ) ;
251-
251+
252252 return this ;
253253 } ;
254254
0 commit comments