Skip to content

Commit 2078a45

Browse files
committed
added support for boolean to string
1 parent e1f7d79 commit 2078a45

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

index.js

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const makeString = (value, option) => {
3+
const createString = (value, option) => {
44
if (value === null) {
5-
return null;
5+
return 'null';
66
}
77
option = option || {};
88

@@ -11,11 +11,12 @@ const makeString = (value, option) => {
1111

1212
//stringBasedOnType();
1313
if (isString(value)) {
14-
return option.quotes === 'double' ? '"' + value.replace(/\\/g, '\\\\').replace('"', '\\"') + '"' : "'" + value.replace(/\\/g, '\\\\').replace('"', '\\"') + "'";
14+
const quote = option.quotes === 'no' ? 'double' : option.quotes;
15+
return quote === 'double' ? '"' + value.replace(/\\/g, '\\\\').replace('"', '\\"') + '"' : "'" + value.replace(/\\/g, '\\\\').replace('"', '\\"') + "'";
1516
}
1617

1718
if (isBoolean(value)) {
18-
return option.quotes === 'double' ? '"' + value + '"' : "'" + value + "'";
19+
return '' + value;
1920
}
2021

2122
if (Array.isArray(value)) {
@@ -66,15 +67,12 @@ const isObject = (value) => {
6667
const isDate = (value) => {
6768
return (value != undefined && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Date]')
6869
}
69-
var ajay = {
70-
name: "Ajay",
71-
college: "uncc"
72-
}
73-
console.log(makeString(ajay, { braces: "false" }));
74-
75-
//console.log(typeof JSON.stringify([1, 2, "ajay"]));
76-
7770

78-
//console.log(JSON.stringify(ajay));
79-
console.log(isDate(new Date()));
71+
const makeString = (value, option) => {
72+
if (option && option.quotes === 'no') {
73+
return createString(value, option).replace(/[""]/g, "");
74+
}
75+
return createString(value, option);
76+
}
8077

78+
module.exports = makeString;

test.js

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe("test toString method", () => {
3737

3838
it("test number to string", () => {
3939
const str = makeString(25);
40+
console.log(JSON.stringify(25));
4041
expect(str).to.be.a('string');
4142
expect(str).to.equal(JSON.stringify(25));
4243
});

0 commit comments

Comments
 (0)