Skip to content

Commit 0bb9953

Browse files
authored
Merge pull request #3 from fink-lang/regex
regex module
2 parents df0d4b1 + be20a48 commit 0bb9953

File tree

5 files changed

+98
-61
lines changed

5 files changed

+98
-61
lines changed

src/regex.fnk

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{raw} = import './str'
2+
3+
4+
regex = fn pattern, flags:
5+
new RegExp pattern, flags
6+
7+
8+
rx = fn strings, ...exprs:
9+
regex raw strings, ...exprs
10+
11+
12+
match_all = fn str, rx:
13+
str.matchAll regex rx, 'g'
14+
15+
16+
find_index = fn str, rx:
17+
str.search rx
18+
19+
20+
split = fn str, rx, ...args:
21+
str.split rx, ...args
22+
23+
24+
replace = fn str, rx, replacement:
25+
str.replace rx, replacement
26+
27+
28+
replace_all = fn str, rx, replacement:
29+
rxg = regex rx, 'g'
30+
str.replace rxg, replacement
31+
32+
33+
matches = fn str, rx: rx.test str

src/regex.test.fnk

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{describe, it, expect, to_equal} = import '@fink/jest'
2+
3+
{matches, match_all, find_index, split, replace_all, replace, rx} = import './regex'
4+
5+
6+
describe 'regex', fn:
7+
it 'matches single', fn:
8+
expect
9+
matches 'foobar', rx/.+oo.+/
10+
to_equal
11+
true
12+
13+
expect
14+
matches 'foobar', rx/ni/
15+
to_equal
16+
false
17+
18+
19+
it 'matches multiple', fn:
20+
[...matches] = pipe match_all 'foobar, spam, ham', rx/(?<=\s).+?am/:
21+
map [m]: m
22+
23+
expect
24+
matches
25+
to_equal
26+
['spam', 'ham']
27+
28+
29+
it 'searches', fn:
30+
expect
31+
find_index 'foobar, spam, ham', rx/(?<=\s).+?am/
32+
to_equal 8
33+
34+
35+
it 'splits', fn:
36+
expect
37+
split 'foobar, spam , ham', rx/\s*,\s*/
38+
to_equal
39+
['foobar', 'spam', 'ham']
40+
41+
42+
it 'replaces all', fn:
43+
expect
44+
replace_all 'foobar, spam, ham, ni', rx/,.+?am/, ''
45+
to_equal 'foobar, ni'
46+
47+
48+
it 'replaces single', fn:
49+
expect
50+
replace 'foobar, spam, ham, ni', rx/,.+?am/, ''
51+
to_equal 'foobar, ham, ni'
52+
53+
54+
55+
describe 'string templates', fn:
56+
57+
expect
58+
rx'\(.+?\)(foo{${1},${4}})'
59+
to_equal rx/\(.+?\)(foo{1,4})/
60+
61+

src/stack-trace.test.fnk

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{describe, it, expect, to_equal} = import '@fink/jest'
22

3-
{replace, split} = import './str'
3+
{replace_all, split} = import './regex'
44

55
{stack_trace} = import './stack-trace'
66

@@ -10,7 +10,7 @@ describe 'stack_trace', fn:
1010
foo = fn:
1111
stack = stack_trace()
1212
[, line] = pipe stack:
13-
replace ?, rx/\(.+\//mg, '('
13+
replace_all ?, rx/\(.+\//, '('
1414
split ?, '\n'
1515
line
1616

src/str.fnk

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ code_point = fn str, idx: str.codePointAt idx
1010
ends_with = fn str, end, ...args: str.endsWith end, ...args
1111
starts_with = fn str, end, ...args: str.startsWith end, ...args
1212

13-
# TODO move to regex module?
14-
matches = fn str, rx:
15-
result = str.match rx
16-
match result:
17-
{}: result
18-
else: []
19-
match_all = fn str, rx: str.matchAll rx
20-
find_index = fn str, rx: str.search rx
21-
split = fn str, rx, ...args: str.split rx, ...args
22-
replace = fn str, rx, replacement: str.replace rx, replacement
23-
2413
slice = fn str, ...args: str.slice ...args
2514

2615

@@ -41,6 +30,3 @@ lower_case = fn str: str.toLowerCase()
4130
upper_case = fn str: str.toUpperCase()
4231

4332
raw = String.raw
44-
45-
rx = fn strings, ...exprs:
46-
new RegExp raw strings, ...exprs

src/str.test.fnk

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,46 +56,6 @@ describe 'string assertions', fn:
5656

5757

5858

59-
describe 'regex', fn:
60-
it 'matches single', fn:
61-
[,spam, ham] = matches 'foobar, spam, ham', rx/,\s*(\w+),\s*(\w+)/
62-
63-
expect
64-
[spam, ham]
65-
to_equal
66-
['spam', 'ham']
67-
68-
expect
69-
matches 'foobar', rx/ni/g
70-
to_equal
71-
[]
72-
73-
it 'matches multiple', fn:
74-
[...matches] = pipe match_all 'foobar, spam, ham', rx/(?<=\s).+?am/g:
75-
map [m]: m
76-
77-
expect
78-
matches
79-
to_equal
80-
['spam', 'ham']
81-
82-
it 'searches', fn:
83-
expect
84-
find_index 'foobar, spam, ham', rx/(?<=\s).+?am/g
85-
to_equal 8
86-
87-
it 'splits', fn:
88-
expect
89-
split 'foobar, spam, ham', rx/\s*,\s*/
90-
to_equal
91-
['foobar', 'spam', 'ham']
92-
93-
it 'replaces', fn:
94-
expect
95-
replace 'foobar, spam, ham, ni', rx/,.+?am/g, ''
96-
to_equal 'foobar, ni'
97-
98-
9959
describe 'slice', fn:
10060
it 'slices', fn:
10161
expect
@@ -107,6 +67,7 @@ describe 'slice', fn:
10767
to_equal '67'
10868

10969

70+
11071
describe 'formatting', fn:
11172
it 'pads', fn:
11273
expect
@@ -145,13 +106,9 @@ describe 'formatting', fn:
145106
to_equal 'FOOBAR'
146107

147108

109+
148110
describe 'string templates', fn:
149111
expect
150112
raw'\a\('
151113
to_equal '\\a\\('
152114

153-
expect
154-
rx'\(.+?\)(foo{${1},${4}})'
155-
to_equal rx/\(.+?\)(foo{1,4})/
156-
157-

0 commit comments

Comments
 (0)