Public
Edited
Sep 21, 2024
3 forks
5 stars
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
query = Object.fromEntries(new URLSearchParams(location.search))
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
/**
* Constructs a new URL query string from any number of query objects.
*/
class Query extends URLSearchParams {
/**
* Constructs a new URL query string.
* @param queries - The objects containing the query parameters.
*/
constructor(url, ...queries) {
super()
this.url = url
for (const [key, value] of Object.entries(Object.assign({}, ...queries))) {
if (value === undefined || value === null) continue
this.set(key, value.toString())
}
}
add(key, value) {
super.append(key, value.toString())
return this
}
toString() {
return this.url + "?" + super.toString()
}
}
Insert cell
/**
* Formats a date object into the US date format.
* @param {Date} date The date object to format
* @returns A string in the format of `MM/DD/YYYY`
*/
function toUSLocale(date) {
return new Intl.DateTimeFormat("en-US", {
year: "2-digit",
month: "numeric",
day: "2-digit"
}).format(new Date(date))
}
Insert cell
/**
* Formats a number by adding commas as thousand separators.
* @param {number} n - The number to be formatted.
* @returns {string} The formatted number as a string.
*/
function formatThousand(n) {
return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
Insert cell
/**
* Calculates the number of times n can be divided by b, optionally excluding b from the range.
* @param {number} n - The dividend.
* @param {number} b - The divisor.
* @param {boolean} [exclusive=false] - Whether to exclude b from the end of the range.
* @returns {number} The quotient of n divided by b.
*/
function qmod(n, b, exclusive = false) {
// Calculate the bias for excluding b from the end of the range
const epsilon = 0.0001;
const bias = exclusive * epsilon;

// Calculate the number of times b fits into n
return Math.ceil((n - bias % b) / b);
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more