Skip to content

Commit e161eb2

Browse files
committed
object to string conv
1 parent b1826b1 commit e161eb2

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

index.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const makeString = (value, option) => {
55
return null;
66
}
77
option = option || {};
8-
option.assignment = option.assignment || '=';
8+
99
option.braces = option.braces || "true";
10-
option.quotes = option.quotes === 'double' ? 'double' : 'single';
10+
option.quotes = option.quotes || 'double';
1111

1212
//stringBasedOnType();
1313
if (isString(value)) {
@@ -33,17 +33,24 @@ const makeString = (value, option) => {
3333

3434
if (isObject(value)) {
3535
option.seperator = option.seperator || ',';
36-
Object.keys(value).map(() => {
36+
option.assignment = option.assignment || ':';
37+
return option.braces === "true" ? '{' + iterateObj(value, option) + '}' : '' + iterateObj(value, option) + '';
38+
};
3739

38-
});
39-
}
4040

4141

4242
return option.quotes === 'double' ? "" : '';
4343

4444

4545
}
4646

47+
const iterateObj = (value, option) => {
48+
return Object.keys(value).map((key) => {
49+
const modKey = (option.quotes === 'double') ? '"' + key + '"' : "'" + key + "'";
50+
return (typeof value[key] === 'function') ? null : modKey + option.assignment + makeString(value[key], option);
51+
}).filter(function (i) { return i; });
52+
}
53+
4754
const isString = (value) => {
4855
return (value != undefined && typeof value === 'string');
4956
}
@@ -59,15 +66,15 @@ const isObject = (value) => {
5966
const isDate = (value) => {
6067
return (value != undefined && typeof value === 'object' && Object.prototype.toString.call(value) === '[object Date]')
6168
}
62-
63-
console.log(makeString(["ajay", "prajoth", new Date()], { quotes: 'single', braces: "false" }));
64-
65-
console.log(typeof JSON.stringify([1, 2, "ajay"]));
66-
6769
var ajay = {
6870
name: "Ajay",
69-
college: "UNCC"
71+
college: "uncc"
7072
}
71-
console.log(JSON.stringify(ajay));
73+
console.log(makeString(ajay, { braces: "false" }));
74+
75+
//console.log(typeof JSON.stringify([1, 2, "ajay"]));
76+
77+
78+
//console.log(JSON.stringify(ajay));
7279
console.log(isDate(new Date()));
7380

0 commit comments

Comments
 (0)