Last active
August 29, 2015 13:57
-
-
Save angelobelchior/9840485 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace System | |
{ | |
public static class IntExtentions | |
{ | |
#region [ Times ] | |
public static void Times(this int self, Action action) | |
{ | |
Throw.IfIsNull(action); | |
Throw.IfLessThanOrEqZero(self); | |
for (int i = 0; i < self; i++) | |
action(); | |
} | |
public static void Times(this int self, Action<int> action) | |
{ | |
Throw.IfIsNull(action); | |
Throw.IfLessThanOrEqZero(self); | |
for (int i = 0; i < self; i++) | |
action(i); | |
} | |
#endregion [ Times ] | |
#region [ PercentOf ] | |
public static int PercentOf(this int self, int value) | |
{ | |
return (value * self) / 100; | |
} | |
public static long PercentOf(this int self, long value) | |
{ | |
return (value * self) / 100; | |
} | |
public static float PercentOf(this int self, float value) | |
{ | |
return (value * self) / 100; | |
} | |
public static decimal PercentOf(this int self, decimal value) | |
{ | |
return (value * self) / 100; | |
} | |
public static double PercentOf(this int self, double value) | |
{ | |
return (value * self) / 100; | |
} | |
#endregion [ PercentOf ] | |
public static double Pow(this int self, int y) | |
{ | |
return Math.Pow(self, y); | |
} | |
public static double Factorial(this int self) | |
{ | |
if (self <= 1) | |
return 1; | |
else | |
return self * Factorial(self - 1); | |
} | |
//Test: phrase = "{{|One|Two}} {{|car|cars}}" | |
//Test: phrase = "{{0|1|[value]}} {{|car|cars}}" | |
public static string Pluralization(this int self, string phrase) | |
{ | |
var valuePhrase = Regex.Replace(phrase, @"\[value]", self.ToString()); | |
var result = Regex.Match(valuePhrase, @"\{{.*?\}}"); | |
foreach (Group group in result.Groups) | |
{ | |
if (string.IsNullOrWhiteSpace(group.Value)) | |
break; | |
var content = group.Value.Replace("{{", "").Replace("}}", ""); | |
var parts = content.Split('|'); | |
Throw.IfIsFalse(parts.Length == 3, new IndexOutOfRangeException(string.Format("Invalid content template: {0}", group.Value))); | |
var newPhrase = string.Empty; | |
if (self == 0) | |
newPhrase = valuePhrase.Replace(group.Value, parts[0]); | |
if (self == 1) | |
newPhrase = valuePhrase.Replace(group.Value, parts[1]); | |
if (self > 1) | |
newPhrase = valuePhrase.Replace(group.Value, parts[2]); | |
return self.Pluralization(newPhrase); | |
} | |
return phrase; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment