-
Notifications
You must be signed in to change notification settings - Fork 129
feat(m4): solution exe 013 #2526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
ba1dbaa
9371834
d3c4042
7ff3dcf
c8314b0
ac63330
ba4052b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 => { | ||
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 => { | ||
|
||
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)]) | ||
|
||
.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`) | ||
|
||
} else { | ||
console.error(`something went wrong:`, error) | ||
} | ||
}) | ||
|
||
|
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 |
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. |
There was a problem hiding this comment.
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 = () => { .....