View Source Vectoree.TreePath (Vectoree v0.0.2)

A canonical path implementation for tree structures.

The functions in this module handle TreePath structs, which encapsulate path segments in a list in reverse order. There is no distinction between absolute and relative paths. Printable representations of such a struct use the dot . character as separator between segments.

Developers should avoid creating the TreePath struct directly and instead rely on the functions provided by this module, including the provided sigil macros.

Summary

Functions

Returns a new struct with the segment being appended on the given path.

Returns a new struct which wraps the base segment of the given path.

Returns the base segment name of the given path as a BitString.

Checks if a path ends with the given suffix.

Returns the level of the path.

Creates a new struct from a singular segment or a list of segments.

Returns a new struct which wraps the parent segment of the given path.

Returns a new struct which wraps the root segment of the given path.

Returns the root segment name of the given path as a BitString.

Returns the path separator character as a BitString.

Returns the path separator character replacement as a BitString.

Returns a new struct which wraps a sibling of the given path.

Handles the sigil ~p for tree paths.

Checks if a path starts with the given prefix.

Creates a new struct by wrapping the provided list of segments.

Types

@type t() :: %Vectoree.TreePath{segments: [String.t()]}

Functions

@spec append(t(), String.t()) :: t()
@spec append(t(), t()) :: t()

Returns a new struct with the segment being appended on the given path.

Examples

iex> Vectoree.TreePath.append(~p"", "data")
%Vectoree.TreePath{segments: ["data"]}

iex> Vectoree.TreePath.append(~p"data.lore", "b4")
%Vectoree.TreePath{segments: ["b4", "lore", "data"]}

iex> Vectoree.TreePath.append(~p"data.lore", ~p"b4.soong")
%Vectoree.TreePath{segments: ["soong", "b4", "lore", "data"]}
@spec base(t()) :: t()

Returns a new struct which wraps the base segment of the given path.

Examples

iex> Vectoree.TreePath.base(~p"")
%Vectoree.TreePath{segments: []}

iex> Vectoree.TreePath.base(~p"data")
%Vectoree.TreePath{segments: ["data"]}

iex> Vectoree.TreePath.base(~p"data.lore.b4")
%Vectoree.TreePath{segments: ["b4"]}
@spec basename(t()) :: String.t()

Returns the base segment name of the given path as a BitString.

Examples

iex> Vectoree.TreePath.basename(~p"")
""

iex> Vectoree.TreePath.basename(~p"data")
"data"

iex> Vectoree.TreePath.basename(~p"data.lore.b4")
"b4"
Link to this function

ends_with?(tree_path, suffix)

View Source
@spec ends_with?(t(), String.t() | t()) :: boolean()

Checks if a path ends with the given suffix.

Examples

iex> Vectoree.TreePath.ends_with?(~p"data.lore.b4", "b4")
true

iex> Vectoree.TreePath.ends_with?(~p"data.lore.b4", "lore")
false

iex> Vectoree.TreePath.ends_with?(~p"data.lore.b4", ~p"lore.b4")
true
@spec level(t()) :: integer()

Returns the level of the path.

The level corresponds to the number of path segments.

@spec new(segment) :: t() when segment: String.t()
@spec new(segments) :: t() when segments: list()

Creates a new struct from a singular segment or a list of segments.

An empty singular segment results in a struct with zero segments. Lists of segments are filtered for empty elements. An empty list, also as a consequence after filtering, results in a struct with zero segments. Whitespace on each segment is preserved.

Examples

By passing a singular segment:

iex> Vectoree.TreePath.new("data")
%Vectoree.TreePath{segments: ["data"]}

iex> Vectoree.TreePath.new("  da ta  ")
%Vectoree.TreePath{segments: ["  da ta  "]}

iex> Vectoree.TreePath.new("  ")
%Vectoree.TreePath{segments: ["  "]}

iex> Vectoree.TreePath.new("")
%Vectoree.TreePath{segments: []}

By passing a list of segments:

iex> Vectoree.TreePath.new([])
%Vectoree.TreePath{segments: []}

iex> Vectoree.TreePath.new(["data"])
%Vectoree.TreePath{segments: ["data"]}

iex> Vectoree.TreePath.new(["  data  ", "lo  re", "b4"])
%Vectoree.TreePath{segments: ["b4", "lo  re", "  data  "]}
@spec parent(t()) :: t()

Returns a new struct which wraps the parent segment of the given path.

Examples

iex> Vectoree.TreePath.parent(~p"")
%Vectoree.TreePath{segments: []}

iex> Vectoree.TreePath.parent(~p"data")
%Vectoree.TreePath{segments: []}

iex> Vectoree.TreePath.parent(~p"data.lore.b4")
%Vectoree.TreePath{segments: ["lore", "data"]}
@spec root(t()) :: t()

Returns a new struct which wraps the root segment of the given path.

Examples

iex> Vectoree.TreePath.root(~p"")
%Vectoree.TreePath{segments: []}

iex> Vectoree.TreePath.root(~p"data")
%Vectoree.TreePath{segments: ["data"]}

iex> Vectoree.TreePath.root(~p"data.lore.b4")
%Vectoree.TreePath{segments: ["data"]}
@spec rootname(t()) :: String.t()

Returns the root segment name of the given path as a BitString.

Examples

iex> Vectoree.TreePath.rootname(~p"")
""

iex> Vectoree.TreePath.rootname(~p"data")
"data"

iex> Vectoree.TreePath.rootname(~p"data.lore.b4")
"data"
@spec separator() :: String.t()

Returns the path separator character as a BitString.

@spec separator_replacement() :: String.t()

Returns the path separator character replacement as a BitString.

The replacement is built using the underscore character _ followed by the Base64 encoded form of the separator character.

@spec sibling(t(), String.t()) :: t()

Returns a new struct which wraps a sibling of the given path.

Examples

iex> Vectoree.TreePath.sibling(~p"data.lore", "b4")
%Vectoree.TreePath{segments: ["b4", "data"]}

iex> Vectoree.TreePath.sibling(~p"", "b4")
%Vectoree.TreePath{segments: ["b4"]}
Link to this macro

sigil_p(arg, list)

View Source (macro)

Handles the sigil ~p for tree paths.

Examples

iex> ~p""
%Vectoree.TreePath{segments: []}

iex> ~p"data.lore.b4"
%Vectoree.TreePath{segments: ["b4", "lore", "data"]}

iex> x = "or"
iex> ~p"da#{:t}a.l#{x}e.b4"
%Vectoree.TreePath{segments: ["b4", "lore", "data"]}
Link to this function

starts_with?(tree_path, prefix)

View Source
@spec starts_with?(t(), String.t() | t()) :: boolean()

Checks if a path starts with the given prefix.

Examples

iex> Vectoree.TreePath.starts_with?(~p"data.lore.b4", "data")
true

iex> Vectoree.TreePath.starts_with?(~p"data.lore.b4", "lore")
false

iex> Vectoree.TreePath.starts_with?(~p"data.lore.b4", ~p"data.lore")
true
@spec wrap(segments) :: t() when segments: list()

Creates a new struct by wrapping the provided list of segments.

The given list is taken as-is, i.e. without any filtering and by expecting it to be already in reversed order.

Examples

Here wrapping a path named "data.lore.b4":

iex> Vectoree.TreePath.wrap(["b4", "lore", "data"])
%Vectoree.TreePath{segments: ["b4", "lore", "data"]}