Skip to content

Commit 48182d5

Browse files
committed
included quotes logic and updated readme
1 parent 35c20b6 commit 48182d5

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# make-string
1+
# make-string [![Build Status](https://travis-ci.org/ajay2507/make-string.svg?branch=master)](https://travis-ci.org/ajay2507/make-string)
22

33
make-string converts all data types to string as JSON.toString.
44

@@ -16,13 +16,22 @@ makeString(25); // '25'
1616
## Why? ##
1717
I need to convert the object to string in configurable way like removing curly braces, with single quote, etc. So I created this module. If you face any issues with this module, Free feel to create the issues https://github.com/ajay2507/make-string/issues.
1818

19+
## options ##
20+
Options can be passed as optional second param to makeString to configure few things.
21+
```json
22+
quotes: "single" | "double" | "no"(no quotes) - "double"(default)
23+
braces: "true" | "false" - "true"(default)
24+
assignment: "=" (any assignment operator)
25+
```
1926
## How to use ##
2027

2128
```js
2229
const makeString = require('make-string');
2330
makeString("make-string"); // "make-string"
2431
makeString("make-string", {quotes: 'single'}); // 'make-string'
32+
makeString("make-string", {quotes: 'no'}); // make-string
2533
makeString({package:"make-string"}); // '{"package":"make-string"}'
2634
makeString({package:"make-string"}, {braces: 'false'}); // '"package":"make-string"'
2735
makeString({package:"make-string"}, {assignment: '='}); // '{"package"="make-string"}'
36+
makeString({package:"make-string"}, {assignment: '=', quotes:'no'}); // '{package=make-string}'
2837
```

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const createString = (value, option) => {
5151

5252
const iterateObj = (value, option) => {
5353
return Object.keys(value).map((key) => {
54-
const modKey = (option.quotes === 'double') ? '"' + key + '"' : "'" + key + "'";
54+
const modKey = (option.quotes === 'single') ? "'" + key + "'" : '"' + key + '"';
5555
return (typeof value[key] === 'function') ? null : modKey + option.assignment + makeString(value[key], option);
5656
}).filter(function (i) { return i; });
5757
}

test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ describe("test toString method", () => {
100100
expect(str).to.equal('"name"="Ajay","city"="san jose"');
101101
});
102102

103+
it("test Object to string with and without quotes", () => {
104+
const sampleObject = {
105+
name: "Ajay",
106+
city: "san jose"
107+
}
108+
const str = makeString(sampleObject);
109+
expect(str).to.be.a('string');
110+
expect(str).to.equal(JSON.stringify(sampleObject));
111+
const withoutQuotes = makeString(sampleObject, { quotes: 'no' });
112+
expect(withoutQuotes).to.equal("{name:Ajay,city:san jose}");
113+
const singleQuotes = makeString(sampleObject, { quotes: 'single' });
114+
expect(singleQuotes).to.equal("{'name':'Ajay','city':'san jose'}");
115+
});
116+
103117
it("test Array to string", () => {
104118
const sampleArray = ["New York", "San Jose", "Charlotte"];
105119
const str = makeString(sampleArray);

0 commit comments

Comments
 (0)