Skip to content

dasherize names over camel case #15

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

Merged
merged 1 commit into from
Sep 2, 2016
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
2 changes: 1 addition & 1 deletion JsonApiDotNetCore/Abstractions/JsonApiContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Type GetJsonApiResourceType()
public string GetEntityName()
{
return (!(Route is RelationalRoute) ? Route.BaseRouteDefinition.ContextPropertyName
: Configuration.Routes.Single(r => r.ModelType == ((RelationalRoute)Route).RelationalType).ContextPropertyName).ToCamelCase();
: Configuration.Routes.Single(r => r.ModelType == ((RelationalRoute)Route).RelationalType).ContextPropertyName).Dasherize();
}

public Type GetEntityType()
Expand Down
2 changes: 1 addition & 1 deletion JsonApiDotNetCore/Abstractions/ModelAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static Type GetTypeFromModelRelationshipName(Type modelType, string relat
{
var properties = modelType.GetProperties().Where(propertyInfo => propertyInfo.GetMethod.IsVirtual).ToList();
var relationshipType = properties.FirstOrDefault(
virtualProperty => virtualProperty.Name.ToCamelCase() == relationshipName.ToCamelCase())?.PropertyType;
virtualProperty => virtualProperty.Name.Dasherize() == relationshipName.Dasherize())?.PropertyType;
if(relationshipType.GetTypeInfo().IsGenericType)
{
return relationshipType.GetGenericArguments().Single();
Expand Down
2 changes: 1 addition & 1 deletion JsonApiDotNetCore/Data/ResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public object Get(string id)
private object GetRelated(string id, RelationalRoute relationalRoute)
{
var entity = GetEntityById(relationalRoute.BaseModelType, id, relationalRoute.RelationshipName);
return relationalRoute.BaseModelType.GetProperties().FirstOrDefault(pi => pi.Name.ToCamelCase() == relationalRoute.RelationshipName.ToCamelCase())?.GetValue(entity);
return relationalRoute.BaseModelType.GetProperties().FirstOrDefault(pi => pi.Name.Dasherize() == relationalRoute.RelationshipName.Dasherize())?.GetValue(entity);
}

private IQueryable GetDbSet(Type modelType, string includedRelationship)
Expand Down
39 changes: 31 additions & 8 deletions JsonApiDotNetCore/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
using System;
using System.Text;

namespace JsonApiDotNetCore.Extensions
{
public static class StringExtensions
{
public static string ToCamelCase(this string str)
{
var splittedPhraseChars = str.ToCharArray();
if (splittedPhraseChars.Length > 0)
var chars = str.ToCharArray();
if (chars.Length > 0)
{
splittedPhraseChars[0] = new string(splittedPhraseChars[0], 1).ToLower().ToCharArray()[0];
return new String(splittedPhraseChars);
chars[0] = new string(chars[0], 1).ToLower().ToCharArray()[0];
return new String(chars);
}
return str;
}

public static string ToProperCase(this string str)
{
var splittedPhraseChars = str.ToCharArray();
if (splittedPhraseChars.Length > 0)
var chars = str.ToCharArray();
if (chars.Length > 0)
{
chars[0] = new string(chars[0], 1).ToUpper().ToCharArray()[0];
return new String(chars);
}
return str;
}

public static string Dasherize(this string str)
{
var chars = str.ToCharArray();
if (chars.Length > 0)
{
splittedPhraseChars[0] = new string(splittedPhraseChars[0], 1).ToUpper().ToCharArray()[0];
return new String(splittedPhraseChars);
var builder = new StringBuilder();
for(var i = 0; i < chars.Length; i++)
{
if(char.IsUpper(chars[i])) {
var hashedString = (i > 0) ? $"-{char.ToLower(chars[i])}" : $"{char.ToLower(chars[i])}";
builder.Append(hashedString);
}
else {
builder.Append(chars[i]);
}
}
return builder.ToString();
}
return str;
}
Expand Down
2 changes: 1 addition & 1 deletion JsonApiDotNetCore/JsonApi/DocumentBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private Dictionary<string, object> BuildRelationshipsObject(IJsonApiResource res
_context.GetEntityType().GetProperties().Where(propertyInfo => propertyInfo.GetMethod.IsVirtual).ToList().ForEach(
virtualProperty =>
{
relationships.Add(virtualProperty.Name, GetRelationshipLinks(resource, virtualProperty.Name.ToCamelCase()));
relationships.Add(virtualProperty.Name.Dasherize(), GetRelationshipLinks(resource, virtualProperty.Name.Dasherize().Dasherize()));
});
return relationships;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,19 @@ public void ToProperCase_ConvertsString_ToProperCase(string input, string expect
// assert
Assert.Equal(expectedOutput, result);
}

[Theory]
[InlineData("todoItem", "todo-item")]
[InlineData("TodoItem", "todo-item")]
[InlineData("TodoItemS", "todo-item-s")]
public void Dasherize_Converts_StringToDashed(string input, string expectedOutput)
{
// arrange
// act
var result = input.Dasherize();

// assert
Assert.Equal(expectedOutput, result);
}
}
}