@@ -22,11 +22,11 @@ export function replaceIllegalFileNameCharactersInString(string: string): string
2222 return string . replace ( / [ \\ , # % & { } / * < > $ " @ . ? ] * / g, '' ) . replace ( / : + / g, ' -' ) ;
2323}
2424
25- export function replaceTags ( template : string , mediaTypeModel : MediaTypeModel ) : string {
26- return template . replace ( new RegExp ( '{{.*?}}' , 'g' ) , ( match : string ) => replaceTag ( match , mediaTypeModel ) ) ;
25+ export function replaceTags ( template : string , mediaTypeModel : MediaTypeModel , ignoreUndefined : boolean = false ) : string {
26+ return template . replace ( new RegExp ( '{{.*?}}' , 'g' ) , ( match : string ) => replaceTag ( match , mediaTypeModel , ignoreUndefined ) ) ;
2727}
2828
29- function replaceTag ( match : string , mediaTypeModel : MediaTypeModel ) : string {
29+ function replaceTag ( match : string , mediaTypeModel : MediaTypeModel , ignoreUndefined : boolean ) : string {
3030 let tag = match ;
3131 tag = tag . substring ( 2 ) ;
3232 tag = tag . substring ( 0 , tag . length - 2 ) ;
@@ -39,7 +39,7 @@ function replaceTag(match: string, mediaTypeModel: MediaTypeModel): string {
3939 const obj = traverseMetaData ( path , mediaTypeModel ) ;
4040
4141 if ( obj === undefined ) {
42- return '{{ INVALID TEMPLATE TAG - object undefined }}' ;
42+ return ignoreUndefined ? '' : '{{ INVALID TEMPLATE TAG - object undefined }}' ;
4343 }
4444
4545 return obj ;
@@ -51,7 +51,7 @@ function replaceTag(match: string, mediaTypeModel: MediaTypeModel): string {
5151 const obj = traverseMetaData ( path , mediaTypeModel ) ;
5252
5353 if ( obj === undefined ) {
54- return '{{ INVALID TEMPLATE TAG - object undefined }}' ;
54+ return ignoreUndefined ? '' : '{{ INVALID TEMPLATE TAG - object undefined }}' ;
5555 }
5656
5757 if ( operator === 'LIST' ) {
@@ -64,6 +64,16 @@ function replaceTag(match: string, mediaTypeModel: MediaTypeModel): string {
6464 return '{{ INVALID TEMPLATE TAG - operator ENUM is only applicable on an array }}' ;
6565 }
6666 return obj . join ( ', ' ) ;
67+ } else if ( operator === 'FIRST' ) {
68+ if ( ! Array . isArray ( obj ) ) {
69+ return '{{ INVALID TEMPLATE TAG - operator FIRST is only applicable on an array }}' ;
70+ }
71+ return obj [ 0 ] ;
72+ } else if ( operator === 'LAST' ) {
73+ if ( ! Array . isArray ( obj ) ) {
74+ return '{{ INVALID TEMPLATE TAG - operator LAST is only applicable on an array }}' ;
75+ }
76+ return obj [ obj . length - 1 ] ;
6777 }
6878
6979 return `{{ INVALID TEMPLATE TAG - unknown operator ${ operator } }}` ;
0 commit comments