Skip to content

Commit b82dafe

Browse files
committed
Added project files from the Working with data in JavaScript series
1 parent 9185131 commit b82dafe

8 files changed

Lines changed: 142 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Import dependencies
2+
const fs = require("fs");
3+
4+
// Load the config
5+
const config = require("./config.json");
6+
7+
// Show the config
8+
console.log(config);
9+
10+
// Modify the config
11+
config.apiKey = "gj3j#4K321hIO"; // 2i3f2399g23y43q9o
12+
13+
// Saved the config
14+
fs.writeFile("./config.json", JSON.stringify(config), function (err, file) {
15+
if (err) throw err;
16+
console.log("Saved!");
17+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"connectionString": "192.168.1.234/db",
3+
"apiKey": "2i3f2399g23y43q9o",
4+
"services": [
5+
{
6+
"id": 1,
7+
"name": "products"
8+
},
9+
{
10+
"id": 2,
11+
"name": "users"
12+
}
13+
]
14+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Import dependencies
2+
const fs = require("fs");
3+
const { parseString, Builder } = require("xml2js");
4+
5+
// Load the XML
6+
const xml = fs.readFileSync("data.xml").toString();
7+
parseString(xml, function (err, data) {
8+
9+
// Show the XML
10+
console.log(data);
11+
12+
// Modify the XML
13+
data.people.person[0].$.id = 2;
14+
15+
// Saved the XML
16+
const builder = new Builder();
17+
const xml = builder.buildObject(data);
18+
fs.writeFileSync("data.xml", xml, function (err, file) {
19+
if (err) throw err;
20+
console.log("Saved!");
21+
});
22+
23+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<people>
3+
<person id="2">
4+
<name>Bob</name>
5+
<address>
6+
<road>Spring Street</road>
7+
<city>New York</city>
8+
<zipcode>10012</zipcode>
9+
</address>
10+
</person>
11+
<person id="2">
12+
<name>Smith</name>
13+
<address>
14+
<road>Briarwood Road</road>
15+
<city>Arma</city>
16+
<zipcode>20315</zipcode>
17+
</address>
18+
</person>
19+
</people>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "working-with-xml",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "MIT",
11+
"dependencies": {
12+
"xml2js": "^0.4.22"
13+
}
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Import dependencies
2+
const fs = require("fs");
3+
const YAML = require("js-yaml");
4+
5+
// Get YAML or throw exception on error
6+
try {
7+
8+
// Load the YAML
9+
const raw = fs.readFileSync("data.yaml");
10+
const data = YAML.safeLoad(raw);
11+
12+
// Show the YAML
13+
console.log(data);
14+
15+
// Modify the YAML
16+
data.customer.first_name = "Bob"; // Dorothy
17+
18+
// Saved the YAML
19+
const yaml = YAML.safeDump(data);
20+
fs.writeFileSync("data.yaml", yaml, function (err, file) {
21+
if (err) throw err;
22+
console.log("Saved!");
23+
});
24+
25+
} catch (ex) {
26+
// Show error
27+
console.log(ex);
28+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
date: 2019-08-07T00:00:00.000Z
2+
customer:
3+
first_name: Dorothy
4+
family_name: Gale
5+
items:
6+
- name: Water Bottle
7+
description: Water Bottle (Filled)
8+
price: 1.47
9+
quantity: 4
10+
- name: Knife and fork set
11+
description: Knife and fork set in stainless steel
12+
price: 133.7
13+
quantity: 1
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "working-with-yaml",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "app.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "MIT",
11+
"dependencies": {
12+
"js-yaml": "^3.13.1"
13+
}
14+
}

0 commit comments

Comments
 (0)