forked from ServiceStack/ServiceStack.Text
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListExtensions.cs
More file actions
76 lines (63 loc) · 1.98 KB
/
Copy pathListExtensions.cs
File metadata and controls
76 lines (63 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//Copyright (c) Service Stack LLC. All Rights Reserved.
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Text.Common;
namespace ServiceStack
{
public static class ListExtensions
{
public static string Join<T>(this IEnumerable<T> values)
{
return Join(values, JsWriter.ItemSeperatorString);
}
public static string Join<T>(this IEnumerable<T> values, string seperator)
{
var sb = new StringBuilder();
foreach (var value in values)
{
if (sb.Length > 0)
sb.Append(seperator);
sb.Append(value);
}
return sb.ToString();
}
public static bool IsNullOrEmpty<T>(this List<T> list)
{
return list == null || list.Count == 0;
}
//TODO: make it work
public static IEnumerable<TFrom> SafeWhere<TFrom>(this List<TFrom> list, Func<TFrom, bool> predicate)
{
return list.Where(predicate);
}
public static int NullableCount<T>(this List<T> list)
{
return list == null ? 0 : list.Count;
}
public static void AddIfNotExists<T>(this List<T> list, T item)
{
if (!list.Contains(item))
list.Add(item);
}
public static T[] NewArray<T>(this T[] array, T with = null, T without = null) where T : class
{
var to = new List<T>(array);
if (with != null)
to.Add(with);
if (without != null)
to.Remove(without);
return to.ToArray();
}
public static List<T> InList<T>(this T value)
{
return new List<T> { value };
}
public static T[] InArray<T>(this T value)
{
return new[] { value };
}
}
}