Can a library be developed that works with both Java and JavaScript/TypeScript?

I specialize in Angular development.

Our front- and backend both contain specialized calculation methods that work like magic.

Although the classes are the same, any bugs found in the calculations have to be fixed separately in two different projects. Is there a way to create a universal codebase (perhaps a function) that can be translated into JavaScript (or TypeScript) and Java, allowing us to update both libraries based on the results obtained?

Answer №1

Consider exploring the capabilities of Kotlin for your project.

Kotlin is a versatile language that can transpile to JavaScript and also compile to Java bytecode.

While using Kotlin, you have access to its utilities, though not directly to Java or JS/TS types when integrating with both Java and ts/js code; however, you can utilize the kotlin stdlib.

If your task mainly involves calculations, specific java/js classes/functions may not be necessary.

It's worth considering conducting the calculation on the backend, as suggested by VLAZ in the comments.

Integration with IntelliJ IDE

To set up such a project in IntelliJ, choose Kotlin in the New Project Window and select the Library project template.

https://i.sstatic.net/HjpKf.png

Ensure you include targets like common, jvm, and js. For non-native development, omit the native target.

https://i.sstatic.net/obopj.png

You can then use the kotlin library in various projects, including JavaScript frameworks (e.g., Angular) and Java applications, as a dependency.

In a Java project, reference

KOTLIN_PROJECT/build/classes/kotlin/jvm/main
(compiled Java classes location).

In a JavaScript (e.g., Angular) project, refer to

KOTLIN_PROJECT/build/js/packages/kotlinToJavaAndJS
.

For demonstration purposes, I've created this GitHub repository.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Clearing the ng-model value in a controller following an ng-change event

Is there a way to reset the ng-model value in the controller after an ngChange event without needing to use a directive? <div ng-repeat="i in items"> <!-- Some DOM comes here --> <select ng-model="i.avail" ng-change="changeAvail(i. ...

A Java class that ensures thread safety while having its parameters refreshed on an hourly basis

Here is a class that I have: public class TextSuggester { private Suggester suggester; public TextSuggester() { createSuggester(); public void refresh() { createSuggester(); } private void createSuggester() { ...

Using CSS height 100% does not function properly when the content overflows

Here's what's going on with this code snippet: HTML <div class="one"> <div class="will-overflow"> </div> </div> CSS html, body { width: 100%; height: 100%; } .one { height: 100%; background: r ...

Unable to alter the pagination's selected page color to grey with material UI pagination components

Currently, I am implementing pagination using material UI. While I have successfully changed the page color to white, I am facing difficulty in changing the selected page color to grey. This issue arises because the background color is dark. import React, ...

Is there a way to retrieve the current object as a JSON string from inside the object using either jquery or javascript?

Looking for a way to return the current instance as a JSON text so that the values can be sent via an ajax request to a server-side script. Uncertain about where to apply the "this" keyword in a jQuery selector function Actor(){ this.input=function(pnam ...

Set datatables to perform regex searches that specifically target the beginning of text

Here is my JavaScript code for using datatables, I want to have the search function only filter results that start with the specified keyword. For example, if I have [hello, hello_all, all_hello] and my search term is "hel", I should only get [hello, hel ...

Redirect the URL in react-snap to a new URL with a forward slash at the end

I recently implemented react-snap to generate static HTML for my website. However, I encountered some SEO issues after implementing react-snap. The old URLs (which were without slashes) are now redirecting to new URLs with slashes. For example: This URL: ...

Tips for smoothly animating and showing content as the user scrolls to a specific element on the page

Here is a sample template: <template> <div id="Test"> <transition name="fade"> <div class="row" id="RowOne"> <p>Lorem ipsum dolor odit qui sit?</p> </div> ...

React component allowing for the reuse of avatars

As a novice in React, I've encountered a challenge. My goal is to develop a simple reusable component using MUI. Specifically, I aim to create an avatar component that can be easily customized with different images when called upon. I want the flexibi ...

Volta revolutionizing the use of global npm packages

I recently experimented with Volta tools on a pre-existing Node project and found them to be very useful :) However, I'm encountering an issue with using global packages. For example, I installed the gitmoji-cli package globally using npm like so: n ...

Troubleshooting Problem with MVC Ajax Requests

When working with an MVC view, I have a submit button that includes validation checks. If the validation fails, I return false to prevent the form from being submitted. In addition to standard validation, I also use an Ajax GET request to check for duplic ...

What is the best method for inserting a line break into a string?

Can you please explain how I can insert a line break into a string with JavaScript? I have tried using the <br/> tag, but it's not working. What is the correct way to achieve this? JavaScript var str="Click Here" +"<br>"+ "To Set" +"< ...

Encountering the error message "Error: 'preserveValueImports' is an unknown compiler option" while setting up a SvelteKit project

https://i.stack.imgur.com/fnidk.png Every time I set up a new sveltekit project using TypeScript, I keep encountering the error "Unknown compiler option 'preserveValueImports'.ts" in the tsconfig.json file. The error shows up above the line wher ...

Sharing data between sibling components becomes necessary when they are required to utilize the *ngIf directive simultaneously

Within my parent component, I am hosting 2 sibling components in the following manner: <div *ngif="somecode()"> <sibling1> </sibling1> </div> <div *ngif="somecode()"> <sibling1 [dataParams]=sibling1object.somedata> ...

Understanding Aspect Ratios in CSS for Div Containers and their Components

I am crafting an HTML5 game without the use of canvas and aiming to maintain a 16:9 aspect ratio within my div. Thanks to javascript, achieving this is straightforward and it functions flawlessly. However, the elements inside the div occasionally appear to ...

Using function overloading in TypeScript causes an error

I'm currently exploring the concept of function overloading in TypeScript and how it functions. type CreateElement = { (tag: 'a'): HTMLAnchorElement (tag: 'canvas'): HTMLCanvasElement (tag: 'table'): HTMLTableElem ...

JavaScript pause until the DOM has been modified

My current situation involves using a JavaScript file to make changes to the DOM of a page by adding a navigation menu. Following this code, there is another function that further modifies the newly added navigation menu. However, I am facing an issue wher ...

What is the best way to combine data from two rows into one row?

Can someone help me with merging 2 sets of Line data into a single row for each entry? For example, the 'Green Bay Packers @ Chicago Bears' row should display '+3.5000 and -3.5000', while 'Atlanta @ Minnesota' should show &apo ...

Error message "ag-grid: Unable to perform the key.forEach function in the console when resizing columns"

Within the application I am working on, I have implemented the ag-grid view. To address the issue related to the last empty pseudo column, I decided to resize the last displayed column using the 'autoSizeColumns' method of ag-grid. While this sol ...

Locate the index of an item within an array of objects based on a matching property in the

My current situation involves having an array of objects structured like this : [ {label: "<p>Opacity | %<br/></p>", customRow: false, id: 0, items: Array(13)}, {label: "Brand_hs_id", customRow: false, id: 0, items: A ...