@@ -653,19 +653,19 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
653653
654654 // Map raw to external fragment
655655 if ( ! raw ) {
656- // It first retrieves the @id property from the first element
656+ // It first retrieves the @id property from the first element
657657 // of referenceIdData array and assigns it to referenceId.
658658 const referenceId = referenceIdData [ 0 ] [ '@id' ]
659659
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
662662 // the result to rootReferences.
663663 const rootReferences = this . _computeReferences ( this . amf )
664664
665665 // 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.
667667 // 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
669669 // encoded external fragments, which is assigned to encodesOfExternalFragments.
670670 const encodesOfExternalFragments = rootReferences . map ( ( item ) => {
671671 const shapeFragment = this . _computeReferences ( item )
@@ -675,16 +675,16 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
675675 return this . _computeEncodes ( externalFragmentExample )
676676 } )
677677
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
680680 // it to exmapleExternalFragmentByReferenceId.
681681 const exmapleExternalFragmentByReferenceId = encodesOfExternalFragments . find ( externalFrament => (
682682 externalFrament [ '@id' ] === referenceId
683683 ) )
684684
685685 const rawKey = this . _getAmfKey ( this . ns . aml . vocabularies . document . raw )
686686 // Finally, it calls the _getValue method with
687- // exmapleExternalFragmentByReferenceId and rawKey
687+ // exmapleExternalFragmentByReferenceId and rawKey
688688 // as arguments and assigns the result to raw.
689689 raw = this . _getValue ( exmapleExternalFragmentByReferenceId , rawKey )
690690 }
@@ -801,7 +801,7 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
801801 }
802802 } ) ;
803803 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
805805 // if the parse process fails then use parts to build example value
806806 if ( result . raw ) {
807807 try {
@@ -847,27 +847,44 @@ export class ExampleGenerator extends AmfHelperMixin(Object) {
847847 return undefined ;
848848 }
849849
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+
850878 /**
851- * @param {String } raw
879+ * @param {String } raw
852880 * @returns string JSON formatted
853881 */
854882 computeRaw ( raw ) {
855883 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 ) ;
871888 }
872889
873890
0 commit comments