Skip to content

Commit

Permalink
Fixed the config for the new hash style line comment conditional.
Browse files Browse the repository at this point in the history
Modified JExtension.ToBool() to also convert strings.
  • Loading branch information
seancpeters committed Sep 22, 2016
1 parent 684be95 commit f63440d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static List<IOperationProvider> HashSignLineCommentConditionalSetup(strin
IfTokens = new[] { "#if" },
ElseTokens = new[] { "#else" },
ElseIfTokens = new[] { "#elseif" },
EndIfTokens = new[] { "#endif" },
EndIfTokens = new[] { "#endif", "##endif" },
ActionableIfTokens = new[] { "##if" },
ActionableElseIfTokens = new[] { "##elseif" },
ActionableElseTokens = new[] { "##else" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,81 @@ public static string ToString(this JToken token, string key)

public static bool ToBool(this JToken token, string key = null, bool defaultValue = false)
{
if (key == null)
{
if (token == null || token.Type != JTokenType.Boolean)
{
return defaultValue;
}
JToken checkToken;

return string.Equals(token.ToString(), "true", StringComparison.OrdinalIgnoreCase);
// determine which token to bool-ify
if (token == null)
{
return defaultValue;
}

JObject obj = token as JObject;

if (obj == null)
else if (key == null)
{
checkToken = token;
}
else if (! ((JObject)token).TryGetValue(key, StringComparison.OrdinalIgnoreCase, out checkToken))
{
return defaultValue;
}

JToken element;
if (!obj.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out element) || element.Type != JTokenType.Boolean)
// do the conversion on checkToken
if (checkToken.Type == JTokenType.Boolean || checkToken.Type == JTokenType.String)
{
return string.Equals(checkToken.ToString(), "true", StringComparison.OrdinalIgnoreCase);
}
else
{
return defaultValue;
}

return string.Equals(element.ToString(), "true", StringComparison.OrdinalIgnoreCase);
// all old
//if (key == null)
//{
// if (token != null
// && (token.Type == JTokenType.Boolean || token.Type == JTokenType.String))
// {
// return string.Equals(token.ToString(), "true", StringComparison.OrdinalIgnoreCase);
// }
// else
// {
// return defaultValue;
// }

// // old
// //if (token == null || token.Type != JTokenType.Boolean)
// //{
// // return defaultValue;
// //}

// //return string.Equals(token.ToString(), "true", StringComparison.OrdinalIgnoreCase);
//}

//JObject obj = token as JObject;

//if (obj == null)
//{
// return defaultValue;
//}

//JToken element;

//// new
//if (obj.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out element)
// && (element.Type == JTokenType.Boolean || element.Type == JTokenType.String))
//{
// return string.Equals(element.ToString(), "true", StringComparison.OrdinalIgnoreCase);
//}
//else
//{
// return defaultValue;
//}

//// old
////if (!obj.TryGetValue(key, StringComparison.OrdinalIgnoreCase, out element) || element.Type != JTokenType.Boolean)
////{
//// return defaultValue;
////}

////return string.Equals(element.ToString(), "true", StringComparison.OrdinalIgnoreCase);
}

public static int ToInt32(this JToken token, string key = null, int defaultValue = 0)
Expand Down

0 comments on commit f63440d

Please sign in to comment.