Skip to content
Mojtaba Nabavi edited this page Dec 5, 2021 · 2 revisions

Examples of how to use loby tools.

Loby.Serializer

Provides functionality to serialize objects or value types to JSON, XML and to deserialize JSON, XML into objects or value types.

object data = new string[] { "a", "b", "c" };

string toJsonData = Serializer.ToJson(data);
string[] fromJsonData = Serializer.FromJson<string[]>(toJsonData);

string toXmlData = Serializer.ToXml(data);
string[] fromXmlData = Serializer.FromXml<string[]>(toXmlData);

Loby.Pluralizer

Includes a set of practical methods for pluralizing and singularizing words (english only).

string pluralWord = "people";
string singularWord = "person";

string pluralizedWord = Pluralizer.Pluralize(singularWord);
string singularizeWord = Pluralizer.Singularize(pluralWord);

Loby.Validator

Includes a set of practical methods for validation.

var input = "http://www.address.com";

bool isValidUrl = Validator.IsValidUrl(input);
bool isValidEmail = Validator.IsValidEmail(input);

Loby.PasswordHasher

An implementation of PBKDF2 hash algorithm for hashing and verifying passwords.

string password = "p4$$w0rd";

string hashedPassword = PasswordHasher.Hash(password);
bool verifyingHashResult = PasswordHasher.Verify(password, hashedPassword);

Loby.Dater

A set of methods for date conversions along with other practical methods.

DateTime dateTime = DateTime.Now;

// Iranian Date
string toIranianDate = Dater.ToIranSolar(dateTime, format: "yyyy MMMM dd");
DateTime fromIranianDate = Dater.FromIranSolar(toIranianDate);

// Custom - based on culture name
string toCanadaDate = Dater.ToSolar(dateTime, format: "yyyy/MMMM/dd", culture: "ca");
DateTime fromCanadaDate = Dater.FromSolar(toCanadaDate, culture: "ca");

Loby.Mailer

Allows applications to send email by using the Simple Mail Transfer Protocol (SMTP).

string subject = "my subject";
string body = "my text or html";
string recipient = "[email protected]";
string[] recipients = new string[] { "[email protected]", "[email protected]" };

// custom smtp server
Mailer customMailer = new Mailer(host: "smtp.gmail.com", port: 587, "username", "password");

// predefined smtp server
Mailer predefinedMailer = new Mailer(Mailer.ClientTypes.Gmail, "username", "password");

// sending an email
customMailer.Send(recipient, subject, body);
await customMailer.SendAsync(recipient, subject, body);

// sending group email
customMailer.Send(recipients, subject, body);
await customMailer.SendAsync(recipients, subject, body);

Loby.Paginator

Provides pagination for collections.

int[] list = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Paginator.PagingResult<int> result = Paginator.ApplyPaging(source: list, page: 2, pageSize: 4);

// paging metadata
Console.WriteLine(
	$"current page index: {result.CurrentPage}, " +
	$"total pages count: {result.TotalPages}, " +
	$"total items count: {result.TotalItems}");

foreach (var number in result.Items)
{
	Console.WriteLine(number);
}

Loby.Randomizer

Represents a pseudo-random data generator, which is an algorithm that produces a sequence of data that meet certain statistical requirements for randomness.

// Random byte
byte randomByte = Randomizer.RandomByte();
byte[] randomBytes = Randomizer.RandomBytes(8);

// Rnadom int32
int randomInt_1 = Randomizer.RandomInt();
int randomInt_2 = Randomizer.RandomInt(maxValue: 100);
int randomInt_3 = Randomizer.RandomInt(minValue: 10, maxValue: 100);

// Random int64
long randomLong_1 = Randomizer.RandomLong();
long randomLong_2 = Randomizer.RandomLong(maxValue: 100);
long randomLong_3 = Randomizer.RandomLong(minValue: 10, maxValue: 100);

// Random float
float randomFloat = Randomizer.RandomFloat();

// Random double
double randomDouble = Randomizer.RandomDouble();

// Rnadom bool
bool randomBool = Randomizer.RandomBool();

// Random color
Color randomColor = Randomizer.RandomColor();

// Random date and time
DateTime now = DateTime.Now;

DateTime randomDate_1 = Randomizer.RandomDateTime();
DateTime randomDate_2 = Randomizer.RandomDateTime(fromDate: now.AddDays(-10), toDate: now);

// Random GUID
string guid = Randomizer.RandomGuid();

// Random AlphaNmerics
string alphaNmerics = Randomizer.RandomAlphaNmeric(10);

// Random Word(s)
string randomWord = Randomizer.RandomWord();
string randomWords_1 = Randomizer.RandomWords(count: 2);
string randomWords_2 = Randomizer.RandomWords(minCount: 3, maxCount: 5);

// Random Select
string[] listForSelection = new string[] { "a", "b", "c", "d" };

int selected_1 = Randomizer.RandomSelect(1, 2, 3);
string selected_2 = Randomizer.RandomSelect(listForSelection);
IEnumerable<string> selected_3 = Randomizer.RandomSelect(listForSelection, count: 2);
Clone this wiki locally