Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
73 changes: 73 additions & 0 deletions projects/m4/013-redacting-text-in-a-file/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//013

//packages
const fs = require('fs').promises;

//files name
const story = 'text.txt'
const sensitive = 'sensitive words.txt'
const redacted = 'redacted.txt'

//function to read the content of the story
const readStory = story => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

story -> global scope ;-)
const readStory = () => { .....

return fs.readFile(story, {encoding:'utf-8'})
.then((data) => {
const dataArray = data.replaceAll(/[\r]/g, '').split('\n')
return dataArray})

.catch ((error) => {
console.error(`this ${story} file does not exixt`), error
return Promise.reject(error)
});
}

// function to read the content of sesitive words
const readSensitive = sensitive => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sensitive -> global scope ;-)
const readSensitive = () => { .....

Copy link
Author

@drbabol drbabol Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? it's just in this exercise, because I don't call the fucntion?

return fs.readFile(sensitive, {encoding:'utf-8'})
.then((data) => {
const dataArray = data.replaceAll(/[\r]/g, '').split('\n')
return dataArray})

.catch ((error) => {
console.error(`this ${sensitive} file does not exixt`), error
return Promise.reject(error)
});
}

//function to elaborate the story
//[[text],[sensitive]]
const redactText = textAndSensitive => {

let text = textAndSensitive[0]
const sensitive = textAndSensitive[1]

text.forEach((sentence, index) => {
sensitive.forEach(secret => {
if (sentence.toLowerCase().replaceAll(/[^a-zA-Z0-9 ]/g,'').includes(secret)) {
sentence = sentence.replace(RegExp(`\\b${secret}\\b`, 'gi'), '****')
}
})
text[index] = sentence
});
text = text.join('\n')
return text
}

Promise.all([readStory(story), readSensitive(sensitive)])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise.all( [ readStory(), readSensitive() ] )

.then((data) => {
const outputContent = redactText(data)
return fs.writeFile(redacted, outputContent, { flag: 'r+' })
})
.then(() => {
// file written successfully
console.log(`${redacted} file updated successfully!`)
})
.catch((error) => {
if (error.code === 'ENOENT') {
console.error(`${redacted} file does not exist`)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try renaming sensitive words -> sensitive_words.
Check if the problem prints correctly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the problem, I will try to fix it by adding a function to read the redacted file and add it to the Promise.all()

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I can optimize the code buy writing only one function to read files and called it 3 times in the Promise.all...
Working in Progress

} else {
console.error(`something went wrong:`, error)
}
})


25 changes: 25 additions & 0 deletions projects/m4/013-redacting-text-in-a-file/js/redacted.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Once upon a time, in a small village nestled in **** rolling hills of **** countryside,
there lived a young girl named ****. She was a curious and adventurous girl,
always eager to explore **** world around her.

One day, **** decided to venture into **** **** that bordered **** village.
She had heard many stories about **** ****, some good and some bad, but she was not afraid.
She packed a small bag with some food and water and set off early in **** morning,
determined to see all that **** **** had to offer.

As she walked deeper into **** ****, **** began to notice strange and wonderful things.
She saw bright, colorful birds singing in **** branches above her,
and spotted a family of rabbits hopping through **** underbrush.
She even caught a glimpse of a majestic deer with antlers that seemed to stretch up to **** sky.

But as **** sun began to set and **** shadows grew longer,
**** realized that she had been walking for hours and had no idea how to get back home.
She started to panic, but then she remembered **** stories her mother had told her about **** **** spirits.
She closed her eyes and called out to them, asking for their help.

Suddenly, she felt a gentle breeze on her face and opened her eyes to see a beautiful fairy standing in front of her.
**** fairy smiled and offered to guide **** back to **** village.
And so, with **** fairy's help, **** made her way safely back home,
grateful for **** magic of **** **** and **** kindness of **** **** spirits.

****, ****, ****.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
forest
maria
the
25 changes: 25 additions & 0 deletions projects/m4/013-redacting-text-in-a-file/js/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Once upon a time, in a small village nestled in the rolling hills of the countryside,
there lived a young girl named Maria. She was a curious and adventurous girl,
always eager to explore the world around her.

One day, Maria decided to venture into the forest that bordered the village.
She had heard many stories about the forest, some good and some bad, but she was not afraid.
She packed a small bag with some food and water and set off early in the morning,
determined to see all that the forest had to offer.

As she walked deeper into the forest, Maria began to notice strange and wonderful things.
She saw bright, colorful birds singing in the branches above her,
and spotted a family of rabbits hopping through the underbrush.
She even caught a glimpse of a majestic deer with antlers that seemed to stretch up to the sky.

But as the sun began to set and the shadows grew longer,
Maria realized that she had been walking for hours and had no idea how to get back home.
She started to panic, but then she remembered the stories her mother had told her about the forest spirits.
She closed her eyes and called out to them, asking for their help.

Suddenly, she felt a gentle breeze on her face and opened her eyes to see a beautiful fairy standing in front of her.
The fairy smiled and offered to guide Maria back to the village.
And so, with the fairy's help, Maria made her way safely back home,
grateful for the magic of the forest and the kindness of the forest spirits.

MarIa, ForeST, THE.