Published
Edited
Jun 25, 2020
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
mutable cleanData = rawData => {
const domainRegExp = RegExp('(http:\/\/|https:\/\/)(www.)?([a-z.]+)');

const pathRegExp = RegExp(
'(https:\/\/)(www\.)?([a-zA-Z0-9\.?=\-]+)\/([a-zA-Z0-9\.?=\-]+)\/?$'
);

return rawData
.filter(
d =>
pathRegExp.test(d['Link URL']) &&
domainRegExp.test(d['Referring Page URL'])
)
.map(
d =>
new Object({
title: _.startCase(_.camelCase(pathRegExp.exec(d['Link URL'])[4])),
'Referring Domain': domainRegExp.exec(d['Referring Page URL'])[3],
'First Seen Date': d['First Seen'].split(' ')[0],
'Last Checked Date': d['First Seen'].split(' ')[0],
...d
})
);
}
Insert cell
groupByTitle = cleanData =>
d3
.groups(cleanData, d => d.title, d => d['Referring Domain'])
.map(([title, domains]) => ({
title,
length: domains.length,
domains: domains
}))
.slice()
.sort((a, b) => d3.descending(a.length, b.length))
Insert cell
groupByRef = cleanData =>
d3
.groups(cleanData, d => d['Referring Domain'], d => d.title)
.map(([title, domains]) => ({
title,
length: domains.length,
domains: domains
}))
.slice()
.sort((a, b) => d3.descending(a.length, b.length))
Insert cell
Insert cell
Insert cell
buildNodes = (rawData, slice) => {
const group = groupByTitle(cleanData(rawData));

const articleNodes = group
.slice(0, slice)
.map(d => new Object({ id: d.title, group: 1 }));

const domainNodes = _.uniqBy(
group
.slice(0, slice)
.map(d =>
d.domains.map(i =>
i[0] === "campusreform.org" || i[0] === "thecollegefix.com"
? new Object({ id: i[0], group: 2 })
: new Object({ id: i[0], group: 3 })
)
)
.flat(),
'id'
);

return [...articleNodes, ...domainNodes];
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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