Coder Social home page Coder Social logo

rogierpennink / stringy Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 3.0 34 KB

A simple and light-weight runtime string template engine and expression evaluator

License: Apache License 2.0

C# 100.00%
string-manipulation substitution expression-evaluator expression-engine

stringy's Introduction

Stringy

Stringy is a simple and light-weight runtime string interpolation engine. Built as a utility for a text-based game, the purpose of the library is to provide something that's easier to use and more light-weight than Runtime Text Templates or RazorEngine, but a little more powerful than string.Replace().

On top of string interpolation, Stringy can also be used to facilitate simple expression evaluation. For example, suppose you want to store an arbitrary conditional statement as a "saved filter" for some list of items. You could write your filtering code like so:

string filterStr = "person.Age < 18 && person.Firstname.StartsWith('A')"; // Fetch from database
IStringy engine = new Stringy();

// Assume "people" is an IEnumerable of Person objects
var people = GetListOfPersonObjects();
var filteredPeople = people.Where(p => engine.Set("person", p).EvaluateExpression<bool>(filterStr));

// filteredPeople contains only those Persons with a first name
// that starts with the letter A, and an age lower than 18

Use cases

Stringy favours light string replacement and basic logic and branching needs. If you want to produce large bodies of (formatted) text using complex logic, you will probably find it worth it to use a full templating solution like RazorEngine. Use Stringy if:

  • Your string templates require basic logic and branching but you don't need the full power of C#.

  • You keep strings in a database/persistent storage and your substitution needs are greater than what can be achieved with regular expressions and string.Replace().

  • You need to execute methods and access properties on your model objects from inside your string templates.

  • You need to evaluate stored/persisted expressions

Installation

Simply add the Stringy NuGet package to your solution:

dotnet add package Stringy
dotnet restore

Or, using powershell:

Install-Package Stringy

Examples

Simple string substitution

string str = "Hello World";
const string template = "Substitute this: {value}";

IStringy engine = new Stringy();
engine.Set("value", str);

// Prints: Substitute this: Hello World
Console.WriteLine(engine.Execute(template));

Using engine.Set() you can add any object and its methods and properties will be available in your string templates.

Executing basic math

int num1 = 6;
int num2 = 7;
const string template = "{a} * {b} = {a * b}";

IStringy engine = new Stringy();
engine.Set("a", num1);
engine.Set("b", num2);

// Prints: 6 * 7 = 42
Console.WriteLine(engine.Execute(template));

Calling methods and properties of objects

string str = "Hello World";
const string template = "The lowercase string '{value.ToLower()}' has {value.Length} characters";

IStringy engine = new Stringy();
engine.Set("value", str);

// Prints: The lowercase string 'hello world' has 11 characters
Console.WriteLine(engine.Execute(template));

Simple if/else statements

bool userIsFemale = true;
const string template = "Welcome, dear {isFemale ? 'Lady' : 'Sir'}";

IStringy engine = new Stringy();
engine.Set("isFemale", userIsFemale);

// Prints: Welcome, dear Lady
Console.WriteLine(engine.Execute(template));

License

Apache 2.0

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.