Make an ordinal formatting funcrion
I am hungry
This commit is contained in:
parent
78e416eeaf
commit
3345b20f1f
3 changed files with 23 additions and 2 deletions
|
|
@ -2,6 +2,7 @@ const std = @import("std");
|
|||
const jetzig = @import("jetzig");
|
||||
const jetquery = @import("jetzig").jetquery;
|
||||
const dateFmt = @import("../../date_fmt.zig").dateFmt;
|
||||
const ordinalFmt = @import("../../ordinal_fmt.zig").ordinalFmt;
|
||||
|
||||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
|
|
@ -57,7 +58,11 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||
|
||||
if (try artist_jq_result.postgresql.result.next()) |artist_row| {
|
||||
const artist = try artist_row.to(ArtistResult, .{ .dupe = true, .allocator = request.allocator });
|
||||
try root.put("artist", artist);
|
||||
var artist_view = try root.put("artist", .object);
|
||||
try artist_view.put("name", artist.name);
|
||||
try artist_view.put("id", artist.id);
|
||||
try artist_view.put("scrobbles", artist.scrobbles);
|
||||
try artist_view.put("rank", ordinalFmt(request.allocator, artist.rank));
|
||||
}
|
||||
try artist_jq_result.drain();
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
<body>
|
||||
@partial partials/header
|
||||
<h1>{{.artist.name}}</h1>
|
||||
{{.artist.scrobbles}} scrobbles ({{.artist.rank}}th place)
|
||||
{{.artist.scrobbles}} scrobbles ({{.artist.rank}} place)
|
||||
<br>
|
||||
First listen: <a href="/songs/{{.first_song_id}}">{{.first_song_name}}</a> ({{.first_song_date}})
|
||||
<br>
|
||||
|
|
|
|||
16
src/ordinal_fmt.zig
Normal file
16
src/ordinal_fmt.zig
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
const std = @import("std");
|
||||
//const log = std.math.log10;
|
||||
|
||||
pub fn ordinalFmt(allocator: std.mem.Allocator, ord: isize) ![]const u8 {
|
||||
const buff = try allocator.alloc(u8, 3 + @as(usize, @intFromFloat(@floor(@log10(@as(f64, @floatFromInt(ord)))))));
|
||||
const ind: []const u8 = switch (@mod(ord, 100)) {
|
||||
11, 12, 13 => "th",
|
||||
else => switch (@mod(ord, 10)) {
|
||||
1 => "st",
|
||||
2 => "nd",
|
||||
3 => "rd",
|
||||
else => "th",
|
||||
},
|
||||
};
|
||||
return std.fmt.bufPrint(buff, "{}{s}", .{ ord, ind });
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue