Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ NonEmpty{List,Set}: make @JsExport - KOCHA-1924 #2

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up JDK 11
uses: actions/setup-java@v1
- name: Set up JDK
uses: actions/setup-java@v4
with:
java-version: '11'
java-version: '21'
distribution: corretto
architecture: x64

- name: Set up Gradle
uses: gradle/actions/setup-gradle@v3
uses: gradle/actions/setup-gradle@v4

- name: Run linters and unit tests
run: ./gradlew check --stacktrace

- name: Publish test reports
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v4
with:
name: test-reports
path: build/reports/tests
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompileCommon

plugins {
kotlin("multiplatform") version "1.9.22"
kotlin("multiplatform") version "2.0.20"
`maven-publish`
}

group = "com.eidu"
version = "1.1.1"
version = "1.1.1-eidu-3"

repositories {
mavenCentral()
Expand Down
2 changes: 0 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@

rootProject.name = "nonemptycollections"

Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
@file:OptIn(ExperimentalJsExport::class)

package com.quickbirdstudios.nonEmptyCollection

import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

@JsExport
interface NonEmptyCollection<out T> : Collection<T> {

@Deprecated(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
@file:OptIn(ExperimentalJsExport::class)

package com.quickbirdstudios.nonEmptyCollection.list

import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection
import kotlin.js.ExperimentalJsExport
import kotlin.js.ExperimentalJsStatic
import kotlin.js.JsExport
import kotlin.js.JsStatic

@JsExport
class NonEmptyList<out T> internal constructor(
internal val full: List<T>
) : List<T> by full, NonEmptyCollection<T> {
Expand All @@ -27,4 +34,10 @@ class NonEmptyList<out T> internal constructor(
override fun equals(other: Any?): Boolean = full == other

override fun hashCode(): Int = full.hashCode()

companion object {
@OptIn(ExperimentalJsStatic::class)
@JsStatic
fun <T> fromArray(array: Array<out T>): NonEmptyList<T> = NonEmptyList(array.first(), array.drop(1))
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
@file:OptIn(ExperimentalJsExport::class)

package com.quickbirdstudios.nonEmptyCollection.set

import com.quickbirdstudios.nonEmptyCollection.NonEmptyCollection
import kotlin.js.ExperimentalJsExport
import kotlin.js.ExperimentalJsStatic
import kotlin.js.JsExport
import kotlin.js.JsStatic

@JsExport
class NonEmptySet<out T> internal constructor(
internal val full: Set<T>
) : Set<T> by full, NonEmptyCollection<T> {
Expand All @@ -27,4 +34,11 @@ class NonEmptySet<out T> internal constructor(
"Fatal Error! This is a bug. Please contact the library author."
}
}

companion object {
@OptIn(ExperimentalJsStatic::class)
@JsStatic
fun <T> fromArray(array: Array<out T>): NonEmptySet<T> =
array.toSet().let { NonEmptySet(it.first(), it - it.first()) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ import com.quickbirdstudios.nonEmptyCollection.unsafe.toNonEmptySet

fun <T> List<T>.toNonEmptyListOrNull() = if (isEmpty()) null else toNonEmptyList()

fun <T> Set<T>.toNonEmptyMapOrNull() = if (isEmpty()) null else toNonEmptySet()
fun <T> Set<T>.toNonEmptySetOrNull() = if (isEmpty()) null else toNonEmptySet()

fun <K, V> Map<K, V>.toNonEmptyMapOrNull() = if (isEmpty()) null else toNonEmptyMap()
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ToNonEmptyOrNullTest {
val map = mapOf<Int, String>()

assertNull(list.toNonEmptyListOrNull())
assertNull(set.toNonEmptyMapOrNull())
assertNull(set.toNonEmptySetOrNull())
assertNull(map.toNonEmptyMapOrNull())
}

Expand All @@ -23,7 +23,7 @@ class ToNonEmptyOrNullTest {
val map = mapOf(69 to "430", -12 to "Hallo")

assertEquals<List<Int>?>(list, list.toNonEmptyListOrNull())
assertEquals<Set<Int>?>(set, set.toNonEmptyMapOrNull())
assertEquals<Set<Int>?>(set, set.toNonEmptySetOrNull())
assertEquals<Map<Int, String>?>(map, map.toNonEmptyMapOrNull())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class NonEmptyListOperatorsTest {
@Test
fun flatMap() {
assertOperationEquals(
Iterable<String>::flatMap,
{ transform: (String) -> Iterable<String> -> flatMap(transform) },
NonEmptyCollection<String>::flatMap,
{ value -> nonEmptyListOf(value, value) },
"dffd", "324334", "3434", "Stefan", "234234"
Expand All @@ -143,7 +143,7 @@ class NonEmptyListOperatorsTest {
@Test
fun flatMapIndexed() {
assertOperationEquals(
Iterable<String>::flatMapIndexed,
{ transform: (Int, String) -> Iterable<Int> -> flatMapIndexed(transform) },
NonEmptyCollection<String>::flatMapIndexed,
{ index, _ -> nonEmptyListOf(index * 2) },
"dffd", "324334", "3434", "Stefan", "234234"
Expand Down