-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstringify.js
More file actions
65 lines (57 loc) · 1.96 KB
/
stringify.js
File metadata and controls
65 lines (57 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// stringify any non-object, only handles String, Number, Boolean, null, undefined
const stringifyItem = (item) => {
if (item === null)
return 'null'
if (item === undefined)
return 'undefined'
if (typeof(item) === 'string')
return `"${item}"`
return item.toString()
}
// recursive function to stringify any Object or List
const stringifyObject = (item) => {
// base case, item is not an object, just return string directly
if (!item || typeof(item) !== 'object')
return stringifyItem(item)
// recursive case where item is a list
if (item.map) {
let str_items = ''
for (let subitem of item) {
str_items += `${stringifyObject(subitem)},`
}
// remove trailing comma
if (str_items.length)
str_items = str_items.slice(0, -1)
return `[${str_items}]`
}
// recursive case where item is an object
let str_items = ''
for (let key of Object.keys(item)) {
const str_key = stringifyObject(key)
const str_value = stringifyObject(item[key])
str_items += `${str_key}:${str_value},`
}
// remove trailing comma
if (str_items.length)
str_items = str_items.slice(0, -1)
return `{${str_items}}`
}
const test_object = {
1: 'value_one',
'key_two': 2,
3: [1,2,3, 'four', {5: 6}],
7: {
'nested_key1': 11,
'nested_key2': 22,
},
9: null,
'10': 'WHATSUP DAWG'
}
const stringified = stringifyObject(test_object)
const professionally_stringified = JSON.stringify(test_object)
console.log(JSON.stringify(test_object))
// {"1":"value_one","3":[1,2,3,"four",{"5":6}],"7":{"nested_key1":11,"nested_key2":22},"9":null,"10":"WHATSUP DAWG","key_two":2}
console.log(stringified)
// {"1":"value_one","3":[1,2,3,"four",{"5":6}],"7":{"nested_key1":11,"nested_key2":22},"9":null,"10":"WHATSUP DAWG","key_two":2}
console.log(stringified == professionally_stringified ? 'Success!' : 'Fail!')
// Success!