Skip to content

Commit a7860d9

Browse files
committed
feat(str): add is_str func
1 parent 0180dda commit a7860d9

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/str.fnk

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{new} = import '@fink/js-interop/reflect'
2+
{is_instance, get_type} = import '@fink/js-interop/reflect'
3+
{String, TextEncoder, TextDecoder} = import '@fink/js-interop/globals'
24

35

46
from_char_codes = String.fromCharCode
@@ -25,6 +27,7 @@ trim = fn str: str.trim _
2527
trim_start = fn str: str.trimStart _
2628
trim_end = fn str: str.trimEnd _
2729

30+
2831
# TODO should this be handled by '...' * 3 in loxia?
2932
repeat = fn str, num: str.repeat num
3033

@@ -41,4 +44,11 @@ encode = fn text, encoding:
4144

4245
decode = fn bytes, encoding:
4346
encoder = new TextDecoder, encoding
44-
encoder.decode bytes
47+
encoder.decode bytes
48+
49+
50+
is_str = fn str:
51+
match str:
52+
'string' == get_type ?: true
53+
is_instance ?, String: true
54+
else: false

src/str.test.fnk

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
{describe, it, expect, to_equal} = import '@fink/jest'
2+
{new} = import '@fink/js-interop/reflect'
3+
{Uint8Array, String} = import '@fink/js-interop/globals'
4+
25

36
{
47
from_char_codes, from_code_points, char_code, code_point
58

69
ends_with, starts_with
710

8-
matches, match_all, find_index, split, replace
9-
1011
slice
1112

1213
pad_start, pad_end, trim, trim_start, trim_end
1314

1415
repeat, lower_case, upper_case
1516

16-
raw, rx
17+
raw
1718

1819
encode, decode
20+
21+
is_str
1922
} = import './str'
2023

2124

@@ -130,3 +133,19 @@ describe 'codecs', fn:
130133
to_equal 'foobar'
131134

132135

136+
137+
describe 'is_str', fn:
138+
it 'is a string', fn:
139+
expect
140+
is_str 'foobar'
141+
to_equal true
142+
143+
expect
144+
is_str new String, 1234
145+
to_equal true
146+
147+
it 'is not a string', fn:
148+
expect
149+
is_str 1234
150+
to_equal false
151+

0 commit comments

Comments
 (0)