浏览代码

change range a bit

Khaleel Al-Adhami 4 月之前
父节点
当前提交
d0208e678c
共有 2 个文件被更改,包括 37 次插入37 次删除
  1. 30 30
      reflex/.templates/web/utils/helpers/range.js
  2. 7 7
      reflex/vars/sequence.py

+ 30 - 30
reflex/.templates/web/utils/helpers/range.js

@@ -1,43 +1,43 @@
 /**
  * Simulate the python range() builtin function.
  * inspired by https://dev.to/guyariely/using-python-range-in-javascript-337p
- * 
+ *
  * If needed outside of an iterator context, use `Array.from(range(10))` or
  * spread syntax `[...range(10)]` to get an array.
- * 
+ *
  * @param {number} start: the start or end of the range.
  * @param {number} stop: the end of the range.
  * @param {number} step: the step of the range.
  * @returns {object} an object with a Symbol.iterator method over the range
  */
 export default function range(start, stop, step) {
-    return {
-      [Symbol.iterator]() {
-        if (stop === undefined) {
-          stop = start;
-          start = 0;
-        }
-        if (step === undefined) {
-          step = 1;
-        }
-  
-        let i = start - step;
-  
-        return {
-          next() {
-            i += step;
-            if ((step > 0 && i < stop) || (step < 0 && i > stop)) {
-              return {
-                value: i,
-                done: false,
-              };
-            }
+  return {
+    [Symbol.iterator]() {
+      if ((stop ?? undefined) === undefined) {
+        stop = start;
+        start = 0;
+      }
+      if ((step ?? undefined) === undefined) {
+        step = 1;
+      }
+
+      let i = start - step;
+
+      return {
+        next() {
+          i += step;
+          if ((step > 0 && i < stop) || (step < 0 && i > stop)) {
             return {
-              value: undefined,
-              done: true,
+              value: i,
+              done: false,
             };
-          },
-        };
-      },
-    };
-  }
+          }
+          return {
+            value: undefined,
+            done: true,
+          };
+        },
+      };
+    },
+  };
+}

+ 7 - 7
reflex/vars/sequence.py

@@ -189,20 +189,18 @@ def string_strip_operation(string: Var[str]):
 def string_contains_field_operation(
     haystack: Var[str],
     needle: Var[str],
-    field: VarWithDefault[str] = VarWithDefault(""),
 ):
     """Check if a string contains another string.
 
     Args:
         haystack: The haystack.
         needle: The needle.
-        field: The field to check.
 
     Returns:
         The string contains operation.
     """
     return var_operation_return(
-        js_expression=f"isTrue({field}) ? {haystack}.some(obj => obj[{field}] === {needle}) : {haystack}.some(obj => obj === {needle})",
+        js_expression=f"{haystack}.includes({needle})",
         var_type=bool,
         var_data=VarData(
             imports=_IS_TRUE_IMPORT,
@@ -360,7 +358,7 @@ def array_pluck_operation(
         The reversed array.
     """
     return var_operation_return(
-        js_expression=f"{array}.map(e=>e?.[{field}])",
+        js_expression=f"Array.prototype.map.apply({array}, [e=>e?.[{field}]])",
         var_type=List[Any],
     )
 
@@ -378,7 +376,9 @@ def array_join_operation(
     Returns:
         The joined elements.
     """
-    return var_operation_return(js_expression=f"{array}.join({sep})", var_type=str)
+    return var_operation_return(
+        js_expression=f"Array.prototype.join.apply({array},[{sep}])", var_type=str
+    )
 
 
 @var_operation
@@ -631,7 +631,7 @@ def array_range_operation(
         The range of numbers.
     """
     return var_operation_return(
-        js_expression=f"range({e1}, {e2}, {step})",
+        js_expression=f"[...range({e1}, {e2}, {step})]",
         var_type=List[int],
         var_data=VarData(
             imports=_RANGE_IMPORT,
@@ -769,7 +769,7 @@ def map_array_operation(
         return (ReflexCallable[[], List[args[0]._var_type]], None)
 
     return var_operation_return(
-        js_expression=f"{array}.map({function})",
+        js_expression=f"Array.prototype.map.apply({array}, [{function}])",
         type_computer=nary_type_computer(
             ReflexCallable[[List[Any], ReflexCallable], List[Any]],
             ReflexCallable[[ReflexCallable], List[Any]],