Skip to content

Commit 52547f7

Browse files
authored
Merge pull request #84 from shinji-san/remove-ObsoleteIsValidMethods
Remove obsolete IsValid methods Resolves: #84
2 parents 534dc9a + 5e42882 commit 52547f7

File tree

3 files changed

+21
-87
lines changed

3 files changed

+21
-87
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
### Removed
9+
- Removed `Luhn.IsValid` methods
10+
711
## [1.3.0] - 2024-12-27
812
### Added
913
- Add project icon

src/Luhn.cs

+17-75
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public static class Luhn
5252
/// </summary>
5353
private const int Modulus = 10;
5454

55+
/// <summary>
56+
/// Represents the ASCII code for the character '0'.
57+
/// </summary>
58+
private const int AsciiCodeForZero = 48;
59+
5560
#if NET8_0_OR_GREATER
5661
/// <summary>
5762
/// Computes the Luhn check digit
@@ -63,7 +68,7 @@ public static class Luhn
6368
[SuppressMessage("ReSharper", "UnusedMember.Global")]
6469
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
6570
public static byte ComputeLuhnCheckDigit(this ReadOnlySpan<char> number) =>
66-
(byte)((Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
71+
(byte)((Modulus - number.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
6772
#endif
6873

6974
/// <summary>
@@ -79,7 +84,7 @@ public static byte ComputeLuhnCheckDigit(this string number) =>
7984
#if NET8_0_OR_GREATER
8085
number.AsSpan().ComputeLuhnCheckDigit();
8186
#else
82-
(byte)((Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
87+
(byte)((Modulus - number.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
8388
#endif
8489

8590
#if NET8_0_OR_GREATER
@@ -123,21 +128,6 @@ public static string ComputeLuhnNumber(this string number)
123128
}
124129

125130
#if NET8_0_OR_GREATER
126-
/// <summary>
127-
/// Checks whether the Luhn Number is valid
128-
/// </summary>
129-
/// <param name="luhnNumber">An identification number w/ check digit (Luhn Number).</param>
130-
/// <returns><see langword="true" /> if the <paramref name="luhnNumber"/> is valid;
131-
/// otherwise <see langword="false" /></returns>
132-
/// <exception cref="ArgumentException"><paramref name="luhnNumber"/> is not valid.
133-
/// It contains none-numeric characters.</exception>
134-
/// <remarks>The check digit must be at the end of the <paramref name="luhnNumber"/>
135-
/// (on the right side).</remarks>
136-
[SuppressMessage("ReSharper", "UnusedMember.Global")]
137-
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
138-
[Obsolete("Use the IsValidLuhnNumber method instead.", false)]
139-
public static bool IsValid(this ReadOnlySpan<char> luhnNumber) => luhnNumber.IsValidLuhnNumber();
140-
141131
/// <summary>
142132
/// Checks whether the Luhn Number is valid
143133
/// </summary>
@@ -151,24 +141,9 @@ public static string ComputeLuhnNumber(this string number)
151141
[SuppressMessage("ReSharper", "UnusedMember.Global")]
152142
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
153143
public static bool IsValidLuhnNumber(this ReadOnlySpan<char> luhnNumber) =>
154-
luhnNumber.IsNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
144+
luhnNumber.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
155145
#endif
156146

157-
/// <summary>
158-
/// Checks whether the Luhn Number is valid
159-
/// </summary>
160-
/// <param name="luhnNumber">An identification number w/ check digit (Luhn Number).</param>
161-
/// <returns><see langword="true" /> if the <paramref name="luhnNumber"/> is valid;
162-
/// otherwise <see langword="false" /></returns>
163-
/// <exception cref="ArgumentException"><paramref name="luhnNumber"/> is not valid.
164-
/// It contains none-numeric characters.</exception>
165-
/// <remarks>The check digit must be at the end of the <paramref name="luhnNumber"/>
166-
/// (on the right side).</remarks>
167-
[SuppressMessage("ReSharper", "UnusedMember.Global")]
168-
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
169-
[Obsolete("Use the IsValidLuhnNumber method instead.", false)]
170-
public static bool IsValid(this string luhnNumber) => luhnNumber.IsValidLuhnNumber();
171-
172147
/// <summary>
173148
/// Checks whether the Luhn Number is valid
174149
/// </summary>
@@ -185,27 +160,10 @@ public static bool IsValidLuhnNumber(this string luhnNumber) =>
185160
#if NET8_0_OR_GREATER
186161
luhnNumber.AsSpan().IsValidLuhnNumber();
187162
#else
188-
luhnNumber.IsNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
163+
luhnNumber.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
189164
#endif
190165

191166
#if NET8_0_OR_GREATER
192-
/// <summary>
193-
/// Checks whether the concatenation of number and corresponding Luhn check digit is valid
194-
/// </summary>
195-
/// <param name="number">Identification number w/o Luhn check digit</param>
196-
/// <param name="checkDigit">The Luhn check digit</param>
197-
/// <returns><see langword="true" /> if the <paramref name="number"/> is valid;
198-
/// otherwise <see langword="false" /></returns>
199-
/// <exception cref="ArgumentException"><paramref name="number"/> is not valid.
200-
/// It contains none-numeric characters.</exception>
201-
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="checkDigit"/> value is greater than 9.
202-
/// The <paramref name="checkDigit"/> value must be between 0 and 9.</exception>
203-
[SuppressMessage("ReSharper", "UnusedMember.Global")]
204-
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
205-
[Obsolete("Use the IsValidLuhnCheckDigit method instead.", false)]
206-
public static bool IsValid(this ReadOnlySpan<char> number, byte checkDigit) =>
207-
checkDigit.IsValidLuhnCheckDigit(number);
208-
209167
/// <summary>
210168
/// Checks whether the concatenation of number and corresponding Luhn check digit is valid
211169
/// </summary>
@@ -231,29 +189,13 @@ public static bool IsValidLuhnCheckDigit(this byte checkDigit, ReadOnlySpan<char
231189

232190
return string.Concat(number.Trim(), checkDigit.ToString(CultureInfo.InvariantCulture))
233191
.AsSpan()
234-
.IsNumber()
192+
.ValidateAndTrimNumber()
235193
.GetDigits()
236194
.DoubleEverySecondDigit(true)
237195
.SumDigits() == 0;
238196
}
239197
#endif
240198

241-
/// <summary>
242-
/// Checks whether the concatenation of number and corresponding Luhn check digit is valid
243-
/// </summary>
244-
/// <param name="number">Identification number w/o Luhn check digit</param>
245-
/// <param name="checkDigit">The Luhn check digit</param>
246-
/// <returns><see langword="true" /> if the <paramref name="number"/> is valid;
247-
/// otherwise <see langword="false" /></returns>
248-
/// <exception cref="ArgumentException"><paramref name="number"/> is not valid.
249-
/// It contains none-numeric characters.</exception>
250-
/// <exception cref="ArgumentOutOfRangeException">The <paramref name="checkDigit"/> value is greater than 9.
251-
/// The <paramref name="checkDigit"/> value must be between 0 and 9.</exception>
252-
[SuppressMessage("ReSharper", "UnusedMember.Global")]
253-
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
254-
[Obsolete("Use the IsValidLuhnCheckDigit method instead.", false)]
255-
public static bool IsValid(this string number, byte checkDigit) => checkDigit.IsValidLuhnCheckDigit(number);
256-
257199
/// <summary>
258200
/// Checks whether the concatenation of number and corresponding Luhn check digit is valid
259201
/// </summary>
@@ -285,7 +227,7 @@ public static bool IsValidLuhnCheckDigit(this byte checkDigit, string number)
285227
"{0}{1}",
286228
number.Trim(),
287229
checkDigit.ToString(CultureInfo.InvariantCulture))
288-
.IsNumber()
230+
.ValidateAndTrimNumber()
289231
.GetDigits()
290232
.DoubleEverySecondDigit(true)
291233
.SumDigits() == 0;
@@ -357,12 +299,12 @@ public static string ConvertAlphaNumericToNumeric(this string alphaNumeric)
357299
#endif
358300

359301
/// <summary>
360-
/// Doubling of every second digit.
302+
/// Doubles every second digit of the <paramref name="digits"/> enumeration.
361303
/// </summary>
362304
/// <param name="digits">The digits represent a number w/ or w/o check digit.</param>
363305
/// <param name="forValidation"><see langword="true"/> if the <paramref name="digits"/> represent
364306
/// a Luhn number including a check digit; otherwise <see langword="false"/></param>
365-
/// <returns></returns>
307+
/// <returns>Enumeration of digits</returns>
366308
private static IEnumerable<uint> DoubleEverySecondDigit(this IEnumerable<uint> digits, bool forValidation)
367309
{
368310
int index = 0;
@@ -399,7 +341,7 @@ private static IEnumerable<uint> DoubleEverySecondDigit(this IEnumerable<uint> d
399341
/// <param name="number">An identification number</param>
400342
/// <returns>The trimmed identification number if valid</returns>
401343
/// <exception cref="ArgumentException"><paramref name="number"/> is not a valid number</exception>
402-
private static string IsNumber(this string number)
344+
private static string ValidateAndTrimNumber(this string number)
403345
{
404346
string trimmedNumber = number?.Trim();
405347
if (string.IsNullOrWhiteSpace(trimmedNumber) || !Regex.IsMatch(trimmedNumber, @"^\d+$"))
@@ -418,7 +360,7 @@ private static string IsNumber(this string number)
418360
/// <param name="number">An identification number</param>
419361
/// <returns>The trimmed identification number if valid</returns>
420362
/// <exception cref="ArgumentException"><paramref name="number"/> is not a valid number</exception>
421-
private static ReadOnlySpan<char> IsNumber(this ReadOnlySpan<char> number)
363+
private static ReadOnlySpan<char> ValidateAndTrimNumber(this ReadOnlySpan<char> number)
422364
{
423365
var trimmedNumber = number.Trim();
424366
if (trimmedNumber.Length == 0 || !trimmedNumber.IsDigits())
@@ -460,7 +402,7 @@ private static IEnumerable<uint> GetDigits(this ReadOnlySpan<char> number)
460402
uint[] digits = new uint[number.Length];
461403
for (int i = 0; i < number.Length; i++)
462404
{
463-
digits[number.Length - i - 1] = (uint)number[i] - 48;
405+
digits[number.Length - i - 1] = (uint)number[i] - AsciiCodeForZero;
464406
}
465407

466408
return digits;
@@ -470,7 +412,7 @@ private static IEnumerable<uint> GetDigits(this string number)
470412
{
471413
for (int i = number.Length - 1; i >= 0; i--)
472414
{
473-
yield return (uint)number[i] - 48;
415+
yield return (uint)number[i] - AsciiCodeForZero;
474416
}
475417
}
476418
#endif

tests/LuhnTest.cs

-12
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,7 @@ public void ComputeLuhnNumber_ValidRawNumber_ReturnsExpectedLuhnNumber(
187187
[MemberData(nameof(LuhnNumberValidationSet), MemberType = typeof(LuhnTest))]
188188
public void LuhnNumberValidationTest(bool expectedResult, string luhnNumber)
189189
{
190-
Assert.Equal(expectedResult, luhnNumber.IsValid());
191190
Assert.Equal(expectedResult, luhnNumber.IsValidLuhnNumber());
192-
Assert.Equal(expectedResult, luhnNumber.AsSpan().IsValid());
193191
Assert.Equal(expectedResult, luhnNumber.AsSpan().IsValidLuhnNumber());
194192
}
195193

@@ -203,9 +201,7 @@ public void LuhnNumberValidationTest(bool expectedResult, string luhnNumber)
203201
[MemberData(nameof(LuhnCheckDigitValidationSet), MemberType = typeof(LuhnTest))]
204202
public void LuhnCheckDigitValidationTest(bool expectedResult, string number, byte checkDigit)
205203
{
206-
Assert.Equal(expectedResult, number.IsValid(checkDigit));
207204
Assert.Equal(expectedResult, checkDigit.IsValidLuhnCheckDigit(number));
208-
Assert.Equal(expectedResult, number.AsSpan().IsValid(checkDigit));
209205
Assert.Equal(expectedResult, checkDigit.IsValidLuhnCheckDigit(number.AsSpan()));
210206
}
211207

@@ -243,9 +239,7 @@ public void ComputeLuhnNumber_InvalidRawNumber_ThrowsArgumentException(string in
243239
[MemberData(nameof(InvalidNumbers), MemberType = typeof(LuhnTest))]
244240
public void LuhnNumberValidationExceptionTest(string invalidNumber)
245241
{
246-
Assert.Throws<ArgumentException>(() => invalidNumber.IsValid());
247242
Assert.Throws<ArgumentException>(() => invalidNumber.IsValidLuhnNumber());
248-
Assert.Throws<ArgumentException>(() => invalidNumber.AsSpan().IsValid());
249243
Assert.Throws<ArgumentException>(() => invalidNumber.AsSpan().IsValidLuhnNumber());
250244
}
251245

@@ -257,9 +251,7 @@ public void LuhnNumberValidationExceptionTest(string invalidNumber)
257251
[MemberData(nameof(InvalidNumbersAndCheckDigits), MemberType = typeof(LuhnTest))]
258252
public void NumberValidationExceptionTest(string invalidNumber, byte checkDigit)
259253
{
260-
Assert.Throws<ArgumentException>(() => invalidNumber.IsValid(checkDigit));
261254
Assert.Throws<ArgumentException>(() => checkDigit.IsValidLuhnCheckDigit(invalidNumber));
262-
Assert.Throws<ArgumentException>(() => invalidNumber.AsSpan().IsValid(checkDigit));
263255
Assert.Throws<ArgumentException>(() => checkDigit.IsValidLuhnCheckDigit(invalidNumber.AsSpan()));
264256
}
265257

@@ -271,9 +263,7 @@ public void NumberValidationExceptionTest(string invalidNumber, byte checkDigit)
271263
[MemberData(nameof(NumbersWithInvalidCheckDigits), MemberType = typeof(LuhnTest))]
272264
public void LuhnCheckDigitValidationExceptionTest(string invalidNumber, byte checkDigit)
273265
{
274-
Assert.Throws<ArgumentOutOfRangeException>(() => invalidNumber.IsValid(checkDigit));
275266
Assert.Throws<ArgumentOutOfRangeException>(() => checkDigit.IsValidLuhnCheckDigit(invalidNumber));
276-
Assert.Throws<ArgumentOutOfRangeException>(() => invalidNumber.AsSpan().IsValid(checkDigit));
277267
Assert.Throws<ArgumentOutOfRangeException>(() => checkDigit.IsValidLuhnCheckDigit(invalidNumber.AsSpan()));
278268
}
279269

@@ -340,9 +330,7 @@ public void ConvertAlphaNumericToNumeric_InvalidInput_ThrowsArgumentException()
340330
[MemberData(nameof(IsValidWithConvertData), MemberType = typeof(LuhnTest))]
341331
public void IsValidWithConvertTest(string input, bool expected)
342332
{
343-
Assert.Equal(expected, input.ConvertAlphaNumericToNumeric().IsValid());
344333
Assert.Equal(expected, input.ConvertAlphaNumericToNumeric().IsValidLuhnNumber());
345-
Assert.Equal(expected, input.ConvertAlphaNumericToNumeric().AsSpan().IsValid());
346334
Assert.Equal(expected, input.ConvertAlphaNumericToNumeric().AsSpan().IsValidLuhnNumber());
347335
}
348336

0 commit comments

Comments
 (0)