Skip to content

Commit 162a249

Browse files
richard-vineylpil
authored andcommitted
Make bit_array.inspect work on JS
1 parent 29652e4 commit 162a249

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

src/gleam/bit_array.gleam

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ pub fn base16_encode(input: BitArray) -> String
157157
@external(javascript, "../gleam_stdlib.mjs", "base16_decode")
158158
pub fn base16_decode(input: String) -> Result(BitArray, Nil)
159159

160+
@target(javascript)
160161
/// Converts a bit array to a string containing the decimal value of each byte.
161162
///
162163
/// ## Examples
@@ -168,12 +169,17 @@ pub fn base16_decode(input: String) -> Result(BitArray, Nil)
168169
/// inspect(<<100, 5:3>>)
169170
/// // -> "<<100, 5:size(3)>>"
170171
/// ```
171-
///
172+
///
173+
@external(javascript, "../gleam_stdlib.mjs", "bit_array_inspect")
174+
pub fn inspect(input: BitArray) -> String
175+
176+
@target(erlang)
172177
pub fn inspect(input: BitArray) -> String {
173178
do_inspect(input, "<<") <> ">>"
174179
}
175180

176-
fn do_inspect(input: BitArray, accumulator: String) -> String {
181+
@target(erlang)
182+
pub fn do_inspect(input: BitArray, accumulator: String) -> String {
177183
case input {
178184
<<>> -> accumulator
179185

src/gleam_stdlib.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,3 +879,7 @@ export function base16_decode(string) {
879879
}
880880
return new Ok(new BitArray(bytes));
881881
}
882+
883+
export function bit_array_inspect(bits) {
884+
return `<<${[...bits.buffer].join(", ")}>>`;
885+
}

test/gleam/bit_array_test.gleam

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,10 @@ pub fn inspect_test() {
289289

290290
bit_array.inspect(<<0, 20, 0x20, 255>>)
291291
|> should.equal("<<0, 20, 32, 255>>")
292+
}
292293

294+
@target(erlang)
295+
pub fn inspect_partial_bytes_test() {
293296
bit_array.inspect(<<4:5>>)
294297
|> should.equal("<<4:size(5)>>")
295298

0 commit comments

Comments
 (0)