Skip to content

Commit afe6ba3

Browse files
committed
add missing lib
Signed-off-by: George Lemon <georgelemon@protonmail.com>
1 parent 44e7221 commit afe6ba3

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

src/tim/engine/stdlib/libjson.nim

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# A super fast template engine for cool kids
2+
#
3+
# (c) iLiquid, 2019-2020
4+
# https://github.com/liquidev/
5+
#
6+
# (c) 2025 George Lemon | LGPL-v3 License
7+
# Made by Humans from OpenPeeps
8+
# https://github.com/openpeeps/tim | https://openpeeps.dev/packages/tim
9+
10+
import std/[options, json]
11+
import pkg/voodoo/language/[chunk, ast, sym, value]
12+
13+
import ./inliner
14+
15+
proc initJSON*(script: Script, systemModule: Module): Module =
16+
# foreign stuff
17+
result = newModule("json", some"json.timl")
18+
result.load(systemModule)
19+
20+
script.addProc(result, "join", @[paramDef("s", ttyJson), paramDef("sep", ttyString, initValue(", "))], ttyString,
21+
proc (args: StackView, argc: int): Value =
22+
# joins an array of strings with ", "
23+
result = initvalue("")
24+
let sep = args[1].stringVal[]
25+
for i, v in args[0].jsonVal.elems:
26+
case v.kind
27+
of JString:
28+
result.stringVal[] = result.stringVal[] & v.getStr()
29+
of JInt, JFloat, JBool:
30+
result.stringVal[] = result.stringVal[] & $v.getInt()
31+
else: discard
32+
if i < args[0].jsonVal.elems.len - 1:
33+
result.stringVal[] = result.stringVal[] & sep
34+
)

0 commit comments

Comments
 (0)