11import { isInjectable } from "../../shared/predicates.ts" ;
22import {
3- assign ,
43 hasOwn ,
54 isArray ,
65 isDefined ,
@@ -13,6 +12,7 @@ import { ParamType } from "./param-type.ts";
1312import type {
1413 ParamDeclaration ,
1514 ParamDefaultValueFactory ,
15+ ParamDefaultValueProvider ,
1616 RawParams ,
1717 Replace ,
1818} from "./interface.ts" ;
@@ -62,11 +62,13 @@ function getParamDeclaration(
6262) : ParamDeclaration {
6363 const { dynamic } = state ;
6464
65- const defaultConfig = isDefined ( dynamic ) ? { dynamic } : { } ;
66-
6765 const paramConfig = unwrapShorthand ( state ?. params ?. [ paramName ] ) ;
6866
69- return assign ( defaultConfig , paramConfig ) ;
67+ if ( isDefined ( dynamic ) && ! hasOwn ( paramConfig , "dynamic" ) ) {
68+ paramConfig . dynamic = dynamic ;
69+ }
70+
71+ return paramConfig ;
7072}
7173
7274/**
@@ -88,7 +90,9 @@ function unwrapShorthand(cfg: unknown): ParamDeclaration {
8890 ? paramConfig . value
8991 : getStaticDefaultValue ;
9092
91- return assign ( paramConfig , { _fn } ) ;
93+ paramConfig . _fn = _fn as ParamDefaultValueProvider ;
94+
95+ return paramConfig ;
9296}
9397
9498/**
@@ -172,32 +176,39 @@ function getReplace(
172176 isOptional : boolean ,
173177 squash : string | boolean ,
174178) : Replace [ ] {
175- const defaultPolicy = [
176- { from : "" , to : isOptional || arrayMode ? undefined : "" } ,
177- { from : null , to : isOptional || arrayMode ? undefined : "" } ,
178- ] as Replace [ ] ;
179-
180179 const replace = isArray ( config . replace ) ? config . replace : [ ] ;
181180
182- if ( isString ( squash ) ) replace . push ( { from : squash , to : undefined } ) ;
181+ const result : Replace [ ] = [ ] ;
183182
184- const configuredKeys : Array < string | null > = [ ] ;
183+ let hasEmptyReplace = false ;
185184
186- replace . forEach ( ( item ) => {
187- configuredKeys . push ( item . from ) ;
188- } ) ;
185+ let hasNullReplace = false ;
189186
190- const result : Replace [ ] = [ ] ;
187+ for ( let i = 0 ; i < replace . length ; i ++ ) {
188+ const item = replace [ i ] ;
191189
192- defaultPolicy . forEach ( ( item ) => {
193- if ( configuredKeys . indexOf ( item . from ) === - 1 ) {
194- result . push ( item ) ;
190+ if ( item . from === "" ) {
191+ hasEmptyReplace = true ;
192+ } else if ( item . from === null ) {
193+ hasNullReplace = true ;
195194 }
196- } ) ;
195+ }
196+
197+ const defaultReplacement = isOptional || arrayMode ? undefined : "" ;
198+
199+ if ( ! hasEmptyReplace ) result . push ( { from : "" , to : defaultReplacement } ) ;
200+
201+ if ( ! hasNullReplace ) {
202+ result . push ( { from : null as unknown as string , to : defaultReplacement } ) ;
203+ }
204+
205+ for ( let i = 0 ; i < replace . length ; i ++ ) {
206+ const item = replace [ i ] ;
197207
198- replace . forEach ( ( item ) => {
199208 result . push ( item ) ;
200- } ) ;
209+ }
210+
211+ if ( isString ( squash ) ) result . push ( { from : squash , to : undefined } ) ;
201212
202213 return result ;
203214}
@@ -207,13 +218,11 @@ function getArrayMode(
207218 location : DefTypeValue ,
208219 config : ParamDeclaration ,
209220) : boolean | "auto" {
210- const arrayDefaults = {
211- array : location === DefType . _SEARCH ? "auto" : false ,
212- } ;
221+ if ( location !== DefType . _SEARCH ) return false ;
213222
214- const arrayParamNomenclature = id . match ( / \[ \] $ / ) ? { array : true } : { } ;
223+ if ( isDefined ( config . array ) ) return config . array ;
215224
216- return assign ( arrayDefaults , arrayParamNomenclature , config ) . array ;
225+ return id . endsWith ( "[]" ) ? true : "auto" ;
217226}
218227
219228export class Param {
@@ -228,7 +237,6 @@ export class Param {
228237 inherit : boolean ;
229238 array : boolean | "auto" ;
230239 config : ParamDeclaration ;
231- matchingKeys : RawParams | undefined ;
232240 /** @internal */
233241 _defaultValueCache ?: { defaultValue : unknown } ;
234242 /** @internal */
@@ -256,9 +264,7 @@ export class Param {
256264 type = getType ( config , type , location , id , urlConfig . _paramTypes ) ;
257265 const arrayMode = getArrayMode ( id , location , config ) ;
258266
259- type = arrayMode
260- ? type && type . $asArray ( arrayMode , location === DefType . _SEARCH )
261- : type ;
267+ type = arrayMode ? type && type . $asArray ( arrayMode ) : type ;
262268 const isOptional =
263269 config . value !== undefined || location === DefType . _SEARCH ;
264270
@@ -289,7 +295,6 @@ export class Param {
289295 this . inherit = inherit ;
290296 this . array = arrayMode ;
291297 this . config = config ;
292- this . matchingKeys = undefined ;
293298 this . _runtime = runtime ;
294299 }
295300
@@ -306,7 +311,14 @@ export class Param {
306311 * @param {undefined } [value]
307312 */
308313 value ( value ?: unknown ) : unknown {
309- value = this . _replaceSpecialValues ( value ) ;
314+ for ( let i = 0 ; i < this . replace . length ; i ++ ) {
315+ const tuple = this . replace [ i ] ;
316+
317+ if ( tuple . from === value ) {
318+ value = tuple . to ;
319+ break ;
320+ }
321+ }
310322
311323 return isUndefined ( value )
312324 ? this . _getDefaultValue ( )
@@ -346,21 +358,6 @@ export class Param {
346358 return defaultValue ;
347359 }
348360
349- /** @internal */
350- _replaceSpecialValues ( value : unknown ) : unknown {
351- for ( let i = 0 ; i < this . replace . length ; i ++ ) {
352- const tuple = this . replace [ i ] ;
353-
354- if ( tuple . from === value ) return tuple . to ;
355- }
356-
357- return value ;
358- }
359-
360- isSearch ( ) : boolean {
361- return this . location === DefType . _SEARCH ;
362- }
363-
364361 /**
365362 * @param {null } value
366363 */
@@ -433,7 +430,15 @@ export class Param {
433430 values1 : RawParams = { } ,
434431 values2 : RawParams = { } ,
435432 ) : boolean {
436- return Param . changed ( params , values1 , values2 ) . length === 0 ;
433+ for ( let i = 0 ; i < params . length ; i ++ ) {
434+ const param = params [ i ] ;
435+
436+ if ( ! param . type . equals ( values1 [ param . id ] , values2 [ param . id ] ) ) {
437+ return false ;
438+ }
439+ }
440+
441+ return true ;
437442 }
438443
439444 /**
0 commit comments