• Stars
    star
    533
  • Rank 83,238 (Top 2 %)
  • Language
    Java
  • License
    MIT License
  • Created over 10 years ago
  • Updated 20 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

java port of Underscore.js

underscore-java

Maven Central MIT License Java CI CodeQL Semgrep Scorecard supply-chain security OSSAR OpenSSF Best Practices Coverage Status codecov CircleCI Build status Build Status Known Vulnerabilities javadoc Quality Gate Status Maintainability Rating Build Status Hits-of-Code codebeat badge Java Version

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

Requirements

Java 11 and later, Java 17 or Kotlin

Installation

To configure your Maven project, add the following code to your pom.xml file:

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

Gradle configuration:

implementation 'com.github.javadev:underscore:1.100'

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, 3
    
U.of(1, 2, 3) // or java.util.Arrays.asList(1, 2, 3) or new Integer[] {1, 2, 3}
    .mapMulti((num, consumer) -> {
        for (int i = 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>", Xml.XmlStringBuilder.Step.TWO_SPACES);
    // <a>
    //   <b>data</b>
    // </a>

U.formatJson("{\"a\":{\"b\":\"data\"}}", Json.JsonStringBuilder.Step.TWO_SPACES);
    // {
    //   "a": {
    //     "b": "data"
    //   }
    // }

U.xmlToJson(
    "<mydocument has=\"an attribute\">\n"
        + "   <and>\n"
        + "   <many>elements</many>\n"
        + "    <many>more elements</many>\n"
        + "   </and>\n"
        + "   <plus a=\"complex\">\n"
        + "     element as well\n"
        + "   </plus>\n"
        + "</mydocument>",
    Json.JsonStringBuilder.Step.TWO_SPACES);
    // {
    //   "mydocument": {
    //     "-has": "an attribute",
    //     "and": {
    //       "many": [
    //         "elements",
    //         "more elements"
    //       ]
    //     },
    //     "plus": {
    //       "-a": "complex",
    //       "#text": "\n     element as well\n   "
    //     }
    //   },
    //   "#omit-xml-declaration": "yes"
    // }

U.jsonToXml(
    "{\n"
        + "  \"mydocument\": {\n"
        + "    \"-has\": \"an attribute\",\n"
        + "    \"and\": {\n"
        + "      \"many\": [\n"
        + "        \"elements\",\n"
        + "        \"more elements\"\n"
        + "      ]\n"
        + "    },\n"
        + "    \"plus\": {\n"
        + "      \"-a\": \"complex\",\n"
        + "      \"#text\": \"\\n     element as well\\n   \"\n"
        + "    }\n"
        + "  },\n"
        + "  \"#omit-xml-declaration\": \"yes\"\n"
        + "}",
    Xml.XmlStringBuilder.Step.TWO_SPACES);
    // <mydocument has="an attribute">
    //   <and>
    //     <many>elements</many>
    //     <many>more elements</many>
    //   </and>
    //   <plus a="complex">
    //      element as well
    //    </plus>
    // </mydocument>

U.Builder builder = 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")
            .addNull("cityId")
            .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",
      "cityId": null,
      "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>
  <age number="true">25</age>
  <address array="true">
    <streetAddress>21 2nd Street</streetAddress>
    <city>New York</city>
    <cityId null="true"/>
    <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>
String inventory =
    "{\n"
        + "  \"inventory\": {\n"
        + "    \"#comment\": \"Test is test comment\",\n"
        + "    \"book\": [\n"
        + "      {\n"
        + "        \"-year\": \"2000\",\n"
        + "        \"title\": \"Snow Crash\",\n"
        + "        \"author\": \"Neal Stephenson\",\n"
        + "        \"publisher\": \"Spectra\",\n"
        + "        \"isbn\": \"0553380958\",\n"
        + "        \"price\": \"14.95\"\n"
        + "      },\n"
        + "      {\n"
        + "        \"-year\": \"2005\",\n"
        + "        \"title\": \"Burning Tower\",\n"
        + "        \"author\": [\n"
        + "          \"Larry Niven\",\n"
        + "          \"Jerry Pournelle\"\n"
        + "        ],\n"
        + "        \"publisher\": \"Pocket\",\n"
        + "        \"isbn\": \"0743416910\",\n"
        + "        \"price\": \"5.99\"\n"
        + "      },\n"
        + "      {\n"
        + "        \"-year\": \"1995\",\n"
        + "        \"title\": \"Zodiac\",\n"
        + "        \"author\": \"Neal Stephenson\",\n"
        + "        \"publisher\": \"Spectra\",\n"
        + "        \"isbn\": \"0553573862\",\n"
        + "        \"price\": \"7.50\"\n"
        + "      }\n"
        + "    ]\n"
        + "  }\n"
        + "}";
String title = U.selectToken(U.fromJsonMap(inventory), "//book[@year>2001]/title/text()");
// "Burning Tower"

String json =
    "{\n"
        + "  \"Stores\": [\n"
        + "    \"Lambton Quay\",\n"
        + "    \"Willis Street\"\n"
        + "  ],\n"
        + "  \"Manufacturers\": [\n"
        + "    {\n"
        + "      \"Name\": \"Acme Co\",\n"
        + "      \"Products\": [\n"
        + "        {\n"
        + "          \"Name\": \"Anvil\",\n"
        + "          \"Price\": 50\n"
        + "        }\n"
        + "      ]\n"
        + "    },\n"
        + "    {\n"
        + "      \"Name\": \"Contoso\",\n"
        + "      \"Products\": [\n"
        + "        {\n"
        + "          \"Name\": \"Elbow Grease\",\n"
        + "          \"Price\": 99.95\n"
        + "        },\n"
        + "        {\n"
        + "          \"Name\": \"Headlight Fluid\",\n"
        + "          \"Price\": 4\n"
        + "        }\n"
        + "      ]\n"
        + "    }\n"
        + "  ]\n"
        + "}";
List<String> names = U.selectTokens(U.fromJsonMap(json), "//Products[Price>=50]/Name/text()");
// [Anvil, Elbow Grease]

Simplify XML document creation by structuring your code like the final document.

This code:

XmlBuilder builder = XmlBuilder.create("Projects")
    .e("underscore-java").a("language", "Java").a("scm", "SVN")
        .e("Location").a("type", "URL")
            .t("https://github.com/javadev/underscore-java/")
        .up()
    .up()
    .e("JetS3t").a("language", "Java").a("scm", "CVS")
        .e("Location").a("type", "URL")
            .t("https://jets3t.s3.amazonaws.com/index.html");

Generates the following XML document:

<?xml version="1.0" encoding="UTF-8"?>
<Projects>
    <underscore-java language="Java" scm="SVN">
        <Location type="URL">https://github.com/javadev/underscore-java/</Location>
    </underscore-java>
    <JetS3t language="Java" scm="CVS">
        <Location type="URL">https://jets3t.s3.amazonaws.com/index.html</Location>
    </JetS3t>
</Projects>

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.

More Repositories

1

LeetCode-in-Java

Java-based LeetCode algorithm problem solutions, regularly updated.
Java
134
star
2

file-manager

A java/swing basic File Manager
Java
88
star
3

moneytostr

The tool to convert amount to words in Ukrainian/English/Russian languages. Сумма прописью.
HTML
40
star
4

LeetCode-in-Kotlin

Kotlin-based LeetCode algorithm problem solutions, regularly updated.
Kotlin
34
star
5

hexeditor

The binary file editor
Java
31
star
6

qrcode-generator

The java/swing application to generate QR codes
Java
18
star
7

android-sample-app

Android sample application, committed from AIDE (IDE for android)
Java
18
star
8

jasper-xml-to-pdf-generator

The command line tool to generate DOCX, HTML, ODT, PDF, PNG, PPTX, RTF, XLS, XLSX files from jrxml template
Java
16
star
9

xml-to-json

Xml to json javascript library.
13
star
10

sqltojava

The utility to convert SQL files to the java sources
Java
12
star
11

calculator

The java/swing, javascript, android calculator
Java
12
star
12

pt-backend

Spring-boot 2.7.18 application with flyway and javascript frontend
JavaScript
11
star
13

odt-to-pdf-converter

The java/swing application to convert odt files to the pdf files
Java
11
star
14

multiscan-antivirus

The antivirus project for DOS, written in assembler program language, still works on my Win7 PC
Assembly
9
star
15

codingame-traning

CodinGames are online challenges, open to all, the aim of which is to showcase programmers' technical skills
JavaScript
8
star
16

jregexp

The swing/java GUI application to check regexp expressions
Java
7
star
17

excel-to-json

Command line utility to convert excel files (all of them) to json. Uses Apache POI to convert files.
Java
7
star
18

ukrainiantolatin

The tool to convert Ukrainian words to the Latin characters, supports NBU transliteration
Java
6
star
19

underscore-java17

java 17 port of Underscore.js
Java
6
star
20

xml-viewer

The java/swing xml viewer applications
Java
5
star
21

password-generator

The java/swing application to generate 8-15 length passwords
Java
5
star
22

classic-cherries-kotlin

Implementation of some classical computer science algorithms and leetcode problems in kotlin.
Kotlin
5
star
23

leethub

Collection of LeetCode questions to ace the coding interview! - Created using LeetHub
Kotlin
5
star
24

underscore-java8

java 8 port of Underscore.js
Java
4
star
25

SimpleNet

Java
4
star
26

json-kotlin

Json parser library for kotlin
Kotlin
4
star
27

search-file-by-template

The swing/java application to search files by template with content
Java
4
star
28

declination-of-russian-names-and-surnames

The java/swing application for declination of Russian names and surnames (склонение русских имён и фамилий)
Java
4
star
29

superav

The command line antivirus program written in C program language.
C
4
star
30

scheduler

Web application, with technologies: Spring MVC, Hibernate, Maven, JUnit, HTML, JSP, Servlet, Spring Security, JPA, SQL and Git.
Java
4
star
31

LeetCode-in-All

104 LeetCode algorithm problem solutions.
Java
4
star
32

swing-todo-application

The java/swing todo application
Java
3
star
33

jsf-jdbc-sample-application

JSF 2 + jdbc sample application
Java
3
star
34

jar-file-scanner

The command line utility to check duplicates in jar files
Java
3
star
35

resource-viewer

The java/swing application to encode/decode property files
Java
3
star
36

dsnviewer

The java/swing application to view DSN files
Java
3
star
37

undescriptive-project

Dragons of Mugloar solution
Java
2
star
38

nazk-client

Java client for declarations from the nazk.gov.ua.
HTML
2
star
39

jsoup-sample-application

The web scraping sample application with jsoup library
Python
2
star
40

orderdatabase

The java/swing application to create/update/search orders
Java
2
star
41

pdf-server

PDF server application
Java
2
star
42

xlstosql

The utility class to convert XLS files to the SQL file
Java
2
star
43

jaxb-like-message-parser

The annotation based message parser
Java
2
star
44

vaadin-sqlcontainer-demo

The vaadin based sqlcontainer demo application
Java
2
star
45

file-download-utility2

File download utility
Java
2
star
46

generate-pom-and-cmd

generate-pom-and-cmd
Batchfile
2
star
47

latintoukrainian

The utility class to convert latin words to ukrainian characters
Java
2
star
48

Personal-Development-Plan-from-get-no

Personal Development Plan from get.no
2
star
49

scan-google

Scan google with selenium web driver
Java
2
star
50

file-download-utility

Java
2
star
51

cms-demo

CMS demo application
Java
1
star
52

GFG-in-Java

Java Solution for GeeksforGeeks algorithm problems, continually updating.
1
star
53

database-diff

Database diff application
Batchfile
1
star
54

javascript-task

HTML
1
star
55

liferay-guice-demo

Shell
1
star
56

csv-server

A jhipster application to store and view csv files
Java
1
star
57

spring-ws-sample

Spring Web Service server and client sample application
Java
1
star
58

javascript-code-coverage

Javascript code coverage example with coveralls
JavaScript
1
star
59

typeonline

Type online javascript application
CoffeeScript
1
star
60

FirstAlgorithm

Swing/java application to encrypt/decrypt texts
Java
1
star
61

phone-utils

A convenient phone library helping to work with phone numbers.
Java
1
star
62

expert-system

The expert system java/swing application
Java
1
star
63

annuity-amortization-calculation

The tool to calculate amortization plan
Java
1
star
64

sample-web-sites

Sample static web sites
HTML
1
star
65

log-parser

The log file parser utility
Java
1
star
66

wsdl-codegen-plugin

Codegen maven plugin for wsdl files.
Java
1
star
67

cpp_task01

Cpp console application
C++
1
star
68

cms-valentyn

Jhipster/angular MySQL application
Java
1
star
69

cpp_task02

Записная книжка
C++
1
star
70

matrix-uility

Matrix implementation
Java
1
star
71

test-task-rest-application

Test application with a rest service
Java
1
star
72

hello-gae

Helloworld google app engine endpoints application
JavaScript
1
star
73

todo-backbone-thucydides-demo

Todo application with thucydides BDD tests
Java
1
star
74

codingame-1

Java
1
star
75

ruby-hw

Ruby home work
Ruby
1
star
76

bitstarter

HW3 Programming: Your First Crowdfunding Site
HTML
1
star
77

urlencoder

The url encoder java/javascript utility
Java
1
star
78

task-tracker

Task tracker application
Java
1
star
79

address-search-utility

Java
1
star
80

scala-code-coverage

The scala sample project with code coverage
Scala
1
star
81

javadev.github.io

home
1
star
82

LeetCode-in-Rust

Rust Solution for LeetCode algorithm problems, continually updating.
1
star
83

practice.geeksforgeeks

Solutions from task in practice.geeksforgeeks.org/problems web site
Java
1
star
84

vaadin-invient-charts-demo

The vaadin based invient charts demo application
HTML
1
star
85

verylargenumber

The library to support very large number operations
Java
1
star
86

pim-system

Java
1
star
87

graphql-demo

A graphql demo application
1
star
88

wicket-guice-demo

The web demo application. Wicket 6.8.0 + guice 3.0 + hibernate JPA.
Java
1
star
89

wicket-guestbook-demo

The web application with wicket 6.15.0
Java
1
star
90

stringcreator

The java/swing application to create strings from template
Java
1
star
91

template-sample

Velocity sample template command line program
Java
1
star
92

binary-calendar-generator

The command line tool to generate binary calendar
Java
1
star
93

jasmine-sample-project

Jasmine maven plugin sample project
JavaScript
1
star
94

terraform

The terraform scripts
HCL
1
star
95

loancalculator

The java/swing application to calculate good loans
Java
1
star
96

todo-backbone-cucumber-demo

The demo javascript web application with cucumber tests
JavaScript
1
star
97

portlet-scanner

The liferay portlet source code scanner/generator
Java
1
star
98

marionette-requirejs-sample-application

The sample javascript client based on Marionette, RequireJS frameworks
JavaScript
1
star
99

binary-file-to-json

Easily convert binary data to JSON format with this lightweight library, simplifying data handling and improving interoperability for your applications. Perfect for developers looking to streamline data processing!
Java
1
star