-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generators): adiciona gerador de texto
- Loading branch information
1 parent
361fa1d
commit 921ef64
Showing
8 changed files
with
228 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
node_modules | ||
post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { palavras } from '../../data/db-general.js'; | ||
|
||
/** | ||
* Gera uma palavra aleatória. | ||
* | ||
* @returns {string} Uma palavra aleatória. | ||
* @example | ||
* // Gera uma palavra aleatória | ||
* console.log(palavra()); // Ex: "Lorem", "ipsum", "dolor", "sit", "amet" | ||
*/ | ||
export function palavra() { | ||
return palavras[Math.floor(Math.random() * palavras.length)]; | ||
} | ||
|
||
/** | ||
* Gera uma sentença aleatória. | ||
* | ||
* @param {number} [qttWords] - Quantidade de palavras na sentença. Se não for informado, será um número aleatório entre 10 e 20. | ||
* @returns {string} Uma sentença aleatória. | ||
* @example | ||
* // Gera uma sentença aleatória com quantidade de palavras aleatórias | ||
* console.log(sentenca()); // Ex: "Ea placeat, labore quidem, harum expedita fuga." | ||
* | ||
* // Gera uma sentença aleatória com a quantidade de palavras informada. | ||
* console.log(sentenca(10)); // Ex: "Dolorem sit modi deleniti nemo sint laborum vitae repellendus ipsa." | ||
*/ | ||
export function sentenca(qttWords) { | ||
const numberOfWords = qttWords ?? Math.floor(Math.random() * 10) + 10; | ||
let sentence = Array.from({ length: numberOfWords }, () => palavra()).join(' '); | ||
sentence = `${sentence.charAt(0).toUpperCase() + sentence.slice(1)}.`; | ||
|
||
return `${sentence.replace(/,\.|\.\.|\!\.|\?\./g, '.').trim()}`; | ||
} | ||
|
||
/** | ||
* Gera um parágrafo aleatório. | ||
* | ||
* @param {number} [qttParagraph=1] - Quantidade de parágrafos. | ||
* @param {number} [qttWords] - Quantidade de palavras por sentença. Se não for informado, será um número aleatório entre 10 e 20. | ||
* @returns {string} Um parágrafo aleatório. | ||
* @example | ||
* // Gera um parágrafo aleatório com quantidade de palavras aleatórias | ||
* console.log(paragrafo()); | ||
* // Ex: "Recusandae. facilis consequuntur quae fuga. debitis quaerat architecto do placeat." | ||
* | ||
* // Gera um parágrafo aleatório a quantidade de sentença informada, cada uma com quantidade de palavras aleatórias | ||
* console.log(paragrafo(3)); | ||
* // Ex: | ||
* "Lorem ipsum dolor sit amet. | ||
* Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. | ||
* Quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat." | ||
* | ||
* // Gera um parágrafo aleatório com quantidade de sentenças e de palavras informadas. | ||
* console.log(paragrafo(3, 8)); | ||
* // Ex: | ||
* "Labore ipsa, eius dolore corporis ipsam doloremque nobis. | ||
* Enim facilis fuga. voluptatem debitis veniam, hic atque. | ||
* Voluptates fugiat nesciunt, numquam reprehenderit, quae commodo minus." | ||
*/ | ||
export function paragrafo(qttParagraph = 1, qttWords) { | ||
return Array.from({ length: qttParagraph }, () => sentenca(qttWords)) | ||
.map(sentence => sentence.charAt(0).toUpperCase() + sentence.slice(1)) | ||
.join('\n') | ||
.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { describe, test } from 'node:test'; | ||
import { strictEqual, match } from 'node:assert'; | ||
import { palavra, sentenca, paragrafo } from '../src/generators/texto.js'; | ||
|
||
describe('Tests for text generation functions', () => { | ||
|
||
test('Should return a random word from the word list', () => { | ||
const result = palavra(); | ||
strictEqual(typeof result, 'string'); | ||
}); | ||
|
||
test('Should generate a sentence with the default number of words (10 to 20)', () => { | ||
const result = sentenca(); | ||
const wordCount = result.split(' ').length; | ||
strictEqual(wordCount >= 10 && wordCount <= 20, true, `Sentence contains ${wordCount} words`); | ||
strictEqual(result.endsWith('.'), true, 'Sentence should end with a period'); | ||
}); | ||
|
||
test('Should generate a sentence with the exact number of words provided', () => { | ||
const wordCount = 15; | ||
const result = sentenca(wordCount); | ||
const generatedWordCount = result.split(' ').length; | ||
strictEqual(generatedWordCount, wordCount, `Sentence contains ${generatedWordCount} words`); | ||
}); | ||
|
||
test('Should generate a paragraph with 1 sentence and a random number of words', () => { | ||
const result = paragrafo(); | ||
const sentenceCount = result.split('\n').length; | ||
strictEqual(sentenceCount, 1, 'Paragraph should contain 1 sentence'); | ||
strictEqual(result.endsWith('.'), true, 'Paragraph should end with a period'); | ||
}); | ||
|
||
test('Should generate a paragraph with 3 sentences and the default number of words per sentence', () => { | ||
const sentenceCount = 3; | ||
const result = paragrafo(sentenceCount); | ||
const generatedSentenceCount = result.split('\n').length; | ||
strictEqual(generatedSentenceCount, sentenceCount, `Paragraph contains ${generatedSentenceCount} sentences`); | ||
}); | ||
|
||
test('Should generate a paragraph with 2 sentences and 8 words per sentence', () => { | ||
const sentenceCount = 2; | ||
const wordCount = 8; | ||
const result = paragrafo(sentenceCount, wordCount); | ||
const sentences = result.split('\n'); | ||
strictEqual(sentences.length, sentenceCount, `Paragraph contains ${sentences.length} sentences`); | ||
sentences.forEach(sentence => { | ||
const generatedWordCount = sentence.split(' ').length; | ||
strictEqual(generatedWordCount, wordCount, `Sentence contains ${generatedWordCount} words`); | ||
}); | ||
}); | ||
|
||
}); |