json-to-es-module
v2.0.2
Published
Convert JSON to an ECMAScript module
Downloads
201
Maintainers
Readme
json-to-es-module
Convert JSON to an ECMAScript module
{
"name": "Sam"
}
↓
export default {
name: 'Sam'
};
Installation
npm install json-to-es-module
API
const jsonToEsModule = require('json-to-es-module');
jsonToEsModule(str [, option])
str: string
(JSON string)
option: Object
Return: string
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.
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 © 2018 - 2019 Shinnosuke Watanabe