mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-23 00:38:47 +00:00
Update README and CHANGELOG
This commit is contained in:
parent
6505299c00
commit
aaf0e67679
@ -1,6 +1,9 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
## [3.0.0 (Unreleased)]
|
## [3.0.0]
|
||||||
|
|
||||||
|
In the midst of writing these release notes, I may have missed some important changes. If you notice an API change that is not documented here,
|
||||||
|
please [open an issue](https://github.com/oliverbooth/X10D/issues)!
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- Added `T.AsArrayValue()`
|
- Added `T.AsArrayValue()`
|
||||||
@ -296,7 +299,7 @@
|
|||||||
|
|
||||||
### ***Not documented***
|
### ***Not documented***
|
||||||
|
|
||||||
[3.0.0 (Unreleased)]: https://github.com/oliverbooth/X10D/tree/develop
|
[3.0.0]: https://github.com/oliverbooth/X10D/releases/tag/3.0.0
|
||||||
[2.6.0]: https://github.com/oliverbooth/X10D/releases/tag/2.6.0
|
[2.6.0]: https://github.com/oliverbooth/X10D/releases/tag/2.6.0
|
||||||
[2.5.0]: https://github.com/oliverbooth/X10D/releases/tag/2.5.0
|
[2.5.0]: https://github.com/oliverbooth/X10D/releases/tag/2.5.0
|
||||||
[2.2.0]: https://github.com/oliverbooth/X10D/releases/tag/2.2.0
|
[2.2.0]: https://github.com/oliverbooth/X10D/releases/tag/2.2.0
|
||||||
|
239
README.md
239
README.md
@ -33,245 +33,22 @@ X10D (pronounced *extend*), is a .NET package that provides extension methods fo
|
|||||||
You can find the list of classes that have extension methods by viewing the `README.md` file in any of the respective library folders.
|
You can find the list of classes that have extension methods by viewing the `README.md` file in any of the respective library folders.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
### Unity installation
|
|
||||||
1. Open the package manager, open the `+` menu and select `Add package from git URL...`.
|
|
||||||
|
|
||||||
![](https://user-images.githubusercontent.com/1129769/109844969-8ec7a100-7c44-11eb-9729-9a03be703d1b.png)
|
|
||||||
|
|
||||||
2) Enter the URL below and press Add.
|
|
||||||
```cs
|
|
||||||
https://github.com/oliverbooth/X10D.git#upm
|
|
||||||
```
|
|
||||||
|
|
||||||
### NuGet installation
|
### NuGet installation
|
||||||
```ps
|
```ps
|
||||||
Install-Package X10D -Version 2.6.0
|
Install-Package X10D -Version 3.0.0
|
||||||
```
|
```
|
||||||
|
|
||||||
### Manual installation
|
### Manual installation
|
||||||
Download the [latest release](https://github.com/oliverbooth/X10D/releases/latest) from this repository and adding a direct assembly reference for your chosen platform.
|
Download the [latest release](https://github.com/oliverbooth/X10D/releases/latest) from this repository and adding a direct assembly reference for your chosen platform.
|
||||||
|
|
||||||
|
### What happened to X10D.Unity?
|
||||||
|
I decided it was time for X10D to be migrated to .NET 6. Unity currently supports .NET Framework 4.x / .NET Standard 2.1, and as such is not compatible with X10D at this time.
|
||||||
|
Unity have announced official support for .NET 6 in the future (see [this forum post](https://forum.unity.com/threads/unity-future-net-development-status.1092205/) for more details),
|
||||||
|
but until such a time that Unity supports .NET 6, X10D.Unity will not be maintained or innovated upon.
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
### Numeric extensions
|
I'm planning on writing complete and extensive documentation in the near future. As of this time, feel free to browse the source or the API using your favourite IDE.
|
||||||
> 👍 ProTip: *Most* of the extensions available for `int` will also exist for `short`, `long`, and their unsigned counterparts!
|
For those familiar with the 2.6.0 API, please read [CHANGELOG.md](CHANGELOG.md) for a complete list of changes. **3.0.0 is a major release and introduces many breaking changes.**
|
||||||
|
|
||||||
#### `bool` <-> `int`
|
|
||||||
Convert a `bool` to an `int` by using `ToInt32`. The value returned is 1 if the input is `true`, and 0 if it's `false`.
|
|
||||||
```cs
|
|
||||||
bool b = true;
|
|
||||||
|
|
||||||
int i = b.ToInt32(); // 1
|
|
||||||
```
|
|
||||||
The same also works in reverse. Using `ToBoolean` on an `int` will return `false` if the input is 0, and `true`if the input is anything else.
|
|
||||||
```cs
|
|
||||||
int zero = 0;
|
|
||||||
long nonZero = 1;
|
|
||||||
|
|
||||||
bool b1 = zero.ToBoolean(); // false
|
|
||||||
bool b2 = nonZero.ToBoolean(); // true
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Between
|
|
||||||
Determine if a value is between other values using `Between` like so:
|
|
||||||
```cs
|
|
||||||
int i = 3;
|
|
||||||
|
|
||||||
if (i.Between(2, 4))
|
|
||||||
{
|
|
||||||
// i is between 2 and 4!
|
|
||||||
}
|
|
||||||
```
|
|
||||||
Since the signature of this method is defined with a generic constraint of `IComparable<T>`, this will also work for any object that is `IComparable<T>` - not just numeric types!
|
|
||||||
```cs
|
|
||||||
bool Between<T>(this T actual, T lower, T upper) where T : IComparable<T>
|
|
||||||
```
|
|
||||||
|
|
||||||
#### IsEven (*and IsOdd*)
|
|
||||||
As the names suggest, this method determines if the input value is evenly divisible by 2.
|
|
||||||
```cs
|
|
||||||
int i = 5;
|
|
||||||
bool b = i.IsEven(); // false
|
|
||||||
```
|
|
||||||
There is also an `IsOdd` extension method, which will return the opposite of that returned by `IsEven`.
|
|
||||||
|
|
||||||
#### IsPrime
|
|
||||||
Determine if an integral is a prime number by using `IsPrime`.
|
|
||||||
```cs
|
|
||||||
bool b = 43.IsPrime(); // true
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Clamp
|
|
||||||
Clamp a value between an upper and lower bound
|
|
||||||
```cs
|
|
||||||
int i = 5.Clamp(0, 3); // 3
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Convert degrees <-> radians
|
|
||||||
Easily convert between radians and degrees
|
|
||||||
```cs
|
|
||||||
double rad = 2 * Math.PI;
|
|
||||||
double deg = rad.RadiansToDegrees(); // 360
|
|
||||||
|
|
||||||
rad = deg.DegreesToRadians(); // back to 2*pi
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Round
|
|
||||||
Round a value to the nearest whole number:
|
|
||||||
```cs
|
|
||||||
var d = 2.75;
|
|
||||||
var rounded = d.Round(); // 3
|
|
||||||
```
|
|
||||||
Or specify a value to have it round to the nearest multiple of `x`:
|
|
||||||
```cs
|
|
||||||
double a = 8.0.Round(10); // 10
|
|
||||||
double b = 2.0.Round(10); // 0
|
|
||||||
```
|
|
||||||
|
|
||||||
### String
|
|
||||||
#### Repeat value
|
|
||||||
Repeat a string or a char a specific number of times using `Repeat`
|
|
||||||
```cs
|
|
||||||
var c = '-';
|
|
||||||
var str = "foo";
|
|
||||||
|
|
||||||
string repeatedC = c.Repeat(10); // ----------
|
|
||||||
string repeatedStr = str.Repeat(5); // foofoofoofoofoo
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Base-64 encode/decode
|
|
||||||
```cs
|
|
||||||
var base64 = "Hello World".Base64Encode(); // SGVsbG8gV29ybGQ=
|
|
||||||
var str = "SGVsbG8gV29ybGQ=".Base64Decode(); // Hello World
|
|
||||||
```
|
|
||||||
|
|
||||||
### DateTime
|
|
||||||
|
|
||||||
#### Age
|
|
||||||
Get a rounded integer representing the number of years since a given date. i.e. easily calculate someone's age:
|
|
||||||
```cs
|
|
||||||
var dateOfBirth = new DateTime(1960, 7, 16);
|
|
||||||
int age = dateOfBirth.Age(); // the age as of today
|
|
||||||
```
|
|
||||||
You can also specify a date at which to stop counting the years, by passing an `asOf` date:
|
|
||||||
```cs
|
|
||||||
var dateOfBirth = new DateTime(1960, 7, 16);
|
|
||||||
int age = dateOfBirth.Age(new DateTime(1970, 7, 16)); // 10, the age as of 1970
|
|
||||||
```
|
|
||||||
|
|
||||||
#### To/From Unix Timestamp
|
|
||||||
Convert to/from a Unix timestamp represented in seconds using `FromUnixTimestamp` on a numeric type, and `ToUnixTimestamp` on a `DateTime`.
|
|
||||||
```cs
|
|
||||||
long sec = 1587223415;
|
|
||||||
DateTime time = sec.FromUnixTimestamp(); // 2020-04-18 15:23:35
|
|
||||||
long unix = time.ToUnixTimestamp();
|
|
||||||
```
|
|
||||||
or represent it with milliseconds by passing `true` for the `isMillis` argument:
|
|
||||||
```cs
|
|
||||||
long millis = 1587223415500;
|
|
||||||
DateTime time = millis.FromUnixTimestamp(true); // 2020-04-18 15:23:35.50
|
|
||||||
long unix = time.ToUnixTimestamp(true);
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Get first/last day of month
|
|
||||||
Get the first or last day of the month by using `FirstDayOfMonth` and `LastDayOfMonth`
|
|
||||||
```cs
|
|
||||||
var dt = new DateTime(2016, 2, 4);
|
|
||||||
|
|
||||||
DateTime first = dt.FirstDayOfMonth(); // 2016-02-01
|
|
||||||
DateTime last = dt.LastDayOfMonth(); // 2016-02-29 (2016 is a leap year)
|
|
||||||
```
|
|
||||||
You can also use `First` or `Last` to get the first or final occurrence of a specific day of the week in a given month:
|
|
||||||
```cs
|
|
||||||
var dt = new DateTime(2019, 4, 14);
|
|
||||||
|
|
||||||
DateTime theLastFriday = dt.Last(DayOfWeek.Friday); // 2019-04-24
|
|
||||||
DateTime theLastThursday = dt.Last(DayOfWeek.Thursday); // 2019-04-40
|
|
||||||
```
|
|
||||||
|
|
||||||
### Enumerable
|
|
||||||
#### Split into chunks
|
|
||||||
Split an `IEnumerable<T>` into an `IEnumerable<IEnumerable<T>>`, essentially "chunking" the original IEnumerable into a specific size
|
|
||||||
```cs
|
|
||||||
var arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
|
|
||||||
var chunks = arr.Split(2); // split into chunks of 2
|
|
||||||
|
|
||||||
foreach (var chunk in chunks)
|
|
||||||
{
|
|
||||||
Console.WriteLine(string.Join(", ", chunk));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// 1, 2
|
|
||||||
// 3, 4
|
|
||||||
// 5, 6
|
|
||||||
// 7, 8
|
|
||||||
```
|
|
||||||
This also works for `string`:
|
|
||||||
```cs
|
|
||||||
var str = "Hello World";
|
|
||||||
var chunks = str.Split(2); // split string into chunks of 2
|
|
||||||
|
|
||||||
foreach (var chunk in chunks)
|
|
||||||
{
|
|
||||||
Console.WriteLine(string.Join(string.Empty, chunk));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output:
|
|
||||||
// He
|
|
||||||
// ll
|
|
||||||
// o <-- space is included
|
|
||||||
// Wo
|
|
||||||
// rl
|
|
||||||
// d <-- no space! end of string
|
|
||||||
```
|
|
||||||
|
|
||||||
### Enum
|
|
||||||
#### Parse string into enum
|
|
||||||
You can use the `EnumParse` method to convert a string into a value from an enum, while optionally ignoring case:
|
|
||||||
```cs
|
|
||||||
enum Number
|
|
||||||
{
|
|
||||||
Zero,
|
|
||||||
One,
|
|
||||||
Two,
|
|
||||||
Three,
|
|
||||||
}
|
|
||||||
|
|
||||||
Number num = "two".EnumParse<Number>(true); // num == Number.Two
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `Next` / `Previous` enum cycling
|
|
||||||
Cycle through the values in an enum with `Next` and `Previous`:
|
|
||||||
```cs
|
|
||||||
Number two = Number.Two;
|
|
||||||
|
|
||||||
Number one = two.Previous();
|
|
||||||
Number three = two.Next();
|
|
||||||
```
|
|
||||||
|
|
||||||
### Conversion
|
|
||||||
Easily convert between types using `To`, `ToOrNull`, `ToOrDefault`, or `ToOrOther`, thereby shortening the call to `Convert.ChangeType` or `Convert.ToX`:
|
|
||||||
```CS
|
|
||||||
int i = "43".To<int>();
|
|
||||||
int j = "a".ToOrDefault<int>(); // 0
|
|
||||||
int k = "a".ToOrOther<int>(100); // 100
|
|
||||||
```
|
|
||||||
|
|
||||||
### Random
|
|
||||||
Do more with Random including flip a coin, randomly select an element in an array, or shuffle the array entirely.
|
|
||||||
```cs
|
|
||||||
var random = new Random();
|
|
||||||
|
|
||||||
// flip a coin
|
|
||||||
bool heads = random.CoinToss();
|
|
||||||
|
|
||||||
// randomly choose an item
|
|
||||||
var arr = new int[] { 1, 2, 3, 4 };
|
|
||||||
var item = random.OneOf(arr);
|
|
||||||
|
|
||||||
// shuffle an array or list
|
|
||||||
var shuffled = arr.Shuffle(random);
|
|
||||||
```
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md).
|
||||||
|
Loading…
Reference in New Issue
Block a user