senselogic-gson 0.1.1 → 0.1.2

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 (2) hide show
  1. package/building.js +56 -30
  2. package/package.json +1 -1
package/building.js CHANGED
@@ -1,23 +1,50 @@
1
+ // -- VARIABLES
2
+
3
+ let cachedIndentationText = ' ';
4
+
1
5
  // -- FUNCTIONS
2
6
 
3
- function getEscapedLineContent(
4
- text
7
+ function getIndentationText(
8
+ indentationSpaceCount
5
9
  )
6
10
  {
7
- return text.replaceAll( '\r', '' ).replaceAll( '\\', '\\\\' ).replaceAll( '‴', '\\u2034' );
11
+ while ( cachedIndentationText.length < indentationSpaceCount )
12
+ {
13
+ cachedIndentationText += ' ';
14
+ }
15
+
16
+ return cachedIndentationText.slice( 0, indentationSpaceCount );
8
17
  }
9
18
 
10
19
  // ~~
11
20
 
12
- function getMultilineText(
13
- value,
14
- indentationText,
15
- indentationSpaceCount
21
+ function getEscapedLine(
22
+ text
23
+ )
24
+ {
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
+ );
35
+ }
36
+
37
+ // ~~
38
+
39
+ function getMultilineText(
40
+ value,
41
+ indentationText,
42
+ indentationSpaceCount
16
43
  )
17
44
  {
18
45
  let lineArray = value.replaceAll( '\r', '' ).split( '\n' );
19
46
  let lineCount = lineArray.length;
20
- let lineIndentationText = indentationText + ' '.repeat( indentationSpaceCount );
47
+ let lineIndentationText = indentationText + getIndentationText( indentationSpaceCount );
21
48
  let multilineText = '‴';
22
49
 
23
50
  for ( let lineIndex = 0; lineIndex < lineCount; ++lineIndex )
@@ -27,12 +54,12 @@ function getMultilineText(
27
54
  let trimmedLine = line.trimEnd();
28
55
  let endingSpaceCount = line.length - trimmedLine.length;
29
56
  let lineContent = trimmedLine.slice( startingSpaceCount );
30
-
57
+
31
58
  let startingIndentationText;
32
59
 
33
60
  if ( startingSpaceCount > 0 )
34
61
  {
35
- startingIndentationText = '‗' + ' '.repeat( startingSpaceCount - 1 );
62
+ startingIndentationText = '‗' + getIndentationText( startingSpaceCount - 1 );
36
63
  }
37
64
  else
38
65
  {
@@ -43,14 +70,14 @@ function getMultilineText(
43
70
 
44
71
  if ( endingSpaceCount > 0 )
45
72
  {
46
- endingIndentationText = ' '.repeat( endingSpaceCount - 1 ) + '‗';
73
+ endingIndentationText = getIndentationText( endingSpaceCount - 1 ) + '‗';
47
74
  }
48
75
  else
49
76
  {
50
77
  endingIndentationText = '';
51
78
  }
52
79
 
53
- let encodedLine = startingIndentationText + getEscapedLineContent( lineContent ) + endingIndentationText;
80
+ let encodedLine = startingIndentationText + getEscapedLine( lineContent ) + endingIndentationText;
54
81
 
55
82
  if ( lineIndex === lineCount - 1 )
56
83
  {
@@ -72,14 +99,14 @@ function getMultilineText(
72
99
 
73
100
  // ~~
74
101
 
75
- export function getGsonValueText(
76
- value,
77
- indentationLevel,
78
- indentationSpaceCount,
79
- isPacked
102
+ export function getGsonValueText(
103
+ value,
104
+ indentationLevel,
105
+ indentationSpaceCount,
106
+ isPacked
80
107
  )
81
108
  {
82
- let indentationText = ' '.repeat( indentationLevel * indentationSpaceCount );
109
+ let indentationText = getIndentationText( indentationLevel * indentationSpaceCount );
83
110
 
84
111
  if ( value === null )
85
112
  {
@@ -109,15 +136,14 @@ export function getGsonValueText(
109
136
  }
110
137
  else if ( typeof value === 'string' )
111
138
  {
112
- let usePrimeQuote = value.includes( '\n' ) || value.startsWith( '‼' );
113
-
114
- if ( usePrimeQuote )
139
+ if ( value.includes( '\n' )
140
+ || value.startsWith( '‼' ) )
115
141
  {
116
142
  if ( value.includes( '\n' ) )
117
143
  {
118
144
  if ( isPacked )
119
145
  {
120
- return '‴' + getEscapedLineContent( value ).replaceAll( '\n', '\\n' ) + '‴';
146
+ return '‴' + getEscapedLine( value, true ).replaceAll( '\n', '\\n' ) + '‴';
121
147
  }
122
148
  else
123
149
  {
@@ -126,7 +152,7 @@ export function getGsonValueText(
126
152
  }
127
153
  else
128
154
  {
129
- return '‴' + getEscapedLineContent( value ) + '‴';
155
+ return '‴' + getEscapedLine( value, true ) + '‴';
130
156
  }
131
157
  }
132
158
  else
@@ -153,7 +179,7 @@ export function getGsonValueText(
153
179
  }
154
180
  else
155
181
  {
156
- let valueindentationText = indentationText + ' '.repeat( indentationSpaceCount );
182
+ let valueindentationText = indentationText + getIndentationText( indentationSpaceCount );
157
183
  let elementTextArray = [];
158
184
  let valueArray = value;
159
185
  let elementCount = valueArray.length;
@@ -183,16 +209,16 @@ export function getGsonValueText(
183
209
  for ( let keyIndex = 0; keyIndex < keyCount; ++keyIndex )
184
210
  {
185
211
  let key = keyArray[ keyIndex ];
186
- let pair = JSON.stringify( key ) + ':' + getGsonValueText( valueByKeyMap[ key ], 0, indentationSpaceCount, isPacked );
187
- fieldTextArray.push( pair );
212
+ let fieldText = JSON.stringify( key ) + ':' + getGsonValueText( valueByKeyMap[ key ], 0, indentationSpaceCount, isPacked );
213
+ fieldTextArray.push( fieldText );
188
214
  }
189
215
 
190
216
  return '{' + fieldTextArray.join( ',' ) + '}';
191
217
  }
192
218
  else
193
219
  {
194
- let keyindentationText = indentationText + ' '.repeat( indentationSpaceCount );
195
- let valueindentationText = keyindentationText + ' '.repeat( indentationSpaceCount );
220
+ let keyindentationText = indentationText + getIndentationText( indentationSpaceCount );
221
+ let valueindentationText = keyindentationText + getIndentationText( indentationSpaceCount );
196
222
  let lineArray = [];
197
223
 
198
224
  for ( let keyIndex = 0; keyIndex < keyCount; ++keyIndex )
@@ -221,8 +247,8 @@ export function getGsonValueText(
221
247
  let blockLineCount = blockLineArray.length;
222
248
  let blocakIndentationtext = valueindentationText;
223
249
  let innerIndentationSpaceCount = ( indentationLevel + 2 ) * indentationSpaceCount;
224
- let contentIndentationText = blocakIndentationtext + ' '.repeat( indentationSpaceCount );
225
- let innerIndentationText = ' '.repeat( innerIndentationSpaceCount );
250
+ let contentIndentationText = blocakIndentationtext + getIndentationText( indentationSpaceCount );
251
+ let innerIndentationText = getIndentationText( innerIndentationSpaceCount );
226
252
  let indentedLineArray = [];
227
253
 
228
254
  for ( let blockLineIndex = 1; blockLineIndex < blockLineCount; ++blockLineIndex )
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.1",
4
+ "version": "0.1.2",
5
5
  "author": "Eric Pelzer <ecstatic.coder@gmail.com>",
6
6
  "license": "LGPL-3.0-only",
7
7
  "repository": {