codegen-2 2.8.1__tar.gz → 2.9.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: codegen-2
3
- Version: 2.8.1
3
+ Version: 2.9.0
4
4
  Summary: Meta-programming
5
5
  License: MIT
6
6
  Author: Binh Vu
@@ -128,6 +128,17 @@ class ExprRawPython(Expr):
128
128
  return self.code
129
129
 
130
130
 
131
+ @dataclass
132
+ class ExprRawTypescript(Expr):
133
+ code: str
134
+
135
+ def to_python(self):
136
+ raise NotImplementedError("Raw Typescript cannot be converted to Python")
137
+
138
+ def to_typescript(self):
139
+ return self.code
140
+
141
+
131
142
  @dataclass
132
143
  class ExprVar(Expr): # a special identifier
133
144
  var: Var
@@ -243,7 +254,7 @@ class ExprTernary(Expr):
243
254
  return f"{self.true_expr.to_python()} if {self.condition.to_python()} else {self.false_expr.to_python()}"
244
255
 
245
256
  def to_typescript(self):
246
- return f"{self.true_expr.to_typescript()} ? {self.condition.to_typescript()} : {self.false_expr.to_typescript()}"
257
+ return f"{self.condition.to_typescript()} ? {self.true_expr.to_typescript()} : {self.false_expr.to_typescript()}"
247
258
 
248
259
 
249
260
  class PredefinedFn:
@@ -350,15 +361,26 @@ class PredefinedFn:
350
361
  class map_list(Expr):
351
362
  collection: Expr
352
363
  func: Expr
353
-
354
- def __init__(self, collection: Expr, func: Callable[[ExprIdent], Expr]):
364
+ filter: Optional[Expr] = None
365
+
366
+ def __init__(
367
+ self,
368
+ collection: Expr,
369
+ func: Callable[[ExprIdent], Expr],
370
+ filter: Optional[Callable[[ExprIdent], Expr]] = None,
371
+ ):
355
372
  self.collection = collection
356
373
  self.func = func(ExprIdent("_x"))
374
+ self.filter = filter(ExprIdent("_x")) if filter is not None else None
357
375
 
358
376
  def to_python(self):
377
+ if self.filter is not None:
378
+ return f"[{self.func.to_python()} for _x in {self.collection.to_python()} if {self.filter.to_python()}]"
359
379
  return f"[{self.func.to_python()} for _x in {self.collection.to_python()}]"
360
380
 
361
381
  def to_typescript(self):
382
+ if self.filter is not None:
383
+ return f"{self.collection.to_typescript()}.filter((_x: any) => {self.filter.to_typescript()}).map((_x: any) => {self.func.to_typescript()})"
362
384
  return f"{self.collection.to_typescript()}.map((_x: any) => {self.func.to_typescript()})"
363
385
 
364
386
  @dataclass
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "codegen-2"
3
- version = "2.8.1"
3
+ version = "2.9.0"
4
4
  description = "Meta-programming"
5
5
  authors = ["Binh Vu <binh@toan2.com>"]
6
6
  license = "MIT"
File without changes
File without changes
File without changes