Ejemplo n.º 1
0
        public static string GetUniqueName(IEnumerable <string> names, string name)
        {
            var model = new StructuredName(name);
            var items = names
                        .Where(x => x.InvariantStartsWith(model.Text)) // ignore non-matching names
                        .Select(x => new StructuredName(x));

            // name is empty, and there are no other names with suffixes, so just return " (1)"
            if (model.IsEmptyName() && !items.Any())
            {
                model.Suffix = StructuredName.INITIAL_SUFFIX;

                return(model.FullName);
            }

            // name is empty, and there are other names with suffixes
            if (model.IsEmptyName() && items.SuffixedNameExists())
            {
                var emptyNameSuffix = GetSuffixNumber(items);

                if (emptyNameSuffix > 0)
                {
                    model.Suffix = (uint?)emptyNameSuffix;

                    return(model.FullName);
                }
            }

            //  no suffix - name without suffix does NOT exist, AND name with suffix does NOT exist
            if (!model.Suffix.HasValue && !items.SimpleNameExists(model.Text) && !items.SuffixedNameExists())
            {
                model.Suffix = StructuredName.NO_SUFFIX;

                return(model.FullName);
            }

            // no suffix - name without suffix exists, however name with suffix does NOT exist
            if (!model.Suffix.HasValue && items.SimpleNameExists(model.Text) && !items.SuffixedNameExists())
            {
                var firstSuffix = GetFirstSuffix(items);
                model.Suffix = (uint?)firstSuffix;

                return(model.FullName);
            }

            // no suffix - name without suffix exists, AND name with suffix does exist
            if (!model.Suffix.HasValue && items.SimpleNameExists(model.Text) && items.SuffixedNameExists())
            {
                var nextSuffix = GetSuffixNumber(items);
                model.Suffix = (uint?)nextSuffix;

                return(model.FullName);
            }

            // no suffix - name without suffix does NOT exist, however name with suffix exists
            if (!model.Suffix.HasValue && !items.SimpleNameExists(model.Text) && items.SuffixedNameExists())
            {
                var nextSuffix = GetSuffixNumber(items);
                model.Suffix = (uint?)nextSuffix;

                return(model.FullName);
            }

            // has suffix - name without suffix exists
            if (model.Suffix.HasValue && items.SimpleNameExists(model.Text))
            {
                var nextSuffix = GetSuffixNumber(items);
                model.Suffix = (uint?)nextSuffix;

                return(model.FullName);
            }

            // has suffix - name without suffix does NOT exist
            // a case where the user added the suffix, so add a secondary suffix
            if (model.Suffix.HasValue && !items.SimpleNameExists(model.Text))
            {
                model.Text   = model.FullName;
                model.Suffix = StructuredName.NO_SUFFIX;

                // filter items based on full name with suffix
                items = items.Where(x => x.Text.InvariantStartsWith(model.FullName));
                var secondarySuffix = GetFirstSuffix(items);
                model.Suffix = (uint?)secondarySuffix;

                return(model.FullName);
            }

            // has suffix - name without suffix also exists, therefore we simply increment
            if (model.Suffix.HasValue && items.SimpleNameExists(model.Text))
            {
                var nextSuffix = GetSuffixNumber(items);
                model.Suffix = (uint?)nextSuffix;

                return(model.FullName);
            }

            return(name);
        }
Ejemplo n.º 2
0
 internal static bool Contains(this IEnumerable <StructuredName> items, StructuredName model)
 {
     return(items.Any(x => x.FullName.InvariantEquals(model.FullName)));
 }