Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shinnn/json-to-es-module
Convert JSON to an ECMAScript module
https://github.com/shinnn/json-to-es-module
convert ecmascript json module nodejs
Last synced: 26 days ago
JSON representation
Convert JSON to an ECMAScript module
- Host: GitHub
- URL: https://github.com/shinnn/json-to-es-module
- Owner: shinnn
- License: isc
- Created: 2016-07-01T12:34:59.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-06-12T23:59:10.000Z (over 5 years ago)
- Last Synced: 2024-10-01T23:33:39.182Z (about 1 month ago)
- Topics: convert, ecmascript, json, module, nodejs
- Language: JavaScript
- Size: 92.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# json-to-es-module
[![npm version](https://img.shields.io/npm/v/json-to-es-module.svg)](https://www.npmjs.com/package/json-to-es-module)
[![Build Status](https://travis-ci.com/shinnn/json-to-es-module.svg?branch=master)](https://travis-ci.com/shinnn/json-to-es-module)
[![codecov](https://codecov.io/gh/shinnn/json-to-es-module/branch/master/graph/badge.svg)](https://codecov.io/gh/shinnn/json-to-es-module)Convert [JSON](https://tools.ietf.org/html/rfc8259) to an ECMAScript module
```json
{
"name": "Sam"
}
```↓
```javascript
export default {
name: 'Sam'
};
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).
```
npm install json-to-es-module
```## API
```javascript
const jsonToEsModule = require('json-to-es-module');
```### jsonToEsModule(*str* [, *option*])
*str*: `string` (JSON string)
*option*: `Object`
Return: `string````javascript
jsonToEsModule(`{
"foo": 1,
"bar": [
true,
null
]
}`);
//=> 'export default {\n\tfoo: 1,\n\tbar: [\n\t\ttrue,\n\t\tnull\n\t]\n};\n'
```#### option.filename
Type: `string`
Filename displayed in the error message.
```javascript
try {
jsonToEsModule('"');
} catch (err) {
err.message; //=> Unexpected end of JSON input while parsing near '"'
}try {
jsonToEsModule('"', {filename: 'source.json'});
} catch (err) {
err.message; //=> Unexpected end of JSON input while parsing near '"' in source.json
}
```## License
[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe