gossamer/string

Extensions to gleam/string for JS-specific operations — Unicode normalization, locale-aware comparison and case conversion, code point manipulation, and the JS string methods not covered by Gleam’s standard library.

Values

pub fn at(
  string: String,
  index index: Int,
) -> Result(String, Nil)

Returns the UTF-16 code unit at index as a single-character string, or Error(Nil) if the index is out of range. Negative indices count from the end.

pub fn char_code_at(
  string: String,
  index index: Int,
) -> Result(Int, Nil)

Returns the UTF-16 code unit at index, or Error(Nil) if the index is out of range.

pub fn code_point_at(
  string: String,
  index index: Int,
) -> Result(Int, Nil)

Returns the Unicode code point at index, or Error(Nil) if the index is out of range.

pub fn concat(string: String, and other: String) -> String
pub fn ends_with(string: String, suffix suffix: String) -> Bool
pub fn ends_with_within(
  string: String,
  suffix suffix: String,
  within length: Int,
) -> Bool
pub fn from_char_code(code: Int) -> String
pub fn from_char_codes(codes: List(Int)) -> String
pub fn from_code_point(
  code: Int,
) -> Result(String, js_error.JsError)

Returns an error if the code point is invalid.

pub fn from_code_points(
  codes: List(Int),
) -> Result(String, js_error.JsError)

Returns an error if any code point is invalid.

pub fn includes(in string: String, search search: String) -> Bool
pub fn includes_from(
  in string: String,
  search search: String,
  from position: Int,
) -> Bool
pub fn index_of(
  in string: String,
  search search: String,
) -> Result(Int, Nil)

Returns the first index of search in the string, or Error(Nil) if not found.

pub fn index_of_from(
  in string: String,
  search search: String,
  from position: Int,
) -> Result(Int, Nil)

Like index_of, but starts searching from position.

pub fn is_well_formed(string: String) -> Bool
pub fn last_index_of(
  in string: String,
  search search: String,
) -> Result(Int, Nil)

Returns the last index of search in the string, or Error(Nil) if not found.

pub fn last_index_of_from(
  in string: String,
  search search: String,
  from position: Int,
) -> Result(Int, Nil)

Like last_index_of, but searches backwards from position.

pub fn length(of string: String) -> Int

Returns the number of UTF-16 code units. This differs from gleam/string.length which counts grapheme clusters.

pub fn locale_compare(
  string: String,
  to other: String,
) -> order.Order
pub fn normalize(string: String) -> String

Returns the NFC-normalized form of the string.

pub fn normalize_with(
  string: String,
  form form: normalization_form.NormalizationForm,
) -> String
pub fn pad_end(
  string: String,
  to target_length: Int,
  with pad: String,
) -> Result(String, js_error.JsError)

Pads the end to reach target_length UTF-16 code units. This differs from gleam/string.pad_end which counts grapheme clusters. Returns an error if the padded string would exceed the maximum string length.

pub fn pad_start(
  string: String,
  to target_length: Int,
  with pad: String,
) -> Result(String, js_error.JsError)

Pads the start to reach target_length UTF-16 code units. This differs from gleam/string.pad_start which counts grapheme clusters. Returns an error if the padded string would exceed the maximum string length.

pub fn repeat(
  string: String,
  times times: Int,
) -> Result(String, js_error.JsError)

Returns the string repeated times times. Returns an error if times is negative or the resulting string would exceed the maximum string length.

pub fn replace(
  in string: String,
  pattern pattern: String,
  with replacement: String,
) -> String

Replaces the first occurrence only. Use replace_all to replace every occurrence.

pub fn replace_all(
  in string: String,
  pattern pattern: String,
  with replacement: String,
) -> String
pub fn slice(
  string: String,
  from start: Int,
  to end: Int,
) -> String

Extracts a section of the string between start and end. Indices are UTF-16 code units — this differs from gleam/string.slice which counts grapheme clusters.

pub fn split(
  string: String,
  on separator: String,
) -> List(String)
pub fn split_with_limit(
  string: String,
  on separator: String,
  limit limit: Int,
) -> List(String)
pub fn starts_with(string: String, prefix prefix: String) -> Bool
pub fn starts_with_from(
  string: String,
  prefix prefix: String,
  from position: Int,
) -> Bool
pub fn substring(
  string: String,
  from start: Int,
  to end: Int,
) -> String

Like slice, but swaps start and end if start is greater, and treats negative values as zero. Indices are UTF-16 code units — this differs from gleam/string.slice which counts grapheme clusters.

pub fn to_locale_lower_case(string: String) -> String
pub fn to_locale_upper_case(string: String) -> String
pub fn to_lower_case(string: String) -> String
pub fn to_upper_case(string: String) -> String
pub fn to_well_formed(string: String) -> String
pub fn trim(string: String) -> String
pub fn trim_end(string: String) -> String
pub fn trim_start(string: String) -> String
Search Document