Skip to content

Repository files navigation

underscore-java

Maven CentralMIT LicenseBuild StatusCoverage StatuscodecovCircleCICodeship Status for javadev/underscore-javawercker statusBuild statusBuild StatusRun StatusKnown VulnerabilitiesSputnikCode SceneQuality GateQuality GateScrutinizerBuild StatusHits-of-CodeJava Version

Join the chat at https://gitter.im/javadev/underscore-java

Requirements

Java 1.8 and later or Java 11.

Installation

Include the following in your pom.xml for Maven:

<dependencies>
<dependency>
<groupId>com.github.javadev</groupId>
<artifactId>underscore</artifactId>
<version>1.68</version>
</dependency>
...
</dependencies>

Gradle:

compile 'com.github.javadev:underscore:1.68'

Usage

U.of(/* array | list | set | map | anything based on Iterable interface */)
.filter(..)
.map(..)
...
.sortWith()
.forEach(..);
U.of(value1, value2, value3)...
U.range(0, 10)...
U.of(1, 2, 3) // or java.util.Arrays.asList(1, 2, 3) or new Integer[] {1, 2, 3}
.filter(v -> v > 1)
// 2, 3
.map(v -> v + 1)
// 3, 4
.sortWith((a, b) -> b.compareTo(a))
.forEach(System.out::println);
// 4, 3U.of(1, 2, 3) // or java.util.Arrays.asList(1, 2, 3) or new Integer[] {1, 2, 3}
.mapMulti((num, consumer) -> {
for (inti = 0; i < num; i++) {
consumer.accept("a" + num);
}
})
.forEach(System.out::println);
// "a1", "a2", "a2", "a3", "a3", "a3"U.formatXml("<a><b>data</b></a>");
// <a>// <b>data</b>// </a>U.formatJson("{\"a\":{\"b\":\"data\"}}");
// {// "a": {// "b": "data"// }// }U.xmlToJson("<a attr=\"c\"><b>data</b></a>");
// {// "a": {// "-attr": "c",// "b": "data"// },// "#omit-xml-declaration": "yes"// }U.jsonToXml("{\"a\":{\"-attr\":\"c\",\"b\":\"data\"}}");
// <?xml version="1.0" encoding="UTF-8"?>// <a attr="c">// <b>data</b>// </a>U.Builderbuilder = U.objectBuilder()
.add("firstName", "John")
.add("lastName", "Smith")
.add("age", 25)
.add("address", U.arrayBuilder()
.add(U.objectBuilder()
.add("streetAddress", "21 2nd Street")
.add("city", "New York")
.add("state", "NY")
.add("postalCode", "10021")))
.add("phoneNumber", U.arrayBuilder()
.add(U.objectBuilder()
.add("type", "home")
.add("number", "212 555-1234"))
.add(U.objectBuilder()
.add("type", "fax")
.add("number", "646 555-4567")));
System.out.println(builder.toJson());
System.out.println(builder.toXml());
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": [
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
],
"phoneNumber": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
<?xml version="1.0" encoding="UTF-8"?>
<root>
<firstName>John</firstName>
<lastName>Smith</lastName>
<agenumber="true">25</age>
<addressarray="true">
<streetAddress>21 2nd Street</streetAddress>
<city>New York</city>
<state>NY</state>
<postalCode>10021</postalCode>
</address>
<phoneNumber>
<type>home</type>
<number>212 555-1234</number>
</phoneNumber>
<phoneNumber>
<type>fax</type>
<number>646 555-4567</number>
</phoneNumber>
</root>

Underscore-java is a java port of Underscore.js.

In addition to porting Underscore's functionality, Underscore-java includes matching unit tests.

For docs, license, tests, and downloads, see: https://javadev.github.io/underscore-java

Thanks to Jeremy Ashkenas and all contributors to Underscore.js.

About

java port of Underscore.js

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages