Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Last active February 10, 2024 16:23
Show Gist options
  • Save cbaragao/43a39ca3551234c6a6377f696a90248a to your computer and use it in GitHub Desktop.
Save cbaragao/43a39ca3551234c6a6377f696a90248a to your computer and use it in GitHub Desktop.
let fnSwitchMethod =
(string as text, switches as list, default as any, optional method as number) =>
let
check_method = if method <> null then method else 0,
case = if check_method = 0 then
try List.Select(switches, each _{0} = string){0}{1} otherwise default
else if check_method= 1 then
try List.Select(switches, each Text.Contains(string,_{0})){0}{1} otherwise default
else if check_method = 2 then
try List.Select(switches, each Text.StartsWith(string,_{0})){0}{1} otherwise default
else if check_method = 3 then
try List.Select(switches, each Text.EndsWith(string, _{0})){0}{1} otherwise default
else default
in
case,
fnType = type function (string as text, switches as list, default as text, optional method as number) as any
meta [
Documentation.Name = "fnSwitchMethod",
Documentation.LongDescription
= "This function takes a string and performs a SWITCH replacement using nested lists and different methods. #(lf)" &
"Additionally, it provides a default value if there are no matches. #(lf)" &
"Default method or 0 is match (#lf)" &
"Method 1 is a switch contains (#lf)" &
"Method 2 is a switch starts with (#lf)" &
"Method 3 is a switch ends with (#lf)"
]
in
Value.ReplaceType(fnSwitchMethod, fnType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment