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

wip-node-api #15

Draft
wants to merge 1 commit into
base: master
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
5 changes: 5 additions & 0 deletions src/commonMain/kotlin/com/github/fwilhe/inzell/sheet.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.fwilhe.inzell

import kotlin.js.JsExport

typealias columnFunction = (Int) -> Double

@JsExport
class Column(val title: String, private val function: columnFunction) {
fun eval(i: Int): Double = function.invoke(i)
}

@JsExport
class Sheet(val columns: List<Column>, val caption: String = "(No caption provided)") {
fun row(index: Int): List<Double> = columns.map { it.eval(index) }
fun title(index: Int): String = columns[index].title
Expand All @@ -23,6 +27,7 @@ abstract class SpreadsheetPrinter(val sheet: Sheet, val numberOfRows: Int = 10)
}
}

@JsExport
class CsvPrinter(sheet: Sheet) : SpreadsheetPrinter(sheet) {
override fun toString(): String {
val stringBuilder = StringBuilder()
Expand Down
22 changes: 22 additions & 0 deletions src/jsMain/kotlin/node.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import com.github.fwilhe.inzell.Column
import com.github.fwilhe.inzell.CsvPrinter
import com.github.fwilhe.inzell.Sheet
import com.github.fwilhe.inzell.cosine

@ExperimentalJsExport
@JsExport
fun spreadsheet(columns: List<Column>): Sheet {
return Sheet(columns)
}

@ExperimentalJsExport
@JsExport
fun column(title: String, fn: (Int) -> Double): Column {
return Column(title, fn)
}

@ExperimentalJsExport
@JsExport
fun print(sheet: Sheet) {
println(sheet.columns.first().eval(3))
}