Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation for ToAnotherOrDefault #65

Merged
merged 15 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions DocsReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,59 @@ bool isConverted = invalidSecondValue.TryToAnother(out AnotherEnum anotherEnum);
// [isConverted, anotherEnum] - [false, First]
```

# ToAnotherOrDefault

**Description**

Convert input enum to another enum or default value if not possible.
kurnakovv marked this conversation as resolved.
Show resolved Hide resolved

**Signature**

TAnotherEnum ToAnotherOrDefault<TAnotherEnum>(this Enum, bool [default = true], TAnotherEnum = default(TAnotherEnum)) where TAnotherEnum : struct

**Type Parameters**

`TAnotherEnum` - Enum that we want try to get after convert or `defaultValue` if not possible.

**Parameters**

- `Enum enumValue` - Input enum we want try to convert to `TAnotherEnum` or `defaultValue` if not possible.
- `bool ignoreCase` - Ignore or regard case.
- `defaultValue` - The default value to return when cannot convert `enumValue` to `TAnotherEnum`.

**Returns**

`TAnotherEnum` or `defaultValue` if not possible to convert.

**Code examples**
```csharp
// Enums.
public enum AnotherEnum { First, Second, Third }

public enum InputEnum { First, Second, Third }
public enum InvalidEnum { FirstInvalidValue, SecondInvalidValue }


// Input enums
InputEnum inputEnumFirst = InputEnum.First;
InvalidEnum invalidSecondValue = InvalidEnum.SecondInvalidValue;
AnotherEnum customDefaultValue = AnotherEnum.Third;
InputEnum inputEnumThird = InputEnum.Third;
```

```csharp
AnotherEnum anotherEnumFirstCorrect = inputEnumFirst.ToAnotherOrDefault<AnotherEnum>();
AnotherEnum anotherEnumSecondInvalid = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>();
AnotherEnum anotherEnumWithDefaultValue = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>(customDefaultValue);
AnotherEnum anotherEnumThirdWithIgnoreCase = inputEnumThird.ToAnotherOrDefault<AnotherEnum>(false);

// Output:
// anotherEnumFirstCorrect - First
// anotherEnumSecondInvalid - default(AnotherEnum)
// anotherEnumWithDefaultValue - Third (customDefaultValue)
// anotherEnumThirdWithIgnoreCase - Third
```

# ToOther

**Description**
Expand Down
6 changes: 3 additions & 3 deletions tests/EnumConverter.UnitTests/EnumConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,17 @@ public void ToAnotherOrDefault_CannotConvertInvalidEnum_DefaultEnumValue()
{
// Arrange.
InvalidEnum invalidSecondValue = InvalidEnum.SecondInvalidValue;
AnotherEnum defaultValue = AnotherEnum.Third;
AnotherEnum customDefaultValue = AnotherEnum.Third;

// Act.
AnotherEnum anotherEnum = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>();
AnotherEnum anotherEnumWithDefaultValue = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>(defaultValue);
AnotherEnum anotherEnumWithDefaultValue = invalidSecondValue.ToAnotherOrDefault<AnotherEnum>(customDefaultValue);

// Assert.
Assert.IsType<AnotherEnum>(anotherEnum);
Assert.IsType<AnotherEnum>(anotherEnumWithDefaultValue);
Assert.Equal(default(AnotherEnum), anotherEnum);
Assert.Equal(defaultValue, anotherEnumWithDefaultValue);
Assert.Equal(customDefaultValue, anotherEnumWithDefaultValue);
}

[Fact]
Expand Down
Loading