Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/capricorncd/obj2str
object/function/array to string
https://github.com/capricorncd/obj2str
es6 javascript objecttostring tostring typescript
Last synced: about 1 month ago
JSON representation
object/function/array to string
- Host: GitHub
- URL: https://github.com/capricorncd/obj2str
- Owner: capricorncd
- Created: 2020-12-13T08:17:52.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-12-27T09:25:40.000Z (about 4 years ago)
- Last Synced: 2024-04-24T07:27:23.731Z (8 months ago)
- Topics: es6, javascript, objecttostring, tostring, typescript
- Language: TypeScript
- Homepage:
- Size: 77.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# obj2str
object(Object, Function, Array) to string
```shell script
npm i -S obj2str
# or
yarn add obj2str
```## Usage
./test/index.js
```javascript
const { obj2str } = require('obj2str')// test object
const obj = {
a: 1111,
b: 'fsdfs,d\'fsd',
b2: 'this\'s an object',
b3: `This's a test object`,
212: 'xxdfdf',
c: {
cc: '3333',
c2: 'sdfsf"d"fsd',
c3: {
d: 2222
}
},
k: null,
f: undefined,
g: 0,
fn: function(str) {
return str + 'hello world!'
},
fn2: (a) => a + 10,
arr: [
122,
'xxx',
{
aa: 232434,
bb: 'bb'
},
'dsfsdf'
]
}const str = obj2str(obj, {
// initSpaces: 4,
// indentSpaces: 4,
prefix: 'const obj = ',
// doubleQuotes: true,
// keyQuote: true
})console.log(str)
``````shell
node ./test/index.js
``````
const obj = {
212: 'xxdfdf',
a: 1111,
b: 'fsdfs,d\'fsd',
b2: 'this\'s an object',
b3: 'This\'s a test object',
c: {
cc: '3333',
c2: 'sdfsf"d"fsd',
c3: {
d: 2222
}
},
k: null,
f: undefined,
g: 0,
fn: function(str) {
return str + 'hello world!'
},
fn2: (a) => a + 10,
arr: [
122,
'xxx',
{
aa: 232434,
bb: 'bb'
},
'dsfsdf'
]
}
```## Methods
|Method|Parameters|Description|
|:--|:--|:--|
|obj2str|(o: any, options?: TypeOptions)|return `string`|#### options
|Props|Type|Description|
|:--|:--|:--|
|initSpaces|`number`|Default indentation space for all lines, default: `0`|
|indentSpaces|`number`|Number of spaces to indent when formatting, default: `2`|
|prefix|`string`|Concatenation string prefix of the first line, default: `''`|
|doubleQuotes|`boolean`|Whether the string uses double quotes, default: `false`|
|keyQuote|`boolean`|Whether the key of the object uses quote, default: `false`|