dataprep/prep

Types

Prep(a) is an infallible transformation: fn(a) -> a. It always succeeds and never produces errors.

pub type Prep(a) =
  fn(a) -> a

Values

pub fn collapse_space() -> fn(String) -> String

Collapse consecutive whitespace into a single space.

Uses let assert for the regex compilation. The pattern \s+ is a fixed, known-valid regular expression, so compilation cannot fail at runtime. The assert is intentional and safe.

pub fn default(fallback: String) -> fn(String) -> String

Replace the value with fallback when the input is exactly “”.

Note: this checks the literal empty string only. Whitespace-only inputs like “ “ are NOT treated as empty. If you want whitespace-only values to trigger the default, compose with trim:

prep.trim() |> prep.then(first: _, next: prep.default(“N/A”))

pub fn identity() -> fn(a) -> a

No-op prep. Returns the value unchanged.

pub fn lowercase() -> fn(String) -> String

Convert to lowercase.

pub fn replace(
  target target: String,
  replacement replacement: String,
) -> fn(String) -> String

Replace all occurrences of target with replacement.

pub fn sequence(steps: List(fn(a) -> a)) -> fn(a) -> a

Compose a list of preps into a single prep. Empty list returns identity.

pub fn then(
  first p1: fn(a) -> a,
  next p2: fn(a) -> a,
) -> fn(a) -> a

Sequential composition: apply p1, then apply p2 to the result.

pub fn trim() -> fn(String) -> String

Trim leading and trailing whitespace.

pub fn uppercase() -> fn(String) -> String

Convert to uppercase.

Search Document