-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ Add humanizer for text
- Loading branch information
Showing
13 changed files
with
160 additions
and
160 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Security.Cryptography; | ||
using nHash.Features.Models; | ||
|
||
namespace nHash.Features; | ||
|
||
public class HashAlgorithmFeature : IFeature | ||
{ | ||
public Command Command => GetFeatureCommand(); | ||
private readonly Argument<string> _textArgument; | ||
private readonly Argument<AlgorithmType> _algorithmType; | ||
private readonly Option<string> _fileName; | ||
private readonly Option<bool> _lowerCase; | ||
|
||
|
||
public HashAlgorithmFeature() | ||
{ | ||
_algorithmType = new Argument<AlgorithmType>("type", "Algorithm type"); | ||
_textArgument = new Argument<string>("text", "Text for calculate fingerprint"); | ||
_fileName = new Option<string>(name: "--file", description: "File name for calculate hash"); | ||
_lowerCase = new Option<bool>(name: "--lower", description: "Generate lower case"); | ||
} | ||
|
||
private Command GetFeatureCommand() | ||
{ | ||
var command = new Command("hash", | ||
"Calculate hash fingerprint (MD5, SHA-1, SHA-256, SHA-384, SHA-512)") | ||
{ | ||
_fileName, | ||
_lowerCase, | ||
}; | ||
command.AddArgument(_algorithmType); | ||
command.AddArgument(_textArgument); | ||
command.SetHandler(CalculateText, _textArgument, _algorithmType, _lowerCase, _fileName); | ||
|
||
return command; | ||
} | ||
|
||
private static void CalculateText(string text, AlgorithmType algorithmType, bool lowerCase, string fileName) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(text)) | ||
{ | ||
var inputBytes = System.Text.Encoding.UTF8.GetBytes(text); | ||
CalculateHash(inputBytes, algorithmType, lowerCase); | ||
return; | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(fileName)) | ||
{ | ||
var fileBytes = File.ReadAllBytes(fileName); | ||
CalculateHash(fileBytes, algorithmType, lowerCase); | ||
} | ||
} | ||
|
||
private static void CalculateHash(byte[] inputBytes, AlgorithmType algorithmType, bool lowerCase) | ||
{ | ||
HashAlgorithm provider = algorithmType switch | ||
{ | ||
AlgorithmType.MD5 => MD5.Create(), | ||
AlgorithmType.SHA1 => SHA1.Create(), | ||
AlgorithmType.SHA256 => SHA256.Create(), | ||
AlgorithmType.SHA384 => SHA384.Create(), | ||
AlgorithmType.SHA512 => SHA512.Create(), | ||
_ => throw new ArgumentOutOfRangeException(nameof(algorithmType), algorithmType, null) | ||
}; | ||
|
||
var hashBytes = provider.ComputeHash(inputBytes); | ||
var hashedText = Convert.ToHexString(hashBytes); | ||
|
||
if (lowerCase) | ||
{ | ||
hashedText = hashedText.ToLower(); | ||
} | ||
|
||
Console.WriteLine(hashedText); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using Humanizer; | ||
using nHash.Features.Models; | ||
|
||
namespace nHash.Features; | ||
|
||
public class HumanizeFeature : IFeature | ||
{ | ||
public Command Command => GetFeatureCommand(); | ||
private readonly Argument<string> _textArgument; | ||
private readonly Argument<HumanizeType> _humanizeType; | ||
|
||
public HumanizeFeature() | ||
{ | ||
_humanizeType = new Argument<HumanizeType>("type", "Humanize type"); | ||
_textArgument = new Argument<string>("text", "Text for humanize"); | ||
} | ||
|
||
private Command GetFeatureCommand() | ||
{ | ||
var command = new Command("humanize", | ||
"Humanizer text (Pascal-case, Camel-case, Kebab, Underscore, lowercase etc)"); | ||
command.AddArgument(_humanizeType); | ||
command.AddArgument(_textArgument); | ||
command.SetHandler(CalculateText, _textArgument, _humanizeType); | ||
|
||
return command; | ||
} | ||
|
||
private static void CalculateText(string text, HumanizeType humanizeType) | ||
{ | ||
var resultText = humanizeType switch | ||
{ | ||
HumanizeType.Humanize => text.Humanize(), | ||
HumanizeType.Dehumanize => text.Dehumanize(), | ||
HumanizeType.Pascal => text.Pascalize(), | ||
HumanizeType.Camel => text.Camelize(), | ||
HumanizeType.Kebab => text.Kebaberize(), | ||
HumanizeType.Underscore => text.Underscore(), | ||
HumanizeType.Hyphenate => text.Hyphenate(), | ||
HumanizeType.Lowercase => text.ToLower(), | ||
HumanizeType.Uppercase => text.ToUpper(), | ||
_ => text | ||
}; | ||
|
||
Console.WriteLine(resultText); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace nHash.Features.Models; | ||
|
||
public enum AlgorithmType | ||
{ | ||
MD5, | ||
SHA1, | ||
SHA256, | ||
SHA384, | ||
SHA512, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace nHash.Features.Models; | ||
|
||
public enum HumanizeType | ||
{ | ||
Humanize, | ||
Dehumanize, | ||
Pascal, | ||
Camel, | ||
Kebab, | ||
Underscore, | ||
Hyphenate, | ||
Lowercase, | ||
Uppercase, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
global using nHash.Features; | ||
global using nHash.Base; | ||
global using System.CommandLine; | ||
global using nHash.Features.HashAlgorithms; | ||
global using System.CommandLine; |
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
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