Published
Edited
Jan 17, 2022
5 forks
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = {
const lock = lockfile.parse(await (file ?? sample).text());
if (lock.type !== "success") throw new Error("unable to parse yarn.lock");
const nodes = [];
const edges = [];
const edgeSet = new Set;

for (const [key, value] of Object.entries(lock.object)) {
const name = key.substring(0, key.indexOf("@", 1));
const source = `${name}@${value.version}`;
nodes.push(source);
for (const [depkey, depvalue] of Object.entries({...value.optionalDependencies, ...value.dependencies})) {
const targetKey = `${depkey}@${depvalue}`;
const targetObject = lock.object[targetKey];
const target = targetObject ? `${depkey}@${targetObject.version}` : targetKey;
const edgeKey = `${source}\t${target}`
if (!edgeSet.has(edgeKey)) edgeSet.add(edgeKey), edges.push([source, target]);
}
}

const roots = new Set(nodes);
for (const [source, target] of edges) {
roots.delete(target);
}
for (const root of roots) {
edges.push(["(root)", root]);
}

return {nodes, edges};
}
Insert cell
sample = FileAttachment("yarn.lock")
Insert cell
lockfile = require(await FileAttachment("lockfile-1.1.0.js").url())
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