Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

CommonExtensions.cs

using System.Globalization;
using Humanizer;
using Humanizer.Localisation;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Nop.Web.Framework.Extensions;

/// <summary>
/// Extensions
/// </summary>
public static class CommonExtensions
{
    /// <summary>
    /// Returns a value indicating whether real selection is not possible
    /// </summary>
    /// <param name="items">Items</param>
    /// <param name="ignoreZeroValue">A value indicating whether we should ignore items with "0" value</param>
    /// <returns>A value indicating whether real selection is not possible</returns>
    public static bool SelectionIsNotPossible(this IList<SelectListItem> items, bool ignoreZeroValue = true)
    {
        ArgumentNullException.ThrowIfNull(items);

        //we ignore items with "0" value? Usually it's something like "Select All", "etc
        return items.Count(x => !ignoreZeroValue || !x.Value.ToString().Equals("0")) < 2;
    }

    /// <summary>
    /// Relative formatting of DateTime (e.g. 2 hours ago, a month ago)
    /// </summary>
    /// <param name="source">Source (UTC format)</param>
    /// <param name="languageCode">Language culture code</param>
    /// <returns>Formatted date and time string</returns>
    public static string RelativeFormat(this DateTime source, string languageCode = "en-US")
    {
        var ts = new TimeSpan(DateTime.UtcNow.Ticks - source.Ticks);
        var delta = ts.TotalSeconds;

        CultureInfo culture;
        try
        {
            culture = new CultureInfo(languageCode);
        }
        catch (CultureNotFoundException)
        {
            culture = new CultureInfo("en-US");
        }
        return TimeSpan.FromSeconds(delta).Humanize(precision: 1, culture: culture, maxUnit: TimeUnit.Year);
    }
}