https://github.com/alexcambose/object-plain-string
Convert javascript objects into strings
https://github.com/alexcambose/object-plain-string
convert javascript json object string text
Last synced: 3 months ago
JSON representation
Convert javascript objects into strings
- Host: GitHub
- URL: https://github.com/alexcambose/object-plain-string
- Owner: alexcambose
- License: mit
- Created: 2017-09-28T15:49:23.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-10-06T13:34:59.000Z (almost 9 years ago)
- Last Synced: 2024-05-21T04:21:15.001Z (about 2 years ago)
- Topics: convert, javascript, json, object, string, text
- Language: JavaScript
- Size: 16.6 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/alexcambose/object-plain-string)
# object-plain-string
Convert javascript objects into strings
## Installation
```
npm i -S object-plain-string
```
[npm](https://www.npmjs.com/package/object-plain-string)
## Usage
```js
const convert = require('object-plain-string');
const obj = {
key1: "Value for key1",
key2: "Value for key2",
key3: {
key31: "Value for key31",
key32: "Value for key32",
}
};
const result = convert(obj);
console.log(result);
/*
"{
key1: 'Value for key1',
key2: 'Value for key2',
key3: {
key31: 'Value for key31',
key32: 'Value for key32',
},
},"
*/
```
```js
const convert = require('object-plain-string');
const obj = {
key1: 'This is key1',
key2: 'This is key2',
integer: 123,
boolean: true,
array: [1,2,3,4],
func: function (){
//comment
console.log('this is a function');
},
object: {
deep1: 'This is deep1',
deep2: 'This is deep2'
},
regex: /.css$/,
_undefined: undefined,
_null: null,
}
const result = convert(obj);
console.log(result);
/*
"{
key1: 'This is key1',
key2: 'This is key2',
integer: 123,
boolean: true,
array: [1,2,3,4,],
func: function (){
//comment
console.log('this is a function');
},
object: {
deep1: 'This is deep1',
deep2: 'This is deep2',
},
regex: /.css$/,
_null: null,
}"
*/
```
`undefined` object key values will be ignored and `null` will be kept
```js
const obj = {
undefinedValue: undefined,
nullValue: null
};
const result = convert(obj);
console.log(result);
/*
"{
nullValue: null,
}"
*/
```
### Why would you use `object-plain-string` instead of `JSON.stringify`?
Well, it depends on what you want to achieve, for example if you want to write javascript config files you may want to use `object-plain-string` instead of `JSON.stringify`.
#### `JSON.stringify` vs `object-plain-string`
```js
const convert = require('object-plain-string');
const obj = {
a: 'Some text',
b: ()=>{
console.log('something');
},
c: [1,2,{a:'Something'},3,4]
};
console.log(JSON.stringify(obj));
/*
{
"a":"Some text",
"c":[1,2,{"key":"Something"},3,4]
}.
*/
console.log(convert(obj));
/*
"{
a: 'Some text',
b: ()=>{
console.log('something');
}
c: [1,2,{
key: 'Something',
},3,4,],
},"
*/
```