@@ -653,19 +653,19 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
653
653
654
654
// Map raw to external fragment
655
655
if ( ! raw ) {
656
- // It first retrieves the @id property from the first element
656
+ // It first retrieves the @id property from the first element
657
657
// of referenceIdData array and assigns it to referenceId.
658
658
const referenceId = referenceIdData [ 0 ] [ '@id' ]
659
659
660
- // It calls the _computeReferences method with this.amf as an
661
- // argument to get the root references and assigns
660
+ // It calls the _computeReferences method with this.amf as an
661
+ // argument to get the root references and assigns
662
662
// the result to rootReferences.
663
663
const rootReferences = this . _computeReferences ( this . amf )
664
664
665
665
// It maps over each item in rootReferences,
666
- // and for each item, it computes references twice in a nested manner.
666
+ // and for each item, it computes references twice in a nested manner.
667
667
// It then gets the second element from externalFragments and computes its encodes.
668
- // The result of this map operation is an array of
668
+ // The result of this map operation is an array of
669
669
// encoded external fragments, which is assigned to encodesOfExternalFragments.
670
670
const encodesOfExternalFragments = rootReferences . map ( ( item ) => {
671
671
const shapeFragment = this . _computeReferences ( item )
@@ -675,16 +675,16 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
675
675
return this . _computeEncodes ( externalFragmentExample )
676
676
} )
677
677
678
- // It finds an element in encodesOfExternalFragments where
679
- // the @id property matches referenceId and assigns
678
+ // It finds an element in encodesOfExternalFragments where
679
+ // the @id property matches referenceId and assigns
680
680
// it to exmapleExternalFragmentByReferenceId.
681
681
const exmapleExternalFragmentByReferenceId = encodesOfExternalFragments . find ( externalFrament => (
682
682
externalFrament [ '@id' ] === referenceId
683
683
) )
684
684
685
685
const rawKey = this . _getAmfKey ( this . ns . aml . vocabularies . document . raw )
686
686
// Finally, it calls the _getValue method with
687
- // exmapleExternalFragmentByReferenceId and rawKey
687
+ // exmapleExternalFragmentByReferenceId and rawKey
688
688
// as arguments and assigns the result to raw.
689
689
raw = this . _getValue ( exmapleExternalFragmentByReferenceId , rawKey )
690
690
}
@@ -801,7 +801,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
801
801
}
802
802
} ) ;
803
803
if ( isJson ) {
804
- // if raw (original example) exists try to parse it to JSON
804
+ // if raw (original example) exists try to parse it to JSON
805
805
// if the parse process fails then use parts to build example value
806
806
if ( result . raw ) {
807
807
try {
@@ -847,27 +847,44 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
847
847
return undefined ;
848
848
}
849
849
850
+ parseToJSON ( arr ) {
851
+ return arr
852
+ . filter ( item => item . trim ( ) !== "" ) // Remove empty strings
853
+ . map ( item => {
854
+ if ( item . startsWith ( "- " ) ) {
855
+ // Handle case where item is a list of numbers
856
+ return item . split ( "- " )
857
+ . filter ( subItem => subItem . trim ( ) !== "" )
858
+ . map ( subItem => Number ( subItem . trim ( ) ) ) ;
859
+ }
860
+ // Handle case where item is a key-value pair
861
+ const entries = item . trim ( ) . split ( "\n" ) . map ( line => {
862
+ const index = line . indexOf ( ":" ) ;
863
+ const key = line . slice ( 0 , index ) . trim ( ) ;
864
+ const value = line . slice ( index + 1 ) . trim ( ) ;
865
+ if ( value . startsWith ( "\"" ) && value . endsWith ( "\"" ) ) {
866
+ return [ key , value . slice ( 1 , - 1 ) ] ; // Preserve leading zeros
867
+ }
868
+ if ( ! Number . isNaN ( Number ( value ) ) ) {
869
+ return [ key , Number ( value ) ] ;
870
+ }
871
+ return [ key , value ] ;
872
+ } ) ;
873
+ return Object . fromEntries ( entries ) ;
874
+
875
+ } ) ;
876
+ } ;
877
+
850
878
/**
851
- * @param {String } raw
879
+ * @param {String } raw
852
880
* @returns string JSON formatted
853
881
*/
854
882
computeRaw ( raw ) {
855
883
const accountEntries = raw . split ( '-\n' ) ;
856
- const accounts = [ ] ;
857
- for ( const entry of accountEntries ) {
858
- if ( entry !== '' ) {
859
- const lines = entry . split ( '\n' ) ;
860
- const account = { } ;
861
- for ( const line of lines ) {
862
- if ( line !== '' ) {
863
- const [ key , value ] = line . split ( ': ' ) ;
864
- account [ key . trim ( ) ] = Number ( value ) ? Number ( value ) : value . trim ( )
865
- }
866
- }
867
- accounts . push ( account ) ;
868
- }
869
- }
870
- return JSON . stringify ( accounts , null , 2 ) ;
884
+ const parsed = this . parseToJSON ( accountEntries )
885
+ // Ensure the parsed result is always an array
886
+ const result = Array . isArray ( parsed [ 0 ] ) ? parsed . flat ( ) : parsed ;
887
+ return JSON . stringify ( result , null , 2 ) ;
871
888
}
872
889
873
890
0 commit comments