Skip to content

Commit

Permalink
Merge branch 'master' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuDanK committed Dec 6, 2022
2 parents 7b925ae + c52cc07 commit e51bdc6
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 238 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[toc]

# SageTools

#### 介绍

SageTools 一个简单的常用工具类和拓展方法包


#### 安装教程


#### 使用说明



#### 参与贡献


#### 计划中的功能



#### 详细功能


29 changes: 27 additions & 2 deletions SageTools/Extension/Collection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ public static void PagingToOperate<T>(this ICollection<T> list, Action<ICollecti
/// <param name="list">源数据集合</param>
/// <param name="action">要进行的操作</param>
/// <param name="pageSize">每页大小</param>
public static void PagingToOperate<T>(this ICollection<T> list, Action<ICollection<T>,int> action, int pageSize = 1000)
public static void PagingToOperate<T>(this ICollection<T> list, Action<ICollection<T>, int> action, int pageSize = 1000)
{
if (list.IsNullOrEmpty() || list.Count <= pageSize)
{
action(list,1);
action(list, 1);
}
else
{
Expand All @@ -111,5 +111,30 @@ public static void PagingToOperate<T>(this ICollection<T> list, Action<ICollecti
}
}
}

/// <summary>
/// 页码转偏移量;拒绝大量 pageIndex-1
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public static int PageIndexToOffset(this int pageIndex, int pageSize)
{
return (pageIndex - 1) * pageSize;
}

/// <summary>
/// 计算分页页数
/// </summary>
/// <param name="totalCount">总条数</param>
/// <param name="pageSize">页大小</param>
/// <returns></returns>
public static int ToPageCount(this int totalCount, int pageSize)
{
if (totalCount == 0 || pageSize == 0) return 0;
if (totalCount <= pageSize) return 1;
if (pageSize == 1) return totalCount;
return (int)Math.Ceiling(1D * totalCount / pageSize);
}
}
}
14 changes: 14 additions & 0 deletions SageTools/Extension/DateTime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,19 @@ public static long ToTimeStamp(this DateTime @this)
return (long)(@this - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
}

/// <summary>
/// 时间戳转时间(秒)
/// </summary>
/// <param name="timeStamp">时间戳(秒)</param>
/// <returns></returns>
public static DateTime ToDateTime(this long timeStamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(timeStamp);
}

/// <summary>
/// 转换为日期格式
/// </summary>
public static DateTime ToDateTime(this string @this) => Convert.ToDateTime(@this);
}
}
18 changes: 1 addition & 17 deletions SageTools/Extension/Number.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,23 +105,7 @@ public static bool ToBool(this int num)
return num == 1;
}

/// <summary>
/// 计算分页页数
/// </summary>
/// <param name="totalCount">总条数</param>
/// <param name="pageSize">页大小</param>
/// <returns></returns>
public static int ToPageCount(this int totalCount,int pageSize)
{
if(totalCount==0 || pageSize==0)return 0;
if (totalCount <= pageSize) return 1;
if (pageSize == 1) return totalCount;
return (int)Math.Ceiling(1D * totalCount / pageSize);
}

public static DateTime ToDateTime(this long timeStamp)
{
return new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(timeStamp);
}

}
}
77 changes: 77 additions & 0 deletions SageTools/Extension/Regex.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using System;
using System.Text.RegularExpressions;

namespace SageTools.Extension
{
public static partial class Extension
{
/// <summary>
/// Regex.IsMatch()拓展
/// </summary>
public static bool IsMatch(this string input, string pattern) => Regex.IsMatch(input, pattern);

/// <summary>
/// Regex.IsMatch()拓展
/// </summary>
public static bool IsMatch(this string input, string pattern, RegexOptions options) => Regex.IsMatch(input, pattern, options);

/// <summary>
/// 正则判断是否为数字
/// </summary>
public static bool IsNumeric(this string @this) => !Regex.IsMatch(@this, "[^0-9]");

/// <summary>
/// 是否是正确的base64字符串
/// </summary>
public static bool IsValidBase64String(this string str) => Regex.IsMatch(str, "[A-Za-z0-9\\+\\/\\=]");

/// <summary>
/// 是否是正确的email地址
/// </summary>
public static bool IsValidEmail(this string obj) =>
Regex.IsMatch(obj, "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$");

/// <summary>
/// 是否是正确的ip地址
/// </summary>
public static bool IsValidIP(this string obj) => Regex.IsMatch(obj,
"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$");

/// <summary>
/// 是否是正确的手机号
/// </summary>
public static bool IsValidMobile(string mobile)
{
if (mobile.IsNullOrEmpty())
return false;
mobile = mobile.Trim();
return Regex.IsMatch(mobile, "^(1[3|4|5|6|7|8|9])\\d{9}$", RegexOptions.IgnoreCase);
}

/// <summary>
/// 是否是安全的SQL,防注入
/// </summary>
public static bool IsValidSafeSqlString(this string str) => !Regex.IsMatch(str, "[-|;|,|\\/|\\(|\\)|\\[|\\]|\\}|\\{|%|@|\\*|!|\\']");

/// <summary>
/// 是否是URL
/// </summary>
public static bool IsUrl(this string strUrl) => Regex.IsMatch(strUrl,
"^(http|https)\\://([a-zA-Z0-9\\.\\-]+(\\:[a-zA-Z0-9\\.&%\\$\\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\\-]+\\.)*[a-zA-Z0-9\\-]+\\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\,\\?\\'\\\\\\+&%\\$#\\=~_\\-]+))*$");

/// <summary>
/// 是否为弱密码
/// 注:密码必须包含数字、小写字母、大写字母和其他符号中的两种并且长度大于8
/// </summary>
/// <param name="pwd">密码</param>
/// <returns></returns>
public static bool IsWeakPwd(this string pwd)
{
if (pwd.IsNullOrEmpty())
throw new Exception("pwd不能为空");

const string pattern = "(^[0-9]+$)|(^[a-z]+$)|(^[A-Z]+$)|(^.{0,8}$)";
return Regex.IsMatch(pwd, pattern);
}
}
}
Loading

0 comments on commit e51bdc6

Please sign in to comment.