Prechádzať zdrojové kódy

feat(xtintin): 增加 map/mapIf/filter/filterMap/toUpper

dzp 3 rokov pred
rodič
commit
39461ba7f1

+ 1 - 0
plugins/lib/xtintin/__init__.tin

@@ -3,3 +3,4 @@
 load-file plugins/lib/xtintin/funcs.tin;
 load-file plugins/lib/xtintin/cmds.tin;
 load-file plugins/lib/xtintin/debug.tin;
+load-file plugins/lib/xtintin/fp.tin;

+ 84 - 0
plugins/lib/xtintin/fp.tin

@@ -0,0 +1,84 @@
+#function {filter} {
+    #local table {%1};
+    #local cond {%2};
+
+    #replace {cond} {KEY} {$$key};
+    #replace {cond} {VALUE} {$$value};
+
+    #local newTable {};
+    #local key {};
+    #foreach {*table[]} {key} {
+        #local value {$table[$key]};
+        #line sub var #if { $cond } {
+            #cat newTable {{$key}{$value}};
+        };
+    };
+
+    #return {$newTable}
+};
+
+#function {filterMap} {
+    #local table {%1};
+    #local cond {%2};
+    #local body {%3};
+
+    #replace {cond} {KEY} {$$key};
+    #replace {cond} {VALUE} {$$value};
+    #replace {body} {KEY} {$$key};
+    #replace {body} {VALUE} {$$value};
+
+    #local newTable {};
+    #local key {};
+    #foreach {*table[]} {key} {
+        #local value {$table[$key]};
+        #line sub var #if { $cond } {
+            #cat newTable {{$key}{$body}};
+        };
+    };
+
+    #return {$newTable}
+};
+
+#function {mapIf} {
+    #local table {%1};
+    #local cond {%2};
+    #local body {%3};
+
+    #replace {cond} {KEY} {$$key};
+    #replace {cond} {VALUE} {$$value};
+    #replace {body} {KEY} {$$key};
+    #replace {body} {VALUE} {$$value};
+
+    #local newTable {};
+    #local key {};
+    #foreach {*table[]} {key} {
+        #local value {$table[$key]};
+        #line sub var {
+            #if { $cond } {
+                #cat newTable {{$key}{$body}};
+            };
+            #else {
+                #cat newTable {{$key}{$value}};
+            };
+        };
+    };
+
+    #return {$newTable}
+};
+
+#function {map} {
+    #local table {%1};
+    #local body {%2};
+
+    #replace {body} {KEY} {$$key};
+    #replace {body} {VALUE} {$$value};
+
+    #local newTable {};
+    #local key {};
+    #foreach {*table[]} {key} {
+        #local value {$table[$key]};
+        #cat newTable {{$key}{$body}};
+    };
+
+    #return {$newTable}
+};

+ 1 - 0
plugins/lib/xtintin/funcs.tin

@@ -122,6 +122,7 @@
 #function {listExtend}      {#var l {%1}; #loc len {%2}; #loc size {@listSize{{$l}}}; #math len {$len - $size}; #if { $len > 0 } { #loop {1} {$len} {id} { #list l add {{}} } }; #return {$l} };
 #function {len}             {#format result {%L} {%1}};
 #function {toLower}         {#format result {%l} {%1}};
+#function {toUpper}         {#format result {%u} {%1}};
 #function {capital}         {#format result {%n} {%1}};
 #function {replace}         {#var result %1;#replace result {%2} {%3}};
 #function {inList}          {#math result { @indexOf{{%1};{%2}} > 0 }};