summaryrefslogtreecommitdiff
path: root/dsa/DSALib/Auxiliary/IDataObjectEnumerableExtension.cs
blob: d695baf3ad4adedd2d04bbbac78e9678d147fe2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using DSALib.Auxiliary;
using DSALib.Models.Database;

namespace DSACore.Auxiliary {
    public static class DataObjectEnumerableExtension {
        public static IDataObject Match(this IEnumerable<IDataObject> dataObjects, string name) {
            return (dataObjects as IOrderedEnumerable<IDataObject> ?? throw new InvalidOperationException())
                .OrderBy(x => SpellCorrect.Compare(name, x.Name)).Last();
        }

        public static bool TryMatch(this IEnumerable<IDataObject> dataObjects, out IDataObject data, string name) {
            data = (dataObjects as IOrderedEnumerable<IDataObject> ?? throw new InvalidOperationException())
                .OrderBy(x => SpellCorrect.Compare(name, x.Name)).Last();

            return SpellCorrect.IsMatch(name, data.Name);
        }
    }
}