Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mariusz96/formattable-sb
A mutable FormattableString class
https://github.com/mariusz96/formattable-sb
builder composite formattable formatting mutable nuget string
Last synced: 3 months ago
JSON representation
A mutable FormattableString class
- Host: GitHub
- URL: https://github.com/mariusz96/formattable-sb
- Owner: mariusz96
- License: mit
- Created: 2022-09-27T17:44:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-22T21:53:04.000Z (10 months ago)
- Last Synced: 2024-11-03T16:06:26.102Z (3 months ago)
- Topics: builder, composite, formattable, formatting, mutable, nuget, string
- Language: C#
- Homepage: https://www.nuget.org/packages/FormattableSb
- Size: 49.8 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FormattableSb
A mutable FormattableString class:
```cs
List summerDates = GetSummerDates();FormattableStringBuilder sqlBuilder = new FormattableStringBuilder()
.AppendInterpolated($"INSERT INTO dbo.VacationDates (Date)")
.AppendLine()
.AppendInterpolated($"VALUES ({summerDates.First()})");foreach (DateTime date in summerDates.Skip(1))
{
sqlBuilder
.AppendInterpolated($",")
.AppendLine()
.AppendInterpolated($"({date})");
}// sql.Format:
// INSERT INTO dbo.VacationDates (Date)
// VALUES ({0}),
// ({1}),
// ({2}),
// ...// sql.GetArguments():
// [
// System.DateTime,
// System.DateTime,
// System.DateTime,
// ...
// ]
FormattableString sql = sqlBuilder.ToFormattableString();
```
```cs
static List GetSummerDates()
{
DateTime startDate = new(2040, 6, 20);
DateTime endDate = new(2040, 9, 22);List dates = new();
for (DateTime date = startDate; date <= endDate; date = date.AddDays(1))
{
dates.Add(date);
}return dates;
}
```
### With EF Core:
```cs
using VacationingContext context = new();
int rowsAffected = context.Database.ExecuteSql(sql);
```
## Features:
- Adheres to the C# language specification
- Can be used when you want to modify a FormattableString
- Preserves alignment and format strings
## API:
### AppendInterpolated:
```cs
///
/// Appends the specified interpolated string to the end of the composite format string,
/// replacing its arguments with placeholders and adding them as objects.
///
/// The interpolated string to append, along with the arguments.
/// A reference to this instance after the append operation has completed.
public FormattableStringBuilder AppendInterpolated([InterpolatedStringHandlerArgument("")] ref AppendInterpolatedHandler handler)
```
### AppendLine:
```cs
///
/// Appends the default line terminator to the end of the composite format string.
///
/// A reference to this instance after the append operation has completed.
public FormattableStringBuilder AppendLine()
```
### ToFormattableString:
```cs
///
/// Creates a from this builder.
///
/// The object that represents the composite format string and its arguments.
public FormattableString ToFormattableString()
```
## Setup:
- Install FormattableSb via NuGet Package Manager, Package Manager Console or dotnet CLI:
```
Install-Package FormattableSb
```
```
dotnet add package FormattableSb
```
## Credits:
- Thanks to Stephen Toub for the implementation