source: imaps-frontend/node_modules/@discoveryjs/json-ext/cjs/stringify-chunked.cjs@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 4.5 KB
Line 
1'use strict';
2
3const utils = require('./utils.cjs');
4
5function encodeString(value) {
6 if (/[^\x20\x21\x23-\x5B\x5D-\uD799]/.test(value)) { // [^\x20-\uD799]|[\x22\x5c]
7 return JSON.stringify(value);
8 }
9
10 return '"' + value + '"';
11}
12
13function* stringifyChunked(value, ...args) {
14 const { replacer, getKeys, space, ...options } = utils.normalizeStringifyOptions(...args);
15 const highWaterMark = Number(options.highWaterMark) || 0x4000; // 16kb by default
16
17 const keyStrings = new Map();
18 const stack = [];
19 const rootValue = { '': value };
20 let prevState = null;
21 let state = () => printEntry('', value);
22 let stateValue = rootValue;
23 let stateEmpty = true;
24 let stateKeys = [''];
25 let stateIndex = 0;
26 let buffer = '';
27
28 while (true) {
29 state();
30
31 if (buffer.length >= highWaterMark || prevState === null) {
32 // flush buffer
33 yield buffer;
34 buffer = '';
35
36 if (prevState === null) {
37 break;
38 }
39 }
40 }
41
42 function printObject() {
43 if (stateIndex === 0) {
44 stateKeys = getKeys(stateValue);
45 buffer += '{';
46 }
47
48 // when no keys left
49 if (stateIndex === stateKeys.length) {
50 buffer += space && !stateEmpty
51 ? `\n${space.repeat(stack.length - 1)}}`
52 : '}';
53
54 popState();
55 return;
56 }
57
58 const key = stateKeys[stateIndex++];
59 printEntry(key, stateValue[key]);
60 }
61
62 function printArray() {
63 if (stateIndex === 0) {
64 buffer += '[';
65 }
66
67 if (stateIndex === stateValue.length) {
68 buffer += space && !stateEmpty
69 ? `\n${space.repeat(stack.length - 1)}]`
70 : ']';
71
72 popState();
73 return;
74 }
75
76 printEntry(stateIndex, stateValue[stateIndex++]);
77 }
78
79 function printEntryPrelude(key) {
80 if (stateEmpty) {
81 stateEmpty = false;
82 } else {
83 buffer += ',';
84 }
85
86 if (space && prevState !== null) {
87 buffer += `\n${space.repeat(stack.length)}`;
88 }
89
90 if (state === printObject) {
91 let keyString = keyStrings.get(key);
92
93 if (keyString === undefined) {
94 keyStrings.set(key, keyString = encodeString(key) + (space ? ': ' : ':'));
95 }
96
97 buffer += keyString;
98 }
99 }
100
101 function printEntry(key, value) {
102 value = utils.replaceValue(stateValue, key, value, replacer);
103
104 if (value === null || typeof value !== 'object') {
105 // primitive
106 if (state !== printObject || value !== undefined) {
107 printEntryPrelude(key);
108 pushPrimitive(value);
109 }
110 } else {
111 // If the visited set does not change after adding a value, then it is already in the set
112 if (stack.includes(value)) {
113 throw new TypeError('Converting circular structure to JSON');
114 }
115
116 printEntryPrelude(key);
117 stack.push(value);
118
119 pushState();
120 state = Array.isArray(value) ? printArray : printObject;
121 stateValue = value;
122 stateEmpty = true;
123 stateIndex = 0;
124 }
125 }
126
127 function pushPrimitive(value) {
128 switch (typeof value) {
129 case 'string':
130 buffer += encodeString(value);
131 break;
132
133 case 'number':
134 buffer += Number.isFinite(value) ? String(value) : 'null';
135 break;
136
137 case 'boolean':
138 buffer += value ? 'true' : 'false';
139 break;
140
141 case 'undefined':
142 case 'object': // typeof null === 'object'
143 buffer += 'null';
144 break;
145
146 default:
147 throw new TypeError(`Do not know how to serialize a ${value.constructor?.name || typeof value}`);
148 }
149 }
150
151 function pushState() {
152 prevState = {
153 keys: stateKeys,
154 index: stateIndex,
155 prev: prevState
156 };
157 }
158
159 function popState() {
160 stack.pop();
161 const value = stack.length > 0 ? stack[stack.length - 1] : rootValue;
162
163 // restore state
164 state = Array.isArray(value) ? printArray : printObject;
165 stateValue = value;
166 stateEmpty = false;
167 stateKeys = prevState.keys;
168 stateIndex = prevState.index;
169
170 // pop state
171 prevState = prevState.prev;
172 }
173}
174
175exports.stringifyChunked = stringifyChunked;
Note: See TracBrowser for help on using the repository browser.