https://github.com/commenthol/serialize-to-module
serialize objects to node module
https://github.com/commenthol/serialize-to-module
javascript serialize serialize-objects
Last synced: 7 months ago
JSON representation
serialize objects to node module
- Host: GitHub
- URL: https://github.com/commenthol/serialize-to-module
- Owner: commenthol
- License: mit
- Created: 2019-05-24T16:59:04.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-04-04T10:30:06.000Z (almost 5 years ago)
- Last Synced: 2025-06-26T03:39:21.958Z (7 months ago)
- Topics: javascript, serialize, serialize-objects
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# serialize-to-module
> serialize objects to node module
[](https://www.npmjs.com/package/serialize-to-module/)
[](https://travis-ci.org/commenthol/serialize-to-module)
Serialize objects into a `require`-able module while checking circular structures and respecting references.
The following Objects are supported
- String
- Number
- Boolean
- Object
- Array
- RegExp
- Error
- Date
- Buffer
- Int8Array, Uint8Array, Uint8ClampedArray
- Int16Array, Uint16Array
- Int32Array, Uint32Array, Float32Array
- Float64Array
## Table of Contents
* [Methods](#methods)
* [serializeToModule](#serializetomodule)
* [Contribution and License Agreement](#contribution-and-license-agreement)
* [License](#license)
## Methods
### serializeToModule
`serializeToModule(source, opts, opts.ignoreCircular, opts.reference, opts.comment, opts.beautify) `
serialize to a module which can be `require`ed.
#### Example - serializing while respecting references
```js
const serialTM = require('serialize-to-module')
const obj = { object: { regexp: /^test?$/ } }
obj.reference = obj.object
console.log(serialTM(obj, { reference: true }))
//> var m = {
//> object: {
//> regexp: /^test?$/
//> }
//> };
//> m.reference = m.object;
//> module.exports = m;
```
**Parameters**
**source**: `Object | Array | function | Any`, source to serialize
**opts**: `Object`, options
**opts.ignoreCircular**: `Boolean`, ignore circular objects
**opts.reference**: `Boolean`, reference instead of a copy (requires post-processing of opts.references)
**opts.comment**: `Boolean`, add a comments - useful for linting tools e.g. using 'eslint-disable'
**opts.beautify**: `Boolean | Object`, beautify output - default is `false`. If Object then use je-beautify options.
**opts.unsafe**: `Boolean`, do not escape chars `<>/`
**opts.esm**: `Boolean`, use `export default` instead of common js `module.exports`
**Returns**: `String`, serialized representation of `source` as module
## Contribution and License Agreement
If you contribute code to this project, you are implicitly allowing your
code to be distributed under the MIT license. You are also implicitly
verifying that all code is your original work or correctly attributed
with the source of its origin and licence.
## License
Copyright (c) 2016- commenthol (MIT License)
See [LICENSE][] for more info.
[LICENSE]: ./LICENSE