From 2da71c0cc44d7e23f4fdba77ab619df85c91faaf Mon Sep 17 00:00:00 2001 From: Claude Bot Date: Mon, 12 Jan 2026 02:35:53 +0000 Subject: [PATCH] address review feedback: improve ms.zig and docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Simplify parse loop using for-else pattern (taylordotfish) - Simplify jsMathRound comparison (taylordotfish) - Fix docs: use H3 headings instead of bold text (markovejnovic) - Clarify bundler optimization description in docs 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- docs/runtime/utils.mdx | 8 ++++---- src/bun.js/api/bun/ms.zig | 16 +++++----------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/docs/runtime/utils.mdx b/docs/runtime/utils.mdx index 2eeaf320ca..eca42ef712 100644 --- a/docs/runtime/utils.mdx +++ b/docs/runtime/utils.mdx @@ -450,9 +450,9 @@ namespace Bun { ## `Bun.ms` -Built-in alternative to the [`ms`](https://npmjs.com/package/ms) package. Convert between human-readable time strings and milliseconds. Automatically optimized at compile-time when bundling. +Built-in alternative to the [`ms`](https://npmjs.com/package/ms) package. Convert between human-readable time strings and milliseconds. When bundling, calls with constant arguments are evaluated at compile-time and replaced with the result. -**Convert to Milliseconds** +### Convert to Milliseconds ```ts Bun.ms("2 days"); // 172800000 @@ -469,7 +469,7 @@ Bun.ms("-1h"); // -3600000 Bun.ms("-200"); // -200 ``` -**Convert from Milliseconds** +### Convert from Milliseconds ```ts Bun.ms(60000); // "1m" @@ -483,7 +483,7 @@ Bun.ms(-3 * 60000, { long: true }); // "-3 minutes" Bun.ms(Bun.ms("10 hours"), { long: true }); // "10 hours" ``` -**Bundler Support** +### Bundler Support ```sh $ cat ./example.ts diff --git a/src/bun.js/api/bun/ms.zig b/src/bun.js/api/bun/ms.zig index f63e544eb0..6533a0acc9 100644 --- a/src/bun.js/api/bun/ms.zig +++ b/src/bun.js/api/bun/ms.zig @@ -2,17 +2,11 @@ pub fn parse(input: []const u8) ?f64 { if (input.len == 0 or input.len > 100) return null; - var i: usize = 0; - - next: switch (input[i]) { - '-', '.', '0'...'9' => { - i += 1; - if (i < input.len) continue :next input[i]; - break :next; - }, - ' ', 'a'...'z', 'A'...'Z' => break :next, + const i: usize = for (input, 0..) |c, j| switch (c) { + '-', '.', '0'...'9' => {}, + ' ', 'a'...'z', 'A'...'Z' => break j, else => return null, - } + } else input.len; const value = std.fmt.parseFloat(f64, input[0..i]) catch return null; @@ -88,7 +82,7 @@ const MultiplierMap = bun.ComptimeStringMap(f64, .{ // JavaScript's Math.round uses "round half toward +∞": ties round toward positive infinity (2.5→3, -2.5→-2) fn jsMathRound(x: f64) i64 { const i: f64 = @ceil(x); - if ((i - 0.5) > x) return @intFromFloat(i - 1.0); + if ((i - x) > 0.5) return @intFromFloat(i - 1.0); return @intFromFloat(i); }