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.

installing

Historically, the library was found in jcenter(). But given jcenter()'s service end it was moved to maven central:

Maven:

<dependency>
  <groupId>net.andreinc</groupId>
  <artifactId>aleph</artifactId>
  <version>0.1.1</version>
</dependency>

Gradle:

implementation 'net.andreinc:aleph:0.1.1'

benchmarks

Aleph Formatter performs better than String.format for simple cases. A jmh benchmark is showing the following results (smaller is better):

CPU: AMD Ryzen 7 5800x, PBO
Windows Version	10.0.19041 Build 19041
Benchmark                     (N)  Mode  Cnt    Score    Error  Units
AlephFormatter.alephFormat      1  avgt    5   67.101 ±  2.103  ns/op
AlephFormatter.stringFormat     1  avgt    5  273.048 ±  5.632  ns/op
AlephFormatter.alephFormat     10  avgt    5   76.470 ±  4.423  ns/op
AlephFormatter.stringFormat    10  avgt    5  264.106 ±  3.018  ns/op
AlephFormatter.alephFormat    100  avgt    5  113.705 ±  5.941  ns/op
AlephFormatter.stringFormat   100  avgt    5  328.445 ± 15.986  ns/op
AlephFormatter.alephFormat   1000  avgt    5  257.674 ± 19.363  ns/op
AlephFormatter.stringFormat  1000  avgt    5  437.688 ± 11.813  ns/op

Source here.

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

aleph-formatter's People

Contributors

andreinciobanu avatar arjvik avatar dependabot-preview[bot] avatar nomemory avatar ntninja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

aleph-formatter's Issues

Create a property to define the default style

Hi,
When I am creating format strings, I always set all the strings to use ${...} formatting. Unfortunately, this requires me to put .style(Styles.DOLLARS) at the end of every string I'm formatting, which is repetitive and annoying.

Can you please define a property that we can set so AlephFormatter knows to always pick $-style formatting? Or, could you add a setter method to the class that we can call to set the formatting style prior to executing any fmt() commands? (or do both?)

Can't find a "style" method

Hi, I'm using v0.1.0 of the library but I can't find any style method to use on the str to support $ sign formatting. Is there a class missing from the jar?

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.