Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion reference/fs_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,37 @@ fs.readdir(__dirname, (err, files) => {
fs.copyFile('source.txt', 'destination.txt', err => {
if (err) throw err;
console.log('File copied...')
})
})

// Delete a folder
// first, check the existence of the folder
// if it doesnt exist, create it, otherwise delete it
// create a folder "myRepo"
if (!fs.existsSync) {
fs.mkdir(path.join(__dirname, '/myRepo'), {}, err => {
if (err) throw err
console.log('folder created successfully..')
})
}else{
fs.rmdir(path.join(__dirname, "/myRepo"), err =>{
if (err) throw err
console.log("folder deleted...")
})
}

// Delete a file
// first, create an index.txt file in the test folder
// then you can check for the file existence and remove it if it exists
if (!fs.existsSync) {
fs.writeFile(path.join(__dirname, '/test', 'index.txt'), {}, err => {
if (err) throw err
console.log('file created successfully..')
})
}else{
fs.unlink(path.join(__dirname, "/test", "index.txt"), err => {
if (err) throw err
console.log("file deleted..")
})
}


38 changes: 38 additions & 0 deletions reference/url_demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const url = require('url');

const myUrl = new URL('http://mywebsite.com/hello.html?id=100&status=active');

// let's instantiate an indexUrl object
const indexUrl = new URL("https://example:[email protected]#foo")

// Serialized URL
console.log(myUrl.href);
console.log(myUrl.toString());
Expand All @@ -31,3 +34,38 @@ console.log(myUrl.searchParams);

// Loop through params
myUrl.searchParams.forEach((value, name) => console.log(`${name}: ${value}`));

// get the password of the url
console.log(indexUrl.password)

// to set the password portion of the url
indexUrl.password = "abc"
console.log(indexUrl.href)

// get the fragment portion of the url
console.log(indexUrl.hash)

// set the fragment portion of the url
indexUrl.hash = "bar"
console.log(indexUrl.href)

// to get the serialised url origin
console.log(indexUrl.origin)

// to set the pathname of the url
indexUrl.pathname = "fr"
console.log(indexUrl.href)

// get the url protocol
console.log(indexUrl.protocol)

// to set url protocol
indexUrl.protocol = "ftp"
console.log(indexUrl.href)

// to get the username portion
console.log(indexUrl.username)

// to set the username portion
indexUrl.username = "sample"
console.log(indexUrl.href)