Public
Edited
Nov 23, 2022
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
slashes = rawText.split("").filter((s) => ["/", "\\"].includes(s))
Insert cell
lines = {
let lines = slashes
.map((c, i, arr) => {
return { char: c };
})
.reduce(
(acc, cur, index, arr) => {
const { cursor, prev } = acc;

// If the previos character and current is different
// don't have shift. Else, get offset.
const offset = cur.char !== prev ? 0 : getOffset(cur.char);

const nextCursor = cursor + offset;
return {
cursor: nextCursor,
prev: cur.char,
lines: [...acc.lines, { ...cur, offset: nextCursor }]
};
},
{ cursor: 0, prev: null, lines: [] }
);

lines = lines.lines;

// Get the lowest negative offset, if any.
let min = _.minBy(lines, (l) => l.offset);
let minOffset = min != null ? min.offset : 0;
minOffset = minOffset < 0 ? -1 * minOffset : 0;

// Offset the 'negative offset'
lines = lines.map((l) => ({ ...l, offset: l.offset + minOffset }));

return lines;
}
Insert cell
getOffset("r")
Insert cell
getOffset("\\")
Insert cell
getOffset("/")
Insert cell
function getOffset(char) {
if (char === "/") {
return -1;
} else if (char == "\\") {
return 1;
}
return 0;
}
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