mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-14 19:04:51 +08:00
Merged sum operator.
This commit is contained in:
@@ -20,9 +20,7 @@ namespace Orchard.Tokens.Providers {
|
||||
public void Describe(DescribeContext context) {
|
||||
context.For("List", T("Lists"), T("Handles lists of Content Items"))
|
||||
.Token("Join:*", T("Join:<Tokenized text>[,<separator>]"), T("Join each element in the list by applying the tokenized text and concatenating the output with the optional separator."))
|
||||
.Token("SumInt:*", T("SumInt:<Tokenized text>"), T("Sum each element in the list by applying the tokenized text."))
|
||||
.Token("SumFloat:*", T("SumFloat:<Tokenized text>"), T("Sum each element in the list by applying the tokenized text."))
|
||||
.Token("SumDecimal:*", T("SumDecimal:<Tokenized text>"), T("Sum each element in the list by applying the tokenized text."))
|
||||
.Token("Sum:*", T("Sum:<Tokenized text>"), T("Sum each element in the list by applying the tokenized text."))
|
||||
.Token("First:*", T("First:<Tokenized text>"), T("Apply the tokenized text to the first element."))
|
||||
.Token("Last:*", T("Last:<Tokenized text>"), T("Apply the tokenized text to the last element."))
|
||||
.Token("Count", T("Count"), T("Get the list count."))
|
||||
@@ -30,77 +28,80 @@ namespace Orchard.Tokens.Providers {
|
||||
}
|
||||
|
||||
public void Evaluate(EvaluateContext context) {
|
||||
context.For<IList<IContent>>("List", () => new List<IContent>())
|
||||
.Token( // {List.ForEach:<string>[,<separator>]}
|
||||
token => {
|
||||
if (token.StartsWith("Join:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("Join:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, collection) => {
|
||||
if (String.IsNullOrEmpty(token)) {
|
||||
return String.Empty;
|
||||
}
|
||||
// Split the params to get the tokenized text and optional separator.
|
||||
var index = token.IndexOf(',');
|
||||
var text = index == -1 ? token : token.Substring(0, index);
|
||||
if (String.IsNullOrEmpty(text)) {
|
||||
return String.Empty;
|
||||
}
|
||||
var separator = index == -1 ? String.Empty : token.Substring(index + 1);
|
||||
return String.Join(separator, collection.Select(content => _tokenizer().Replace(text, new {content}, new ReplaceOptions {Encoding = ReplaceOptions.NoEncode})));
|
||||
})
|
||||
.Token( // {List.SumInt:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("SumInt:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("SumInt:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, collection) => collection.Sum(i => long.Parse(_tokenizer().Replace(token, new { Content = i }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }))))
|
||||
.Token( // {List.SumFloat:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("SumFloat:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("SumFloat:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, collection) => collection.Sum(i => double.Parse(_tokenizer().Replace(token, new { Content = i }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }))))
|
||||
.Token( // {List.SumDecimal:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("SumDecimal:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("SumDecimal:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, collection) => collection.Sum(i => decimal.Parse(_tokenizer().Replace(token, new { Content = i }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }))))
|
||||
.Token( // {List.First:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("First:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("First:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, list) => list.Any() ? _tokenizer().Replace(token, new { Content = list.First() }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }) : String.Empty)
|
||||
.Token( // {List.Last:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("Last:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("Last:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, list) => list.Any() ? _tokenizer().Replace(token, new { Content = list.Last() }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode }) : String.Empty)
|
||||
.Token( // {List.Count}
|
||||
"Count",
|
||||
list => list.Count)
|
||||
;
|
||||
Func<string, IContent, string> getValue = (t, i) => _tokenizer().Replace(t, new { Content = i }, new ReplaceOptions { Encoding = ReplaceOptions.NoEncode });
|
||||
|
||||
context.For<IList<IContent>>("List", () => new List<IContent>())
|
||||
.Token( // {List.Join:<string>[,<separator>]}
|
||||
token => {
|
||||
if (token.StartsWith("Join:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("Join:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, collection) => {
|
||||
if (String.IsNullOrEmpty(token)) {
|
||||
return String.Empty;
|
||||
}
|
||||
// Split the params to get the tokenized text and optional separator.
|
||||
var index = token.IndexOf(',');
|
||||
var text = index == -1 ? token : token.Substring(0, index);
|
||||
if (String.IsNullOrEmpty(text)) {
|
||||
return String.Empty;
|
||||
}
|
||||
var separator = index == -1 ? String.Empty : token.Substring(index + 1);
|
||||
return String.Join(separator, collection.Select(content => getValue(text, content)));
|
||||
})
|
||||
.Token( // {List.Sum:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("Sum:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("Sum:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, collection) => {
|
||||
if (!collection.Any()) return "0";
|
||||
|
||||
// try long.
|
||||
long longValue;
|
||||
if (long.TryParse(collection.Select(i => getValue(token, i)).First(), out longValue)) {
|
||||
return collection.Sum(i => long.Parse(getValue(token, i)));
|
||||
}
|
||||
// try float.
|
||||
float floatValue;
|
||||
if (float.TryParse(collection.Select(i => getValue(token, i)).First(), out floatValue)) {
|
||||
return collection.Sum(i => float.Parse(getValue(token, i)));
|
||||
}
|
||||
// try decimal.
|
||||
decimal decimalValue;
|
||||
if (decimal.TryParse(collection.Select(i => getValue(token, i)).First(), out decimalValue)) {
|
||||
return collection.Sum(i => decimal.Parse(getValue(token, i)));
|
||||
}
|
||||
return "";
|
||||
})
|
||||
.Token( // {List.First:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("First:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("First:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, list) => list.Any() ? list.Select(i => getValue(token, i)).First() : String.Empty)
|
||||
.Token( // {List.Last:<string>}
|
||||
token => {
|
||||
if (token.StartsWith("Last:", StringComparison.OrdinalIgnoreCase)) {
|
||||
// html decode to stop double encoding.
|
||||
return HttpUtility.HtmlDecode(token.Substring("Last:".Length));
|
||||
}
|
||||
return null;
|
||||
},
|
||||
(token, list) => list.Any() ? list.Select(i => getValue(token, i)).Last() : String.Empty)
|
||||
.Token( // {List.Count}
|
||||
"Count",
|
||||
list => list.Count)
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
@@ -35,7 +35,7 @@ namespace Orchard.Tokens.Tests {
|
||||
|
||||
[Test]
|
||||
public void TestListSumTokens() {
|
||||
Assert.That(_tokenizer.Replace("{List.SumInt:{Content.Id}}", new {List = new List<IContent> {new TestUser {Id = 5}, new TestUser {Id = 10}}}), Is.EqualTo("15"));
|
||||
Assert.That(_tokenizer.Replace("{List.Sum:{Content.Id}}", new {List = new List<IContent> {new TestUser {Id = 5}, new TestUser {Id = 10}}}), Is.EqualTo("15"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
Reference in New Issue
Block a user