Skip to content

Commit fc1721b

Browse files
Merge pull request #109 from SixLabors/js/code-quality
Code Quality
2 parents c870521 + a171613 commit fc1721b

File tree

13 files changed

+145
-255
lines changed

13 files changed

+145
-255
lines changed

src/ImageSharp.Web/Commands/CommandDescriptor.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -50,14 +50,13 @@ public static ICommandConverter GetConverter(Type type)
5050
/// </exception>
5151
public static void AddConverter(Type type, Type converter)
5252
{
53-
Guard.IsTrue(typeof(ICommandConverter).IsAssignableFrom(converter), nameof(converter), "Converter does not implement ICommandConverter.");
53+
const string Message = "Converter does not implement ICommandConverter.";
54+
Guard.IsTrue(typeof(ICommandConverter).IsAssignableFrom(converter), nameof(converter), Message);
5455

55-
if (ConverterCache.ContainsKey(type))
56+
if (!ConverterCache.ContainsKey(type))
5657
{
57-
return;
58+
ConverterCache[type] = (ICommandConverter)Activator.CreateInstance(converter);
5859
}
59-
60-
ConverterCache[type] = (ICommandConverter)Activator.CreateInstance(converter);
6160
}
6261
}
63-
}
62+
}

src/ImageSharp.Web/Commands/CommandParser.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -63,10 +63,7 @@ private CommandParser()
6363
/// <returns>The converted instance or the default.</returns>
6464
public T ParseValue<T>(string value, CultureInfo culture)
6565
{
66-
if (culture == null)
67-
{
68-
culture = CultureInfo.InvariantCulture;
69-
}
66+
culture ??= CultureInfo.InvariantCulture;
7067

7168
Type type = typeof(T);
7269
ICommandConverter converter = CommandDescriptor.GetConverter(type);

src/ImageSharp.Web/Commands/ComparableExtensions.cs

Lines changed: 0 additions & 164 deletions
This file was deleted.
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
55
using System.Collections.Generic;
66
using System.Globalization;
7-
using System.Linq;
87

98
namespace SixLabors.ImageSharp.Web.Commands.Converters
109
{
@@ -16,11 +15,6 @@ internal sealed class ArrayConverter<T> : ListConverter<T>
1615
{
1716
/// <inheritdoc/>
1817
public override object ConvertFrom(CultureInfo culture, string value, Type propertyType)
19-
{
20-
object result = base.ConvertFrom(culture, value, propertyType);
21-
22-
var list = result as IList<T>;
23-
return list?.ToArray() ?? result;
24-
}
18+
=> ((List<T>)base.ConvertFrom(culture, value, propertyType)).ToArray();
2519
}
26-
}
20+
}

src/ImageSharp.Web/Commands/Converters/ColorConverter.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -38,13 +38,6 @@ public override object ConvertFrom(CultureInfo culture, string value, Type prope
3838
return default(Color);
3939
}
4040

41-
// Special case. HTML requires LightGrey, but NamedColors has LightGray to conform with System.Drawing
42-
// Check early on.
43-
if (value.Equals("LightGrey", StringComparison.OrdinalIgnoreCase))
44-
{
45-
return Color.LightGray;
46-
}
47-
4841
// Numeric r,g,b - r,g,b,a
4942
char separator = culture.TextInfo.ListSeparator[0];
5043

