-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
299 lines (286 loc) · 36 KB
/
index.html
File metadata and controls
299 lines (286 loc) · 36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!DOCTYPE html><html class="default" lang="en" data-base="./"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>@lopatnov/javascripttostring</title><meta name="description" content="Documentation for @lopatnov/javascripttostring"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="assets/style.css"/><link rel="stylesheet" href="assets/highlight.css"/><script defer src="assets/main.js"></script><script async src="assets/icons.js" id="tsd-icons-script"></script><script async src="assets/search.js" id="tsd-search-script"></script><script async src="assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="index.html" class="title">@lopatnov/javascripttostring</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><h1>@lopatnov/javascripttostring</h1></div><div class="tsd-panel tsd-typography"><h1 id="lopatnovjavascripttostring" class="tsd-anchor-link">@lopatnov/javascripttostring<a href="#lopatnovjavascripttostring" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h1>
<p><a href="https://www.npmjs.com/package/@lopatnov/javascripttostring"><img src="https://img.shields.io/npm/dt/@lopatnov/javascripttostring" alt="npm"></a>
<a href="https://www.npmjs.com/package/@lopatnov/javascripttostring"><img src="https://badge.fury.io/js/%40lopatnov%2Fjavascripttostring.svg" alt="NPM version"></a>
<a href="https://github.com/lopatnov/jsToString/blob/master/LICENSE"><img src="https://img.shields.io/github/license/lopatnov/jsToString" alt="License"></a>
<a href="https://www.typescriptlang.org/"><img src="https://img.shields.io/badge/TypeScript-5.9-blue" alt="TypeScript"></a>
<a href="https://github.com/lopatnov/jsToString/stargazers"><img src="https://img.shields.io/github/stars/lopatnov/jsToString" alt="GitHub stars"></a></p>
<p>A TypeScript library that converts any JavaScript runtime value into its string source code representation. Supports objects, arrays, functions, circular references, cross-references, and more.</p>
<h2 id="table-of-contents" class="tsd-anchor-link">Table of Contents<a href="#table-of-contents" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<ul>
<li><a href="#installation">Installation</a></li>
<li><a href="#usage">Usage</a></li>
<li><a href="#api">API</a></li>
<li><a href="#examples">Examples</a></li>
<li><a href="#supported-types">Supported Types</a></li>
<li><a href="#demo">Demo</a></li>
<li><a href="#documentation">Documentation</a></li>
<li><a href="#related-packages">Related Packages</a></li>
<li><a href="#contributing">Contributing</a></li>
<li><a href="#built-with">Built With</a></li>
<li><a href="#license">License</a></li>
</ul>
<h2 id="installation" class="tsd-anchor-link">Installation<a href="#installation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<pre><code class="bash"><span class="hl-0">npm</span><span class="hl-1"> </span><span class="hl-2">install</span><span class="hl-1"> </span><span class="hl-2">@lopatnov/javascripttostring</span>
</code><button type="button">Copy</button></pre>
<h3 id="browser-cdn" class="tsd-anchor-link">Browser (CDN)<a href="#browser-cdn" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="html"><span class="hl-3"><</span><span class="hl-4">script</span><span class="hl-5"> </span><span class="hl-6">src</span><span class="hl-5">=</span><span class="hl-7">"https://unpkg.com/@lopatnov/javascripttostring/dist/javascripttostring.umd.js"</span><span class="hl-3">></</span><span class="hl-4">script</span><span class="hl-3">></span>
</code><button type="button">Copy</button></pre>
<h2 id="usage" class="tsd-anchor-link">Usage<a href="#usage" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<h3 id="es-modules" class="tsd-anchor-link">ES Modules<a href="#es-modules" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="typescript"><span class="hl-8">import</span><span class="hl-1"> </span><span class="hl-9">javaScriptToString</span><span class="hl-1"> </span><span class="hl-8">from</span><span class="hl-1"> </span><span class="hl-2">"@lopatnov/javascripttostring"</span><span class="hl-1">;</span>
</code><button type="button">Copy</button></pre>
<h3 id="commonjs" class="tsd-anchor-link">CommonJS<a href="#commonjs" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="javascript"><span class="hl-10">const</span><span class="hl-1"> </span><span class="hl-11">javaScriptToString</span><span class="hl-1"> = </span><span class="hl-0">require</span><span class="hl-1">(</span><span class="hl-2">"@lopatnov/javascripttostring"</span><span class="hl-1">);</span>
</code><button type="button">Copy</button></pre>
<h3 id="browser-umd" class="tsd-anchor-link">Browser (UMD)<a href="#browser-umd" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="javascript"><span class="hl-10">const</span><span class="hl-1"> </span><span class="hl-11">javaScriptToString</span><span class="hl-1"> = </span><span class="hl-9">window</span><span class="hl-1">.</span><span class="hl-9">javaScriptToString</span><span class="hl-1">;</span>
</code><button type="button">Copy</button></pre>
<h2 id="api" class="tsd-anchor-link">API<a href="#api" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<h3 id="javascripttostringvalue-options-string" class="tsd-anchor-link">javaScriptToString(value, options?): string<a href="#javascripttostringvalue-options-string" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>Converts a JavaScript value to its string source code representation.</p>
<table>
<thead>
<tr>
<th>Parameter</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>value</code></td>
<td><code>any</code></td>
<td>The value to convert</td>
</tr>
<tr>
<td><code>options</code></td>
<td><code>IJ2SOptions</code></td>
<td>Optional configuration</td>
</tr>
</tbody>
</table>
<p><strong>Returns:</strong> <code>string</code> - Source code representation that can be evaluated back to the original value</p>
<h3 id="options" class="tsd-anchor-link">Options<a href="#options" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<table>
<thead>
<tr>
<th>Option</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>includeFunctionProperties</code></td>
<td><code>boolean</code></td>
<td><code>true</code></td>
<td>Include function's own properties</td>
</tr>
<tr>
<td><code>includeFunctionPrototype</code></td>
<td><code>boolean</code></td>
<td><code>true</code></td>
<td>Include function's prototype properties</td>
</tr>
<tr>
<td><code>includeBuffers</code></td>
<td><code>boolean</code></td>
<td><code>true</code></td>
<td>Include ArrayBuffer and TypedArray contents</td>
</tr>
<tr>
<td><code>nestedObjectsAmount</code></td>
<td><code>number</code></td>
<td><code>Infinity</code></td>
<td>Max depth for nested objects</td>
</tr>
<tr>
<td><code>nestedArraysAmount</code></td>
<td><code>number</code></td>
<td><code>Infinity</code></td>
<td>Max depth for nested arrays</td>
</tr>
<tr>
<td><code>nestedFunctionsAmount</code></td>
<td><code>number</code></td>
<td><code>Infinity</code></td>
<td>Max depth for nested functions</td>
</tr>
<tr>
<td><code>throwOnNonSerializable</code></td>
<td><code>boolean</code></td>
<td><code>false</code></td>
<td>Throw an error for non-serializable values (Promise, Generator, WeakRef, WeakMap, WeakSet, FinalizationRegistry)</td>
</tr>
</tbody>
</table>
<h2 id="examples" class="tsd-anchor-link">Examples<a href="#examples" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<h3 id="primitives" class="tsd-anchor-link">Primitives<a href="#primitives" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="typescript"><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-2">"Hello world"</span><span class="hl-1">); </span><span class="hl-12">// '"Hello world"'</span><br/><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-13">42</span><span class="hl-1">); </span><span class="hl-12">// '42'</span><br/><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-10">true</span><span class="hl-1">); </span><span class="hl-12">// 'true'</span><br/><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-10">undefined</span><span class="hl-1">); </span><span class="hl-12">// 'undefined'</span><br/><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-10">null</span><span class="hl-1">); </span><span class="hl-12">// 'null'</span>
</code><button type="button">Copy</button></pre>
<h3 id="arrays" class="tsd-anchor-link">Arrays<a href="#arrays" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="typescript"><span class="hl-0">javaScriptToString</span><span class="hl-1">([</span><span class="hl-2">"Hello"</span><span class="hl-1">, </span><span class="hl-2">"World"</span><span class="hl-1">]);</span><br/><span class="hl-12">// '["Hello", "World"]'</span>
</code><button type="button">Copy</button></pre>
<h3 id="objects" class="tsd-anchor-link">Objects<a href="#objects" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="typescript"><span class="hl-0">javaScriptToString</span><span class="hl-1">({</span><br/><span class="hl-1"> </span><span class="hl-9">name:</span><span class="hl-1"> </span><span class="hl-2">"Alex"</span><span class="hl-1">,</span><br/><span class="hl-1"> </span><span class="hl-9">friends:</span><span class="hl-1"> [</span><span class="hl-2">"Shurik"</span><span class="hl-1">, </span><span class="hl-2">"Hola"</span><span class="hl-1">],</span><br/><span class="hl-1"> </span><span class="hl-0">greet</span><span class="hl-9">:</span><span class="hl-1"> () </span><span class="hl-10">=></span><span class="hl-1"> {</span><br/><span class="hl-1"> </span><span class="hl-9">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">"How you doing?"</span><span class="hl-1">);</span><br/><span class="hl-1"> }</span><br/><span class="hl-1">});</span><br/><span class="hl-12">// '{name: "Alex", friends: ["Shurik", "Hola"], greet: () => { console.log("How you doing?"); }}'</span>
</code><button type="button">Copy</button></pre>
<h3 id="functions-with-properties" class="tsd-anchor-link">Functions with Properties<a href="#functions-with-properties" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<pre><code class="typescript"><span class="hl-10">function</span><span class="hl-1"> </span><span class="hl-0">Simple</span><span class="hl-1">(</span><span class="hl-9">title</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-10">this</span><span class="hl-1">.</span><span class="hl-9">title</span><span class="hl-1"> = </span><span class="hl-9">title</span><span class="hl-1"> || </span><span class="hl-2">"world"</span><span class="hl-1">;</span><br/><span class="hl-1">}</span><br/><span class="hl-9">Simple</span><span class="hl-1">.</span><span class="hl-9">count</span><span class="hl-1"> = </span><span class="hl-13">0</span><span class="hl-1">;</span><br/><span class="hl-14">Simple</span><span class="hl-1">.</span><span class="hl-9">prototype</span><span class="hl-1">.</span><span class="hl-0">show</span><span class="hl-1"> = </span><span class="hl-10">function</span><span class="hl-1"> () {</span><br/><span class="hl-1"> </span><span class="hl-9">Simple</span><span class="hl-1">.</span><span class="hl-9">count</span><span class="hl-1">++;</span><br/><span class="hl-1"> </span><span class="hl-9">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-2">"title ="</span><span class="hl-1">, </span><span class="hl-10">this</span><span class="hl-1">.</span><span class="hl-9">title</span><span class="hl-1">);</span><br/><span class="hl-1">};</span><br/><br/><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-9">Simple</span><span class="hl-1">);</span><br/><span class="hl-12">// '(function(){ var Simple = function Simple(title) { ... }; Simple.count = 0; Simple.prototype.show = function(){ ... }; return Simple; }())'</span>
</code><button type="button">Copy</button></pre>
<h3 id="circular-references" class="tsd-anchor-link">Circular References<a href="#circular-references" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>Objects that reference themselves are fully supported:</p>
<pre><code class="typescript"><span class="hl-10">var</span><span class="hl-1"> </span><span class="hl-9">x</span><span class="hl-1"> = [</span><span class="hl-13">1</span><span class="hl-1">, </span><span class="hl-13">2</span><span class="hl-1">, </span><span class="hl-13">3</span><span class="hl-1">];</span><br/><span class="hl-9">x</span><span class="hl-1">[</span><span class="hl-13">0</span><span class="hl-1">] = </span><span class="hl-9">x</span><span class="hl-1">;</span><br/><br/><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-9">x</span><span class="hl-1">);</span><br/><span class="hl-12">// '(function(){ var ___ref1 = [null, 2, 3]; ___ref1[0] = ___ref1; return ___ref1; }())'</span>
</code><button type="button">Copy</button></pre>
<h3 id="cross-references" class="tsd-anchor-link">Cross-References<a href="#cross-references" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>Objects shared between different branches are preserved as references:</p>
<pre><code class="typescript"><span class="hl-10">var</span><span class="hl-1"> </span><span class="hl-9">shared</span><span class="hl-1"> = { </span><span class="hl-9">value:</span><span class="hl-1"> </span><span class="hl-13">42</span><span class="hl-1"> };</span><br/><span class="hl-10">var</span><span class="hl-1"> </span><span class="hl-9">obj</span><span class="hl-1"> = { </span><span class="hl-9">a:</span><span class="hl-1"> </span><span class="hl-9">shared</span><span class="hl-1">, </span><span class="hl-9">b:</span><span class="hl-1"> </span><span class="hl-9">shared</span><span class="hl-1"> };</span><br/><br/><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-9">obj</span><span class="hl-1">);</span><br/><span class="hl-12">// Generates code where obj.a === obj.b (same reference):</span><br/><span class="hl-12">// (function(){ var ___ref1 = {</span><br/><span class="hl-12">// a: { value: 42 },</span><br/><span class="hl-12">// b: null</span><br/><span class="hl-12">// }; ___ref1.b = ___ref1.a; return ___ref1; }())</span>
</code><button type="button">Copy</button></pre>
<h3 id="using-with-web-workers" class="tsd-anchor-link">Using with Web Workers<a href="#using-with-web-workers" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>Combine with <a href="https://www.npmjs.com/package/@lopatnov/worker-from-string">@lopatnov/worker-from-string</a> to serialize functions and data for execution in a Web Worker:</p>
<pre><code class="typescript"><span class="hl-8">import</span><span class="hl-1"> </span><span class="hl-9">javaScriptToString</span><span class="hl-1"> </span><span class="hl-8">from</span><span class="hl-1"> </span><span class="hl-2">"@lopatnov/javascripttostring"</span><span class="hl-1">;</span><br/><span class="hl-8">import</span><span class="hl-1"> </span><span class="hl-9">workerFromString</span><span class="hl-1"> </span><span class="hl-8">from</span><span class="hl-1"> </span><span class="hl-2">"@lopatnov/worker-from-string"</span><span class="hl-1">;</span><br/><br/><span class="hl-12">// Function with attached lookup data</span><br/><span class="hl-10">function</span><span class="hl-1"> </span><span class="hl-0">classify</span><span class="hl-1">(</span><span class="hl-9">value</span><span class="hl-1">) {</span><br/><span class="hl-1"> </span><span class="hl-10">const</span><span class="hl-1"> </span><span class="hl-11">range</span><span class="hl-1"> = </span><span class="hl-9">classify</span><span class="hl-1">.</span><span class="hl-9">ranges</span><span class="hl-1">.</span><span class="hl-0">find</span><span class="hl-1">(</span><span class="hl-9">r</span><span class="hl-1"> </span><span class="hl-10">=></span><span class="hl-1"> </span><span class="hl-9">value</span><span class="hl-1"> >= </span><span class="hl-9">r</span><span class="hl-1">.</span><span class="hl-9">min</span><span class="hl-1"> && </span><span class="hl-9">value</span><span class="hl-1"> < </span><span class="hl-9">r</span><span class="hl-1">.</span><span class="hl-9">max</span><span class="hl-1">);</span><br/><span class="hl-1"> </span><span class="hl-8">return</span><span class="hl-1"> </span><span class="hl-9">range</span><span class="hl-1"> ? </span><span class="hl-9">range</span><span class="hl-1">.</span><span class="hl-9">label</span><span class="hl-1"> : </span><span class="hl-2">"unknown"</span><span class="hl-1">;</span><br/><span class="hl-1">}</span><br/><span class="hl-9">classify</span><span class="hl-1">.</span><span class="hl-9">ranges</span><span class="hl-1"> = [</span><br/><span class="hl-1"> { </span><span class="hl-9">min:</span><span class="hl-1"> </span><span class="hl-13">0</span><span class="hl-1">, </span><span class="hl-9">max:</span><span class="hl-1"> </span><span class="hl-13">30</span><span class="hl-1">, </span><span class="hl-9">label:</span><span class="hl-1"> </span><span class="hl-2">"cold"</span><span class="hl-1"> },</span><br/><span class="hl-1"> { </span><span class="hl-9">min:</span><span class="hl-1"> </span><span class="hl-13">30</span><span class="hl-1">, </span><span class="hl-9">max:</span><span class="hl-1"> </span><span class="hl-13">60</span><span class="hl-1">, </span><span class="hl-9">label:</span><span class="hl-1"> </span><span class="hl-2">"warm"</span><span class="hl-1"> },</span><br/><span class="hl-1"> { </span><span class="hl-9">min:</span><span class="hl-1"> </span><span class="hl-13">60</span><span class="hl-1">, </span><span class="hl-9">max:</span><span class="hl-1"> </span><span class="hl-13">100</span><span class="hl-1">, </span><span class="hl-9">label:</span><span class="hl-1"> </span><span class="hl-2">"hot"</span><span class="hl-1"> },</span><br/><span class="hl-1">];</span><br/><br/><span class="hl-12">// Serialize and send to a worker</span><br/><span class="hl-12">// javaScriptToString preserves the function AND its properties:</span><br/><span class="hl-10">const</span><span class="hl-1"> </span><span class="hl-11">code</span><span class="hl-1"> = </span><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-9">classify</span><span class="hl-1">);</span><br/><br/><span class="hl-10">const</span><span class="hl-1"> </span><span class="hl-11">worker</span><span class="hl-1"> = </span><span class="hl-0">workerFromString</span><span class="hl-1">(</span><span class="hl-2">`</span><br/><span class="hl-2"> const classify = </span><span class="hl-10">${</span><span class="hl-9">code</span><span class="hl-10">}</span><span class="hl-2">;</span><br/><span class="hl-2"> self.onmessage = (e) => postMessage(classify(e.data));</span><br/><span class="hl-2">`</span><span class="hl-1">);</span><br/><br/><span class="hl-9">worker</span><span class="hl-1">.</span><span class="hl-0">onmessage</span><span class="hl-1"> = (</span><span class="hl-9">e</span><span class="hl-1">) </span><span class="hl-10">=></span><span class="hl-1"> </span><span class="hl-9">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-9">e</span><span class="hl-1">.</span><span class="hl-9">data</span><span class="hl-1">);</span><br/><span class="hl-9">worker</span><span class="hl-1">.</span><span class="hl-0">postMessage</span><span class="hl-1">(</span><span class="hl-13">45</span><span class="hl-1">); </span><span class="hl-12">// "warm"</span>
</code><button type="button">Copy</button></pre>
<h3 id="restoring-values" class="tsd-anchor-link">Restoring Values<a href="#restoring-values" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>The generated string can be evaluated back to a working JavaScript value:</p>
<pre><code class="typescript"><span class="hl-10">var</span><span class="hl-1"> </span><span class="hl-9">original</span><span class="hl-1"> = { </span><span class="hl-9">name:</span><span class="hl-1"> </span><span class="hl-2">"test"</span><span class="hl-1"> };</span><br/><span class="hl-9">original</span><span class="hl-1">.</span><span class="hl-9">self</span><span class="hl-1"> = </span><span class="hl-9">original</span><span class="hl-1">;</span><br/><br/><span class="hl-10">var</span><span class="hl-1"> </span><span class="hl-9">code</span><span class="hl-1"> = </span><span class="hl-0">javaScriptToString</span><span class="hl-1">(</span><span class="hl-9">original</span><span class="hl-1">);</span><br/><span class="hl-10">var</span><span class="hl-1"> </span><span class="hl-9">restored</span><span class="hl-1"> = </span><span class="hl-0">Function</span><span class="hl-1">(</span><span class="hl-2">"return "</span><span class="hl-1"> + </span><span class="hl-9">code</span><span class="hl-1">)();</span><br/><br/><span class="hl-9">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-9">restored</span><span class="hl-1">.</span><span class="hl-9">self</span><span class="hl-1"> === </span><span class="hl-9">restored</span><span class="hl-1">); </span><span class="hl-12">// true</span><br/><span class="hl-9">console</span><span class="hl-1">.</span><span class="hl-0">log</span><span class="hl-1">(</span><span class="hl-9">restored</span><span class="hl-1">.</span><span class="hl-9">name</span><span class="hl-1">); </span><span class="hl-12">// "test"</span>
</code><button type="button">Copy</button></pre>
<h2 id="supported-types" class="tsd-anchor-link">Supported Types<a href="#supported-types" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<table>
<thead>
<tr>
<th>Type</th>
<th>Example</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
<tr>
<td>Primitives</td>
<td><code>string</code>, <code>number</code>, <code>boolean</code>, <code>undefined</code>, <code>null</code></td>
<td>Including <code>-0</code> and <code>NaN</code></td>
</tr>
<tr>
<td>BigInt</td>
<td><code>BigInt(123)</code></td>
<td></td>
</tr>
<tr>
<td>Symbol</td>
<td><code>Symbol("desc")</code>, <code>Symbol.for("key")</code></td>
<td>Registry symbols preserved</td>
</tr>
<tr>
<td>RegExp</td>
<td><code>/pattern/gi</code></td>
<td><code>lastIndex</code> preserved when non-zero</td>
</tr>
<tr>
<td>Date</td>
<td><code>new Date("...")</code></td>
<td>Invalid dates → <code>new Date(NaN)</code></td>
</tr>
<tr>
<td>Error</td>
<td><code>new Error()</code>, <code>new TypeError()</code></td>
<td>TypeError, RangeError, ReferenceError, SyntaxError, URIError, EvalError</td>
</tr>
<tr>
<td>Array</td>
<td><code>[1, 2, 3]</code></td>
<td>Sparse arrays preserved</td>
</tr>
<tr>
<td>Object</td>
<td><code>{ key: "value" }</code></td>
<td>Including <code>Object.create(null)</code></td>
</tr>
<tr>
<td>Function</td>
<td><code>function() {}</code>, <code>() => {}</code>, <code>async function() {}</code></td>
<td>Properties and prototype included</td>
</tr>
<tr>
<td>Generator Function</td>
<td><code>function*() {}</code>, <code>async function*() {}</code></td>
<td></td>
</tr>
<tr>
<td>Map</td>
<td><code>new Map([["key", "value"]])</code></td>
<td></td>
</tr>
<tr>
<td>Set</td>
<td><code>new Set([1, 2, 3])</code></td>
<td></td>
</tr>
<tr>
<td>TypedArray</td>
<td><code>Int8Array</code>, <code>Float64Array</code>, etc.</td>
<td></td>
</tr>
<tr>
<td>ArrayBuffer</td>
<td><code>new ArrayBuffer(8)</code>, <code>SharedArrayBuffer</code></td>
<td></td>
</tr>
<tr>
<td>DataView</td>
<td><code>new DataView(buffer)</code></td>
<td></td>
</tr>
</tbody>
</table>
<h3 id="non-serializable-types" class="tsd-anchor-link">Non-serializable Types<a href="#non-serializable-types" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h3>
<p>The following types cannot be serialized and return <code>"undefined"</code> by default. Use <code>throwOnNonSerializable: true</code> to throw an error instead:</p>
<p><code>Promise</code>, <code>Generator</code>, <code>WeakRef</code>, <code>WeakMap</code>, <code>WeakSet</code>, <code>FinalizationRegistry</code></p>
<h2 id="demo" class="tsd-anchor-link">Demo<a href="#demo" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<p>Try the library interactively:</p>
<ul>
<li><a href="media/index.html">Interactive Demo</a></li>
<li><a href="https://npm.runkit.com/%40lopatnov%2Fjavascripttostring">RunKit Playground</a></li>
</ul>
<h2 id="documentation" class="tsd-anchor-link">Documentation<a href="#documentation" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<ul>
<li><a href="media/index-1.html">API Reference</a></li>
<li><a href="media/CHANGELOG.md">Changelog</a></li>
</ul>
<h2 id="related-packages" class="tsd-anchor-link">Related Packages<a href="#related-packages" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<table>
<thead>
<tr>
<th>Package</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://www.npmjs.com/package/@lopatnov/worker-from-string">@lopatnov/worker-from-string</a></td>
<td>Create Web Workers from strings — pairs well with <code>javaScriptToString</code></td>
</tr>
<tr>
<td><a href="https://www.npmjs.com/package/@lopatnov/get-internal-type">@lopatnov/get-internal-type</a></td>
<td>Runtime type detection used internally by this library</td>
</tr>
</tbody>
</table>
<h2 id="contributing" class="tsd-anchor-link">Contributing<a href="#contributing" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<p>Contributions are welcome! Please read <a href="media/CONTRIBUTING.md">CONTRIBUTING.md</a> before opening a pull request.</p>
<ul>
<li>Bug reports → <a href="https://github.com/lopatnov/jsToString/issues">open an issue</a></li>
<li>Found it useful? A <a href="https://github.com/lopatnov/jsToString">star on GitHub</a> helps others discover the project</li>
</ul>
<hr>
<h2 id="built-with" class="tsd-anchor-link">Built With<a href="#built-with" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<ul>
<li><a href="https://www.typescriptlang.org/">TypeScript</a> — strict typing throughout</li>
<li><a href="https://rollupjs.org/">Rollup</a> — bundled to ESM, CJS, and UMD formats</li>
<li><a href="https://biomejs.dev/">Biome</a> — linting and formatting</li>
<li><a href="https://vitest.dev/">Vitest</a> — unit testing</li>
<li><a href="https://typedoc.org/">TypeDoc</a> — API documentation generation</li>
<li><a href="https://github.com/lopatnov/get-internal-type">@lopatnov/get-internal-type</a> — reliable runtime type detection</li>
</ul>
<hr>
<h2 id="license" class="tsd-anchor-link">License<a href="#license" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="assets/icons.svg#icon-anchor"></use></svg></a></h2>
<p><a href="https://github.com/lopatnov/jsToString/blob/master/LICENSE">Apache-2.0</a> © 2019–2026 <a href="https://github.com/lopatnov">Oleksandr Lopatnov</a> · <a href="https://www.linkedin.com/in/lopatnov/">LinkedIn</a></p>
</div></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><a href="#lopatnovjavascripttostring"><span>@lopatnov/javascripttostring</span></a><ul><li><a href="#table-of-contents"><span>Table of <wbr/>Contents</span></a></li><li><a href="#installation"><span>Installation</span></a></li><li><ul><li><a href="#browser-cdn"><span>Browser (<wbr/>CDN)</span></a></li></ul></li><li><a href="#usage"><span>Usage</span></a></li><li><ul><li><a href="#es-modules"><span>ES <wbr/>Modules</span></a></li><li><a href="#commonjs"><span>Common<wbr/>JS</span></a></li><li><a href="#browser-umd"><span>Browser (<wbr/>UMD)</span></a></li></ul></li><li><a href="#api"><span>API</span></a></li><li><ul><li><a href="#javascripttostringvalue-options-string"><span>java<wbr/>Script<wbr/>To<wbr/>String(value, options?): string</span></a></li><li><a href="#options"><span>Options</span></a></li></ul></li><li><a href="#examples"><span>Examples</span></a></li><li><ul><li><a href="#primitives"><span>Primitives</span></a></li><li><a href="#arrays"><span>Arrays</span></a></li><li><a href="#objects"><span>Objects</span></a></li><li><a href="#functions-with-properties"><span>Functions with <wbr/>Properties</span></a></li><li><a href="#circular-references"><span>Circular <wbr/>References</span></a></li><li><a href="#cross-references"><span>Cross-<wbr/>References</span></a></li><li><a href="#using-with-web-workers"><span>Using with <wbr/>Web <wbr/>Workers</span></a></li><li><a href="#restoring-values"><span>Restoring <wbr/>Values</span></a></li></ul></li><li><a href="#supported-types"><span>Supported <wbr/>Types</span></a></li><li><ul><li><a href="#non-serializable-types"><span>Non-<wbr/>serializable <wbr/>Types</span></a></li></ul></li><li><a href="#demo"><span>Demo</span></a></li><li><a href="#documentation"><span>Documentation</span></a></li><li><a href="#related-packages"><span>Related <wbr/>Packages</span></a></li><li><a href="#contributing"><span>Contributing</span></a></li><li><a href="#built-with"><span>Built <wbr/>With</span></a></li><li><a href="#license"><span>License</span></a></li></ul></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="modules.html">@lopatnov/javascripttostring</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>