{"id":17757843,"url":"https://github.com/lopatnov/jstostring","last_synced_at":"2026-04-29T14:32:15.296Z","repository":{"id":99288812,"uuid":"216306502","full_name":"lopatnov/jsToString","owner":"lopatnov","description":"A TypeScript library, that converts JavaScript value to string in run-time. ","archived":false,"fork":false,"pushed_at":"2021-10-03T14:26:46.000Z","size":681,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-09-27T18:47:09.209Z","etag":null,"topics":["converts","javascript","object-to-string","reverse-engineering","to-string","typescript"],"latest_commit_sha":null,"homepage":"https://lopatnov.github.io/jsToString/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lopatnov.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"patreon":null}},"created_at":"2019-10-20T04:18:32.000Z","updated_at":"2024-09-04T01:48:52.000Z","dependencies_parsed_at":"2023-06-12T05:15:36.424Z","dependency_job_id":null,"html_url":"https://github.com/lopatnov/jsToString","commit_stats":{"total_commits":86,"total_committers":1,"mean_commits":86.0,"dds":0.0,"last_synced_commit":"1924adf720e370fc021aabf25ea33ed6302ecd32"},"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopatnov%2FjsToString","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopatnov%2FjsToString/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopatnov%2FjsToString/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lopatnov%2FjsToString/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lopatnov","download_url":"https://codeload.github.com/lopatnov/jsToString/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246637110,"owners_count":20809552,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["converts","javascript","object-to-string","reverse-engineering","to-string","typescript"],"created_at":"2024-10-26T17:09:11.345Z","updated_at":"2026-04-29T14:32:15.285Z","avatar_url":"https://github.com/lopatnov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @lopatnov/javascripttostring\n\n[![npm](https://img.shields.io/npm/dt/@lopatnov/javascripttostring)](https://www.npmjs.com/package/@lopatnov/javascripttostring)\n[![NPM version](https://badge.fury.io/js/%40lopatnov%2Fjavascripttostring.svg)](https://www.npmjs.com/package/@lopatnov/javascripttostring)\n[![License](https://img.shields.io/github/license/lopatnov/jsToString)](https://github.com/lopatnov/jsToString/blob/master/LICENSE)\n[![TypeScript](https://img.shields.io/badge/TypeScript-5.9-blue)](https://www.typescriptlang.org/)\n[![GitHub stars](https://img.shields.io/github/stars/lopatnov/jsToString)](https://github.com/lopatnov/jsToString/stargazers)\n\nA TypeScript library that converts any JavaScript runtime value into its string source code representation. Supports objects, arrays, functions, circular references, cross-references, and more.\n\n## Table of Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [API](#api)\n- [Examples](#examples)\n- [Supported Types](#supported-types)\n- [Demo](#demo)\n- [Documentation](#documentation)\n- [Related Packages](#related-packages)\n- [Contributing](#contributing)\n- [Built With](#built-with)\n- [License](#license)\n\n## Installation\n\n```bash\nnpm install @lopatnov/javascripttostring\n```\n\n### Browser (CDN)\n\n```html\n\u003cscript src=\"https://unpkg.com/@lopatnov/javascripttostring/dist/javascripttostring.umd.js\"\u003e\u003c/script\u003e\n```\n\n## Usage\n\n### ES Modules\n\n```typescript\nimport javaScriptToString from \"@lopatnov/javascripttostring\";\n```\n\n### CommonJS\n\n```javascript\nconst javaScriptToString = require(\"@lopatnov/javascripttostring\");\n```\n\n### Browser (UMD)\n\n```javascript\nconst javaScriptToString = window.javaScriptToString;\n```\n\n## API\n\n### javaScriptToString(value, options?): string\n\nConverts a JavaScript value to its string source code representation.\n\n| Parameter | Type | Description |\n|-----------|------|-------------|\n| `value` | `any` | The value to convert |\n| `options` | `IJ2SOptions` | Optional configuration |\n\n**Returns:** `string` - Source code representation that can be evaluated back to the original value\n\n### Options\n\n| Option | Type | Default | Description |\n|--------|------|---------|-------------|\n| `includeFunctionProperties` | `boolean` | `true` | Include function's own properties |\n| `includeFunctionPrototype` | `boolean` | `true` | Include function's prototype properties |\n| `includeBuffers` | `boolean` | `true` | Include ArrayBuffer and TypedArray contents |\n| `nestedObjectsAmount` | `number` | `Infinity` | Max depth for nested objects |\n| `nestedArraysAmount` | `number` | `Infinity` | Max depth for nested arrays |\n| `nestedFunctionsAmount` | `number` | `Infinity` | Max depth for nested functions |\n| `throwOnNonSerializable` | `boolean` | `false` | Throw an error for non-serializable values (Promise, Generator, WeakRef, WeakMap, WeakSet, FinalizationRegistry) |\n\n## Examples\n\n### Primitives\n\n```typescript\njavaScriptToString(\"Hello world\");   // '\"Hello world\"'\njavaScriptToString(42);              // '42'\njavaScriptToString(true);            // 'true'\njavaScriptToString(undefined);       // 'undefined'\njavaScriptToString(null);            // 'null'\n```\n\n### Arrays\n\n```typescript\njavaScriptToString([\"Hello\", \"World\"]);\n// '[\"Hello\", \"World\"]'\n```\n\n### Objects\n\n```typescript\njavaScriptToString({\n  name: \"Alex\",\n  friends: [\"Shurik\", \"Hola\"],\n  greet: () =\u003e {\n    console.log(\"How you doing?\");\n  }\n});\n// '{name: \"Alex\", friends: [\"Shurik\", \"Hola\"], greet: () =\u003e { console.log(\"How you doing?\"); }}'\n```\n\n### Functions with Properties\n\n```typescript\nfunction Simple(title) {\n  this.title = title || \"world\";\n}\nSimple.count = 0;\nSimple.prototype.show = function () {\n  Simple.count++;\n  console.log(\"title =\", this.title);\n};\n\njavaScriptToString(Simple);\n// '(function(){ var Simple = function Simple(title) { ... }; Simple.count = 0; Simple.prototype.show = function(){ ... }; return Simple; }())'\n```\n\n### Circular References\n\nObjects that reference themselves are fully supported:\n\n```typescript\nvar x = [1, 2, 3];\nx[0] = x;\n\njavaScriptToString(x);\n// '(function(){ var ___ref1 = [null, 2, 3]; ___ref1[0] = ___ref1; return ___ref1; }())'\n```\n\n### Cross-References\n\nObjects shared between different branches are preserved as references:\n\n```typescript\nvar shared = { value: 42 };\nvar obj = { a: shared, b: shared };\n\njavaScriptToString(obj);\n// Generates code where obj.a === obj.b (same reference):\n// (function(){ var ___ref1 = {\n//   a: { value: 42 },\n//   b: null\n// }; ___ref1.b = ___ref1.a; return ___ref1; }())\n```\n\n### Using with Web Workers\n\nCombine 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:\n\n```typescript\nimport javaScriptToString from \"@lopatnov/javascripttostring\";\nimport workerFromString from \"@lopatnov/worker-from-string\";\n\n// Function with attached lookup data\nfunction classify(value) {\n  const range = classify.ranges.find(r =\u003e value \u003e= r.min \u0026\u0026 value \u003c r.max);\n  return range ? range.label : \"unknown\";\n}\nclassify.ranges = [\n  { min: 0, max: 30, label: \"cold\" },\n  { min: 30, max: 60, label: \"warm\" },\n  { min: 60, max: 100, label: \"hot\" },\n];\n\n// Serialize and send to a worker\n// javaScriptToString preserves the function AND its properties:\nconst code = javaScriptToString(classify);\n\nconst worker = workerFromString(`\n  const classify = ${code};\n  self.onmessage = (e) =\u003e postMessage(classify(e.data));\n`);\n\nworker.onmessage = (e) =\u003e console.log(e.data);\nworker.postMessage(45); // \"warm\"\n```\n\n### Restoring Values\n\nThe generated string can be evaluated back to a working JavaScript value:\n\n```typescript\nvar original = { name: \"test\" };\noriginal.self = original;\n\nvar code = javaScriptToString(original);\nvar restored = Function(\"return \" + code)();\n\nconsole.log(restored.self === restored); // true\nconsole.log(restored.name);             // \"test\"\n```\n\n## Supported Types\n\n| Type | Example | Notes |\n|------|---------|-------|\n| Primitives | `string`, `number`, `boolean`, `undefined`, `null` | Including `-0` and `NaN` |\n| BigInt | `BigInt(123)` | |\n| Symbol | `Symbol(\"desc\")`, `Symbol.for(\"key\")` | Registry symbols preserved |\n| RegExp | `/pattern/gi` | `lastIndex` preserved when non-zero |\n| Date | `new Date(\"...\")` | Invalid dates → `new Date(NaN)` |\n| Error | `new Error()`, `new TypeError()` | TypeError, RangeError, ReferenceError, SyntaxError, URIError, EvalError |\n| Array | `[1, 2, 3]` | Sparse arrays preserved |\n| Object | `{ key: \"value\" }` | Including `Object.create(null)` |\n| Function | `function() {}`, `() =\u003e {}`, `async function() {}` | Properties and prototype included |\n| Generator Function | `function*() {}`, `async function*() {}` | |\n| Map | `new Map([[\"key\", \"value\"]])` | |\n| Set | `new Set([1, 2, 3])` | |\n| TypedArray | `Int8Array`, `Float64Array`, etc. | |\n| ArrayBuffer | `new ArrayBuffer(8)`, `SharedArrayBuffer` | |\n| DataView | `new DataView(buffer)` | |\n\n### Non-serializable Types\n\nThe following types cannot be serialized and return `\"undefined\"` by default. Use `throwOnNonSerializable: true` to throw an error instead:\n\n`Promise`, `Generator`, `WeakRef`, `WeakMap`, `WeakSet`, `FinalizationRegistry`\n\n## Demo\n\nTry the library interactively:\n\n- [Interactive Demo](./demo/index.html)\n- [RunKit Playground](https://npm.runkit.com/%40lopatnov%2Fjavascripttostring)\n\n## Documentation\n\n- [API Reference](./docs/index.html)\n- [Changelog](./CHANGELOG.md)\n\n## Related Packages\n\n| Package | Description |\n|---|---|\n| [@lopatnov/worker-from-string](https://www.npmjs.com/package/@lopatnov/worker-from-string) | Create Web Workers from strings — pairs well with `javaScriptToString` |\n| [@lopatnov/get-internal-type](https://www.npmjs.com/package/@lopatnov/get-internal-type) | Runtime type detection used internally by this library |\n\n## Contributing\n\nContributions are welcome! Please read [CONTRIBUTING.md](CONTRIBUTING.md) before opening a pull request.\n\n- Bug reports → [open an issue](https://github.com/lopatnov/jsToString/issues)\n- Found it useful? A [star on GitHub](https://github.com/lopatnov/jsToString) helps others discover the project\n\n---\n\n## Built With\n\n- [TypeScript](https://www.typescriptlang.org/) — strict typing throughout\n- [Rollup](https://rollupjs.org/) — bundled to ESM, CJS, and UMD formats\n- [Biome](https://biomejs.dev/) — linting and formatting\n- [Vitest](https://vitest.dev/) — unit testing\n- [TypeDoc](https://typedoc.org/) — API documentation generation\n- [@lopatnov/get-internal-type](https://github.com/lopatnov/get-internal-type) — reliable runtime type detection\n\n---\n\n## License\n\n[Apache-2.0](https://github.com/lopatnov/jsToString/blob/master/LICENSE) © 2019–2026 [Oleksandr Lopatnov](https://github.com/lopatnov) · [LinkedIn](https://www.linkedin.com/in/lopatnov/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flopatnov%2Fjstostring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flopatnov%2Fjstostring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flopatnov%2Fjstostring/lists"}