| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="QuantifyExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.Globalization; |
| | | 8 | | using MyNet.Primitives; |
| | | 9 | | using MyNet.Text.Templating; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Humanizer; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Provides extensions for formatting a <see cref="string"/> word as a quantity. |
| | | 17 | | /// </summary> |
| | | 18 | | public static class QuantifyExtensions |
| | | 19 | | { |
| | | 20 | | extension(string input) |
| | | 21 | | { |
| | | 22 | | /// <summary> |
| | | 23 | | /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="quantity">The quantity of the word.</param> |
| | | 26 | | /// <param name="format">A standard or custom numeric format string.</param> |
| | | 27 | | /// <param name="culture">An object that supplies culture-specific formatting information.</param> |
| | | 28 | | /// <example> |
| | | 29 | | /// "request".Quantify(0) => "0 requests" |
| | | 30 | | /// "request".Quantify(10000, format: "N0") => "10,000 requests" |
| | | 31 | | /// "request".Quantify(1, format: "N0") => "1 request". |
| | | 32 | | /// </example> |
| | 15 | 33 | | public string Quantify(int quantity, string? format = null, CultureInfo? culture = null) => input.Quantify((deci |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="quantity">The quantity of the word.</param> |
| | | 39 | | /// <param name="format">A standard or custom numeric format string.</param> |
| | | 40 | | /// <param name="culture">An object that supplies culture-specific formatting information.</param> |
| | | 41 | | /// <example> |
| | | 42 | | /// "request".Quantify(0) => "0 requests" |
| | | 43 | | /// "request".Quantify(10000, format: "N0") => "10,000 requests" |
| | | 44 | | /// "request".Quantify(1, format: "N0") => "1 request". |
| | | 45 | | /// </example> |
| | 21 | 46 | | public string Quantify(double quantity, string? format = null, CultureInfo? culture = null) => input.Quantify((d |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="quantity">The quantity of the word.</param> |
| | | 52 | | /// <param name="format">A standard or custom numeric format string.</param> |
| | | 53 | | /// <param name="culture">An object that supplies culture-specific formatting information.</param> |
| | | 54 | | /// <returns>The formatted quantity string.</returns> |
| | | 55 | | public string Quantify(decimal quantity, string? format = null, CultureInfo? culture = null) |
| | 36 | 56 | | => TextTemplating.Create(x => x.PrefixWithQuantity(quantity, format)).Apply(input, culture.OrCurrent()); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | extension(int quantity) |
| | | 60 | | { |
| | | 61 | | /// <summary> |
| | | 62 | | /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="word">The word to be formatted.</param> |
| | | 65 | | /// <param name="format">A standard or custom numeric format string.</param> |
| | | 66 | | /// <param name="culture">An object that supplies culture-specific formatting information.</param> |
| | | 67 | | /// <returns>The formatted quantity string.</returns> |
| | 3 | 68 | | public string Quantify(string word, string? format = null, CultureInfo? culture = null) => word.Quantify(quantit |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | extension(double quantity) |
| | | 72 | | { |
| | | 73 | | /// <summary> |
| | | 74 | | /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word. |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="word">The word to be formatted.</param> |
| | | 77 | | /// <param name="format">A standard or custom numeric format string.</param> |
| | | 78 | | /// <param name="culture">An object that supplies culture-specific formatting information.</param> |
| | | 79 | | /// <returns>The formatted quantity string.</returns> |
| | 0 | 80 | | public string Quantify(string word, string? format = null, CultureInfo? culture = null) => word.Quantify(quantit |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | extension(decimal quantity) |
| | | 84 | | { |
| | | 85 | | /// <summary> |
| | | 86 | | /// Prefixes the provided word with the number and accordingly pluralizes or singularizes the word. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="word">The word to be formatted.</param> |
| | | 89 | | /// <param name="format">A standard or custom numeric format string.</param> |
| | | 90 | | /// <param name="culture">An object that supplies culture-specific formatting information.</param> |
| | | 91 | | /// <returns>The formatted quantity string.</returns> |
| | 0 | 92 | | public string Quantify(string word, string? format = null, CultureInfo? culture = null) => word.Quantify(quantit |
| | | 93 | | } |
| | | 94 | | } |
| | | 95 | | |