Skip to content

Commit

Permalink
Update .gitignore (#7)
Browse files Browse the repository at this point in the history
* Update .gitignore

* Update check.yml

* Update check.yml

* ss

* as

* toJS
  • Loading branch information
mpetuska authored Oct 22, 2021
1 parent e1093fe commit 17d754b
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 4 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
name: Check
defaults:
run:
shell: bash

on:
pull_request:
Expand All @@ -12,10 +9,11 @@ on:
env:
GRADLE_OPTS: "-Dorg.gradle.daemon=false"


jobs:
check:
name: Check on ${{ matrix.os.runner }}
runs-on: ${{ matrix.os }}
runs-on: ${{ matrix.os.runner }}
defaults:
run:
shell: ${{ matrix.os.shell }}
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ out/
dist/
local.properties
lint.xml
tmp-*
*.hprof
*.out
*.log
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## Changes
* New safe array setter `"key".to[1]` that works with single int arrays
* New object setter `"key" to {}` to allow consistent usage with value keys
* New helper `KON::toJS: Json` to convert KON objects to plain JS objects on js sourceSets
* `karr` array builder moved out of `KObject` interface

# v1.1.0
Expand Down
10 changes: 10 additions & 0 deletions lib/kon-core/src/commonMain/kotlin/KArray.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public object KARR {
public operator fun <T> get(vararg items: T): KArray<T> {
return karr(items = items)
}

/**
* Builds an array
* @param items array value items
* @return built array
*/
@KONBuilderDsl
public operator fun <T> invoke(vararg items: T): KArray<T> {
return karr(items = items)
}
}

/** Array builder hook. Useless by its own... */
Expand Down
9 changes: 9 additions & 0 deletions lib/kon-core/src/commonMain/kotlin/KObject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public interface KObject : KON {
public operator fun <T> get(vararg items: T) {
map[key] = karr(items = items)
}

/**
* Adds an array field
* @param items array value items
*/
@KONBuilderDsl
public operator fun <T> invoke(vararg items: T) {
map[key] = karr(items = items)
}
}

/** Array builder hook. Useless by its own... */
Expand Down
26 changes: 26 additions & 0 deletions lib/kon-core/src/jsMain/kotlin/toJS.kt
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")
}
37 changes: 37 additions & 0 deletions lib/kon-core/src/jsTest/kotlin/ToJSTest.kt
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)
}
}
4 changes: 4 additions & 0 deletions lib/kon-core/src/jsTest/kotlin/__klips__/ToJSTest.kt.klip
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]}
:::::>>:::::>>:::::>>

0 comments on commit 17d754b

Please sign in to comment.