Jetons.DTCG (jetons v0.2.0)

Copy Markdown View Source

DTCG (Design Tokens Community Group) format understanding.

Handles the $-key conventions of the DTCG token format: flattening nested token maps, resolving $extends inheritance, and extracting $type, $description, and $deprecated metadata.

Summary

Functions

Resolves $extends inheritance in a config map.

Extracts $deprecated values from a raw config map.

Extracts $description values from a raw config map.

Flattens a nested token map into {path, value} tuples.

Extracts a map of {path => type} from a config, inheriting $type from parents.

Functions

apply_extends(config)

Resolves $extends inheritance in a config map.

Examples

iex> config = %{"colors" => %{
...>   "base" => %{"$value" => "#f00"},
...>   "extended" => %{"$extends" => "colors.base"}
...> }}
iex> resolved = Jetons.DTCG.apply_extends(config)
iex> resolved["colors"]["extended"]["$value"]
"#f00"

deprecated(config)

Extracts $deprecated values from a raw config map.

Returns a map of %{path => true | String.t()} for all tokens that are deprecated. $deprecated is inherited from parent groups — a child can override with false to opt out.

Examples

iex> Jetons.DTCG.deprecated(%{"colors" => %{"old" => %{"$value" => "#f00", "$deprecated" => true}}})
%{"colors.old" => true}

iex> Jetons.DTCG.deprecated(%{"colors" => %{"old" => %{"$value" => "#f00", "$deprecated" => "Use new instead"}}})
%{"colors.old" => "Use new instead"}

descriptions(config)

Extracts $description values from a raw config map.

Returns a map of %{path => description} for all tokens that have a $description key. Unlike $type, descriptions are NOT inherited by children.

Examples

iex> Jetons.DTCG.descriptions(%{"colors" => %{"red" => %{"$value" => "#f00", "$description" => "Red color"}}})
%{"colors.red" => "Red color"}

iex> Jetons.DTCG.descriptions(%{"colors" => %{"red" => %{"$value" => "#f00"}}})
%{}

flatten(map, prefix)

Flattens a nested token map into {path, value} tuples.

Examples

iex> Jetons.DTCG.flatten(%{"red" => %{"$value" => "#f00"}, "blue" => %{"$value" => "#00f"}}, "colors") |> Enum.sort()
[{"colors.blue", "#00f"}, {"colors.red", "#f00"}]

type_map(config)

Extracts a map of {path => type} from a config, inheriting $type from parents.

Examples

iex> Jetons.DTCG.type_map(%{"colors" => %{"$type" => "color", "red" => %{"$value" => "#f00"}}})
%{"colors.red" => "color"}