generated from mpetuska/template-kmp-library
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update .gitignore * Update check.yml * Update check.yml * ss * as * toJS
- Loading branch information
Showing
8 changed files
with
90 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ out/ | |
dist/ | ||
local.properties | ||
lint.xml | ||
tmp-* | ||
*.hprof | ||
*.out | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package dev.petuska.kon | ||
|
||
import kotlin.js.Json | ||
import kotlin.js.json | ||
|
||
/** | ||
* Converts a given [KON] to JavaScript [Json] | ||
* @receiver kon object to convert | ||
* @return converted [Json] | ||
*/ | ||
public fun KON.toJS(): Json { | ||
return json(pairs = entries.map { (k, v) -> k to v.toJSElement() }.toTypedArray()) | ||
} | ||
|
||
private fun Any?.toJSElement(): Any? = | ||
when (this) { | ||
is Long -> toInt() | ||
is Boolean?, is String?, is Number? -> this | ||
is Array<*> -> Array(size) { this[it].toJSElement() } | ||
is Collection<*> -> map { it.toJSElement() }.toTypedArray() | ||
is Pair<*, *> -> arrayOf(first.toJSElement(), second.toJSElement()) | ||
is Triple<*, *, *> -> arrayOf(first.toJSElement(), second.toJSElement(), third.toJSElement()) | ||
is Map<*, *> -> | ||
json(pairs = entries.map { (k, v) -> k.toString() to v.toJSElement() }.toTypedArray()) | ||
else -> error("${this!!::class} is not a valid Json element") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package dev.petuska.kon | ||
|
||
import dev.petuska.klip.api.assertKlip | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import local.test.BlockingTest | ||
|
||
class ToJSTest : BlockingTest { | ||
@Test | ||
fun test() = blockingTest { | ||
val kon: KON = kobj { | ||
"str" to "string" | ||
"number" to 420 | ||
"boolean" to true | ||
"object" { | ||
"str" to "string" | ||
"number" to 1 | ||
"pair" to (1 to null) | ||
"triple" to Triple(1, "2", 3) | ||
"boolean" to true | ||
"withArray"[1, "2"] | ||
"nested" { | ||
"stillGood" to true | ||
"nullable" to null | ||
} | ||
} | ||
"consistentObject" to { "nice?" to 69 } | ||
"array"[1, "2", true, karr[1, "2", false], kobj { "inner" to true }] | ||
"singleIntArray".to[1] | ||
"singleIntArray2" to karr[1] | ||
} | ||
val json = kon.toJS() | ||
val jsonStr = JSON.stringify(json) | ||
jsonStr.assertKlip() | ||
assertEquals(kon.toString(), jsonStr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
:::::>> KLIPS :::::>> | ||
:::::>>dev.petuska.kon.ToJSTest.test#0:::::>> | ||
{"str":"string","number":420,"boolean":true,"object":{"str":"string","number":1,"pair":[1,null],"triple":[1,"2",3],"boolean":true,"withArray":[1,"2"],"nested":{"stillGood":true,"nullable":null}},"consistentObject":{"nice?":69},"array":[1,"2",true,[1,"2",false],{"inner":true}],"singleIntArray":[1],"singleIntArray2":[1]} | ||
:::::>>:::::>>:::::>> |