Published
Edited
Aug 2, 2020
Importers
Insert cell
Insert cell
toPascalCase = text =>
text.replace(
/(\w)(\w*)/g,
(g0, g1, g2) => g1.toUpperCase() + g2.toLowerCase()
)
Insert cell
function flatten(obj) {
let arr = [];
for (var i in obj) {
if (typeof obj[i] !== 'object' || Array.isArray(obj[i])) {
arr.push(obj[i]);
} else {
arr.push(flatten(obj[i]));
}
}
return arr.flat();
}
Insert cell
range = number => {
return [...Array(number).keys()].map(i => i + 1);
}
Insert cell
queryStringToObject = query => {
if (!query) return {};
return query
.substring(1)
.split('&')
.reduce((result, item) => {
const [key, value] = item.split('=');
try {
return { ...result, [key]: JSON.parse(decodeURIComponent(value)) };
} catch (error) {
return { ...result, [key]: decodeURIComponent(value) };
}
}, {});
}
Insert cell
objectToQueryString = obj => {
return Object.entries(obj).reduce(
(result, [key, value]) =>
`${result}${result ? '&' : ''}${encodeURIComponent(
key
)}=${encodeURIComponent(value)}`,
'?'
);
}
Insert cell
searchQuery = queryStringToObject(window.location.search)
Insert cell
parseNotebookUrl = url => {
const [_, user, notebook, search] = url.match(
/https:\/\/observablehq.com\/(\S+)\/([^?\s]+)(\?.*)?/
);
return { user, notebook, query: queryStringToObject(search) };
}
Insert cell
notebookUrl = parseNotebookUrl(document.baseURI)
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