Morning all, I'm quite new at interfacing JS and K...
# javascript
j
Morning all, I'm quite new at interfacing JS and Kotlin. Say I have a very simple NPM dependency (@jlengrand/test-js) with an
index.js
file like this :
Copy code
function defaultComparator (a, b) {
    return a - b
}

export function isSorted (array, comparator) {
    if (!Array.isArray(array)) throw new TypeError('Expected Array, got ' + (typeof array))
    comparator = comparator || defaultComparator

    for (var i = 1, length = array.length; i < length; ++i) {
        if (comparator(array[i - 1], array[i]) > 0) return false
    }

    return true
}

export function returnValue(value) {
    return value;
}
I would like to be able to import both functions from Kotlin, so I do as such
Copy code
@JsModule("@jlengrand/test-js")
@JsNonModule
external fun <T> isSorted(a: Array<T>): Boolean

@JsModule("@jlengrand/test-js")
@JsNonModule
external fun returnValue(s: String): String
I can use the
returnValue
function in my Kotlin code, however, it seems to always try to import the
isSorted
function, given the error I get in my console :
Copy code
Uncaught TypeError: Expected Array, got string....
As you can see, I have tried to extend from that documentation page (https://kotlinlang.org/docs/using-packages-from-npm.html), however I clearly seem to be missing a crucial piece of info. Would you have any pointers by any chance?
b
This should help.
j
Hum, is your blog not talking about using Kotlin to create Javascript, rather than importing NPM from Kotlin?
hum, right it explains both. I think my issue may come from my lack of knowledge of Javascript, mostly
Ok, trying to changing to fit your blog
Copy code
function defaultComparator (a, b) {
    return a - b
}

 export function isSorted (array, comparator) {
    if (!Array.isArray(array)) throw new TypeError('Expected Array, got ' + (typeof array))
    comparator = comparator || defaultComparator

    for (var i = 1, length = array.length; i < length; ++i) {
        if (comparator(array[i - 1], array[i]) > 0) return false
    }

    return true
}

export function returnValue(value) {
    return value;
}
Copy code
@JsModule("@jlengrand/js-tests")
@JsNonModule
external object Ports{
    fun <T> isSorted(a: Array<T>): Boolean
    fun returnValue(s: String): String
}
I'm now getting
Copy code
Uncaught TypeError: tmp.isSorted is not a function
b
Can you try removing JSNonModule annotation and enable commonJs in gradle config?
j
yes let me do that. I intend to run in the browser, will that be an issue?
wait
wtf
b
Shouldn't be. But I know what's up. You JS module is "local" (in resources), so you need to copy them over to kotlinJsCompile task outputDir before it runs 😄
j
I copy pasted the whole project to make a repo for you
in the specific place it works
same code
b
Hah 😄
j
I hate being a developer sometimes
Let's see what's up
b
Maybe build output cache got corrupted
./gradlew clean build
j
let me try that
that did the trick
lol
alright thanks
Great blog btw!
b
Sweet! Good luck!