UNPKG

@tanstack/charts

Version:

A chart grammar for TypeScript and JavaScript. Marks consume your data directly, channels describe visual encodings, and the engine compiles them into a renderer-neutral keyed scene. TanStack's compact scales cover common numeric and categorical mappings.

37 lines (36 loc) 1.05 kB
import { toArray } from "./transform-internal.js"; function fold(source, options) { const data = toArray(source); const keyName = options.as?.key ?? "key"; const valueName = options.as?.value ?? "value"; assertFoldOptions(options.fields, keyName, valueName); return data.flatMap( (datum, sourceIndex) => options.fields.map((field) => ({ ...datum, source: [datum], sourceIndexes: [sourceIndex], [keyName]: field, [valueName]: datum[field] })) ); } function assertFoldOptions(fields, keyName, valueName) { if (keyName === valueName) { throw new TypeError("fold: output names must be distinct"); } for (const name of [keyName, valueName]) { if (name === "source" || name === "sourceIndexes") { throw new TypeError(`fold: output name "${name}" is reserved`); } } const seen = /* @__PURE__ */ new Set(); for (const field of fields) { if (seen.has(field)) { throw new TypeError(`fold: duplicate field "${field}"`); } seen.add(field); } } export { fold };