From afdb749ea5b0d636660b58acf1345426c09f9b47 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 13 Feb 2024 20:20:02 +1100 Subject: [PATCH] improve perf of ResourceKeys.GetResourceKey (#1313) --- .../Localisation/ResourceKeys.DateHumanize.cs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Humanizer/Localisation/ResourceKeys.DateHumanize.cs b/src/Humanizer/Localisation/ResourceKeys.DateHumanize.cs index 7b57b44f6..02389fc3f 100644 --- a/src/Humanizer/Localisation/ResourceKeys.DateHumanize.cs +++ b/src/Humanizer/Localisation/ResourceKeys.DateHumanize.cs @@ -1,4 +1,6 @@ -namespace Humanizer.Localisation +using System; + +namespace Humanizer.Localisation { public partial class ResourceKeys { @@ -42,9 +44,19 @@ public static string GetResourceKey(TimeUnit timeUnit, Tense timeUnitTense, int return Now; } - var singularity = count == 1 ? Single : Multiple; + string singularity; + var unit = timeUnit.ToString(); + if (count == 1) + { + singularity = Single; + } + else + { + unit += "s"; + singularity = Multiple; + } + var tense = timeUnitTense == Tense.Future ? FromNow : Ago; - var unit = timeUnit.ToString().ToQuantity(count, ShowQuantityAs.None); return DateTimeFormat.FormatWith(singularity, unit, tense); } }