Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

- Fixed a bug where `string.inspect` on the JavaScript target would incorrectly
print numbers like `1.0e300`.
- Fixed a bug where `stting.inspect` on the JavaScript target would incorrectly
print NaN and Infinity.

## v1.0.5 - 2026-08-06

- Fixed a bug where `bit_array.pad_to_bytes` could crash.
Expand Down
7 changes: 5 additions & 2 deletions src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,11 @@ export function float_to_string(float) {
const index = string.indexOf("e");
if (index >= 0) {
return string.slice(0, index) + ".0" + string.slice(index);
} else {
} else if (globalThis.Number.isInteger(float)) {
return string + ".0";
} else {
// For NaN and Infinity we return it as JS representation.
return "//js(" + string + ")";
}
}
}
Expand All @@ -718,7 +721,7 @@ class Inspector {
if (v === null) return "//js(null)";
if (v === undefined) return "Nil";
if (t === "string") return this.#string(v);
if (t === "bigint" || Number.isInteger(v)) return v.toString();
if (t === "bigint" || Number.isSafeInteger(v)) return v.toString();
if (t === "number") return float_to_string(v);
if (v instanceof UtfCodepoint) return this.#utfCodepoint(v);
if (v instanceof BitArray) return this.#bit_array(v);
Expand Down
21 changes: 21 additions & 0 deletions test/gleam/string_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,14 @@ pub fn inspect_451_test() {
assert string.inspect(<<255, 2, 0>>) == "<<255, 2, 0>>"
}

pub fn inspect_456_test() {
assert string.inspect(1.0e300) == "1.0e300"
}

pub fn inspect_461_test() {
assert string.inspect(1.0e-300) == "1.0e-300"
}

pub fn inspect_charlist_test() {
let list = [
70, 97, 105, 108, 101, 100, 32, 116, 111, 32, 108, 111, 97, 100, 32, 78, 73,
Expand Down Expand Up @@ -1317,6 +1325,19 @@ pub fn target_inspect_bit_array_test() {
assert string.inspect(<<"abc":utf8>>) == "<<97, 98, 99>>"
}

@target(javascript)
pub fn target_inspect_nan_test() {
let infinity = 1.0e300 *. 1.0e300
let nan = infinity /. infinity
assert string.inspect(nan) == "//js(NaN)"
}

@target(javascript)
pub fn target_inspect_infinity_test() {
let infinity = 1.0e300 *. 1.0e300
assert string.inspect(infinity) == "//js(Infinity)"
}

@target(erlang)
@external(erlang, "erlang", "self")
fn create_erlang_pid() -> String
Expand Down
Loading