Last active
February 6, 2024 03:15
-
-
Save cbaragao/305779d19a71df4b12f7a9449072bc1f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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