moving logic from java to JS library