11"use strict" ;
22Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
3- exports . isPlainTextDiagram = exports . readPlainTextString = exports . isPlainTextString = exports . writePlainTextMatrix = exports . writePlainTextFromCoordinates = exports . readPlainTextDiagramToRowColCoordinates = exports . readPlainTextDiagramToMatrix = exports . readPlainTextDiagramToXYCoordinates = void 0 ;
4- const util_1 = require ( "core/util" ) ;
5- const util_2 = require ( "core/util" ) ;
6- const strRead_1 = require ( "core/strRead" ) ;
3+ exports . isPlainTextDiagram = exports . readPlainTextDiagramToMatrix = exports . readPlainTextDiagramToXY = exports . readPlainTextString = exports . isPlainTextString = exports . writePlainTextMatrix = exports . writePlainTextFromCoordinates = void 0 ;
4+ const util_1 = require ( "../../core/util" ) ;
5+ const strRead_1 = require ( "../../core/strRead" ) ;
76const VALID_DEAD_CELL_CHARACTERS = [ "." ] ;
87const VALID_LIVE_CELL_CHARACTERS = [ "O" , "*" ] ;
8+ // --------------------------------------------------------------
9+ // --------------------------------------------------------------
10+ // --------------------------------------------------------------
11+ // ------------------------- WRITING ----------------------------
12+ // --------------------------------------------------------------
13+ // --------------------------------------------------------------
14+ // --------------------------------------------------------------
915function writePlainTextMetadata ( byteArray , name , description ) {
1016 const newArray = [ ...byteArray ] ;
1117 ( 0 , util_1 . pushASCIIBytes ) ( newArray , "!Name: " + name + "\n" ) ;
1218 if ( description . length > 0 ) {
1319 if ( typeof ( description ) === "string" ) {
14- const lines = description . split ( "\n" ) ;
20+ const lines = description . replace ( "\r" , "" ) . split ( "\n" ) ;
1521 lines . forEach ( line => ( 0 , util_1 . pushASCIIBytes ) ( newArray , `!${ line } \n` ) ) ;
1622 }
1723 else {
@@ -22,52 +28,13 @@ function writePlainTextMetadata(byteArray, name, description) {
2228 ( 0 , util_1 . pushASCIIBytes ) ( newArray , "!\n" ) ;
2329 return newArray ;
2430}
25- function readPlainTextDiagramToXYCoordinates ( str ) {
26- if ( isPlainTextDiagram ( str ) ) {
27- const lines = str . split ( "\n" ) ;
28- return lines . flatMap ( ( line , row ) => line . split ( "" ) . map ( ( char , col ) => VALID_LIVE_CELL_CHARACTERS . some ( valid => valid === char ) ? [ col , - row ] : [ ] ) . filter ( point => point . length > 0 ) ) ;
29- }
30- throw new Error ( "error" ) ;
31- }
32- exports . readPlainTextDiagramToXYCoordinates = readPlainTextDiagramToXYCoordinates ;
33- function readPlainTextDiagramToMatrix ( str ) {
34- if ( isPlainTextDiagram ( str ) ) {
35- const lines = ( 0 , util_1 . trimTrailing ) ( str , "\n" ) . split ( "\n" ) ;
36- const width = Math . max ( ...lines . map ( line => line . length ) ) ;
37- return lines . map ( line => {
38- const newLine = new Array ( width ) ;
39- for ( let i = 0 ; i < width ; i ++ ) {
40- if ( i >= line . length ) {
41- newLine [ i ] = 0 ;
42- }
43- else if ( VALID_LIVE_CELL_CHARACTERS . some ( ch => ch === line [ i ] ) ) {
44- newLine [ i ] = 1 ;
45- }
46- else if ( VALID_DEAD_CELL_CHARACTERS . some ( ch => ch === line [ i ] ) ) {
47- newLine [ i ] = 0 ;
48- }
49- else if ( line [ i ] !== " " ) {
50- throw new Error ( ) ;
51- }
52- }
53- return newLine ;
54- } ) ;
55- }
56- throw new Error ( "" ) ;
57- }
58- exports . readPlainTextDiagramToMatrix = readPlainTextDiagramToMatrix ;
59- function readPlainTextDiagramToRowColCoordinates ( str ) {
60- const coordinates = readPlainTextDiagramToXYCoordinates ( str ) ;
61- return coordinates . map ( coord => ( [ coord [ 0 ] , - coord [ 1 ] ] ) ) ;
62- }
63- exports . readPlainTextDiagramToRowColCoordinates = readPlainTextDiagramToRowColCoordinates ;
6431function writePlainTextFromCoordinates ( positions , name , description ) {
6532 for ( let i = 0 ; i < positions . length ; i ++ ) {
6633 if ( ! Number . isInteger ( positions [ i ] [ 0 ] ) || ! Number . isInteger ( positions [ i ] [ 1 ] ) ) {
6734 throw new Error ( `Attempted to write plain text with Invalid Coordinates: Coordinates must all be integers (Error at coordinate #${ i } x: ${ positions [ i ] [ 0 ] } y: ${ positions [ i ] [ 1 ] } ` ) ;
6835 }
6936 }
70- return writePlainTextMatrix ( ( 0 , util_2 . numberPairArrayToMatrix ) ( positions ) , name , description ) ;
37+ return writePlainTextMatrix ( ( 0 , util_1 . numberPairArrayToMatrix ) ( positions ) , name , description ) ;
7138}
7239exports . writePlainTextFromCoordinates = writePlainTextFromCoordinates ;
7340function writePlainTextMatrix ( data , name , description ) {
@@ -88,6 +55,13 @@ function writePlainTextMatrix(data, name, description) {
8855 return ( 0 , util_1 . byteArrayAsASCII ) ( byteArray ) ;
8956}
9057exports . writePlainTextMatrix = writePlainTextMatrix ;
58+ // --------------------------------------------------------------
59+ // --------------------------------------------------------------
60+ // --------------------------------------------------------------
61+ // ------------------------- READING ----------------------------
62+ // --------------------------------------------------------------
63+ // --------------------------------------------------------------
64+ // --------------------------------------------------------------
9165function isPlainTextString ( str ) {
9266 try {
9367 readPlainTextString ( str ) ;
@@ -102,15 +76,15 @@ function readPlainTextString(str) {
10276 if ( str . length === 0 ) {
10377 throw new Error ( "" ) ;
10478 }
105- const lines = str . split ( "\n" ) . map ( line => line . trim ( ) ) ;
79+ const lines = str . replace ( "\r" , "" ) . split ( "\n" ) . map ( line => line . trim ( ) ) ;
10680 if ( lines . length === 0 ) {
10781 throw new Error ( "" ) ;
10882 }
10983 const contents = {
11084 name : "" ,
11185 description : [ ] ,
11286 matrix : [ ] ,
113- cellCoordinates : [ ]
87+ liveCoordinates : [ ]
11488 } ;
11589 //reads header line
11690 if ( ( 0 , strRead_1 . isNextChar ) ( lines [ 0 ] , "!" ) ) {
@@ -129,7 +103,7 @@ function readPlainTextString(str) {
129103 name : "" ,
130104 description : [ ] ,
131105 matrix : readPlainTextDiagramToMatrix ( trimmedStr ) ,
132- cellCoordinates : readPlainTextDiagramToXYCoordinates ( trimmedStr )
106+ liveCoordinates : readPlainTextDiagramToXY ( trimmedStr )
133107 } ;
134108 }
135109 throw new Error ( "" ) ;
@@ -145,7 +119,7 @@ function readPlainTextString(str) {
145119 }
146120 const diagramLines = lines . slice ( currentLine ) . join ( "\n" ) ;
147121 if ( isPlainTextDiagram ( diagramLines ) ) {
148- contents . cellCoordinates = readPlainTextDiagramToXYCoordinates ( diagramLines ) ;
122+ contents . liveCoordinates = readPlainTextDiagramToXY ( diagramLines ) ;
149123 contents . matrix = readPlainTextDiagramToMatrix ( diagramLines ) ;
150124 }
151125 else {
@@ -154,6 +128,40 @@ function readPlainTextString(str) {
154128 return contents ;
155129}
156130exports . readPlainTextString = readPlainTextString ;
131+ function readPlainTextDiagramToXY ( str ) {
132+ if ( isPlainTextDiagram ( str ) ) {
133+ const lines = str . split ( "\n" ) ;
134+ return lines . flatMap ( ( line , row ) => line . split ( "" ) . map ( ( char , col ) => VALID_LIVE_CELL_CHARACTERS . some ( valid => valid === char ) ? [ col , - row ] : [ ] ) . filter ( point => point . length > 0 ) ) ;
135+ }
136+ throw new Error ( "error" ) ;
137+ }
138+ exports . readPlainTextDiagramToXY = readPlainTextDiagramToXY ;
139+ function readPlainTextDiagramToMatrix ( str ) {
140+ if ( isPlainTextDiagram ( str ) ) {
141+ const lines = ( 0 , util_1 . trimTrailing ) ( str , "\n" ) . split ( "\n" ) ;
142+ const width = Math . max ( ...lines . map ( line => line . length ) ) ;
143+ return lines . map ( line => {
144+ const newLine = new Array ( width ) ;
145+ for ( let i = 0 ; i < width ; i ++ ) {
146+ if ( i >= line . length ) {
147+ newLine [ i ] = 0 ;
148+ }
149+ else if ( VALID_LIVE_CELL_CHARACTERS . some ( ch => ch === line [ i ] ) ) {
150+ newLine [ i ] = 1 ;
151+ }
152+ else if ( VALID_DEAD_CELL_CHARACTERS . some ( ch => ch === line [ i ] ) ) {
153+ newLine [ i ] = 0 ;
154+ }
155+ else if ( line [ i ] !== " " ) {
156+ throw new Error ( ) ;
157+ }
158+ }
159+ return newLine ;
160+ } ) ;
161+ }
162+ throw new Error ( "" ) ;
163+ }
164+ exports . readPlainTextDiagramToMatrix = readPlainTextDiagramToMatrix ;
157165function isPlainTextDiagram ( line ) {
158166 return line . split ( "" ) . every ( char => VALID_DEAD_CELL_CHARACTERS . some ( ch => ch === char ) || VALID_LIVE_CELL_CHARACTERS . some ( ch => ch === char ) || char === " " || char === "\n" ) ;
159167}
0 commit comments