contador-de-metas 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/contador.js +28 -2
  2. package/date.js +64 -0
  3. package/io.js +12 -0
  4. package/package.json +4 -2
package/contador.js CHANGED
@@ -1,8 +1,11 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
  // const fs = require("fs");
3
3
  // const readline = require("readline");
4
4
  import fs from "fs"
5
5
  import readline from "readline"
6
+ import { isValidDDMMYYYY } from "./date.js";
7
+ import { extractDateDDMMYYYY } from "./date.js";
8
+ import { readFiles } from "./io.js";
6
9
 
7
10
  const userInput = readline.createInterface({
8
11
  input: process.stdin,
@@ -19,10 +22,33 @@ const askQuestion = (question) => {
19
22
 
20
23
  //Variáveis de configuração
21
24
  const endOfToDo = "Nível atual: "
25
+ const fileToDoMarker = "todo"
26
+
27
+ async function findFile() {
28
+ let files = await readFiles();
29
+ let dates = files.filter(file =>{
30
+ if(file.includes(fileToDoMarker)){
31
+ const date = extractDateDDMMYYYY(file)
32
+ if(isValidDDMMYYYY(date)){
33
+ return true
34
+ }
35
+ }
36
+ })
37
+
38
+ const oldestString = dates.reduce((oldest, current) => {
39
+ return parseDate(current) < parseDate(oldest) ? current : oldest;
40
+ });
41
+
42
+ return oldestString;
43
+ }
44
+
45
+ let file = await findFile();
46
+
47
+ console.log("Seria", file, "o arquivo certo?")
22
48
 
23
49
  // read the text and split it on new lines
24
50
  // const lines = fs.readFileSync("todo-18012025" + ".txt").toString().split("\r\n");
25
- const lines = fs.readFileSync(".todo-16112025" + ".txt").toString().split("\n");
51
+ const lines = fs.readFileSync(`${file}`).toString().split("\n");
26
52
  // const lines = fs.readFileSync("Novo Documento" + ".txt").toString().split("\n");
27
53
 
28
54
  // All of the parse topics
package/date.js ADDED
@@ -0,0 +1,64 @@
1
+ export function isValidDDMMYYYY(dateString) {
2
+ // 1. Verificar o formato usando uma expressão regular (ddmmyyyy)
3
+ // Garante exatamente 8 dígitos numéricos
4
+ const formatRegex = /^\d{8}$/;
5
+ if (!formatRegex.test(dateString)) {
6
+ return false;
7
+ }
8
+
9
+ // 2. Extrair dia, mês e ano da string
10
+ const day = parseInt(dateString.substring(0, 2), 10);
11
+ const month = parseInt(dateString.substring(2, 4), 10);
12
+ const year = parseInt(dateString.substring(4, 8), 10);
13
+
14
+ // 3. Verificar os limites básicos (mês entre 1 e 12, dia entre 1 e 31)
15
+ if (month < 1 || month > 12 || day < 1 || day > 31) {
16
+ return false;
17
+ }
18
+
19
+ // 4. Verificar o número de dias em meses específicos (incluindo anos bissextos)
20
+ const daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
21
+
22
+ // Lógica para ano bissexto:
23
+ // Um ano é bissexto se for divisível por 4, exceto se for divisível por 100,
24
+ // mas não se for divisível por 400.
25
+ if (year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0)) {
26
+ daysInMonth[2] = 29;
27
+ }
28
+
29
+ // Verificar se o dia está dentro do intervalo válido para o mês e ano específicos
30
+ if (day > daysInMonth[month]) {
31
+ return false;
32
+ }
33
+
34
+ // Se todas as verificações passarem, a data é válida
35
+ return true;
36
+ }
37
+
38
+ // Exemplos de uso:
39
+ // console.log(`"25122024" é válido? ${isValidDDMMYYYY("25122024")}`); // true
40
+ // console.log(`"31022024" é válido? ${isValidDDMMYYYY("31022024")}`); // false (fevereiro só tem 29 dias em 2024)
41
+ // console.log(`"29022024" é válido? ${isValidDDMMYYYY("29022024")}`); // true (2024 é bissexto)
42
+ // console.log(`"29022023" é válido? ${isValidDDMMYYYY("29022023")}`); // false (2023 não é bissexto)
43
+ // console.log(`"010124" é válido? ${isValidDDMMYYYY("010124")}`); // false (formato incorreto, apenas 6 dígitos)
44
+ // console.log(`"abcde" é válido? ${isValidDDMMYYYY("abcde")}`); // false (não é numérico)
45
+ // console.log(`"12122024" é válido? ${isValidDDMMYYYY("12122024")}`); // true
46
+
47
+ export function extractDateDDMMYYYY(string){
48
+ try {
49
+ const reDDMMYYYY = /(\d{2})(\d{2})(\d{4})/;
50
+ const correspondencia = string.match(reDDMMYYYY);
51
+ return correspondencia[0]
52
+ } catch (err) {
53
+ return false;
54
+ }
55
+ }
56
+
57
+ export function parseDate(str) {
58
+ const digits = str.replace(/\D/g, ""); // remove letras
59
+ const day = Number(digits.slice(0, 2));
60
+ const month = Number(digits.slice(2, 4)) - 1; // JS começa em 0
61
+ const year = Number(digits.slice(4, 8));
62
+
63
+ return new Date(year, month, day);
64
+ }
package/io.js ADDED
@@ -0,0 +1,12 @@
1
+ import fs from "fs/promises"
2
+
3
+ export async function readFiles() {
4
+ try {
5
+ const arquivos = await fs.readdir('.');
6
+ return arquivos;
7
+ } catch (err) {
8
+ console.error('Erro ao ler o diretório:', err);
9
+ }
10
+ }
11
+
12
+
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "contador-de-metas",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "",
5
5
  "main": "contador.js",
6
6
  "type": "module",
7
7
  "files": [
8
- "contador.js"
8
+ "contador.js",
9
+ "date.js",
10
+ "io.js"
9
11
  ],
10
12
  "scripts": {
11
13
  "test": "echo \"Error: no test specified\" && exit 1"