Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Last active September 12, 2024 19:59
Show Gist options
  • Select an option

  • Save cbaragao/5e65f908b6711b2a914859182d7d113c to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/5e65f908b6711b2a914859182d7d113c to your computer and use it in GitHub Desktop.
(str as text, optional keep_upper as logical, optional keep_lower as logical, optional keep_nums as logical, optional keep_specials as logical, optional keep_chars as text) =>
let
// Function to remove characters from the string
fnRemove =
(string as text, l as list)=>
Text.Combine(
List.Select(
Text.ToList(str),
each List.Contains(l, Character.ToNumber(_))=false
),
""
),
// Convert characters to keep into a list of their ASCII values
chars_to_keep = if Text.Length(keep_chars) > 0 then List.Transform(Text.ToList(keep_chars), each Character.ToNumber(_)) else {},
// ASCII values for uppercase letters
upper = {65..90},
// ASCII values for lowercase letters
lower = {97..122},
// Combine uppercase and lowercase letters
alphas = List.Combine({upper, lower}),
// ASCII values for numbers
nums = {48..57},
// ASCII values for special characters
specials = List.RemoveItems({0..255}, List.Combine({alphas, nums})),
// Determine if uppercase letters should be removed
u = if keep_upper = true then {} else upper,
// Determine if lowercase letters should be removed
l = if keep_lower = true then {} else lower,
// Determine if numbers should be removed
n = if keep_nums = true then {} else nums,
// Determine if special characters should be removed
s = if keep_specials = true then {} else specials,
// Create a list of characters to remove, excluding those to keep
remove_list = List.Distinct(List.RemoveItems(List.Combine({u, l, n, s}), chars_to_keep)),
// Apply the removal function
return = fnRemove(str, remove_list)
in
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment