2012-08-17 04:27:47 +06:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2012-10-04 01:31:08 +05:00
|
|
|
|
using Umbraco.Core.Models;
|
2012-08-17 04:27:47 +06:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Dynamics
|
|
|
|
|
|
{
|
|
|
|
|
|
internal static class ExtensionMethods
|
|
|
|
|
|
{
|
|
|
|
|
|
public static IEnumerable<TSource> Map<TSource>(
|
|
|
|
|
|
this IEnumerable<TSource> source,
|
|
|
|
|
|
Func<TSource, bool> selectorFunction,
|
|
|
|
|
|
Func<TSource, IEnumerable<TSource>> getChildrenFunction)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!source.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
return source;
|
|
|
|
|
|
}
|
|
|
|
|
|
// Add what we have to the stack
|
|
|
|
|
|
var flattenedList = source.Where(selectorFunction);
|
|
|
|
|
|
// Go through the input enumerable looking for children,
|
|
|
|
|
|
// and add those if we have them
|
|
|
|
|
|
foreach (TSource element in source)
|
|
|
|
|
|
{
|
|
|
|
|
|
var secondInner = getChildrenFunction(element);
|
|
|
|
|
|
if (secondInner.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
secondInner = secondInner.Map(selectorFunction, getChildrenFunction);
|
|
|
|
|
|
}
|
|
|
|
|
|
flattenedList = flattenedList.Concat(secondInner);
|
|
|
|
|
|
}
|
|
|
|
|
|
return flattenedList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-02 22:51:53 +05:00
|
|
|
|
public static DynamicPublishedContentList Random(this DynamicPublishedContentList all, int min, int max)
|
2012-08-17 04:27:47 +06:00
|
|
|
|
{
|
|
|
|
|
|
//get a random number generator
|
|
|
|
|
|
Random r = new Random();
|
|
|
|
|
|
//choose the number of elements to be returned between Min and Max
|
|
|
|
|
|
int Number = r.Next(min, max);
|
|
|
|
|
|
//Call the other method
|
|
|
|
|
|
return Random(all, Number);
|
|
|
|
|
|
}
|
2012-10-02 22:51:53 +05:00
|
|
|
|
public static DynamicPublishedContentList Random(this DynamicPublishedContentList all, int max)
|
2012-08-17 04:27:47 +06:00
|
|
|
|
{
|
|
|
|
|
|
//Randomly order the items in the set by a Guid, Take the correct number, and return this wrapped in a new DynamicNodeList
|
2012-10-02 22:51:53 +05:00
|
|
|
|
return new DynamicPublishedContentList(all.Items.OrderBy(x => Guid.NewGuid()).Take(max));
|
2012-08-17 04:27:47 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-04 01:31:08 +05:00
|
|
|
|
public static DynamicPublishedContentBase Random(this DynamicPublishedContentList all)
|
2012-08-17 04:27:47 +06:00
|
|
|
|
{
|
|
|
|
|
|
return all.Items.OrderBy(x => Guid.NewGuid()).First();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-17 06:22:51 +06:00
|
|
|
|
public static bool ContainsAny(this string haystack, IEnumerable<string> needles)
|
2012-08-17 04:27:47 +06:00
|
|
|
|
{
|
|
|
|
|
|
if (haystack == null) throw new ArgumentNullException("haystack");
|
2012-08-17 06:22:51 +06:00
|
|
|
|
if (!string.IsNullOrEmpty(haystack) || needles.Any())
|
2012-08-17 04:27:47 +06:00
|
|
|
|
{
|
|
|
|
|
|
return needles.Any(haystack.Contains);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static bool ContainsAny(this string haystack, params string[] needles)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (haystack == null) throw new ArgumentNullException("haystack");
|
|
|
|
|
|
if (!string.IsNullOrEmpty(haystack) || needles.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return needles.Any(haystack.Contains);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2012-08-17 06:22:51 +06:00
|
|
|
|
public static bool ContainsAny(this string haystack, StringComparison comparison, IEnumerable<string> needles)
|
2012-08-17 04:27:47 +06:00
|
|
|
|
{
|
|
|
|
|
|
if (haystack == null) throw new ArgumentNullException("haystack");
|
2012-08-17 06:22:51 +06:00
|
|
|
|
if (!string.IsNullOrEmpty(haystack) || needles.Any())
|
2012-08-17 04:27:47 +06:00
|
|
|
|
{
|
|
|
|
|
|
return needles.Any(value => haystack.IndexOf(value, comparison) >= 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static bool ContainsAny(this string haystack, StringComparison comparison, params string[] needles)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (haystack == null) throw new ArgumentNullException("haystack");
|
|
|
|
|
|
if (!string.IsNullOrEmpty(haystack) || needles.Length > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return needles.Any(value => haystack.IndexOf(value, comparison) >= 0);
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static bool ContainsInsensitive(this string haystack, string needle)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (haystack == null) throw new ArgumentNullException("haystack");
|
|
|
|
|
|
return haystack.IndexOf(needle, StringComparison.CurrentCultureIgnoreCase) >= 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static bool HasValue(this string s)
|
|
|
|
|
|
{
|
|
|
|
|
|
return !string.IsNullOrWhiteSpace(s);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|