src/ImageSharp.Web/Commands/Converters/IntegralNumberConverter.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System;
@@ -27,39 +27,39 @@ public override object ConvertFrom(CultureInfo culture, string value, Type prope
2727
// Now clamp it to the type ranges
2828
if (propertyType == TypeConstants.Sbyte)
2929
{
30-
rounded = rounded.Clamp(sbyte.MinValue, sbyte.MaxValue);
30+
rounded = Math.Clamp(rounded, sbyte.MinValue, sbyte.MaxValue);
3131
}
3232
else if (propertyType == TypeConstants.Byte)
3333
{
34-
rounded = rounded.Clamp(byte.MinValue, byte.MaxValue);
34+
rounded = Math.Clamp(rounded, byte.MinValue, byte.MaxValue);
3535
}
3636
else if (propertyType == TypeConstants.Short)
3737
{
38-
rounded = rounded.Clamp(short.MinValue, short.MaxValue);
38+
rounded = Math.Clamp(rounded, short.MinValue, short.MaxValue);
3939
}
4040
else if (propertyType == TypeConstants.UShort)
4141
{
42-
rounded = rounded.Clamp(ushort.MinValue, ushort.MaxValue);
42+
rounded = Math.Clamp(rounded, ushort.MinValue, ushort.MaxValue);
4343
}
4444
else if (propertyType == TypeConstants.Int)
4545
{
46-
rounded = rounded.Clamp(int.MinValue, int.MaxValue);
46+
rounded = Math.Clamp(rounded, int.MinValue, int.MaxValue);
4747
}
4848
else if (propertyType == TypeConstants.UInt)
4949
{
50-
rounded = rounded.Clamp(uint.MinValue, uint.MaxValue);
50+
rounded = Math.Clamp(rounded, uint.MinValue, uint.MaxValue);
5151
}
5252
else if (propertyType == TypeConstants.Long)
5353
{
54-
rounded = rounded.Clamp(long.MinValue, long.MaxValue);
54+
rounded = Math.Clamp(rounded, long.MinValue, long.MaxValue);
5555
}
5656
else if (propertyType == TypeConstants.ULong)
5757
{
58-
rounded = rounded.Clamp(ulong.MinValue, ulong.MaxValue);
58+
rounded = Math.Clamp(rounded, ulong.MinValue, ulong.MaxValue);
5959
}
6060

6161
// Now it's rounded an clamped we should be able to correctly parse the string.
6262
return (T)Convert.ChangeType(rounded.ToString(CultureInfo.InvariantCulture), typeof(T), culture);
6363
}
6464
}
65-
}
65+
}

src/ImageSharp.Web/FormattedImage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public sealed class FormattedImage : IDisposable
2121
/// </summary>
2222
/// <param name="image">The image.</param>
2323
/// <param name="format">The format.</param>
24-
public FormattedImage(Image<Rgba32> image, IImageFormat format)
24+
internal FormattedImage(Image<Rgba32> image, IImageFormat format)
2525
{
2626
this.format = format;
2727
this.Image = image;

src/ImageSharp.Web/Processors/WebProcessingExtensions.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Apache License, Version 2.0.
33

44
using System.Collections.Generic;
@@ -18,25 +18,22 @@ internal static class WebProcessingExtensions
1818
/// <param name="logger">The type used for performing logging.</param>
1919
/// <param name="processors">The collection of available processors.</param>
2020
/// <param name="commands">The parsed collection of processing commands.</param>
21-
/// <returns>The <see cref="Image{Rgba32}"/>.</returns>
22-
/// <remarks>Passing zero for one of height or width will automatically preserve the aspect ratio of the original image.</remarks>
21+
/// <returns>The <see cref="FormattedImage"/>.</returns>
2322
public static FormattedImage Process(
2423
this FormattedImage source,
2524
ILogger logger,
2625
IEnumerable<IImageWebProcessor> processors,
2726
IDictionary<string, string> commands)
2827
{
29-
if (commands.Count == 0)
28+
if (commands.Count != 0)
3029
{
31-
return source;
32-
}
33-
34-
foreach (IImageWebProcessor processor in processors)
35-
{
36-
source = processor.Process(source, logger, commands);
30+
foreach (IImageWebProcessor processor in processors)
31+
{
32+
source = processor.Process(source, logger, commands);
33+
}
3734
}
3835

3936
return source;
4037
}
4138
}
42-
}
39+
}

0 commit comments

Comments
 (0)