Public
Edited
Jul 6, 2024
Importers
3 stars
Insert cell
Insert cell
Insert cell
price = amount => {
const text = amount.toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0
});
const element = html`${text}`;
element.value = amount;
return element;
}
Insert cell
percentage = (amount, minimumFractionDigits = 2) => {
const text = amount.toLocaleString('en-US', {
style: 'percent',
minimumFractionDigits
});
const element = html`${text}`;
element.value = amount;
return element;
}
Insert cell
Insert cell
range = number => {
return [...Array(number).keys()].map(i => i + 1);
}
Insert cell
Insert cell
objectToArray = (obj, [keyName, valueName] = ['sku', 'quantity']) =>
Object.entries(obj).map(([key, value]) => ({
[keyName]: key,
[valueName]: value
}))
Insert cell
flattenObject = flatten
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
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]) => {
const pair = `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
if (!value) return result;
if (result === '?') return `${result}${pair}`;
return `${result}&${pair}`;
}, '?');
}
Insert cell
Insert cell
formatPrice = value =>
parseFloat(value).toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2,
maximumFractionDigits: 2
})
Insert cell
formatDate = value => {
const date = new Date(value);
return date.toLocaleDateString('en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit'
});
}
Insert cell
toPascalCase = (text) =>
text.replace(/(\w)(\w*)/g, function (g0, g1, g2) {
return g1.toUpperCase() + g2.toLowerCase();
})
Insert cell
(String.prototype.toPascalCase = function() {
return this.replace(new RegExp(/[-_]+/, 'g'), ' ')
.replace(new RegExp(/[^\w\s]/, 'g'), '')
.replace(
new RegExp(/\s+(.)(\w+)/, 'g'),
($1, $2, $3) => `${$2.toUpperCase() + $3.toLowerCase()}`
)
.replace(new RegExp(/\s/, 'g'), '')
.replace(new RegExp(/\w/), s => s.toLowerCase());
})
Insert cell
Insert cell
random = (min, max) => Math.floor(Math.random() * (max - min) + min)
Insert cell
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
Insert cell
import { table } from "@gnestor/table"
Insert cell
import {
composeTable,
currencyColumn,
percentageColumn,
numberColumn,
withFormulae,
withCheckbox,
withHtml
} from "@gnestor/table-utilities"
Insert cell
d3 = require('d3')
Insert cell
import { vl } from '@vega/vega-lite-api'
Insert cell
import { copy } from '@mbostock/copy-to-clipboard'
Insert cell
import { localStorage } from '@mbostock/safe-local-storage'
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