Published
Edited
Oct 24, 2019
Insert cell
md`# Scratchpad`
Insert cell
viewof GIS_noMatch = html`<input type=file accept=".csv">`
Insert cell
GIS = {
let data = d3.csvParse(await Files.text(GIS_noMatch)).map(d => d.SSL);
return data;
}
Insert cell
viewof SSL_noMatch = html`<input type=file accept=".csv">`
Insert cell
SSL = {
let data = d3.csvParse(await Files.text(SSL_noMatch)).map(d => d.SSL);
return data;
}
Insert cell
function matches(GIS, SSL) {
let possible = SSL.slice();
function match(item) {
let match = item;
let matched = false;
const type1 = /^(\d{4})\s{4}/;
const type2 = /^\D{3}\s(\d{4})/;
const type3 = /^(\d{4})\D\s{3}/;
if (type1.test(item)) {
const block = item.match(type1)[1];
const regexp = new RegExp(`^${block}\\s{4}`);
let filtered = possible.filter(d => regexp.test(d));
if (filtered.length > 0) {
match = filtered[0];
_.remove(possible, d => d === match);
matched = true;
}
}
else if (type2.test(item)) {
const block = item.match(type2)[1];
const regexp = new RegExp(`^\\D{3}\\s${block}`);
let filtered = possible.filter(d => regexp.test(d));
if (filtered.length > 0) {
match = filtered[0];
_.remove(possible, d => d === match);
matched = true;
}
}
else if (type3.test(item)) {
const block = item.match(type3)[1];
const regexp = new RegExp(`^${block}\\s{3}`);
let filtered = possible.filter(d => regexp.test(d));
if (filtered.length > 0) {
match = filtered[0];
_.remove(possible, d => d === match);
matched = true;
}
}
return {match: match, matched: matched};
}
return GIS.map(d => {
let m = match(d);
return {original: d, match: m.match, matched: m.matched};
});
}
Insert cell
Insert cell
Insert cell
hits = remap.filter(d => d.matched).length / remap.length
Insert cell
d3 = require("d3@v5")
Insert cell
json2csv = require("json2csv")
Insert cell
function serialize (data) {
let parser = new json2csv.Parser();
let csv = parser.parse(data);
return new Blob([csv], {type: "text/csv"})
}
//from @palewire/saving-csv
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more