Skip to content

Commit 0c4de7e

Browse files
committed
Add runnable examples for expanded stdlib families
1 parent 06d0268 commit 0c4de7e

7 files changed

Lines changed: 186 additions & 2 deletions

File tree

ROADMAP.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,13 @@ Goal: reduce host-side boilerplate for common scripting tasks.
301301

302302
### Testing and Docs
303303

304-
- [ ] Add comprehensive docs pages and examples for each new family.
304+
- [x] Add comprehensive docs pages and examples for each new family.
305305
- [x] Add negative tests for malformed JSON/regex patterns.
306306
- [x] Add benchmark coverage for hot stdlib paths.
307307

308308
### v0.18.0 Definition of Done
309309

310-
- [ ] New stdlib is documented and example-backed.
310+
- [x] New stdlib is documented and example-backed.
311311
- [ ] Runtime behavior is deterministic across supported OSes.
312312
- [ ] Security/performance guardrails are validated by tests.
313313

docs/stdlib_core_utilities.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ def parse_seen_at(raw)
6565
Time.parse(raw, in: "UTC")
6666
end
6767
```
68+
69+
For a runnable end-to-end sample, see `examples/stdlib/core_utilities.vibe`.

examples/arrays/extras.vibe

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,17 @@ end
114114
def tally_statuses(statuses)
115115
statuses.tally
116116
end
117+
118+
def chunk_values(values, size)
119+
values.chunk(size)
120+
end
121+
122+
def window_values(values, size)
123+
values.window(size)
124+
end
125+
126+
def group_by_stable_status(players)
127+
players.group_by_stable do |player|
128+
player[:status]
129+
end
130+
end

examples/hashes/transformations.vibe

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,21 @@ def non_zero(record)
5555
value == 0
5656
end
5757
end
58+
59+
def remap_profile(record)
60+
record.remap_keys({ first_name: :name, total_raised: :raised })
61+
end
62+
63+
def deep_transform_profile(record)
64+
record.deep_transform_keys do |key|
65+
if key == :player_id
66+
:playerId
67+
elsif key == :total_raised
68+
:totalRaised
69+
elsif key == :amount_cents
70+
:amountCents
71+
else
72+
key
73+
end
74+
end
75+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# vibe: 0.2
2+
3+
def run
4+
parsed = JSON.parse("{\"id\":\"p-1\",\"score\":10}")
5+
event_id = uuid()
6+
token = random_id(8)
7+
parsed_time = Time.parse("2024-05-01 10:30:00", in: "UTC")
8+
9+
{
10+
json_id: parsed.fetch("id"),
11+
json_score: parsed.fetch("score"),
12+
json_encoded: JSON.stringify({ id: parsed.fetch("id"), score: parsed.fetch("score") }),
13+
regex_match: Regex.match("ID-[0-9]+", "ID-12 ID-34"),
14+
regex_replace: Regex.replace("ID-12 ID-34", "ID-[0-9]+", "X"),
15+
regex_replace_all: Regex.replace_all("ID-12 ID-34", "ID-[0-9]+", "X"),
16+
uuid_length: event_id.length,
17+
uuid_has_dash: event_id.include?("-"),
18+
random_length: token.length,
19+
to_int: to_int("42"),
20+
to_float: to_float("1.25"),
21+
parsed_time: parsed_time.format("2006-01-02T15:04:05Z07:00")
22+
}
23+
end

examples/strings/operations.vibe

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ def run
3737
scan: ids.scan("ID-[0-9]+"),
3838
strip_bang: immutable.strip!,
3939
strip_bang_nochange: "hello".strip!,
40+
squish: " hello \n\t world ".squish,
41+
squish_bang: " hello \n\t world ".squish!,
42+
template: "Player {{user.name}} scored {{user.score}}".template({ user: { name: "Alex", score: 42 } }),
43+
template_missing: "Hello {{missing}}".template({ name: "Alex" }),
4044
immutable_after: immutable,
4145
clear: "hello".clear,
4246
concat: "he".concat("llo", "!"),

vibes/examples_test.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,60 @@ func TestExamples(t *testing.T) {
570570
"complete": intVal(1),
571571
}),
572572
},
573+
{
574+
name: "arrays/chunk_values",
575+
file: "arrays/extras.vibe",
576+
function: "chunk_values",
577+
args: []Value{
578+
arrayVal(intVal(1), intVal(2), intVal(3), intVal(4), intVal(5)),
579+
intVal(2),
580+
},
581+
want: arrayVal(
582+
arrayVal(intVal(1), intVal(2)),
583+
arrayVal(intVal(3), intVal(4)),
584+
arrayVal(intVal(5)),
585+
),
586+
},
587+
{
588+
name: "arrays/window_values",
589+
file: "arrays/extras.vibe",
590+
function: "window_values",
591+
args: []Value{
592+
arrayVal(intVal(1), intVal(2), intVal(3), intVal(4)),
593+
intVal(3),
594+
},
595+
want: arrayVal(
596+
arrayVal(intVal(1), intVal(2), intVal(3)),
597+
arrayVal(intVal(2), intVal(3), intVal(4)),
598+
),
599+
},
600+
{
601+
name: "arrays/group_by_stable_status",
602+
file: "arrays/extras.vibe",
603+
function: "group_by_stable_status",
604+
args: []Value{
605+
arrayVal(
606+
hashVal(map[string]Value{"id": strVal("p1"), "status": strVal("active")}),
607+
hashVal(map[string]Value{"id": strVal("p2"), "status": strVal("complete")}),
608+
hashVal(map[string]Value{"id": strVal("p3"), "status": strVal("active")}),
609+
),
610+
},
611+
want: arrayVal(
612+
arrayVal(
613+
strVal("active"),
614+
arrayVal(
615+
hashVal(map[string]Value{"id": strVal("p1"), "status": strVal("active")}),
616+
hashVal(map[string]Value{"id": strVal("p3"), "status": strVal("active")}),
617+
),
618+
),
619+
arrayVal(
620+
strVal("complete"),
621+
arrayVal(
622+
hashVal(map[string]Value{"id": strVal("p2"), "status": strVal("complete")}),
623+
),
624+
),
625+
),
626+
},
573627
{
574628
name: "collections/make_player",
575629
file: "collections/hashes.vibe",
@@ -1321,6 +1375,52 @@ func TestExamples(t *testing.T) {
13211375
"d": intVal(3),
13221376
}),
13231377
},
1378+
{
1379+
name: "hashes/remap_profile",
1380+
file: "hashes/transformations.vibe",
1381+
function: "remap_profile",
1382+
args: []Value{
1383+
hashVal(map[string]Value{
1384+
"first_name": strVal("Alex"),
1385+
"total_raised": intVal(12),
1386+
"team": strVal("north"),
1387+
}),
1388+
},
1389+
want: hashVal(map[string]Value{
1390+
"name": strVal("Alex"),
1391+
"raised": intVal(12),
1392+
"team": strVal("north"),
1393+
}),
1394+
},
1395+
{
1396+
name: "hashes/deep_transform_profile",
1397+
file: "hashes/transformations.vibe",
1398+
function: "deep_transform_profile",
1399+
args: []Value{
1400+
hashVal(map[string]Value{
1401+
"player_id": intVal(7),
1402+
"profile": hashVal(map[string]Value{
1403+
"total_raised": intVal(10),
1404+
}),
1405+
"events": arrayVal(
1406+
hashVal(map[string]Value{
1407+
"amount_cents": intVal(300),
1408+
}),
1409+
),
1410+
}),
1411+
},
1412+
want: hashVal(map[string]Value{
1413+
"playerId": intVal(7),
1414+
"profile": hashVal(map[string]Value{
1415+
"totalRaised": intVal(10),
1416+
}),
1417+
"events": arrayVal(
1418+
hashVal(map[string]Value{
1419+
"amountCents": intVal(300),
1420+
}),
1421+
),
1422+
}),
1423+
},
13241424
{
13251425
name: "blocks/total_raised_by_currency",
13261426
file: "blocks/enumerable_reports.vibe",
@@ -1860,6 +1960,10 @@ func TestExamples(t *testing.T) {
18601960
"scan": arrayVal(strVal("ID-12"), strVal("ID-34")),
18611961
"strip_bang": strVal("hello"),
18621962
"strip_bang_nochange": nilVal(),
1963+
"squish": strVal("hello world"),
1964+
"squish_bang": strVal("hello world"),
1965+
"template": strVal("Player Alex scored 42"),
1966+
"template_missing": strVal("Hello {{missing}}"),
18631967
"immutable_after": strVal(" hello "),
18641968
"clear": strVal(""),
18651969
"concat": strVal("hello!"),
@@ -1888,6 +1992,25 @@ func TestExamples(t *testing.T) {
18881992
"length": intVal(5),
18891993
}),
18901994
},
1995+
{
1996+
name: "stdlib/core_utilities_run",
1997+
file: "stdlib/core_utilities.vibe",
1998+
function: "run",
1999+
want: hashVal(map[string]Value{
2000+
"json_id": strVal("p-1"),
2001+
"json_score": intVal(10),
2002+
"json_encoded": strVal("{\"id\":\"p-1\",\"score\":10}"),
2003+
"regex_match": strVal("ID-12"),
2004+
"regex_replace": strVal("X ID-34"),
2005+
"regex_replace_all": strVal("X X"),
2006+
"uuid_length": intVal(36),
2007+
"uuid_has_dash": boolVal(true),
2008+
"random_length": intVal(8),
2009+
"to_int": intVal(42),
2010+
"to_float": floatVal(1.25),
2011+
"parsed_time": strVal("2024-05-01T10:30:00Z"),
2012+
}),
2013+
},
18912014
}
18922015

18932016
for _, tc := range cases {

0 commit comments

Comments
 (0)