File size: 1,566 Bytes
8628c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
ALLI Domain Nodes: String Transformations & Operations.
Covers: string.transform, string.concat, string.split.
"""

from typing import Any, Dict

from language_core.nodes.loader import register_domain_nodes


def _exec_string_transform(inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
    raw_text = inputs.get("text_in") if inputs.get("text_in") is not None else inputs.get("text", "")
    text = str(raw_text)
    op = params.get("operation", "upper")
    ops = {
        "upper": text.upper,
        "lower": text.lower,
        "title": text.title,
        "capitalize": text.capitalize,
        "strip": text.strip,
    }
    res = ops[op]() if op in ops else text
    return {"text_out": res, "result": res, "val": res, "out": res, "length": len(res)}


def _exec_string_concat(inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
    a = str(inputs.get("a", ""))
    b = str(inputs.get("b", ""))
    sep = str(params.get("separator", ""))
    res = a + sep + b
    return {"result": res, "length": len(res)}


def _exec_string_split(inputs: Dict[str, Any], params: Dict[str, Any]) -> Dict[str, Any]:
    text = str(inputs.get("text", ""))
    delim = str(params.get("delimiter", " "))
    items = text.split(delim)
    return {"items": items, "count": len(items)}


EXECUTORS = {
    "string.transform": _exec_string_transform,
    "string.concat": _exec_string_concat,
    "string.split": _exec_string_split,
}


def register_strings_nodes(registry: Any) -> None:
    register_domain_nodes(registry, "strings", EXECUTORS)