Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Add ToSentenceCaseAttribute #16

Open
centenario opened this issue Apr 14, 2021 · 2 comments
Open

Add ToSentenceCaseAttribute #16

centenario opened this issue Apr 14, 2021 · 2 comments
Labels
up-for-grabs Thank you!

Comments

@centenario
Copy link

Hi,
id like to know if its possible mutates value of property:

  • First char like Uppercase
  • The others chars in lowercase
    f.i. : the REAL thing ===> The real thing

thanks in advance

@roydukkey

This comment has been minimized.

@roydukkey
Copy link
Owner

roydukkey commented Apr 14, 2021

Sorry. I was off target with my first attempt. Here is something I've slapped together referencing a post on StackOverflow. This is untested, but it should get you moving in the right direction.

using System;
using System.Text.RegularExpressions;

namespace Dado.ComponentModel.DataMutations
{
	[AttributeUsage(AttributeTargets.Property)]
	public class ToSentenceCaseAttribute : MutationAttribute
	{
		public override int Priority { get; set; } = 40;
		
		protected override object MutateValue(object value, IMutationContext context)
		{
			if (value != null && value is string valueAsString) {
				return new Regex(@"(?<=(^|[.;:])\s*)[a-z]", RegexOptions.Compiled)
					.Replace(valueAsString, (match) => match.Value.ToUpper());
			}

			return value;
		}
	}
}

@roydukkey roydukkey changed the title Replace Firts letter withr Uppercase value and the other with lowercase Add ToSentenceCaseAttribute Apr 14, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
up-for-grabs Thank you!
Projects
None yet
Development

No branches or pull requests

2 participants