senselogic-gson 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/building.js +35 -21
  2. package/package.json +1 -1
  3. package/test.js +5 -1
package/building.js CHANGED
@@ -19,25 +19,27 @@ export function getIndentationText(
19
19
  // ~~
20
20
 
21
21
  function getEscapedLine(
22
- text
22
+ line,
23
+ primedTextIsEscaped = true
23
24
  )
24
25
  {
25
- return (
26
- text
27
- .replaceAll( '\\', '\\\\' )
28
- .replaceAll( '\b', '\\b' )
29
- .replaceAll( '\f', '\\f' )
30
- .replaceAll( '\r', '' )
31
- .replaceAll( '\t', '\\t' )
32
- .replaceAll( /[-\u001F]/g, character => `\\u${ character.charCodeAt( 0 ).toString( 16 ).padStart( 4, '0' ) }` )
33
- .replaceAll( '‴', '\\u2034' )
34
- );
26
+ let escapedLine = JSON.stringify( line ).slice( 1, -1 ).replaceAll( '‴', '\\u2034' );
27
+
28
+ if ( primedTextIsEscaped )
29
+ {
30
+ return escapedLine.replaceAll( '', '\\u203C' ).replaceAll( '‗', '\\u2017' );
31
+ }
32
+ else
33
+ {
34
+ return escapedLine;
35
+ }
35
36
  }
36
37
 
37
38
  // ~~
38
39
 
39
40
  function getMultilineString(
40
41
  value,
42
+ primedTextIsEscaped = true,
41
43
  indentationText
42
44
  )
43
45
  {
@@ -84,7 +86,7 @@ function getMultilineString(
84
86
  lineSuffix = '';
85
87
  }
86
88
 
87
- let multilineStringLine = linePrefix + getEscapedLine( lineContent ) + lineSuffix;
89
+ let multilineStringLine = linePrefix + getEscapedLine( lineContent, primedTextIsEscaped ) + lineSuffix;
88
90
 
89
91
  if ( lineIndex === lineCount - 1 )
90
92
  {
@@ -108,6 +110,8 @@ function getMultilineString(
108
110
 
109
111
  function buildGsonString(
110
112
  value,
113
+ primedTextIsGenerated = true,
114
+ primedTextIsEscaped = true,
111
115
  context,
112
116
  level,
113
117
  lineSuffix = ''
@@ -115,17 +119,18 @@ function buildGsonString(
115
119
  {
116
120
  let indentationText = getIndentationText( level * context.levelSpaceCount );
117
121
 
118
- if ( value.startsWith( '‼' )
119
- || value.includes( '\n' ) )
122
+ if ( primedTextIsGenerated
123
+ && ( value.startsWith( '' )
124
+ || value.includes( '\n' ) ) )
120
125
  {
121
126
  if ( value.startsWith( '‼' ) )
122
127
  {
123
- let text = '‴' + getEscapedLine( value ) + '‴' + lineSuffix;
128
+ let text = '‴' + getEscapedLine( value, primedTextIsEscaped ) + '‴' + lineSuffix;
124
129
  context.lineArray.push( indentationText + text );
125
130
  }
126
131
  else
127
132
  {
128
- let multilineString = getMultilineString( value, indentationText );
133
+ let multilineString = getMultilineString( value, primedTextIsEscaped, indentationText );
129
134
  let lineArray = multilineString.split( '\n' );
130
135
  let lastIndex = lineArray.length - 1;
131
136
 
@@ -147,7 +152,8 @@ function buildGsonString(
147
152
  }
148
153
  else
149
154
  {
150
- let text = JSON.stringify( value ) + lineSuffix;
155
+ let text = '"' + getEscapedLine( value, primedTextIsEscaped ) + '"' + lineSuffix;
156
+
151
157
  context.lineArray.push( indentationText + text );
152
158
  }
153
159
  }
@@ -156,6 +162,8 @@ function buildGsonString(
156
162
 
157
163
  function buildGsonValue(
158
164
  value,
165
+ primedTextIsGenerated = true,
166
+ primedTextIsEscaped = true,
159
167
  context,
160
168
  level
161
169
  )
@@ -164,7 +172,7 @@ function buildGsonValue(
164
172
 
165
173
  if ( typeof value === 'string' )
166
174
  {
167
- buildGsonString( value, context, level, '' );
175
+ buildGsonString( value, primedTextIsGenerated, primedTextIsEscaped, context, level, '' );
168
176
  }
169
177
  else if ( Array.isArray( value ) )
170
178
  {
@@ -181,6 +189,8 @@ function buildGsonValue(
181
189
 
182
190
  buildGsonValue(
183
191
  element,
192
+ primedTextIsGenerated,
193
+ primedTextIsEscaped,
184
194
  context,
185
195
  level + 1
186
196
  );
@@ -215,6 +225,8 @@ function buildGsonValue(
215
225
 
216
226
  buildGsonValue(
217
227
  value[ key ],
228
+ primedTextIsGenerated,
229
+ primedTextIsEscaped,
218
230
  context,
219
231
  valueIndentLevel
220
232
  );
@@ -238,9 +250,9 @@ function buildGsonValue(
238
250
 
239
251
  export function buildGsonText(
240
252
  value,
241
- {
242
- indentationSpaceCount = 4
243
- } = {}
253
+ primedTextIsGenerated = true,
254
+ primedTextIsEscaped = true,
255
+ indentationSpaceCount = 4
244
256
  )
245
257
  {
246
258
  let context =
@@ -251,6 +263,8 @@ export function buildGsonText(
251
263
 
252
264
  buildGsonValue(
253
265
  value,
266
+ primedTextIsGenerated,
267
+ primedTextIsEscaped,
254
268
  context,
255
269
  0
256
270
  );
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "senselogic-gson",
3
3
  "description": "Granular Structured Object Notation.",
4
- "version": "0.1.4",
4
+ "version": "0.1.5",
5
5
  "author": "Eric Pelzer <ecstatic.coder@gmail.com>",
6
6
  "license": "LGPL-3.0-only",
7
7
  "repository": {
package/test.js CHANGED
@@ -19,9 +19,13 @@ jsonText = readGsonFileText( '../../../DATA/test.gson', false );
19
19
  console.log( jsonText );
20
20
  writeFileText( 'OUT/unprocessed_test.json', jsonText );
21
21
 
22
+ gsonText = buildGsonText( jsonValue, false );
23
+ console.log( gsonText );
24
+ writeFileText( 'OUT/unprocessed_test.gson', gsonText, 4 );
25
+
22
26
  jsonValue = readGsonFileValue( '../../../DATA/test.gson', false );
23
27
  console.log( JSON.stringify( jsonValue ) );
24
28
 
25
- gsonText = buildGsonText( jsonValue );
29
+ gsonText = buildGsonText( jsonValue, true, false );
26
30
  console.log( gsonText );
27
31
  writeFileText( 'OUT/unprocessed_test.gson', gsonText, 4 );