Skip to content

pantoken / packages/core/src / collectLeaves

Function: collectLeaves()

collectLeaves(obj, path?, out?): Leaf[]

Beta

Collect every leaf of a Tokens Studio tree. A leaf is a node with a .value: a string value yields one leaf; a composite object value (typography) yields one leaf per sub-property named <token>-<subProperty>; array values (box-shadow lists) are skipped.

Parameters

obj

unknown

path?

string[] = []

out?

Leaf[] = []

Returns

Leaf[]

Example

ts
import { collectLeaves } from "@pantoken/core";

const tree = {
  color: {
    white: { value: "#ffffff", type: "color" },
    hover: { value: "{color.white}", type: "color" },
  },
  typography: { body: { value: { fontFamily: "Lato", fontSize: "1rem", type: "typography" } } },
};

collectLeaves(tree).map((l) => [l.path.join("."), l.value]);
// → [
//   ["color.white", "#ffffff"],
//   ["color.hover", "{color.white}"],
//   ["typography.body.fontFamily", "Lato"],
//   ["typography.body.fontSize", "1rem"],
// ]