CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

Kotlin Standard Library for JavaScript target with comprehensive W3C DOM bindings and JavaScript interoperability features.

Pending
Overview
Eval results
Files

math-time.mddocs/

Math and Time Utilities

JavaScript-adapted mathematical functions and time/date utilities with proper type conversions and JavaScript Date integration. All math functions are implemented using JavaScript's Math object for optimal performance.

Capabilities

Mathematical Functions

Complete set of mathematical functions with Float and Double variants.

// Trigonometric functions
fun sin(x: Double): Double
fun sin(x: Float): Float
fun cos(x: Double): Double
fun cos(x: Float): Float  
fun tan(x: Double): Double
fun tan(x: Float): Float

// Inverse trigonometric functions
fun asin(x: Double): Double
fun asin(x: Float): Float
fun acos(x: Double): Double
fun acos(x: Float): Float
fun atan(x: Double): Double
fun atan(x: Float): Float
fun atan2(y: Double, x: Double): Double
fun atan2(y: Float, x: Float): Float

// Hyperbolic functions
fun sinh(x: Double): Double
fun sinh(x: Float): Float
fun cosh(x: Double): Double
fun cosh(x: Float): Float
fun tanh(x: Double): Double
fun tanh(x: Float): Float

// Exponential and logarithmic functions
fun exp(x: Double): Double
fun exp(x: Float): Float
fun expm1(x: Double): Double
fun expm1(x: Float): Float
fun log(x: Double): Double
fun log(x: Float): Float
fun log10(x: Double): Double
fun log10(x: Float): Float
fun log2(x: Double): Double
fun log2(x: Float): Float
fun ln(x: Double): Double
fun ln(x: Float): Float
fun ln1p(x: Double): Double
fun ln1p(x: Float): Float

// Power and root functions
fun pow(base: Double, exp: Double): Double
fun pow(base: Float, exp: Float): Float
fun sqrt(x: Double): Double
fun sqrt(x: Float): Float
fun hypot(x: Double, y: Double): Double
fun hypot(x: Float, y: Float): Float

// Rounding and absolute value
fun ceil(x: Double): Double
fun ceil(x: Float): Float
fun floor(x: Double): Double
fun floor(x: Float): Float
fun round(x: Double): Double
fun round(x: Float): Float
fun abs(x: Double): Double
fun abs(x: Float): Float
fun abs(x: Int): Int
fun abs(x: Long): Long

// Min/max functions
fun min(a: Double, b: Double): Double
fun min(a: Float, b: Float): Float
fun min(a: Int, b: Int): Int
fun min(a: Long, b: Long): Long
fun max(a: Double, b: Double): Double
fun max(a: Float, b: Float): Float
fun max(a: Int, b: Int): Int
fun max(a: Long, b: Long): Long

// Sign and truncation
fun sign(x: Double): Double
fun sign(x: Float): Float
fun truncate(x: Double): Double
fun truncate(x: Float): Float

Time and Date Conversion

Seamless conversion between Kotlin time types and JavaScript Date objects.

/**
 * Convert Kotlin Instant to JavaScript Date
 */
fun Instant.toJSDate(): Date

/**
 * Convert JavaScript Date to Kotlin Instant  
 */
fun Date.toKotlinInstant(): Instant

/**
 * JavaScript Date object
 */
external class Date {
    constructor()
    constructor(milliseconds: Number)
    constructor(year: Int, month: Int, day: Int = 1, hours: Int = 0, minutes: Int = 0, seconds: Int = 0, ms: Int = 0)
    
    fun getTime(): Double
    fun getFullYear(): Int
    fun getMonth(): Int
    fun getDate(): Int
    fun getHours(): Int
    fun getMinutes(): Int
    fun getSeconds(): Int
    fun getMilliseconds(): Int
    
    fun setTime(time: Double)
    fun setFullYear(year: Int)
    fun setMonth(month: Int)
    fun setDate(date: Int)
    fun setHours(hours: Int)
    fun setMinutes(minutes: Int)
    fun setSeconds(seconds: Int)
    fun setMilliseconds(ms: Int)
    
    fun toISOString(): String
    fun toDateString(): String
    fun toTimeString(): String
    
    companion object {
        fun now(): Double
        fun parse(dateString: String): Double
    }
}

Duration and Time Units

Duration units with JavaScript-specific scaling factors.

/**
 * Duration units adapted for JavaScript
 */
actual enum class DurationUnit(val scale: Double) {
    NANOSECONDS(1e-6),
    MICROSECONDS(1e-3), 
    MILLISECONDS(1.0),
    SECONDS(1e3),
    MINUTES(6e4),
    HOURS(3.6e6),
    DAYS(8.64e7);
    
    fun toMillis(value: Double): Double
    fun fromMillis(millis: Double): Double
}

/**
 * Convert duration value between units
 */
fun convertDurationUnit(value: Double, sourceUnit: DurationUnit, targetUnit: DurationUnit): Double

Usage Examples:

import kotlin.time.*
import kotlin.math.*

// Mathematical calculations
val angle = PI / 4
val sinValue = sin(angle) // 0.7071...
val cosValue = cos(angle) // 0.7071...

val logValue = ln(E) // 1.0
val powerValue = pow(2.0, 3.0) // 8.0
val sqrtValue = sqrt(16.0) // 4.0

// Rounding operations
val rounded = round(3.7) // 4.0
val floored = floor(3.7) // 3.0  
val ceiled = ceil(3.2) // 4.0

// Date conversions
val now = Clock.System.now()
val jsDate = now.toJSDate()
console.log("Current time: ${jsDate.toISOString()}")

val jsNow = Date()
val kotlinInstant = jsNow.toKotlinInstant()
console.log("Kotlin instant: $kotlinInstant")

// Duration unit conversions
val seconds = 5.0
val millis = DurationUnit.SECONDS.toMillis(seconds) // 5000.0
val minutes = DurationUnit.MILLISECONDS.fromMillis(millis) / 60000.0 // 0.0833...

Types

// JavaScript Date
external class Date {
    constructor()
    constructor(milliseconds: Number)
    constructor(year: Int, month: Int, day: Int, hours: Int, minutes: Int, seconds: Int, ms: Int)
}

// Duration units
actual enum class DurationUnit {
    NANOSECONDS, MICROSECONDS, MILLISECONDS, SECONDS, MINUTES, HOURS, DAYS
}

// Math constants (from kotlin.math)
const val PI: Double
const val E: Double

Install with Tessl CLI

npx tessl i tessl/maven-org-jetbrains-kotlin--kotlin-stdlib-js

docs

browser-integration.md

collections.md

coroutines.md

index.md

io-encoding.md

javascript-interop.md

math-time.md

reflection.md

uuid.md

w3c-dom-apis.md

tile.json