예제 #1
0
        private static bool CanMakeReadable(this ReadablilityCondition conditions, string value)
        {
            if (conditions.HasFlag(ReadablilityCondition.StopIfAnyWhitespace) &&
                value.Contains(WhiteSpace))
            {
                return(false);
            }

            return(true);
        }
예제 #2
0
        public static string AsReadable(this string value,
                                        ReadablilityCondition normalizeConditions = ReadablilityCondition.Default)
        {
            if (value.IsNullEmptyOrWhiteSpace())
            {
                return(value);
            }

            if (normalizeConditions == 0)
            {
                normalizeConditions = ReadablilityCondition.Default;
            }

            string workingValue = value;

            // Validate Whitespace Trim Conditions
            if (normalizeConditions.TrimStartWhitespace())
            {
                workingValue = workingValue.TrimStart();
            }

            if (normalizeConditions.TrimEndWhitespace())
            {
                workingValue = workingValue.TrimEnd();
            }

            // Validate Normalization Conditions
            if (!normalizeConditions.CanMakeReadable(workingValue))
            {
                return(workingValue);
            }

            // Declarations
            StringBuilder        returnValue   = new StringBuilder();
            IEnumerable <string> workingValues = workingValue.SeperateForReadability(normalizeConditions);
            IEnumerator <string> iterator      = workingValues.GetEnumerator();
            bool hasValue = iterator.MoveNext();
            bool isFirst  = true;

            while (hasValue)
            {
                returnValue.Append(isFirst ? normalizeConditions.Capitalize(iterator.Current) : iterator.Current);
                hasValue = iterator.MoveNext();
                isFirst  = false;
                if (hasValue)
                {
                    returnValue.Append(WhiteSpace);
                }
            }

            return(returnValue.ToString());
        }
        private static string Capitalize(this ReadablilityCondition conditions, string value)
        {
            if (conditions.Capitalize() && !value.IsNullEmptyOrWhiteSpace())
            {
                char   firstChar = value[0];
                string substring = value.Length == 0 ? string.Empty : value.Substring(1);

                if (Char.IsLower(firstChar))
                {
                    return(string.Format("{0}{1}", Char.ToUpper(firstChar), substring));
                }
            }
            return(value);
        }
예제 #4
0
 private static void Capitalize(this ReadablilityCondition conditions, ref string value)
 {
     value = conditions.Capitalize(value);
 }
예제 #5
0
 private static bool Capitalize(this ReadablilityCondition conditions)
 {
     return(conditions.HasFlag(ReadablilityCondition.CapitalizeFirstCharacter));
 }
예제 #6
0
 private static bool TrimEndWhitespace(this ReadablilityCondition conditions)
 {
     return(conditions.HasFlag(ReadablilityCondition.TrimTrailingWhiteSpace));
 }
예제 #7
0
 private static bool TrimStartWhitespace(this ReadablilityCondition conditions)
 {
     return(conditions.HasFlag(ReadablilityCondition.TrimLeadingWhiteSpace));
 }
예제 #8
0
        private static IEnumerable <string> SeperateForReadability(this string value, ReadablilityCondition conditions)
        {
            char[] valueContent      = value.ToCharArray();
            List <List <char> > vals = new List <List <char> >();

            for (int i = 0; i < valueContent.Length; i++)
            {
                if (i == 0 ||
                    (valueContent.IsUpper(i) &&
                     (valueContent.PreviousIsLower(i) ||
                      (valueContent.PreviousIsDigit(i) && conditions.HasFlag(ReadablilityCondition.ByDigit)) ||
                      valueContent.PreviousIsUnderscore(i) || valueContent.NextIsLower(i))) ||
                    (conditions.HasFlag(ReadablilityCondition.ByDigit) && valueContent.IsDigit(i) &&
                     !valueContent.PreviousIsDigit(i)) ||
                    (conditions.HasFlag(ReadablilityCondition.ByUnderscore) && valueContent.IsUnderscore(i)))
                {
                    vals.Add(new List <char>());
                }

                if (!valueContent.IsUnderscore(i))
                {
                    vals[vals.Count - 1].Add(valueContent[i]);
                }
            }

            foreach (List <char> i in vals)
            {
                yield return(new string(i.ToArray()));
            }
        }
예제 #9
0
        private static IEnumerable<string> SeperateForReadability(this string value, ReadablilityCondition conditions)
        {
            char[] valueContent = value.ToCharArray();
            List<List<char>> vals = new List<List<char>>();
            for (int i = 0; i < valueContent.Length; i++)
            {
                if (i == 0
                    || (valueContent.IsUpper(i)
                        && (valueContent.PreviousIsLower(i)
                            || (valueContent.PreviousIsDigit(i) && conditions.HasFlag(ReadablilityCondition.ByDigit))
                            || valueContent.PreviousIsUnderscore(i) || valueContent.NextIsLower(i)))
                    || (conditions.HasFlag(ReadablilityCondition.ByDigit) && valueContent.IsDigit(i)
                        && !valueContent.PreviousIsDigit(i))
                    || (conditions.HasFlag(ReadablilityCondition.ByUnderscore) && valueContent.IsUnderscore(i)))
                    vals.Add(new List<char>());

                if (!valueContent.IsUnderscore(i))
                    vals[vals.Count - 1].Add(valueContent[i]);
            }

            foreach (List<char> i in vals)
                yield return new string(i.ToArray());
        }
예제 #10
0
        public static string AsReadable(this string value,
            ReadablilityCondition normalizeConditions = ReadablilityCondition.Default)
        {
            if (value.IsNullEmptyOrWhiteSpace())
                return value;

            if (normalizeConditions == 0)
                normalizeConditions = ReadablilityCondition.Default;

            string workingValue = value;

            // Validate Whitespace Trim Conditions
            if (normalizeConditions.TrimStartWhitespace())
                workingValue = workingValue.TrimStart();

            if (normalizeConditions.TrimEndWhitespace())
                workingValue = workingValue.TrimEnd();

            // Validate Normalization Conditions
            if (!normalizeConditions.CanMakeReadable(workingValue))
                return workingValue;

            // Declarations
            StringBuilder returnValue = new StringBuilder();
            IEnumerable<string> workingValues = workingValue.SeperateForReadability(normalizeConditions);
            IEnumerator<string> iterator = workingValues.GetEnumerator();
            bool hasValue = iterator.MoveNext();
            bool isFirst = true;

            while (hasValue)
            {
                returnValue.Append(isFirst ? normalizeConditions.Capitalize(iterator.Current) : iterator.Current);
                hasValue = iterator.MoveNext();
                isFirst = false;
                if (hasValue)
                    returnValue.Append(WhiteSpace);
            }

            return returnValue.ToString();
        }