Skip to content

Instantly share code, notes, and snippets.

@cbaragao
Last active February 6, 2024 03:15
Show Gist options
  • Select an option

  • Save cbaragao/305779d19a71df4b12f7a9449072bc1f to your computer and use it in GitHub Desktop.

Select an option

Save cbaragao/305779d19a71df4b12f7a9449072bc1f to your computer and use it in GitHub Desktop.
let
fnSwitch = (string as text, switches as list, default as any) =>
let
// Get max index of replacement list
Max = List.Count(switches) - 1,
// Use List.Accumulate to loop through replacements
Switch = List.Accumulate(
switches,
default,
// if the string matches first index, then replace with second item in that nested list
// else keep the default
(state, current) => if string = current{0} then current{1} else state
)
in
Switch,
// Documentation
fnType = type function (string as text, switches as list, default as text) as any
meta [
Documentation.Name = "fnSwitch",
Documentation.LongDescription
= "This function takes a string and performs a SWITCH replacement using nested lists. Additionally, it provides a default value if there are no matches."
]
in
Value.ReplaceType(fnSwitch, fnType)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment