-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform.js
150 lines (139 loc) · 4.45 KB
/
transform.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const first = require("lodash.first");
const dayjs = require("dayjs");
const customParseFormat = require("dayjs/plugin/customParseFormat");
dayjs.extend(customParseFormat);
/**
* @desc Corrects missing accented chars in Rutgers' data
* @param {string} rawString
* @return {string} - correctedString
*/
const cleanAccentedString = (rawString) => {
return rawString
.replace("Besan?on", "Besançon")
.replace("Br?nn", "Brünn")
.replace("C?teaux", "Cîteaux")
.replace("Cogl?s", "Coglès")
.replace("H?xter", "Hüxter")
.replace("L?beck", "Lübeck")
.replace("l'?cu", "l'écu")
.replace("Li?ge", "Liège")
.replace("Lw?w", "Lwów")
.replace("R?mi", "Rémi")
.replace("Th?rouanne", "Thérouanne")
.replace("Z?rich", "Zürich")
.replace(/([^a-zA-Z])\?cu([^a-z])/, "$1écu$2")
.replace(/(\s)\?(cus\s)/, "$1é$2") // " ?cus " -> " écus "
.replace(/\s\?\s/, " à ");
};
/**
* @param {string} dateStr
* @return {Date|string} - Date object, or string if date could not be parsed
*/
const cleanDate = (dateStr) => {
return !dayjs(dateStr, ["YYYY", "YYYY.MM.DD", "YYYY.MM"], true).isValid()
? dateStr
: dayjs(dateStr, ["YYYY", "YYYY.MM.DD", "YYYY.MM"], true).toDate();
};
/**
* @desc Store "invalid-but-acceptable" status for certain fields in DB
* @param {Object} record
* @return {string[]} keys of a record's invalid fields
*/
const getInvalidSpufFields = (record = {}) =>
[
["Date (Start)", dayjs(record["Date (Start)"]).isValid()],
["Date (End)", dayjs(record["Date (End)"]).isValid()],
["Amount (From)", record["Amount (From)"] >= 0],
["Amount (To)", record["Amount (To)"] >= 0],
]
.filter(([_, v]) => !v)
.map(([k, _]) => k);
/**
* @desc Cleans a Spufford Currency Exchanges record
* @param {Object|string[]} srcRecord - original record
* @return {Object|null} - cleaned record plus some new fields
*/
function cleanSpuffordRecord(srcRecord = {}) {
try {
const [
num,
place,
startDate,
endDate,
exchangeType,
currFrom,
amtFromAndCurrTo,
amtTo,
notes,
source,
] = Object.values(srcRecord).map((v) => v.trim());
let record = {};
record["_rawEntry"] = JSON.stringify(srcRecord, null, 2);
record["Num"] = parseInt(num, 10);
record["Place"] = cleanAccentedString(place);
record["Date (Start)"] = cleanDate(startDate);
record["Date (End)"] = cleanDate(endDate);
record["Type of Exchange"] = exchangeType || "";
record["Currency (From)"] = cleanAccentedString(currFrom);
const amtFrom = first(amtFromAndCurrTo.match(/^[0-9]+/)) || "";
record["Amount (From)"] = !amtFrom
? -1
: !Number.isNaN(+amtFrom)
? +amtFrom
: -1;
const currTo = amtFromAndCurrTo.slice(amtFrom.length);
record["Currency (To)"] = cleanAccentedString(currTo).trim();
record["Amount (To)"] = !Number.isNaN(+amtTo) ? +amtTo : -1;
record["Notes"] = notes || "";
record["Source"] = source || "";
record["Modified"] = new Date();
record["Invalid Fields"] = getInvalidSpufFields(record);
return record;
} catch (e) {
console.warn("Error transforming srcRecord: ", e.message);
return null;
}
}
/**
* @desc Cleans a Posthumus Prices record
* @param {Object|string[]} srcRecord - original record
* @return {Object|null} - cleaned record plus some new fields
*/
const cleanPosthumusRecord = (srcRecord = {}) => {
try {
const [
num,
year,
month,
productEnglish,
productDutch,
volume,
currencyAndPrice,
notes = -1,
] = Object.values(srcRecord).map((v) => v.trim());
let record = {};
record["_rawEntry"] = JSON.stringify(srcRecord, null, 2);
record["Num"] = parseInt(num, 10);
record["Year"] = parseInt(year, 10);
record["Month"] = parseInt(month, 10);
record["ProductEnglish"] = productEnglish;
record["ProductDutch"] = productDutch;
record["Volume"] = volume;
record["Notes"] = parseInt(notes, 10);
const [rawCurrency = "", rawPrice = -1] = currencyAndPrice.split(/\s/, 2);
record["Currency"] = rawCurrency;
record["Price"] = !Number.isNaN(+rawPrice) ? +rawPrice : -1;
record["Modified"] = new Date();
return record;
} catch (e) {
console.warn("Posthumus prices: Error transforming srcRecord: ", e.message);
return null;
}
};
const cleanRecord = {
spuffordCurrency: cleanSpuffordRecord,
posthumusPrices: cleanPosthumusRecord,
};
module.exports = {
cleanRecord,
};