Initializing playground…
← Back to roadmap
attrs: user-defined metadata
← tsb playground
Basic usage
TypeScript
▶ Run
↺ Reset
import { getAttrs, setAttrs, updateAttrs, copyAttrs, withAttrs, clearAttrs, hasAttrs, getAttr, setAttr, deleteAttr, attrsCount, attrsKeys, mergeAttrs, } from "tsb"; import { DataFrame, Series } from "tsb"; // ─── annotate a DataFrame ───────────────────────────────────────────────── const df = DataFrame.fromColumns({ temperature: [22.1, 23.5, 21.8], humidity: [55, 60, 58], }); setAttrs(df, { source: "weather_station_42", unit: "Celsius", notes: "Morning readings", }); console.log(getAttrs(df)); // → { source: "weather_station_42", unit: "Celsius", notes: "Morning readings" } console.log(getAttr(df, "unit")); // → "Celsius" console.log(getAttr(df, "missing")); // → undefined console.log(attrsCount(df)); // → 3 console.log(attrsKeys(df)); // → ["source", "unit", "notes"] console.log(hasAttrs(df)); // → true
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Merging and updating
TypeScript
▶ Run
↺ Reset
// updateAttrs merges new keys, preserves existing console.log(updateAttrs(df, { version: 2, notes: "Updated notes" })); console.log(getAttrs(df)); // → { source: "weather_station_42", unit: "Celsius", notes: "Updated notes", version: 2 } // setAttr / deleteAttr for single keys console.log(setAttr(df, "sensor_id", "WS-042")); console.log(deleteAttr(df, "notes")); console.log(getAttrs(df)); // → { source: "weather_station_42", unit: "Celsius", version: 2, sensor_id: "WS-042" }
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Propagating metadata to derived objects
TypeScript
▶ Run
↺ Reset
// copyAttrs: copy all attrs from one object to another const s = new Series({ data: [22.1, 23.5, 21.8], name: "temperature" }); console.log(setAttrs(s, { unit: "Celsius", source: "sensor_A" })); const derived = new Series({ data: [71.8, 74.3, 71.2], name: "fahrenheit" }); console.log(copyAttrs(s, derived)); console.log(getAttrs(derived)); // → { unit: "Celsius", source: "sensor_A" } // Then update the copy console.log(setAttr(derived, "unit", "Fahrenheit")); console.log(getAttrs(derived)); // → { unit: "Fahrenheit", source: "sensor_A" } console.log(getAttrs(s)); // → { unit: "Celsius", source: "sensor_A" } ← unchanged
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Fluent helper — withAttrs
TypeScript
▶ Run
↺ Reset
// withAttrs sets attrs and returns the same object reference // Handy for inline annotation const annotated = withAttrs( DataFrame.fromColumns({ x: [1, 2, 3] }), { source: "lab_experiment", date: "2026-04-09" }, ); annotated === annotated; // true — same reference, not a copy console.log(getAttrs(annotated)); // → { source: "lab_experiment", date: "2026-04-09" }
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Merging from multiple sources
TypeScript
▶ Run
↺ Reset
// mergeAttrs: combine attrs from multiple objects into a target const s1 = new Series({ data: [1, 2, 3], name: "a" }); const s2 = new Series({ data: [4, 5, 6], name: "b" }); console.log(setAttrs(s1, { source: "sensor_A", unit: "kg" })); console.log(setAttrs(s2, { source: "sensor_B", scale: 2.5 })); const combined = DataFrame.fromColumns({ a: [1, 2, 3], b: [4, 5, 6] }); console.log(mergeAttrs([s1, s2], combined)); // Later sources win on conflicts: source="sensor_B" console.log(getAttrs(combined)); // → { source: "sensor_B", unit: "kg", scale: 2.5 }
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Clearing metadata
TypeScript
▶ Run
↺ Reset
console.log(setAttrs(df, { x: 1, y: 2 })); console.log(hasAttrs(df)); // → true console.log(attrsCount(df)); // → 2 console.log(clearAttrs(df)); console.log(hasAttrs(df)); // → false console.log(getAttrs(df)); // → {}
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent