Code Monkey home page Code Monkey logo

aleph-formatter's Introduction

aleph-formatter

Aleph Formatter is a lightweight library for string formatting that supports both named and positional parameters with a twist: it has a limited support for object introspection.

Example - basic usage:

import static net.andreinc.aleph.AlephFormatter.str

//...

String s1 = str("#{1} #{0} #{1} #{0}", 1, 2)
            .fmt();
System.out.println(s1);

Output:

2 1 2 1

Example - simple introspection

Each parameter supports limited method invocation:

String s2 = str("#{1}#{0.simpleName}", String.class, "Class:")
            .fmt();
System.out.println(s2);

Output

Class:String

On the String.class you can invoke the method: getSimpleName directly in the template. String.class is the #{0} param.

Example - Named arguments

String s3 = str("#{date.dayOfMonth}-#{date.month}-#{date.year}")
            .arg("date", LocalDate.now())
            .fmt();
System.out.println(s3);

String s4 = str("#{2.simpleName}, #{1}, #{0}, #{aNumber}, #{anArray}", 1, "A", String.class)
            .args("aNumber", 100, "anArray", new int[]{1,2,3,})
            .fmt();
System.out.println(s4);                        

Output:

8-MAY-2018
String, A, 1, 100, [1, 2, 3]

Example - Escaping #{

String s5 = str("`#{escaped} #{notEscaped}").args("escaped", 1, "notEscaped", 2)
            .fmt();
System.out.println(s5);

Output:

#{escaped} 2

Example - Changing Interpolation Styles

//Note the use of ${var} not #{var}
String s6 = str("${dollars}").args("dollars", "notPound")
            .style(Styles.DOLLARS).fmt();

Output:

notPound

installing

The library is found in the jcenter() repo.

jcenter() needs to be added as a repository.

For gradle:

compile 'net.andreinc.aleph:aleph-formatter:0.1.0'

For maven:

<dependency>
  <groupId>net.andreinc.aleph</groupId>
  <artifactId>aleph-formatter</artifactId>
  <version>0.1.0</version>
</dependency>

aleph-formatter's People

Contributors

arjvik avatar gcameo avatar nomemory avatar ntninja avatar

Watchers

 avatar

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.