Skip to content

Commit

Permalink
Develop (#13)
Browse files Browse the repository at this point in the history
* Refactor M.pq and M_Creator.py files

* Refactor TextClean function to improve documentation and parameter handling

* Rename Fx
  • Loading branch information
OscarValerock authored Apr 30, 2024
1 parent 6b9204c commit 24a43fc
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 13 deletions.
126 changes: 126 additions & 0 deletions Functions/Text/CleanString.pq
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
let
// Step 1: Define metadata for each parameter describing its purpose and usage
metaDocumentation = type function (
mytext as (type text meta [
Documentation.FieldCaption = "Input Text",
Documentation.FieldDescription = "The primary text input on which cleaning and manipulation operations will be performed.",
Documentation.SampleValues = {"Example text"},
Formatting.IsMultiLine = false,
Formatting.IsCode = false
]),
optional characters as (type text meta [
Documentation.FieldCaption = "Characters to Replace",
Documentation.FieldDescription = "A semicolon-separated list of character pairs to be replaced in the input text. Each pair should be delimited by a comma.",
Documentation.SampleValues = {"a,b;c,d"},
Formatting.IsMultiLine = false,
Formatting.IsCode = true
]),
optional words as (type text meta [
Documentation.FieldCaption = "Words to Replace",
Documentation.FieldDescription = "A semicolon-separated list of word pairs to be replaced in the input text. Each pair should be delimited by a comma.",
Documentation.SampleValues = {"hello,hi;world,earth"},
Formatting.IsMultiLine = false,
Formatting.IsCode = true
]),
optional charactersRemove as (type text meta [
Documentation.FieldCaption = "Characters to Remove",
Documentation.FieldDescription = "A string of characters that will be removed from the input text.",
Documentation.SampleValues = {"abc"},
Formatting.IsMultiLine = false,
Formatting.IsCode = false
]),
optional fullCellContents as (type text meta [
Documentation.FieldCaption = "Full Cell Content Replace",
Documentation.FieldDescription = "A semicolon-separated list of old and new content pairs for full cell replacement. Each pair should be delimited by a comma.",
Documentation.SampleValues = {"old text,new text"},
Formatting.IsMultiLine = false,
Formatting.IsCode = true
])
) as list
// Step 2: Define global metadata in detail
meta [

Documentation.Name = "Text.Clean",
Documentation.LongDescription =
"
<p><b>Text.Clean</b></p>

<li>------------------------------------------------------</li>

<li><b> Creator: </b> Oscar Martinez </li>
<li><b> Web: </b> https://bibb.pro </li>
<li><b> LinkedIn: </b> https://www.linkedin.com/in/oscarmartinezv/ </li>

<li>------------------------------------------------------</li>

<p><b> Function Description: </b></br>
<p>This function provides various text manipulation capabilities such as replacing specific characters or words, removing certain characters, and replacing entire cell contents based on user-defined parameters.</p>

",
Documentation.Examples = {
[
Description = "Replace specific characters in a text string.",
Code = "Text.Clean('example text', 'x,y')",
Result = "example teyt"
]
}
],

// Define the main function and parameters
myFunction = (mytext as text, optional characters as text, optional words as text, optional charactersRemove as text, optional fullCellContents as text) =>

let
// Use the input text as the initial source
Source = mytext,

// Function to convert a delimited string into a list of lists
ListFromString = (string as text, delimiter1 as text, delimiter2 as text) as any =>
let
SplitPairs = Text.Split(string, delimiter1),
ConvertToLists = List.Transform(SplitPairs, each Text.Split(_, delimiter2))
in
ConvertToLists,

// Replace characters if 'characters' parameter is provided
ReplaceMatchingChar = if characters <> null then
Text.Combine(
List.ReplaceMatchingItems(
Text.ToList(Source),
ListFromString(characters, ";", ",")
)
)
else Source,

// Replace words if 'words' parameter is provided
ReplaceMatchingWord = if words <> null then
Text.Combine(
List.ReplaceMatchingItems(
Text.Split(ReplaceMatchingChar, " "),
ListFromString(words, ";", ",")
), " "
)
else ReplaceMatchingChar,

// Remove characters if 'charactersRemove' parameter is provided
CleanedText = if charactersRemove <> null then
Text.Remove(ReplaceMatchingWord, Text.ToList(charactersRemove))
else ReplaceMatchingWord,

// Trim the final text
TrimmedText = Text.Trim(CleanedText),

// Replace full cell contents if 'fullCellContents' parameter is provided
FinalText = if fullCellContents <> null then
let
ReplacementList = ListFromString(fullCellContents, ";", ","),
ReplacedFullContent = List.ReplaceMatchingItems({TrimmedText}, ReplacementList)
in
ReplacedFullContent{0}
else TrimmedText

in
FinalText
// _______________________________ \\
in

Value.ReplaceType(myFunction, metaDocumentation)
59 changes: 59 additions & 0 deletions Functions/Text/StringToList.pq
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
let
// Step 1: Define metadata for each parameter describing its purpose and usage
metaDocumentation = type function (
string as (type text meta [
Documentation.FieldCaption = "String",
Documentation.SampleValues = {"ä,a;ö,o"},
// Documentation.AllowedValues = {"Value1", "Value2", "Value3"},
Formatting.IsMultiLine = false,
Formatting.IsCode = false
])
) as list
// Step 2: Define global metadata in detail
meta [
Documentation.Name = "Text.ListFromString",
Documentation.LongDescription =
"
<p><b> Template Title </b></p>

<li>------------------------------------------------------</li>

<li><b> Web: </b> https://bibb.pro </li>
<li><b> LinkedIn: </b> https://www.linkedin.com/in/oscarmartinezv/ </li>

<li>------------------------------------------------------</li>

<p><b> Function Description: </b></br>
Converts a string separated by commas and semicolons into a list</b></br></br>

<b> Returns: </b></br>
A list.

",
Documentation.Examples = {
[
Description = "Converts a string separated by commas and semicolons into a list",
Code = "Text.ListFromString(""ä,a;ö,o"")",
Result = "{{""ü"", ""u""},{""ö"", ""o""}}"
]
}
],

// Define the main function and parameters
myFunction = (string as text) as any =>

let
// Your original string
OriginalText = string,

// Split the string into rows by semicolon
SplitPairs = Text.Split(OriginalText, ";"),

// Convert each pair into a list by splitting by comma
ConvertToLists = List.Transform(SplitPairs, each Text.Split(_, ","))
in
ConvertToLists

in

Value.ReplaceType(myFunction, metaDocumentation)
8 changes: 8 additions & 0 deletions M.pq
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


let

GitHubUser = "OscarValerock",
Expand Down Expand Up @@ -139,6 +141,7 @@ let
List.Product = List.Product,
List.RemoveItems = List.RemoveItems,
List.RemoveNulls = List.RemoveNulls,
List.ReplaceMatchingItems = List.ReplaceMatchingItems,
List.Select = List.Select,
List.StandardDeviation = List.StandardDeviation,
List.Sum = List.Sum,
Expand Down Expand Up @@ -195,18 +198,23 @@ let
Table.TransformColumnTypes = Table.TransformColumnTypes,
Table.TransformColumns = Table.TransformColumns,
Text.BeforeDelimiter = Text.BeforeDelimiter,
Text.Clean = Text.Clean,
Text.Combine = Text.Combine,
Text.Contains = Text.Contains,
Text.End = Text.End,
Text.From = Text.From,
Text.PadEnd = Text.PadEnd,
Text.PadStart = Text.PadStart,
Text.Proper = Text.Proper,
Text.Remove = Text.Remove,
Text.Repeat = Text.Repeat,
Text.Replace = Text.Replace,
Text.Split = Text.Split,
Text.SplitAny = Text.SplitAny,
Text.Start = Text.Start,
Text.ToBinary = Text.ToBinary,
Text.ToList = Text.ToList,
Text.Trim = Text.Trim,
Text.Type = Text.Type,
Time.Hour = Time.Hour,
Time.Minute = Time.Minute,
Expand Down
1 change: 1 addition & 0 deletions M_Creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
'Text.ContainsAll',
'Text.ContainsAny',
'Text.ReplaceMany',
'Text.ListFromString',
'Web.Contents', #Unfortunately adding this function to the M code will create a dynamic error :(
'www.linkedin',
'youtu.be',
Expand Down
30 changes: 17 additions & 13 deletions M_FxDocTemplate.pq
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,29 @@ let
<p><b> Formula Bar (fx) Output: </b></br>
<code> fn_custom_function_template( TextValue, 200, true ) </code>

"
",
Documentation.Examples = {
[
Description = " Flatten a nested list with mixed levels into a simple list. ",
Code = "List.Flatten({1, {2, 3}, {1, {2, 3}}})",
Result = "{1, 2, 3, 1, 2, 3}"
]
}
],

// Define the main function and parameters
myFunction = (parameter1 as text, parameter2 as number, optional parameter3 as nullable logical) =>

let invoke_custom_function =

// paste your custom function here
// _______________________________ \\
let
test_parameter3 = if (parameter3 ?? true) = true then -1 else 0,
variable = parameter2,
response = parameter1 * test_parameter3
in
response
let
test_parameter3 = if (parameter3 ?? true) = true then -1 else 0,
variable = parameter2,
response = parameter1 * test_parameter3
in
response
// _______________________________ \\
in invoke_custom_function,
in

replaceMetaData = Value.ReplaceType(myFunction, metaDocumentation)

in
replaceMetaData
Value.ReplaceType(myFunction, metaDocumentation)

0 comments on commit 24a43fc

Please sign in to comment.