@lopatnov/javascripttostring 1.7.3 → 2.0.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/README.md +194 -105
- package/dist/javascripttostring.esm.mjs +543 -0
- package/dist/javascripttostring.esm.mjs.map +1 -0
- package/dist/javascripttostring.umd.js +1 -13
- package/dist/javascripttostring.umd.js.map +1 -1
- package/dist/lib/javascripttostring.cjs +545 -0
- package/dist/lib/javascripttostring.cjs.map +1 -0
- package/dist/types/javascripttostring.d.ts +23 -15
- package/package.json +57 -28
- package/dist/javascripttostring.es5.js +0 -6
- package/dist/javascripttostring.es5.js.map +0 -1
- package/dist/javascripttostring.min.es5.js +0 -6
- package/dist/javascripttostring.min.es5.js.map +0 -1
- package/dist/javascripttostring.min.umd.js +0 -14
- package/dist/javascripttostring.min.umd.js.map +0 -1
- package/dist/lib/javascripttostring.js +0 -385
- package/dist/lib/javascripttostring.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,170 +1,259 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lopatnov/javascripttostring
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@lopatnov/javascripttostring)
|
|
4
4
|
[](https://www.npmjs.com/package/@lopatnov/javascripttostring)
|
|
5
5
|
[](https://github.com/lopatnov/jsToString/blob/master/LICENSE)
|
|
6
|
-
[](https://github.com/lopatnov/jsToString/network)
|
|
6
|
+
[](https://www.typescriptlang.org/)
|
|
8
7
|
[](https://github.com/lopatnov/jsToString/stargazers)
|
|
9
|
-

|
|
10
8
|
|
|
11
|
-
|
|
12
|
-
[](https://sobe.ru/na/tech_knigi)
|
|
13
|
-
[](https://www.linkedin.com/in/lopatnov/)
|
|
9
|
+
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.
|
|
14
10
|
|
|
15
|
-
|
|
11
|
+
## Installation
|
|
16
12
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
## Install
|
|
20
|
-
|
|
21
|
-
[](https://www.npmjs.com/package/@lopatnov/javascripttostring)
|
|
22
|
-
|
|
23
|
-
```shell
|
|
13
|
+
```bash
|
|
24
14
|
npm install @lopatnov/javascripttostring
|
|
25
15
|
```
|
|
26
16
|
|
|
27
|
-
|
|
17
|
+
### Browser (CDN)
|
|
28
18
|
|
|
29
19
|
```html
|
|
30
|
-
<script src="
|
|
20
|
+
<script src="https://unpkg.com/@lopatnov/javascripttostring"></script>
|
|
31
21
|
```
|
|
32
22
|
|
|
33
|
-
##
|
|
23
|
+
## Usage
|
|
34
24
|
|
|
35
|
-
###
|
|
25
|
+
### ES Modules
|
|
36
26
|
|
|
37
27
|
```typescript
|
|
38
|
-
import javaScriptToString from
|
|
28
|
+
import javaScriptToString from "@lopatnov/javascripttostring";
|
|
39
29
|
```
|
|
40
30
|
|
|
41
|
-
###
|
|
31
|
+
### CommonJS
|
|
42
32
|
|
|
43
33
|
```javascript
|
|
44
|
-
|
|
34
|
+
const javaScriptToString = require("@lopatnov/javascripttostring");
|
|
45
35
|
```
|
|
46
36
|
|
|
47
|
-
|
|
37
|
+
### Browser (UMD)
|
|
48
38
|
|
|
49
|
-
```
|
|
50
|
-
javaScriptToString
|
|
39
|
+
```javascript
|
|
40
|
+
const javaScriptToString = window.javaScriptToString;
|
|
51
41
|
```
|
|
52
42
|
|
|
53
|
-
|
|
43
|
+
## API
|
|
54
44
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
45
|
+
### javaScriptToString(value, options?): string
|
|
46
|
+
|
|
47
|
+
Converts a JavaScript value to its string source code representation.
|
|
48
|
+
|
|
49
|
+
| Parameter | Type | Description |
|
|
50
|
+
|-----------|------|-------------|
|
|
51
|
+
| `value` | `any` | The value to convert |
|
|
52
|
+
| `options` | `IJ2SOptions` | Optional configuration |
|
|
53
|
+
|
|
54
|
+
**Returns:** `string` - Source code representation that can be evaluated back to the original value
|
|
55
|
+
|
|
56
|
+
### Options
|
|
57
|
+
|
|
58
|
+
| Option | Type | Default | Description |
|
|
59
|
+
|--------|------|---------|-------------|
|
|
60
|
+
| `includeFunctionProperties` | `boolean` | `true` | Include function's own properties |
|
|
61
|
+
| `includeFunctionPrototype` | `boolean` | `true` | Include function's prototype properties |
|
|
62
|
+
| `includeBuffers` | `boolean` | `true` | Include ArrayBuffer and TypedArray contents |
|
|
63
|
+
| `nestedObjectsAmount` | `number` | `Infinity` | Max depth for nested objects |
|
|
64
|
+
| `nestedArraysAmount` | `number` | `Infinity` | Max depth for nested arrays |
|
|
65
|
+
| `nestedFunctionsAmount` | `number` | `Infinity` | Max depth for nested functions |
|
|
66
|
+
| `throwOnNonSerializable` | `boolean` | `false` | Throw an error for non-serializable values (Promise, Generator, WeakRef, WeakMap, WeakSet, FinalizationRegistry) |
|
|
65
67
|
|
|
66
68
|
## Examples
|
|
67
69
|
|
|
70
|
+
### Primitives
|
|
71
|
+
|
|
68
72
|
```typescript
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
73
|
+
javaScriptToString("Hello world"); // '"Hello world"'
|
|
74
|
+
javaScriptToString(42); // '42'
|
|
75
|
+
javaScriptToString(true); // 'true'
|
|
76
|
+
javaScriptToString(undefined); // 'undefined'
|
|
77
|
+
javaScriptToString(null); // 'null'
|
|
72
78
|
```
|
|
73
79
|
|
|
80
|
+
### Arrays
|
|
81
|
+
|
|
74
82
|
```typescript
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
/* expected myStringOfArray value: "[\"Hello\",\"World\",\".\",\"How\",\"do\",\"you\",\"do\",\"?\"]" */
|
|
83
|
+
javaScriptToString(["Hello", "World"]);
|
|
84
|
+
// '["Hello", "World"]'
|
|
78
85
|
```
|
|
79
86
|
|
|
87
|
+
### Objects
|
|
88
|
+
|
|
80
89
|
```typescript
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
friend5: ["Hola", "amigo"],
|
|
88
|
-
friend6: () => {
|
|
89
|
-
console.log("How you doing?");
|
|
90
|
-
}
|
|
91
|
-
}
|
|
90
|
+
javaScriptToString({
|
|
91
|
+
name: "Alex",
|
|
92
|
+
friends: ["Shurik", "Hola"],
|
|
93
|
+
greet: () => {
|
|
94
|
+
console.log("How you doing?");
|
|
95
|
+
}
|
|
92
96
|
});
|
|
93
|
-
|
|
94
|
-
console.log(myObjectString);
|
|
95
|
-
/* expected myObjectString value:
|
|
96
|
-
"{friend1: \"Shurik\",friend2: \"Alex\",friends: {friend3: 123456,friend4: {},friend5: [\"Hola\",\"amigo\"],friend6: () => {
|
|
97
|
-
console.log(\"How you doing?\");
|
|
98
|
-
}}}"
|
|
99
|
-
*/
|
|
97
|
+
// '{name: "Alex", friends: ["Shurik", "Hola"], greet: () => { console.log("How you doing?"); }}'
|
|
100
98
|
```
|
|
101
99
|
|
|
102
|
-
|
|
103
|
-
let myFunctionString = javaScriptToString(function(a,b) {
|
|
104
|
-
console.log("Just a function");
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
console.log(myFunctionString);
|
|
108
|
-
/* expected myFunctionString:
|
|
109
|
-
"function(a,b) {
|
|
110
|
-
console.log(\"Just a function\");
|
|
111
|
-
}"
|
|
112
|
-
*/
|
|
113
|
-
```
|
|
100
|
+
### Functions with Properties
|
|
114
101
|
|
|
115
102
|
```typescript
|
|
116
103
|
function Simple(title) {
|
|
117
104
|
this.title = title || "world";
|
|
118
105
|
}
|
|
119
|
-
|
|
120
106
|
Simple.count = 0;
|
|
121
|
-
|
|
122
|
-
Simple.prototype.show = function(){
|
|
107
|
+
Simple.prototype.show = function () {
|
|
123
108
|
Simple.count++;
|
|
124
|
-
console.log(
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
console.log(javaScriptToString(Simple));
|
|
109
|
+
console.log("title =", this.title);
|
|
110
|
+
};
|
|
128
111
|
|
|
129
|
-
|
|
112
|
+
javaScriptToString(Simple);
|
|
113
|
+
// '(function(){ var Simple = function Simple(title) { ... }; Simple.count = 0; Simple.prototype.show = function(){ ... }; return Simple; }())'
|
|
114
|
+
```
|
|
130
115
|
|
|
131
|
-
|
|
132
|
-
var Simple = function Simple(title) {
|
|
133
|
-
this.title = title || \"world\";
|
|
134
|
-
};
|
|
135
|
-
Simple.count = 0;
|
|
116
|
+
### Circular References
|
|
136
117
|
|
|
137
|
-
|
|
138
|
-
Simple.count++;
|
|
139
|
-
console.log('title = ', this.title);
|
|
140
|
-
console.log('count = ', Simple.count);
|
|
141
|
-
};
|
|
118
|
+
Objects that reference themselves are fully supported:
|
|
142
119
|
|
|
143
|
-
|
|
144
|
-
|
|
120
|
+
```typescript
|
|
121
|
+
var x = [1, 2, 3];
|
|
122
|
+
x[0] = x;
|
|
145
123
|
|
|
146
|
-
|
|
124
|
+
javaScriptToString(x);
|
|
125
|
+
// '(function(){ var ___ref1 = [null, 2, 3]; ___ref1[0] = ___ref1; return ___ref1; }())'
|
|
147
126
|
```
|
|
148
127
|
|
|
149
|
-
|
|
150
|
-
var x = [1,2,3];
|
|
151
|
-
x[0] = x;
|
|
152
|
-
console.log(javaScriptToString(x));
|
|
128
|
+
### Cross-References
|
|
153
129
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
130
|
+
Objects shared between different branches are preserved as references:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
var shared = { value: 42 };
|
|
134
|
+
var obj = { a: shared, b: shared };
|
|
135
|
+
|
|
136
|
+
javaScriptToString(obj);
|
|
137
|
+
// Generates code where obj.a === obj.b (same reference):
|
|
138
|
+
// (function(){ var ___ref1 = {
|
|
139
|
+
// a: { value: 42 },
|
|
140
|
+
// b: null
|
|
141
|
+
// }; ___ref1.b = ___ref1.a; return ___ref1; }())
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Using with Web Workers
|
|
145
|
+
|
|
146
|
+
Combine with [@lopatnov/worker-from-string](https://www.npmjs.com/package/@lopatnov/worker-from-string) to serialize functions and data for execution in a Web Worker:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import javaScriptToString from "@lopatnov/javascripttostring";
|
|
150
|
+
import workerFromString from "@lopatnov/worker-from-string";
|
|
151
|
+
|
|
152
|
+
// Function with attached lookup data
|
|
153
|
+
function classify(value) {
|
|
154
|
+
const range = classify.ranges.find(r => value >= r.min && value < r.max);
|
|
155
|
+
return range ? range.label : "unknown";
|
|
156
|
+
}
|
|
157
|
+
classify.ranges = [
|
|
158
|
+
{ min: 0, max: 30, label: "cold" },
|
|
159
|
+
{ min: 30, max: 60, label: "warm" },
|
|
160
|
+
{ min: 60, max: 100, label: "hot" },
|
|
161
|
+
];
|
|
162
|
+
|
|
163
|
+
// Serialize and send to a worker
|
|
164
|
+
// javaScriptToString preserves the function AND its properties:
|
|
165
|
+
const code = javaScriptToString(classify);
|
|
166
|
+
|
|
167
|
+
const worker = workerFromString(`
|
|
168
|
+
const classify = ${code};
|
|
169
|
+
self.onmessage = (e) => postMessage(classify(e.data));
|
|
170
|
+
`);
|
|
171
|
+
|
|
172
|
+
worker.onmessage = (e) => console.log(e.data);
|
|
173
|
+
worker.postMessage(45); // "warm"
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Restoring Values
|
|
177
|
+
|
|
178
|
+
The generated string can be evaluated back to a working JavaScript value:
|
|
179
|
+
|
|
180
|
+
```typescript
|
|
181
|
+
var original = { name: "test" };
|
|
182
|
+
original.self = original;
|
|
183
|
+
|
|
184
|
+
var code = javaScriptToString(original);
|
|
185
|
+
var restored = Function("return " + code)();
|
|
186
|
+
|
|
187
|
+
console.log(restored.self === restored); // true
|
|
188
|
+
console.log(restored.name); // "test"
|
|
157
189
|
```
|
|
158
190
|
|
|
191
|
+
## Supported Types
|
|
192
|
+
|
|
193
|
+
| Type | Example | Notes |
|
|
194
|
+
|------|---------|-------|
|
|
195
|
+
| Primitives | `string`, `number`, `boolean`, `undefined`, `null` | Including `-0` and `NaN` |
|
|
196
|
+
| BigInt | `BigInt(123)` | |
|
|
197
|
+
| Symbol | `Symbol("desc")`, `Symbol.for("key")` | Registry symbols preserved |
|
|
198
|
+
| RegExp | `/pattern/gi` | `lastIndex` preserved when non-zero |
|
|
199
|
+
| Date | `new Date("...")` | Invalid dates → `new Date(NaN)` |
|
|
200
|
+
| Error | `new Error()`, `new TypeError()` | TypeError, RangeError, ReferenceError, SyntaxError, URIError, EvalError |
|
|
201
|
+
| Array | `[1, 2, 3]` | Sparse arrays preserved |
|
|
202
|
+
| Object | `{ key: "value" }` | Including `Object.create(null)` |
|
|
203
|
+
| Function | `function() {}`, `() => {}`, `async function() {}` | Properties and prototype included |
|
|
204
|
+
| Generator Function | `function*() {}`, `async function*() {}` | |
|
|
205
|
+
| Map | `new Map([["key", "value"]])` | |
|
|
206
|
+
| Set | `new Set([1, 2, 3])` | |
|
|
207
|
+
| TypedArray | `Int8Array`, `Float64Array`, etc. | |
|
|
208
|
+
| ArrayBuffer | `new ArrayBuffer(8)`, `SharedArrayBuffer` | |
|
|
209
|
+
| DataView | `new DataView(buffer)` | |
|
|
210
|
+
|
|
211
|
+
### Non-serializable Types
|
|
212
|
+
|
|
213
|
+
The following types cannot be serialized and return `"undefined"` by default. Use `throwOnNonSerializable: true` to throw an error instead:
|
|
214
|
+
|
|
215
|
+
`Promise`, `Generator`, `WeakRef`, `WeakMap`, `WeakSet`, `FinalizationRegistry`
|
|
216
|
+
|
|
159
217
|
## Demo
|
|
160
218
|
|
|
161
|
-
|
|
219
|
+
Try the library interactively:
|
|
220
|
+
|
|
221
|
+
| | Link |
|
|
222
|
+
|---|---|
|
|
223
|
+
| Interactive Demo | [demo/index.html](./demo/index.html) |
|
|
224
|
+
| RunKit Playground | [runkit.com](https://npm.runkit.com/%40lopatnov%2Fjavascripttostring) |
|
|
225
|
+
|
|
226
|
+
## Documentation
|
|
227
|
+
|
|
228
|
+
| | Link |
|
|
229
|
+
|---|---|
|
|
230
|
+
| API Reference | [docs/index.html](./docs/index.html) |
|
|
231
|
+
| Changelog | [CHANGELOG.md](./CHANGELOG.md) |
|
|
232
|
+
|
|
233
|
+
## Related Packages
|
|
234
|
+
|
|
235
|
+
| Package | Description |
|
|
236
|
+
|---|---|
|
|
237
|
+
| [@lopatnov/worker-from-string](https://www.npmjs.com/package/@lopatnov/worker-from-string) | Create Web Workers from strings — pairs well with `javaScriptToString` |
|
|
238
|
+
| [@lopatnov/get-internal-type](https://www.npmjs.com/package/@lopatnov/get-internal-type) | Runtime type detection used internally by this library |
|
|
239
|
+
|
|
240
|
+
## Contributing
|
|
241
|
+
|
|
242
|
+
Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
243
|
+
|
|
244
|
+
## License
|
|
245
|
+
|
|
246
|
+
[Apache-2.0](LICENSE)
|
|
247
|
+
|
|
248
|
+
Copyright 2019-2026 Oleksandr Lopatnov
|
|
162
249
|
|
|
163
|
-
|
|
250
|
+
---
|
|
164
251
|
|
|
165
|
-
|
|
252
|
+
### Author
|
|
166
253
|
|
|
167
|
-
|
|
254
|
+
**Oleksandr Lopatnov**
|
|
168
255
|
|
|
169
|
-
|
|
256
|
+
[](https://www.linkedin.com/in/lopatnov/)
|
|
257
|
+
[](https://github.com/lopatnov)
|
|
170
258
|
|
|
259
|
+
If you find this project useful, please consider giving it a star on GitHub!
|