|
1 | | -import * as consoleFormatter from 'jsondiffpatch/formatters/console'; |
2 | | -import * as jsondiffpatch from 'jsondiffpatch/with-text-diffs'; |
| 1 | +import * as consoleFormatter from "jsondiffpatch/formatters/console"; |
| 2 | +import * as jsondiffpatch from "jsondiffpatch/with-text-diffs"; |
3 | 3 |
|
4 | 4 | const instance = jsondiffpatch.create({ |
5 | | - objectHash: function (obj) { |
6 | | - const objRecord = obj as Record<string, string>; |
7 | | - return ( |
8 | | - objRecord._id || |
9 | | - objRecord.id || |
10 | | - objRecord.name || |
11 | | - JSON.stringify(objRecord) |
12 | | - ); |
13 | | - }, |
| 5 | + objectHash: (obj) => { |
| 6 | + const objRecord = obj as Record<string, string>; |
| 7 | + return ( |
| 8 | + objRecord._id || |
| 9 | + objRecord.id || |
| 10 | + objRecord.name || |
| 11 | + JSON.stringify(objRecord) |
| 12 | + ); |
| 13 | + }, |
14 | 14 | }); |
15 | 15 |
|
16 | | -interface Data { |
17 | | - name: string; |
18 | | - summary: string; |
19 | | - surface?: number; |
20 | | - timezone: number[]; |
21 | | - demographics: { population: number; largestCities: string[] }; |
22 | | - languages: string[]; |
23 | | - countries: { |
24 | | - name: string; |
25 | | - capital?: string; |
26 | | - independence?: Date; |
27 | | - population?: number; |
28 | | - }[]; |
29 | | - spanishName?: string; |
| 16 | +interface Continent { |
| 17 | + name: string; |
| 18 | + summary: string; |
| 19 | + surface?: number; |
| 20 | + timezone: [number, number]; |
| 21 | + demographics: { population: number; largestCities: string[] }; |
| 22 | + languages: string[]; |
| 23 | + countries: Country[]; |
| 24 | + spanishName?: string; |
30 | 25 | } |
31 | 26 |
|
32 | | -const data: Data = { |
33 | | - name: 'South America', |
34 | | - summary: |
35 | | - 'South America (Spanish: América del Sur, Sudamérica or Suramérica;' + |
36 | | - ' Portuguese: América do Sul; Quechua and Aymara: Urin Awya Yala;' + |
37 | | - ' Guarani: Ñembyamérika; Dutch: Zuid-Amerika; French: Amérique du Sud)' + |
38 | | - ' is a continent situated in the Western Hemisphere, mostly in the' + |
39 | | - ' Southern Hemisphere, with a relatively small portion in the Northern' + |
40 | | - ' Hemisphere. The continent is also considered a subcontinent of the' + |
41 | | - ' Americas.[2][3] It is bordered on the west by the Pacific Ocean and' + |
42 | | - ' on the north and east by the Atlantic Ocean; North America and the' + |
43 | | - ' Caribbean Sea lie to the northwest. It includes twelve countries: ' + |
44 | | - 'Argentina, Bolivia, Brazil, Chile, Colombia, Ecuador, Guyana, Paraguay' + |
45 | | - ', Peru, Suriname, Uruguay, and Venezuela. The South American nations' + |
46 | | - ' that border the Caribbean Sea—including Colombia, Venezuela, Guyana,' + |
47 | | - ' Suriname, as well as French Guiana, which is an overseas region of' + |
48 | | - ' France—are also known as Caribbean South America. South America has' + |
49 | | - ' an area of 17,840,000 square kilometers (6,890,000 sq mi).' + |
50 | | - ' Its population as of 2005 has been estimated at more than 371,090,000.' + |
51 | | - ' South America ranks fourth in area (after Asia, Africa, and' + |
52 | | - ' North America) and fifth in population (after Asia, Africa,' + |
53 | | - ' Europe, and North America). The word America was coined in 1507 by' + |
54 | | - ' cartographers Martin Waldseemüller and Matthias Ringmann, after' + |
55 | | - ' Amerigo Vespucci, who was the first European to suggest that the' + |
56 | | - ' lands newly discovered by Europeans were not India, but a New World' + |
57 | | - ' unknown to Europeans.', |
| 27 | +interface Country { |
| 28 | + name: string; |
| 29 | + capital?: string; |
| 30 | + independence?: Date; |
| 31 | + population?: number; |
| 32 | +} |
| 33 | + |
| 34 | +const data: Continent = { |
| 35 | + name: "South America", |
| 36 | + summary: |
| 37 | + "South America (Spanish: América del Sur, Sudamérica or Suramérica;" + |
| 38 | + " Portuguese: América do Sul; Quechua and Aymara: Urin Awya Yala;" + |
| 39 | + " Guarani: Ñembyamérika; Dutch: Zuid-Amerika; French: Amérique du Sud)" + |
| 40 | + " is a continent situated in the Western Hemisphere, mostly in the" + |
| 41 | + " Southern Hemisphere, with a relatively small portion in the Northern" + |
| 42 | + " Hemisphere. The continent is also considered a subcontinent of the" + |
| 43 | + " Americas.[2][3] It is bordered on the west by the Pacific Ocean and" + |
| 44 | + " on the north and east by the Atlantic Ocean; North America and the" + |
| 45 | + " Caribbean Sea lie to the northwest. It includes twelve countries: " + |
| 46 | + "Argentina, Bolivia, Brazil, Chile, Colombia, Ecuador, Guyana, Paraguay" + |
| 47 | + ", Peru, Suriname, Uruguay, and Venezuela. The South American nations" + |
| 48 | + " that border the Caribbean Sea—including Colombia, Venezuela, Guyana," + |
| 49 | + " Suriname, as well as French Guiana, which is an overseas region of" + |
| 50 | + " France—are also known as Caribbean South America. South America has" + |
| 51 | + " an area of 17,840,000 square kilometers (6,890,000 sq mi)." + |
| 52 | + " Its population as of 2005 has been estimated at more than 371,090,000." + |
| 53 | + " South America ranks fourth in area (after Asia, Africa, and" + |
| 54 | + " North America) and fifth in population (after Asia, Africa," + |
| 55 | + " Europe, and North America). The word America was coined in 1507 by" + |
| 56 | + " cartographers Martin Waldseemüller and Matthias Ringmann, after" + |
| 57 | + " Amerigo Vespucci, who was the first European to suggest that the" + |
| 58 | + " lands newly discovered by Europeans were not India, but a New World" + |
| 59 | + " unknown to Europeans.", |
58 | 60 |
|
59 | | - surface: 17840000, |
60 | | - timezone: [-4, -2], |
61 | | - demographics: { |
62 | | - population: 385742554, |
63 | | - largestCities: [ |
64 | | - 'São Paulo', |
65 | | - 'Buenos Aires', |
66 | | - 'Rio de Janeiro', |
67 | | - 'Lima', |
68 | | - 'Bogotá', |
69 | | - ], |
70 | | - }, |
71 | | - languages: [ |
72 | | - 'spanish', |
73 | | - 'portuguese', |
74 | | - 'english', |
75 | | - 'dutch', |
76 | | - 'french', |
77 | | - 'quechua', |
78 | | - 'guaraní', |
79 | | - 'aimara', |
80 | | - 'mapudungun', |
81 | | - ], |
82 | | - countries: [ |
83 | | - { |
84 | | - name: 'Argentina', |
85 | | - capital: 'Buenos Aires', |
86 | | - independence: new Date(1816, 6, 9), |
87 | | - }, |
88 | | - { |
89 | | - name: 'Bolivia', |
90 | | - capital: 'La Paz', |
91 | | - independence: new Date(1825, 7, 6), |
92 | | - }, |
93 | | - { |
94 | | - name: 'Brazil', |
95 | | - capital: 'Brasilia', |
96 | | - independence: new Date(1822, 8, 7), |
97 | | - }, |
98 | | - { |
99 | | - name: 'Chile', |
100 | | - capital: 'Santiago', |
101 | | - independence: new Date(1818, 1, 12), |
102 | | - }, |
103 | | - { |
104 | | - name: 'Colombia', |
105 | | - capital: 'Bogotá', |
106 | | - independence: new Date(1810, 6, 20), |
107 | | - }, |
108 | | - { |
109 | | - name: 'Ecuador', |
110 | | - capital: 'Quito', |
111 | | - independence: new Date(1809, 7, 10), |
112 | | - }, |
113 | | - { |
114 | | - name: 'Guyana', |
115 | | - capital: 'Georgetown', |
116 | | - independence: new Date(1966, 4, 26), |
117 | | - }, |
118 | | - { |
119 | | - name: 'Paraguay', |
120 | | - capital: 'Asunción', |
121 | | - independence: new Date(1811, 4, 14), |
122 | | - }, |
123 | | - { |
124 | | - name: 'Peru', |
125 | | - capital: 'Lima', |
126 | | - independence: new Date(1821, 6, 28), |
127 | | - }, |
128 | | - { |
129 | | - name: 'Suriname', |
130 | | - capital: 'Paramaribo', |
131 | | - independence: new Date(1975, 10, 25), |
132 | | - }, |
133 | | - { |
134 | | - name: 'Uruguay', |
135 | | - capital: 'Montevideo', |
136 | | - independence: new Date(1825, 7, 25), |
137 | | - }, |
138 | | - { |
139 | | - name: 'Venezuela', |
140 | | - capital: 'Caracas', |
141 | | - independence: new Date(1811, 6, 5), |
142 | | - }, |
143 | | - ], |
| 61 | + surface: 17840000, |
| 62 | + timezone: [-4, -2], |
| 63 | + demographics: { |
| 64 | + population: 385742554, |
| 65 | + largestCities: [ |
| 66 | + "São Paulo", |
| 67 | + "Buenos Aires", |
| 68 | + "Rio de Janeiro", |
| 69 | + "Lima", |
| 70 | + "Bogotá", |
| 71 | + ], |
| 72 | + }, |
| 73 | + languages: [ |
| 74 | + "spanish", |
| 75 | + "portuguese", |
| 76 | + "english", |
| 77 | + "dutch", |
| 78 | + "french", |
| 79 | + "quechua", |
| 80 | + "guaraní", |
| 81 | + "aimara", |
| 82 | + "mapudungun", |
| 83 | + ], |
| 84 | + countries: [ |
| 85 | + { |
| 86 | + name: "Argentina", |
| 87 | + capital: "Buenos Aires", |
| 88 | + independence: new Date(1816, 6, 9), |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "Bolivia", |
| 92 | + capital: "La Paz", |
| 93 | + independence: new Date(1825, 7, 6), |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "Brazil", |
| 97 | + capital: "Brasilia", |
| 98 | + independence: new Date(1822, 8, 7), |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "Chile", |
| 102 | + capital: "Santiago", |
| 103 | + independence: new Date(1818, 1, 12), |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "Colombia", |
| 107 | + capital: "Bogotá", |
| 108 | + independence: new Date(1810, 6, 20), |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "Ecuador", |
| 112 | + capital: "Quito", |
| 113 | + independence: new Date(1809, 7, 10), |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "Guyana", |
| 117 | + capital: "Georgetown", |
| 118 | + independence: new Date(1966, 4, 26), |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "Paraguay", |
| 122 | + capital: "Asunción", |
| 123 | + independence: new Date(1811, 4, 14), |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "Peru", |
| 127 | + capital: "Lima", |
| 128 | + independence: new Date(1821, 6, 28), |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "Suriname", |
| 132 | + capital: "Paramaribo", |
| 133 | + independence: new Date(1975, 10, 25), |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "Uruguay", |
| 137 | + capital: "Montevideo", |
| 138 | + independence: new Date(1825, 7, 25), |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "Venezuela", |
| 142 | + capital: "Caracas", |
| 143 | + independence: new Date(1811, 6, 5), |
| 144 | + }, |
| 145 | + ], |
144 | 146 | }; |
145 | 147 |
|
146 | 148 | const left = JSON.parse(JSON.stringify(data), jsondiffpatch.dateReviver); |
147 | 149 |
|
148 | 150 | data.summary = data.summary |
149 | | - .replace('Brazil', 'Brasil') |
150 | | - .replace('also known as', 'a.k.a.'); |
151 | | -data.languages[2] = 'inglés'; |
| 151 | + .replace("Brazil", "Brasil") |
| 152 | + .replace("also known as", "a.k.a."); |
| 153 | +data.languages[2] = "inglés"; |
152 | 154 | data.countries.pop(); |
153 | 155 | data.countries.pop(); |
154 | | -data.countries[0].capital = 'Rawson'; |
| 156 | +const firstCountry = data.countries[0]; |
| 157 | +if (firstCountry) { |
| 158 | + firstCountry.capital = "Rawson"; |
| 159 | +} |
155 | 160 | data.countries.push({ |
156 | | - name: 'Antártida', |
| 161 | + name: "Antártida", |
157 | 162 | }); |
158 | 163 |
|
159 | 164 | // modify and move |
160 | | -data.countries[4].population = 42888594; |
161 | | -data.countries.splice(11, 0, data.countries.splice(4, 1)[0]); |
162 | | - |
163 | | -data.countries.splice(2, 0, data.countries.splice(7, 1)[0]); |
| 165 | +if (data.countries[4]) { |
| 166 | + data.countries[4].population = 42888594; |
| 167 | +} |
| 168 | +data.countries.splice(11, 0, data.countries.splice(4, 1)[0] as Country); |
| 169 | +data.countries.splice(2, 0, data.countries.splice(7, 1)[0] as Country); |
164 | 170 |
|
| 171 | +// biome-ignore lint/performance/noDelete: allowed for demo purposes |
165 | 172 | delete data.surface; |
166 | | -data.spanishName = 'Sudamérica'; |
| 173 | +data.spanishName = "Sudamérica"; |
167 | 174 | data.demographics.population += 2342; |
168 | 175 |
|
169 | 176 | const right = data; |
|
0 commit comments