stringify-parse

stringify-parse Build Status

A tool like JSON.stringify and JSON.parse but could convert the type Function and RegExp in the js object.

Install

1
npm install stringify-parse

eg.

1
2
3
4
5
6
7
let o = {
name: 'stringify-parse',
method: function() {
console.log('this is a function');
},
reg: /\w+/
}

Now, I want to convert the object into string, if we use JSON.stringify, it will lost the property method and reg

1
{"name":"demo","reg":{}}

This is not what we want. Now we can use the tool stringify-parse

1
2
const stringifyParse = require('stringify-parse');
console.log(stringifyParse(o));

output:

1
2
3
{"name":"demo","method":function() {
console.log('this is a function');
},"reg":/\w+/

Then we can use the method stringifyParse.parse to convert the json string into object.

1
2
3
4
5
let str = `{"name":"demo","method":function() {
console.log('this is a function');
},"reg":/\w+/
`;
console.log(stringifyParse.parse(str));