/
demo.js
106 lines (95 loc) · 2.54 KB
/
demo.js
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { log } = require('./index.js');
log('Hello World!');
log(123);
log(true);
log(['test']);
log({ test: 'test', test2: 2, test3: true, test4: ['test', 1, true, [2]], test5: { test: 'test' } });
log(`Some text { test: 'test' } and some more text`);
log(`some text { key1: true, 'key2': " { inner: 'value', } " } text { a: 1 } text`);
const { repairJson } = require('./index.js');
console.log(repairJson(`{ 'k1': 'v1', 'k2': 123 }`));
console.log(repairJson("{ a: 'somestring', b: 42, e: { c: 82, d: [Circular *1] } }"));
const { toArrayOfPlainStringsOrJson } = require('./index.js');
console.log(toArrayOfPlainStringsOrJson(`text { 'k1': 'v1', 'k2': 123 } text { a: 1 } text`));
const { canParseJson } = require('./index.js');
console.log(canParseJson(`{ 'k1': 'v1', k2: 123 }`));
console.log(canParseJson(`{ 'k1': "v1", "k2": 123 }`));
console.log(canParseJson(`"test"`));
console.log(canParseJson(123));
console.log(canParseJson(true));
console.log();
function isJSON(str) {
try {
JSON.parse(str);
} catch (e) {
return false;
}
return true;
}
console.log(isJSON(`{ 'k1': 'v1', k2: 123 }`));
console.log(isJSON(`{ 'k1': "v1", "k2": 123 }`));
console.log(isJSON(`"test"`));
console.log(isJSON(123));
console.log(isJSON(true));
log(`{
"name": "John Doe",
"age": 30,
"isEmployed": true,
"salary": -75000.50,
"address": {
"street": "123 Main St",
"city": "Anytown",
"zipCode": "12345"
},
"phoneNumbers": [
{
"type": "home",
"number": "212-555-1234"
},
{
"type": "work",
"number": "646-555-5678"
}
],
"skills": ["programming", "design", "communication"],
"education": [
{
"degree": "B.Sc. Computer Science",
"university": "State University",
"graduationYear": 2015
},
{
"degree": "M.Sc. Computer Science",
"university": "Tech Institute",
"graduationYear": 2017,
"thesis": null
}
],
"projects": [
{
"name": "Project Alpha",
"description": "A project to develop something innovative.",
"status": "completed"
},
{
"name": "Project Beta",
"description": "An ongoing project to improve existing systems.",
"status": "in progress"
}
],
"preferences": {
"contactMethod": "email",
"language": "en-US",
"timeZone": "America/New_York"
},
"metadata": {
"created": "2023-01-01T12:00:00Z",
"updated": "2023-04-01T09:30:00Z"
},
"financial": {
"accountBalance": 1.2345e+10,
"transactionAmount": -123456.789,
"interestRate": 5.12e-4
}
}
`);