text: ﻿using System;
  using System.Linq;
  using System.Reflection;
  using System.Text;
  
  namespace Surging.Core.SwaggerGen
  {
      public static class TypeExtensions
      {
          public static string FriendlyId(this Type type, bool fullyQualified = false)
          {
              var typeName = fullyQualified
                  ? type.FullNameSansTypeArguments().Replace("+", ".")
                  : type.Name;
  
              if (type.GetTypeInfo().IsGenericType)
              {
                  var genericArgumentIds = type.GetGenericArguments()
                      .Select(t => t.FriendlyId(fullyQualified))
                      .ToArray();
  
                  return new StringBuilder(typeName)
                      .Replace(string.Format("`{0}", genericArgumentIds.Count()), string.Empty)
                      .Append(string.Format("[{0}]", string.Join(",", genericArgumentIds).TrimEnd(',')))
                      .ToString();
              }
  
              return typeName;
          }
  
          internal static bool IsNullable(this Type type)
          {
              return Nullable.GetUnderlyingType(type) != null;
          }
  
          internal static bool IsFSharpOption(this Type type)
          {
              return type.FullNameSansTypeArguments() == "Microsoft.FSharp.Core.FSharpOption`1";
          }
  
          private static string FullNameSansTypeArguments(this Type type)
          {
              if (string.IsNullOrEmpty(type.FullName)) return string.Empty;
  
              var fullName = type.FullName;
              var chopIndex = fullName.IndexOf("[[");
              return (chopIndex == -1) ? fullName : fullName.Substring(0, chopIndex);
          }
      }
  }
  