-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.ts
93 lines (79 loc) · 2.92 KB
/
solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* eslint-disable no-console */
import * as fs from 'fs';
// Definición de la clase Person
class Person {
name: string;
age: number;
birthDate: string;
programmingLanguages: string[];s
constructor(name: string, age: number, birthDate: string, programmingLanguages: string[]) {
this.name = name;
this.age = age;
this.birthDate = birthDate;
this.programmingLanguages = programmingLanguages;
}
static fromJSON(jsonFileName: string): Person {
const jsonData = JSON.parse(fs.readFileSync(jsonFileName, 'utf8'));
return new Person(
jsonData.name,
jsonData.age,
jsonData.birthDate,
jsonData.programmingLanguages,
);
}
static fromXML(xmlFileName: string): Person {
const xmlData = fs.readFileSync(xmlFileName, 'utf8');
const name = xmlData.match(/<name>(.*?)<\/name>/)?.[1] || '';
const age = parseInt(xmlData.match(/<age>(.*?)<\/age>/)?.[1] || '0', 10);
const birthDate = xmlData.match(/<birthDate>(.*?)<\/birthDate>/)?.[1] || '';
const programmingLanguages = (xmlData.match(/<language>(.*?)<\/language>/g) || [])
.map((lang: string) => lang.replace(/<\/?language>/g, ''));
return new Person(name, age, birthDate, programmingLanguages);
}
display(): void {
console.log(`Nombre: ${this.name}`);
console.log(`Edad: ${this.age}`);
console.log(`Fecha de Nacimiento: ${this.birthDate}`);
console.log(`Lenguajes de Programación: ${this.programmingLanguages.join(', ')}`);
}
}
// Datos a almacenar
const data = {
name: 'Marco Cruz',
age: new Date().getFullYear() - 2001,
birthDate: '2001-08-24',
programmingLanguages: ['JavaScript', 'TypeScript', 'C#', 'PHP'],
};
// Crear archivo JSON
const jsonFileName = 'data.json';
const jsonContent = JSON.stringify(data, null, 2);
fs.writeFileSync(jsonFileName, jsonContent, 'utf8');
console.log(`Archivo JSON creado: ${jsonFileName}`);
// Crear archivo XML
const xmlFileName = 'data.xml';
let xmlContent = '<?xml version="1.0" encoding="UTF-8"?>\n';
xmlContent += '<person>\n';
xmlContent += ` <name>${data.name}</name>\n`;
xmlContent += ` <age>${data.age}</age>\n`;
xmlContent += ` <birthDate>${data.birthDate}</birthDate>\n`;
xmlContent += ' <programmingLanguages>\n';
data.programmingLanguages.forEach((lang) => {
xmlContent += ` <language>${lang}</language>\n`;
});
xmlContent += ' </programmingLanguages>\n';
xmlContent += '</person>';
fs.writeFileSync(xmlFileName, xmlContent, 'utf8');
console.log(`Archivo XML creado: ${xmlFileName}`);
console.log('\nContenido del archivo JSON:');
console.log(jsonContent);
console.log('\nContenido del archivo XML:');
console.log(xmlContent);
const personFromJson = Person.fromJSON(jsonFileName);
console.log('\nDatos desde JSON:');
personFromJson.display();
const personFromXml = Person.fromXML(xmlFileName);
console.log('\nDatos desde XML:');
personFromXml.display();
fs.unlinkSync(jsonFileName);
fs.unlinkSync(xmlFileName);
console.log('\nArchivos borrados.');