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
62 lines (55 loc) · 1.35 KB
/
ListExtensions.cs
File metadata and controls
62 lines (55 loc) · 1.35 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
//
// https://github.com/ServiceStack/ServiceStack.Text
// ServiceStack.Text: .NET C# POCO JSON, JSV and CSV Text Serializers.
//
// Authors:
// Demis Bellot (demis.bellot@gmail.com)
//
// Copyright 2012 ServiceStack Ltd.
//
// Licensed under the same terms of ServiceStack: new BSD license.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Text.Common;
namespace ServiceStack.Text
{
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);
}
}
}