From 638fbd1d8ce9ae00d92c749866ba076b51305aa7 Mon Sep 17 00:00:00 2001 From: Florian Wilhelm Date: Thu, 27 Aug 2020 22:43:09 +0200 Subject: [PATCH] wip-node-api --- .../kotlin/com/github/fwilhe/inzell/sheet.kt | 5 +++++ src/jsMain/kotlin/node.kt | 22 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/jsMain/kotlin/node.kt diff --git a/src/commonMain/kotlin/com/github/fwilhe/inzell/sheet.kt b/src/commonMain/kotlin/com/github/fwilhe/inzell/sheet.kt index 55dec2c..05223c5 100644 --- a/src/commonMain/kotlin/com/github/fwilhe/inzell/sheet.kt +++ b/src/commonMain/kotlin/com/github/fwilhe/inzell/sheet.kt @@ -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, val caption: String = "(No caption provided)") { fun row(index: Int): List = columns.map { it.eval(index) } fun title(index: Int): String = columns[index].title @@ -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() diff --git a/src/jsMain/kotlin/node.kt b/src/jsMain/kotlin/node.kt new file mode 100644 index 0000000..d7267c1 --- /dev/null +++ b/src/jsMain/kotlin/node.kt @@ -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): 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)) +} \ No newline at end of file