senselogic-gson 0.1.0

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.
package/LICENSE.md ADDED
@@ -0,0 +1,163 @@
1
+ GNU Lesser General Public License
2
+ =================================
3
+
4
+ _Version 3, 29 June 2007_
5
+ _Copyright © 2007 Free Software Foundation, Inc. &lt;<http://fsf.org/>&gt;_
6
+
7
+ Everyone is permitted to copy and distribute verbatim copies
8
+ of this license document, but changing it is not allowed.
9
+
10
+
11
+ This version of the GNU Lesser General Public License incorporates
12
+ the terms and conditions of version 3 of the GNU General Public
13
+ License, supplemented by the additional permissions listed below.
14
+
15
+ ### 0. Additional Definitions
16
+
17
+ As used herein, “this License” refers to version 3 of the GNU Lesser
18
+ General Public License, and the “GNU GPL” refers to version 3 of the GNU
19
+ General Public License.
20
+
21
+ “The Library” refers to a covered work governed by this License,
22
+ other than an Application or a Combined Work as defined below.
23
+
24
+ An “Application” is any work that makes use of an interface provided
25
+ by the Library, but which is not otherwise based on the Library.
26
+ Defining a subclass of a class defined by the Library is deemed a mode
27
+ of using an interface provided by the Library.
28
+
29
+ A “Combined Work” is a work produced by combining or linking an
30
+ Application with the Library. The particular version of the Library
31
+ with which the Combined Work was made is also called the “Linked
32
+ Version”.
33
+
34
+ The “Minimal Corresponding Source” for a Combined Work means the
35
+ Corresponding Source for the Combined Work, excluding any source code
36
+ for portions of the Combined Work that, considered in isolation, are
37
+ based on the Application, and not on the Linked Version.
38
+
39
+ The “Corresponding Application Code” for a Combined Work means the
40
+ object code and/or source code for the Application, including any data
41
+ and utility programs needed for reproducing the Combined Work from the
42
+ Application, but excluding the System Libraries of the Combined Work.
43
+
44
+ ### 1. Exception to Section 3 of the GNU GPL
45
+
46
+ You may convey a covered work under sections 3 and 4 of this License
47
+ without being bound by section 3 of the GNU GPL.
48
+
49
+ ### 2. Conveying Modified Versions
50
+
51
+ If you modify a copy of the Library, and, in your modifications, a
52
+ facility refers to a function or data to be supplied by an Application
53
+ that uses the facility (other than as an argument passed when the
54
+ facility is invoked), then you may convey a copy of the modified
55
+ version:
56
+
57
+ * **a)** under this License, provided that you make a good faith effort to
58
+ ensure that, in the event an Application does not supply the
59
+ function or data, the facility still operates, and performs
60
+ whatever part of its purpose remains meaningful, or
61
+
62
+ * **b)** under the GNU GPL, with none of the additional permissions of
63
+ this License applicable to that copy.
64
+
65
+ ### 3. Object Code Incorporating Material from Library Header Files
66
+
67
+ The object code form of an Application may incorporate material from
68
+ a header file that is part of the Library. You may convey such object
69
+ code under terms of your choice, provided that, if the incorporated
70
+ material is not limited to numerical parameters, data structure
71
+ layouts and accessors, or small macros, inline functions and templates
72
+ (ten or fewer lines in length), you do both of the following:
73
+
74
+ * **a)** Give prominent notice with each copy of the object code that the
75
+ Library is used in it and that the Library and its use are
76
+ covered by this License.
77
+ * **b)** Accompany the object code with a copy of the GNU GPL and this license
78
+ document.
79
+
80
+ ### 4. Combined Works
81
+
82
+ You may convey a Combined Work under terms of your choice that,
83
+ taken together, effectively do not restrict modification of the
84
+ portions of the Library contained in the Combined Work and reverse
85
+ engineering for debugging such modifications, if you also do each of
86
+ the following:
87
+
88
+ * **a)** Give prominent notice with each copy of the Combined Work that
89
+ the Library is used in it and that the Library and its use are
90
+ covered by this License.
91
+
92
+ * **b)** Accompany the Combined Work with a copy of the GNU GPL and this license
93
+ document.
94
+
95
+ * **c)** For a Combined Work that displays copyright notices during
96
+ execution, include the copyright notice for the Library among
97
+ these notices, as well as a reference directing the user to the
98
+ copies of the GNU GPL and this license document.
99
+
100
+ * **d)** Do one of the following:
101
+ - **0)** Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+ - **1)** Use a suitable shared library mechanism for linking with the
109
+ Library. A suitable mechanism is one that **(a)** uses at run time
110
+ a copy of the Library already present on the user's computer
111
+ system, and **(b)** will operate properly with a modified version
112
+ of the Library that is interface-compatible with the Linked
113
+ Version.
114
+
115
+ * **e)** Provide Installation Information, but only if you would otherwise
116
+ be required to provide such information under section 6 of the
117
+ GNU GPL, and only to the extent that such information is
118
+ necessary to install and execute a modified version of the
119
+ Combined Work produced by recombining or relinking the
120
+ Application with a modified version of the Linked Version. (If
121
+ you use option **4d0**, the Installation Information must accompany
122
+ the Minimal Corresponding Source and Corresponding Application
123
+ Code. If you use option **4d1**, you must provide the Installation
124
+ Information in the manner specified by section 6 of the GNU GPL
125
+ for conveying Corresponding Source.)
126
+
127
+ ### 5. Combined Libraries
128
+
129
+ You may place library facilities that are a work based on the
130
+ Library side by side in a single library together with other library
131
+ facilities that are not Applications and are not covered by this
132
+ License, and convey such a combined library under terms of your
133
+ choice, if you do both of the following:
134
+
135
+ * **a)** Accompany the combined library with a copy of the same work based
136
+ on the Library, uncombined with any other library facilities,
137
+ conveyed under the terms of this License.
138
+ * **b)** Give prominent notice with the combined library that part of it
139
+ is a work based on the Library, and explaining where to find the
140
+ accompanying uncombined form of the same work.
141
+
142
+ ### 6. Revised Versions of the GNU Lesser General Public License
143
+
144
+ The Free Software Foundation may publish revised and/or new versions
145
+ of the GNU Lesser General Public License from time to time. Such new
146
+ versions will be similar in spirit to the present version, but may
147
+ differ in detail to address new problems or concerns.
148
+
149
+ Each version is given a distinguishing version number. If the
150
+ Library as you received it specifies that a certain numbered version
151
+ of the GNU Lesser General Public License “or any later version”
152
+ applies to it, you have the option of following the terms and
153
+ conditions either of that published version or of any later version
154
+ published by the Free Software Foundation. If the Library as you
155
+ received it does not specify a version number of the GNU Lesser
156
+ General Public License, you may choose any version of the GNU Lesser
157
+ General Public License ever published by the Free Software Foundation.
158
+
159
+ If the Library as you received it specifies that a proxy can decide
160
+ whether future versions of the GNU Lesser General Public License shall
161
+ apply, that proxy's public statement of acceptance of any version is
162
+ permanent authorization for you to choose that version for the
163
+ Library.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ ![](https://github.com/senselogic/GSON/blob/master/LOGO/gson.png)
2
+
3
+ # GSON
4
+
5
+ Granular Structured Object Notation.
6
+
7
+ ## Features
8
+
9
+ * Multiline strings
10
+ * File inclusions
11
+ * Very fast parsing
12
+
13
+ ## Sample
14
+
15
+ ```
16
+ {
17
+ "path":
18
+ "test.json",
19
+ "text":
20
+ ′first line
21
+ second line
22
+ third line ‗
23
+ ‗ fourth line
24
+ fifth line′,
25
+ "person":
26
+ {
27
+ "name":
28
+ "Jack",
29
+ "age":
30
+ 48
31
+ },
32
+ "personArray":
33
+ [
34
+ {
35
+ "name":
36
+ "Mike",
37
+ "age":
38
+ 35
39
+ },
40
+ {
41
+ "name":
42
+ "Nina",
43
+ "age":
44
+ 32
45
+ },
46
+ {
47
+ "name":
48
+ "Pete",
49
+ "age":
50
+ 38
51
+ }
52
+ ],
53
+ "included":
54
+ ′‼@included.gson′
55
+ }
56
+ ```
57
+
58
+ ## Version
59
+
60
+ 0.1
61
+
62
+ ## Author
63
+
64
+ Eric Pelzer (ecstatic.coder@gmail.com).
65
+
66
+ ## License
67
+
68
+ This project is licensed under the GNU Lesser General Public License version 3.
69
+
70
+ See the [LICENSE.md](LICENSE.md) file for details.
package/building.js ADDED
@@ -0,0 +1,278 @@
1
+ // -- FUNCTIONS
2
+
3
+ function getEscapedLineContent(
4
+ text
5
+ )
6
+ {
7
+ return text.replaceAll( '\\', '\\\\' ).replaceAll( '\r', '' );
8
+ }
9
+
10
+ // ~~
11
+
12
+ function getMultilineText(
13
+ value,
14
+ indentationText,
15
+ indentationSpaceCount
16
+ )
17
+ {
18
+ let lineArray = value.replaceAll( '\r', '' ).split( '\n' );
19
+ let lineCount = lineArray.length;
20
+ let lineIndentationText = indentationText + ' '.repeat( indentationSpaceCount );
21
+ let multilineText = '′';
22
+
23
+ for ( let lineIndex = 0; lineIndex < lineCount; ++lineIndex )
24
+ {
25
+ let line = lineArray[ lineIndex ];
26
+ let startingSpaceCount = line.match( /^\s*/ )[ 0 ].length;
27
+ let trimmedLine = line.trimEnd();
28
+ let endingSpaceCount = line.length - trimmedLine.length;
29
+ let lineContent = trimmedLine.slice( startingSpaceCount );
30
+
31
+ let startingIndentationText;
32
+
33
+ if ( startingSpaceCount > 0 )
34
+ {
35
+ startingIndentationText = '‗' + ' '.repeat( startingSpaceCount - 1 );
36
+ }
37
+ else
38
+ {
39
+ startingIndentationText = '';
40
+ }
41
+
42
+ let endingIndentationText;
43
+
44
+ if ( endingSpaceCount > 0 )
45
+ {
46
+ endingIndentationText = ' '.repeat( endingSpaceCount - 1 ) + '‗';
47
+ }
48
+ else
49
+ {
50
+ endingIndentationText = '';
51
+ }
52
+
53
+ let encodedLine = startingIndentationText + getEscapedLineContent( lineContent ) + endingIndentationText;
54
+
55
+ if ( lineIndex === lineCount - 1 )
56
+ {
57
+ encodedLine = encodedLine + '′';
58
+ }
59
+
60
+ if ( lineIndex === 0 )
61
+ {
62
+ multilineText += encodedLine;
63
+ }
64
+ else
65
+ {
66
+ multilineText += '\n' + lineIndentationText + encodedLine;
67
+ }
68
+ }
69
+
70
+ return multilineText;
71
+ }
72
+
73
+ // ~~
74
+
75
+ export function getGsonValueText(
76
+ value,
77
+ indentationLevel,
78
+ indentationSpaceCount,
79
+ isPacked
80
+ )
81
+ {
82
+ let indentationText = ' '.repeat( indentationLevel * indentationSpaceCount );
83
+
84
+ if ( value === null )
85
+ {
86
+ return 'null';
87
+ }
88
+ else if ( typeof value === 'boolean' )
89
+ {
90
+ if ( value )
91
+ {
92
+ return 'true';
93
+ }
94
+ else
95
+ {
96
+ return 'false';
97
+ }
98
+ }
99
+ else if ( typeof value === 'number' )
100
+ {
101
+ if ( Number.isFinite( value ) )
102
+ {
103
+ return String( value );
104
+ }
105
+ else
106
+ {
107
+ return 'null';
108
+ }
109
+ }
110
+ else if ( typeof value === 'string' )
111
+ {
112
+ let usePrimeQuote = value.includes( '\n' ) || value.startsWith( '‼' );
113
+
114
+ if ( usePrimeQuote )
115
+ {
116
+ if ( value.includes( '\n' ) )
117
+ {
118
+ if ( isPacked )
119
+ {
120
+ return '′' + getEscapedLineContent( value ).replaceAll( '\n', '\\n' ) + '′';
121
+ }
122
+ else
123
+ {
124
+ return getMultilineText( value, indentationText, indentationSpaceCount );
125
+ }
126
+ }
127
+ else
128
+ {
129
+ return '′' + getEscapedLineContent( value ) + '′';
130
+ }
131
+ }
132
+ else
133
+ {
134
+ return JSON.stringify( value );
135
+ }
136
+ }
137
+ else if ( Array.isArray( value ) )
138
+ {
139
+ if ( isPacked )
140
+ {
141
+ let elementTextArray = [];
142
+ let valueArray = value;
143
+ let elementCount = valueArray.length;
144
+
145
+ for ( let elementIndex = 0; elementIndex < elementCount; ++elementIndex )
146
+ {
147
+ let element = valueArray[ elementIndex ];
148
+ let elementText = getGsonValueText( element, 0, indentationSpaceCount, isPacked );
149
+ elementTextArray.push( elementText );
150
+ }
151
+
152
+ return '[' + elementTextArray.join( ',' ) + ']';
153
+ }
154
+ else
155
+ {
156
+ let valueindentationText = indentationText + ' '.repeat( indentationSpaceCount );
157
+ let elementTextArray = [];
158
+ let valueArray = value;
159
+ let elementCount = valueArray.length;
160
+
161
+ for ( let elementIndex = 0; elementIndex < elementCount; ++elementIndex )
162
+ {
163
+ let element = valueArray[ elementIndex ];
164
+ let elementText = valueindentationText + getGsonValueText( element, indentationLevel + 1, indentationSpaceCount, isPacked );
165
+ elementTextArray.push( elementText );
166
+ }
167
+
168
+ let inner = elementTextArray.join( ',\n' );
169
+
170
+ return '[\n' + inner + '\n' + indentationText + ']';
171
+ }
172
+ }
173
+ else if ( typeof value === 'object' )
174
+ {
175
+ let valueByKeyMap = value;
176
+ let keyArray = Object.keys( valueByKeyMap );
177
+ let keyCount = keyArray.length;
178
+
179
+ if ( isPacked )
180
+ {
181
+ let fieldTextArray = [];
182
+
183
+ for ( let keyIndex = 0; keyIndex < keyCount; ++keyIndex )
184
+ {
185
+ let key = keyArray[ keyIndex ];
186
+ let pair = JSON.stringify( key ) + ':' + getGsonValueText( valueByKeyMap[ key ], 0, indentationSpaceCount, isPacked );
187
+ fieldTextArray.push( pair );
188
+ }
189
+
190
+ return '{' + fieldTextArray.join( ',' ) + '}';
191
+ }
192
+ else
193
+ {
194
+ let keyindentationText = indentationText + ' '.repeat( indentationSpaceCount );
195
+ let valueindentationText = keyindentationText + ' '.repeat( indentationSpaceCount );
196
+ let lineArray = [];
197
+
198
+ for ( let keyIndex = 0; keyIndex < keyCount; ++keyIndex )
199
+ {
200
+ let key = keyArray[ keyIndex ];
201
+ let keyText = JSON.stringify( key );
202
+ let childValue = valueByKeyMap[ key ];
203
+ let isBlock = ( typeof childValue === 'object' && childValue !== null ) || Array.isArray( childValue );
204
+ let comma;
205
+
206
+ if ( keyIndex < keyCount - 1 )
207
+ {
208
+ comma = ',';
209
+ }
210
+ else
211
+ {
212
+ comma = '';
213
+ }
214
+
215
+ lineArray.push( keyindentationText + keyText + ':' );
216
+
217
+ if ( isBlock )
218
+ {
219
+ let blockText = getGsonValueText( childValue, indentationLevel + 1, indentationSpaceCount, isPacked );
220
+ let blockLineArray = blockText.split( '\n' );
221
+ let blockLineCount = blockLineArray.length;
222
+ let blocakIndentationtext = valueindentationText;
223
+ let innerIndentationSpaceCount = ( indentationLevel + 2 ) * indentationSpaceCount;
224
+ let contentIndentationText = blocakIndentationtext + ' '.repeat( indentationSpaceCount );
225
+ let innerIndentationText = ' '.repeat( innerIndentationSpaceCount );
226
+ let indentedLineArray = [];
227
+
228
+ for ( let blockLineIndex = 1; blockLineIndex < blockLineCount; ++blockLineIndex )
229
+ {
230
+ let blockLine = blockLineArray[ blockLineIndex ];
231
+ let indentedLine;
232
+
233
+ if ( blockLine.slice( 0, innerIndentationSpaceCount ) === innerIndentationText )
234
+ {
235
+ indentedLine = contentIndentationText + blockLine.slice( innerIndentationSpaceCount );
236
+ }
237
+ else
238
+ {
239
+ indentedLine = blocakIndentationtext + blockLine.trimStart();
240
+ }
241
+
242
+ indentedLineArray.push( indentedLine );
243
+ }
244
+
245
+ let indentationTextedBlock = blocakIndentationtext + blockLineArray[ 0 ] + '\n' + indentedLineArray.join( '\n' );
246
+ lineArray.push( indentationTextedBlock + comma );
247
+ }
248
+ else
249
+ {
250
+ lineArray.push( valueindentationText + getGsonValueText( childValue, indentationLevel + 1, indentationSpaceCount, isPacked ) + comma );
251
+ }
252
+ }
253
+
254
+ let inner = lineArray.join( '\n' );
255
+
256
+ return '{\n' + inner + '\n' + indentationText + '}';
257
+ }
258
+ }
259
+ else
260
+ {
261
+ return 'null';
262
+ }
263
+ }
264
+
265
+ // ~~
266
+
267
+ export function getGsonText( value, indentationSpaceCount )
268
+ {
269
+ if ( indentationSpaceCount === undefined )
270
+ {
271
+ indentationSpaceCount = 4;
272
+ }
273
+
274
+ let isPacked = ( indentationSpaceCount === 0 );
275
+ let spaceCount = isPacked ? 0 : Math.max( 0, indentationSpaceCount );
276
+
277
+ return getGsonValueText( value, 0, spaceCount, isPacked );
278
+ }
package/equivalence.js ADDED
@@ -0,0 +1,101 @@
1
+ // -- CONSTANTS
2
+
3
+ export function haveSameValue(
4
+ firstValue,
5
+ secondValue
6
+ )
7
+ {
8
+ if ( firstValue !== firstValue
9
+ && secondValue !== secondValue ) // NaN
10
+ {
11
+ return true;
12
+ }
13
+ else if ( firstValue === secondValue )
14
+ {
15
+ return true;
16
+ }
17
+ else if ( firstValue == null
18
+ || secondValue == null )
19
+ {
20
+ return firstValue === secondValue;
21
+ }
22
+ else if ( firstValue instanceof Map
23
+ && secondValue instanceof Map )
24
+ {
25
+ if ( firstValue.size !== secondValue.size )
26
+ {
27
+ return false;
28
+ }
29
+ else
30
+ {
31
+ for ( let [ firstValueKey, firstValueValue ] of firstValue.entries() )
32
+ {
33
+ let entryWasFound = false;
34
+
35
+ for ( let [ secondValueKey, secondValueValue ] of secondValue.entries() )
36
+ {
37
+ if ( haveSameValue( firstValueKey, secondValueKey )
38
+ && haveSameValue( firstValueValue, secondValueValue ) )
39
+ {
40
+ entryWasFound = true;
41
+
42
+ break;
43
+ }
44
+ }
45
+
46
+ if ( !entryWasFound )
47
+ {
48
+ return false;
49
+ }
50
+ }
51
+ }
52
+
53
+ return true;
54
+ }
55
+ else if ( Array.isArray( firstValue )
56
+ && Array.isArray( secondValue ) )
57
+ {
58
+ if ( firstValue.length !== secondValue.length )
59
+ {
60
+ return false;
61
+ }
62
+
63
+ for ( let valueIndex = 0;
64
+ valueIndex < firstValue.length;
65
+ ++valueIndex )
66
+ {
67
+ if ( !haveSameValue( firstValue[ valueIndex ], secondValue[ valueIndex ] ) )
68
+ {
69
+ return false;
70
+ }
71
+ }
72
+
73
+ return true;
74
+ }
75
+ else if ( typeof firstValue === 'object'
76
+ && typeof secondValue === 'object' )
77
+ {
78
+ let firstValueKeyArray = Object.keys( firstValue );
79
+ let secondValueKeyArray = Object.keys( secondValue );
80
+
81
+ if ( firstValueKeyArray.length !== secondValueKeyArray.length )
82
+ {
83
+ return false;
84
+ }
85
+
86
+ for ( let key of firstValueKeyArray )
87
+ {
88
+ if ( !Object.prototype.hasOwnProperty.call( secondValue, key )
89
+ || !haveSameValue( firstValue[ key ], secondValue[ key ] ) )
90
+ {
91
+ return false;
92
+ }
93
+ }
94
+
95
+ return true;
96
+ }
97
+ else
98
+ {
99
+ return false;
100
+ }
101
+ }
package/fetching.js ADDED
@@ -0,0 +1,105 @@
1
+ // -- FUNCTIONS
2
+
3
+ export async function fetchFileText(
4
+ filePath
5
+ )
6
+ {
7
+ let response = await fetch( filePath );
8
+ let fileText = await response.text();
9
+
10
+ return fileText;
11
+ }
12
+
13
+ // ~~
14
+
15
+ async function getUnprimedFetchedText(
16
+ primedText,
17
+ folderPath,
18
+ primedTextIsProcessed = true,
19
+ fetchFileTextFunction = fetchFileText,
20
+ processPrimedTextFunction = processPrimedText
21
+ )
22
+ {
23
+ if ( primedTextIsProcessed
24
+ && fetchFileTextFunction !== null
25
+ && primedText.startsWith( '‼@' ) )
26
+ {
27
+ let filePath = folderPath + primedText.slice( 2 );
28
+ let fileText = fetchFileTextFunction( filePath );
29
+
30
+ return await getFetchedJsonText( fileText, filePath, primedTextIsProcessed, fetchFileTextFunction, processPrimedTextFunction );
31
+ }
32
+ else if ( primedTextIsProcessed
33
+ && processPrimedTextFunction !== null
34
+ && primedText.startsWith( '‼' ) )
35
+ {
36
+ return '"' + processPrimedTextFunction( primedText ) + '"';
37
+ }
38
+ else
39
+ {
40
+ let lineArray = primedText.replaceAll( '\r', '' ).split( '\n' );
41
+
42
+ for ( let lineIndex = 0;
43
+ lineIndex < lineArray.length;
44
+ ++lineIndex )
45
+ {
46
+ lineArray[ lineIndex ]
47
+ = lineArray[ lineIndex ].trim().replaceAll( '‗', ' ' );
48
+ }
49
+
50
+ return '"' + lineArray.join( '\\n' ) + '"';
51
+ }
52
+ }
53
+
54
+ // ~~
55
+
56
+ export async function getFetchedJsonText(
57
+ gsonText,
58
+ filePath,
59
+ fetchFileTextFunction = fetchFileText,
60
+ processPrimedTextFunction = processPrimedText,
61
+ primedTextIsProcessed = true
62
+ )
63
+ {
64
+ gsonText = gsonText.replaceAll( '\r', '' ).trim();
65
+ filePath = filePath.replaceAll( '\\', '/' );
66
+
67
+ let folderPath = filePath.slice( 0, filePath.lastIndexOf( '/' ) + 1 );
68
+ let primedTextArray = gsonText.split( '′' );
69
+
70
+ for ( let primedTextIndex = 1;
71
+ primedTextIndex < primedTextArray.length;
72
+ primedTextIndex += 2 )
73
+ {
74
+ primedTextArray[ primedTextIndex ]
75
+ = await getUnprimedFetchedText( primedTextArray[ primedTextIndex ], folderPath, primedTextIsProcessed, fetchFileTextFunction, processPrimedTextFunction );
76
+ }
77
+
78
+ return primedTextArray.join( '' );
79
+ }
80
+
81
+ // ~~
82
+
83
+ export async function fetchGsonFileText(
84
+ filePath,
85
+ primedTextIsProcessed = true,
86
+ fetchFileTextFunction = fetchFileText,
87
+ processPrimedTextFunction = processPrimedText
88
+ )
89
+ {
90
+ let gsonText = await fetchFileTextFunction( filePath );
91
+
92
+ return await getFetchedJsonText( gsonText, filePath, primedTextIsProcessed, fetchFileTextFunction, processPrimedTextFunction );
93
+ }
94
+
95
+ // ~~
96
+
97
+ export async function fetchGsonFileValue(
98
+ filePath,
99
+ primedTextIsProcessed = true,
100
+ fetchFileTextFunction = fetchFileText,
101
+ processPrimedTextFunction = processPrimedText
102
+ )
103
+ {
104
+ return JSON.parse( await fetchGsonFileText( filePath, primedTextIsProcessed, fetchFileTextFunction, processPrimedTextFunction ) );
105
+ }
package/index.js ADDED
@@ -0,0 +1,31 @@
1
+ // -- IMPORTS
2
+
3
+ import { getGsonText } from './building.js';
4
+ import { getDumpText } from './dumping.js';
5
+ import { haveSameValue } from './equivalence.js';
6
+ import { fetchFileText, getFetchedJsonText, fetchGsonFileText, fetchGsonFileValue } from './fetching.js';
7
+ import { readFileText, getReadJsonText, readGsonFileText, readGsonFileValue } from './reading.js';
8
+ import { getTextHash, getTextUuid, getTextTuid, processPrimedText } from './processing.js';
9
+ import { writeFileText, writeGsonValue } from './writing.js';
10
+
11
+ // -- EXPORTS
12
+
13
+ export {
14
+ fetchFileText,
15
+ fetchGsonFileText,
16
+ fetchGsonFileValue,
17
+ getDumpText,
18
+ getFetchedJsonText,
19
+ getGsonText,
20
+ getReadJsonText,
21
+ getTextHash,
22
+ getTextTuid,
23
+ getTextUuid,
24
+ haveSameValue,
25
+ processPrimedText,
26
+ readFileText,
27
+ readGsonFileText,
28
+ readGsonFileValue,
29
+ writeFileText,
30
+ writeGsonValue
31
+ };
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "senselogic-gson",
3
+ "description": "Granular Structured Object Notation.",
4
+ "version": "0.1.0",
5
+ "author": "Eric Pelzer <ecstatic.coder@gmail.com>",
6
+ "license": "LGPL-3.0-only",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/senselogic/GSON.git"
10
+ },
11
+ "bugs": {
12
+ "url": "https://github.com/senselogic/GSON/issues"
13
+ },
14
+ "homepage": "https://github.com/senselogic/GSON",
15
+ "type": "module",
16
+ "main": "index.js",
17
+ "files": [
18
+ "building.js",
19
+ "equivalence.js",
20
+ "fetching.js",
21
+ "index.js",
22
+ "parsing.js",
23
+ "processing.js",
24
+ "reading.js",
25
+ "writing.js",
26
+ "test.js",
27
+ "README.md"
28
+ ],
29
+ "keywords": [
30
+ "gson"
31
+ ],
32
+ "dependencies": {
33
+ "md5": "^2.3.0"
34
+ },
35
+ "scripts": {
36
+ "test": "node test.js"
37
+ }
38
+ }
package/processing.js ADDED
@@ -0,0 +1,105 @@
1
+ // -- IMPORTS
2
+
3
+ import md5 from 'md5';
4
+
5
+ // -- CONSTANTS
6
+
7
+ const
8
+ isBrowser = ( typeof window !== 'undefined' && typeof window.document !== 'undefined' );
9
+
10
+ // -- FUNCTIONS
11
+
12
+ export function getTextHash(
13
+ text
14
+ )
15
+ {
16
+ return md5( text );
17
+ }
18
+
19
+ // ~~
20
+
21
+ export function getTextUuid(
22
+ text
23
+ )
24
+ {
25
+ if ( text === '' )
26
+ {
27
+ return '';
28
+ }
29
+ else
30
+ {
31
+ let hash = md5( text );
32
+
33
+ return (
34
+ hash.slice( 0, 8 )
35
+ + '-'
36
+ + hash.slice( 8, 12 )
37
+ + '-'
38
+ + hash.slice( 12, 16 )
39
+ + '-'
40
+ + hash.slice( 16, 20 )
41
+ + '-'
42
+ + hash.slice( 20, 32 )
43
+ );
44
+ }
45
+ }
46
+
47
+ // ~~
48
+
49
+ export function getTextTuid(
50
+ text
51
+ )
52
+ {
53
+ if ( text === '' )
54
+ {
55
+ return '';
56
+ }
57
+ else
58
+ {
59
+ let hash = md5( text );
60
+ let tuid = '';
61
+
62
+ if ( isBrowser )
63
+ {
64
+ let buffer = '';
65
+
66
+ for ( let byteIndex = 0; byteIndex < hash.length; byteIndex += 2 )
67
+ {
68
+ buffer += String.fromCharCode( parseInt( hash.slice( byteIndex, byteIndex + 2 ), 16 ) );
69
+ }
70
+
71
+ tuid = btoa( buffer );
72
+ }
73
+ else
74
+ {
75
+ tuid = Buffer.from( hash, 'hex' ).toString( 'base64' );
76
+ }
77
+
78
+ return (
79
+ tuid
80
+ .replaceAll( '+', '-' )
81
+ .replaceAll( '/', '_' )
82
+ .replaceAll( '=', '' )
83
+ );
84
+ }
85
+ }
86
+
87
+ // ~~
88
+
89
+ export function processPrimedText(
90
+ primedText
91
+ )
92
+ {
93
+ if ( primedText.startsWith( '‼#' ) )
94
+ {
95
+ return getTextUuid( primedText.slice( 2 ) );
96
+ }
97
+ else if ( primedText.startsWith( '‼%' ) )
98
+ {
99
+ return getTextTuid( primedText.slice( 2 ) );
100
+ }
101
+ else
102
+ {
103
+ return primedText;
104
+ }
105
+ }
package/reading.js ADDED
@@ -0,0 +1,107 @@
1
+ // -- IMPORTS
2
+
3
+ import { readFileSync } from 'node:fs';
4
+ import { processPrimedText } from './processing.js';
5
+
6
+ // -- FUNCTIONS
7
+
8
+ export function readFileText(
9
+ filePath
10
+ )
11
+ {
12
+ return readFileSync( filePath, 'utf8' );
13
+ }
14
+
15
+ // ~~
16
+
17
+ function getUnprimedReadText(
18
+ primedText,
19
+ folderPath,
20
+ primedTextIsProcessed = true,
21
+ readFileTextFunction = readFileText,
22
+ processPrimedTextFunction = processPrimedText
23
+ )
24
+ {
25
+ if ( primedTextIsProcessed
26
+ && readFileTextFunction !== null
27
+ && primedText.startsWith( '‼@' ) )
28
+ {
29
+ let filePath = folderPath + primedText.slice( 2 );
30
+ let fileText = readFileTextFunction( filePath );
31
+
32
+ return getReadJsonText( fileText, filePath, primedTextIsProcessed, readFileTextFunction, processPrimedTextFunction );
33
+ }
34
+ else if ( primedTextIsProcessed
35
+ && processPrimedTextFunction !== null
36
+ && primedText.startsWith( '‼' ) )
37
+ {
38
+ return '"' + processPrimedTextFunction( primedText ) + '"';
39
+ }
40
+ else
41
+ {
42
+ let lineArray = primedText.replaceAll( '\r', '' ).split( '\n' );
43
+
44
+ for ( let lineIndex = 0;
45
+ lineIndex < lineArray.length;
46
+ ++lineIndex )
47
+ {
48
+ lineArray[ lineIndex ]
49
+ = lineArray[ lineIndex ].trim().replaceAll( '‗', ' ' );
50
+ }
51
+
52
+ return '"' + lineArray.join( '\\n' ) + '"';
53
+ }
54
+ }
55
+
56
+ // ~~
57
+
58
+ export function getReadJsonText(
59
+ gsonText,
60
+ filePath,
61
+ primedTextIsProcessed = true,
62
+ readFileTextFunction = readFileText,
63
+ processPrimedTextFunction = processPrimedText
64
+ )
65
+ {
66
+ gsonText = gsonText.replaceAll( '\r', '' ).trim();
67
+ filePath = filePath.replaceAll( '\\', '/' );
68
+
69
+ let folderPath = filePath.slice( 0, filePath.lastIndexOf( '/' ) + 1 );
70
+ let primedTextArray = gsonText.split( '′' );
71
+
72
+ for ( let primedTextIndex = 1;
73
+ primedTextIndex < primedTextArray.length;
74
+ primedTextIndex += 2 )
75
+ {
76
+ primedTextArray[ primedTextIndex ]
77
+ = getUnprimedReadText( primedTextArray[ primedTextIndex ], folderPath, primedTextIsProcessed, readFileTextFunction, processPrimedTextFunction );
78
+ }
79
+
80
+ return primedTextArray.join( '' );
81
+ }
82
+
83
+ // ~~
84
+
85
+ export function readGsonFileText(
86
+ filePath,
87
+ primedTextIsProcessed = true,
88
+ readFileTextFunction = readFileText,
89
+ processPrimedTextFunction = processPrimedText
90
+ )
91
+ {
92
+ let gsonText = readFileTextFunction( filePath );
93
+
94
+ return getReadJsonText( gsonText, filePath, primedTextIsProcessed, readFileTextFunction, processPrimedTextFunction );
95
+ }
96
+
97
+ // ~~
98
+
99
+ export function readGsonFileValue(
100
+ filePath,
101
+ primedTextIsProcessed = true,
102
+ readFileTextFunction = readFileText,
103
+ processPrimedTextFunction = processPrimedText
104
+ )
105
+ {
106
+ return JSON.parse( readGsonFileText( filePath, primedTextIsProcessed, readFileTextFunction, processPrimedTextFunction ) );
107
+ }
package/test.js ADDED
@@ -0,0 +1,27 @@
1
+ // -- IMPORTS
2
+
3
+ import { getGsonText, readGsonFileText, readGsonFileValue, writeFileText } from './index.js';
4
+
5
+ // -- STATEMENTS
6
+
7
+ let jsonText = readGsonFileText( '../../../DATA/test.gson' );
8
+ console.log( jsonText );
9
+ writeFileText( 'OUT/processed_test.json', jsonText );
10
+
11
+ let jsonValue = readGsonFileValue( '../../../DATA/test.gson' );
12
+ console.log( JSON.stringify( jsonValue ) );
13
+
14
+ let gsonText = getGsonText( jsonValue );
15
+ console.log( gsonText );
16
+ writeFileText( 'OUT/processed_test.gson', gsonText, 4 );
17
+
18
+ jsonText = readGsonFileText( '../../../DATA/test.gson', false );
19
+ console.log( jsonText );
20
+ writeFileText( 'OUT/unprocessed_test.json', jsonText );
21
+
22
+ jsonValue = readGsonFileValue( '../../../DATA/test.gson', false );
23
+ console.log( JSON.stringify( jsonValue ) );
24
+
25
+ gsonText = getGsonText( jsonValue );
26
+ console.log( gsonText );
27
+ writeFileText( 'OUT/unprocessed_test.gson', gsonText, 4 );
package/writing.js ADDED
@@ -0,0 +1,25 @@
1
+ // -- IMPORTS
2
+
3
+ import { writeFileSync } from 'node:fs';
4
+ import { getGsonText } from './building.js';
5
+
6
+ // -- FUNCTIONS
7
+
8
+ export function writeFileText(
9
+ filePath,
10
+ fileText
11
+ )
12
+ {
13
+ writeFileSync( filePath, fileText, 'utf8' );
14
+ }
15
+
16
+ // ~~
17
+
18
+ export function writeGsonValue(
19
+ filePath,
20
+ value,
21
+ writeFileTextFunction = writeFileText
22
+ )
23
+ {
24
+ writeFileText( filePath, getGsonText( value ) );
25
+ }