mirror of
https://github.com/oven-sh/bun
synced 2026-02-10 10:58:56 +00:00
address review feedback: improve ms.zig and docs
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user