js4j

Wrapper for Java classes, which emulates Javascript variables and its API. Also includes ported Lodash library.

Лицензия

Лицензия

MIT
Группа

Группа

com.github.jshaptic
Идентификатор

Идентификатор

js4j
Последняя версия

Последняя версия

0.1.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

js4j
Wrapper for Java classes, which emulates Javascript variables and its API. Also includes ported Lodash library.
Ссылка на сайт

Ссылка на сайт

https://github.com/jshaptic/js4j
Система контроля версий

Система контроля версий

https://github.com/jshaptic/js4j/tree/master

Скачать js4j

Как подключить последнюю версию

<!-- https://jarcasting.com/artifacts/com.github.jshaptic/js4j/ -->
<dependency>
    <groupId>com.github.jshaptic</groupId>
    <artifactId>js4j</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.jshaptic/js4j/
implementation 'com.github.jshaptic:js4j:0.1.1'
// https://jarcasting.com/artifacts/com.github.jshaptic/js4j/
implementation ("com.github.jshaptic:js4j:0.1.1")
'com.github.jshaptic:js4j:jar:0.1.1'
<dependency org="com.github.jshaptic" name="js4j" rev="0.1.1">
  <artifact name="js4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.jshaptic', module='js4j', version='0.1.1')
)
libraryDependencies += "com.github.jshaptic" % "js4j" % "0.1.1"
[com.github.jshaptic/js4j "0.1.1"]

Зависимости

compile (4)

Идентификатор библиотеки Тип Версия
org.apache.commons : commons-lang3 jar 3.6
org.apache.commons : commons-text jar 1.1
com.eclipsesource.minimal-json : minimal-json jar 0.9.4
com.google.code.gson : gson jar 2.7

Модули Проекта

Данный проект не имеет модулей.

js4j

Bintray Javadocs Travis Coveralls License

Wrapper for Java classes, which emulates Javascript variables and its API. Also includes ported Lodash library. Currently not all functions and features are implemented, but most used are here, check API documentation for a full list of supported features.

Why

Most probably a question just poped up in your head: Why the hell it's needed for?? Well, for example it can be used as a tool for a quick libraries porting from javascript to java. Another use case is that it can be as a tool for quick prototyping, when you don't want to bother about types and things like that. Also it can be used as a JSON container with handy methods. But to tell the truth it was created just 4fun.

Usage

To declare new variables (or containers in js4j terms) UniversalContainer class should be used:

// this line will produce undefined container, which is analogue of "var c = undefined;" in javascript;
UniversalContainer c = ContainerFactory.undefinedContainer(); // var c = undefined;
// this line will also produce undefined container, but will create new instance of it, which is not good, so it's better to use above aproach
UniversalContainer c = new UniversalContainer(); // var c = undefined;

// this will produce null container
UniversalContainer c = ContainerFactory.nullContainer(); // var c = null;

// this will produce liter container with number
UniversalContainer c = new UniversalContainer(69); // var c = 69;

// this will produce empty object container
UniversalContainer c = ContainerFactory.createObject(); // var c = {};
// to add values to the object following methods can be used
UniversalContainer c = ContainerFactory.createObject(0, "a", 1, "b", 2, "c"); // var c = {0: "a", 1: "b", 2: "c"};
UniversalContainer c = ContainerFactory.createObject().set(0, "a").set(1, "b").set(2, "c"); // var c = {}; c[0] = "a"; c[1] = "b"; c[2] = "c"

// this will produce empty array container
UniversalContainer c = ContainerFactory.createArray(); // var c = [];
// to add values to the array following methods can be used
UniversalContainer c = ContainerFactory.createArray("a", "b", "c"); // var c = ["a", "b", "c"];
UniversalContainer c = ContainerFactory.createArray().set(0, "a").set(1, "b").set(2, "c"); // var c = []; c[0] = "a"; c[1] = "b"; c[2] = "c"

To compare containers method equals can be used, it works like === in JS and used to compare containers with each other or pretty much any other objects:

ContainerFactory.undefinedContainer().equals(null) // => true
new UniversalContainer(69).equals(69) // => true
new UniversalContainer(false).equals(0) // => false
ContainerFactory.createArray().equals(ContainerFactory.createArray()) // => false

To check if container is positive in "if" condition method test can be used:

ContainerFactory.undefinedContainer().test() == false // if (undefined) ...
ContainerFactory.nullContainer().test() == false // if (null) ...
new UniversalContainer(0).test() == false // if (0) ...
new UniversalContainer("").test() == false // if ("") ...
new UniversalContainer(10).test() == true // if (10) ...
new UniversalContainer("false").test() == true // if ("false") ...
ContainerFactory.createArray().test() == true // if ([]) ...

In most cases API usage is quite relaxed without any errors, since JS is a generous language, but sometimes even JS throwing runtime errors and to reflect the same behaviour ContainerException class was introduced. For example it will be thrown during runtime in the following cases:

ContainerFactory.undefinedContainer().get(0) // => Cannot get '0' property from Undefined container
new UniversalContainer("abcde").pop() // => Method 'pop' is not available for String container

For all other methods and use cases please consult javadoc.

Backlog

  • Implement all missing methods for boolean, number, string, object and array containers
  • Implement infinity, nan, date
  • Implement simple mathematical operations
  • Implement more functions from Lodash library
  • Implement own JSON parser or at least integrate any suitable open-source parser
  • Make object constructors much more complex and useful
  • Refactor value storage mechanism, currently it's implemented with Map, uses to much memory and can be slow

Версии библиотеки

Версия
0.1.1
0.1.0