Javascript object literal {}

I already have kotlin class which generate the javascript class but without argument(inside Leaderboard kotlin class).

Players.find()

What would be the kotlin code to make the generated javascript codes into something like

Players.find({}, {sort: {score: -1, name: 1}});

My related kotlin codes

native public object Meteor {   public var isClient : Boolean = false   public var isServer : Boolean = false   public var release : String = ""

  native(“Meteor.Collection”)
  public class Collection(name : String) {
  native(“Meteor.Collection.Cursor”)
  public class Cursor {

    }

    fun find(vararg selector : Any) : Cursor = noImpl

  }
}

public class Leaderboard {
  fun meteorVersion() : String {
  return Meteor.release
  }

  fun players(): Meteor.Collection.Cursor {
  return Players.find()
  }
}


Thank you

Hi

Note: I didn’t run below codes with Meteor, so, please, let us know about any results.

Short solution, but hackish:

native class Object

class Pair<K, V>(val first: K, val second: V)
fun <K, V> K.to(v: V) = Pair(this, v)
  
fun json<V>(vararg values: Pair<String, V>): Any {
  [native] val o = Object() as Map<String, Any?>
  for (v in values) {
  o[v.first] = v.second
  }
  return o
}

native
public object Meteor {
  
  public var isClient : Boolean = false
  public var isServer : Boolean = false
  public var release : String = “”

  native(“Meteor.Collection”)
  public class Collection(name : String) {
  native(“Meteor.Collection.Cursor”)
  public class Cursor {

  &nbsp;&nbsp;}

  &nbsp;&nbsp;fun find(selector : Any, options: Any) : Cursor = noImpl

  }
}

fun test() {
  Meteor.Collection(“Foo”).find(json<Any>(), json(“sort” to json(“foo” to “asc”, “bar” to “desc”)))

  Meteor.Collection(“Foo”).find(Object(), json(“sort” to array(array(“foo”, “asc”), array(“bar”, “asc”), “baz”)))
}


Next, more type safe version:

native(“Object”) class Selector

native(“Object”) class SortOptions
  
native(“Object”) class Options {
  var sort: SortOptions = noImpl
  var skip: Number = noImpl
  // …
}

fun options(
  sort: SortOptions? = null,
  skip: Int? = null
): Options {
  val o = Options()
  if (sort != null) o.sort = sort
  if (skip != null) o.skip = skip
  
  return o
}

native class FieldSortOption
val String.ASC: FieldSortOption
  get() = array(this, “asc”) as FieldSortOption

val String.DESC: FieldSortOption
  get() = array(this, “asc”) as FieldSortOption

fun sortOptions(vararg options: FieldSortOption) = options as SortOptions

native
public object Meteor {
  
  public var isClient : Boolean = false
  public var isServer : Boolean = false
  public var release : String = “”

  native(“Meteor.Collection”)
  public class Collection(name : String) {
  native(“Meteor.Collection.Cursor”)
  public class Cursor {

  &nbsp;&nbsp;}

  &nbsp;&nbsp;fun find(selector : Selector, options: Options) : Cursor = noImpl

  }
}

fun test() {
  Meteor.Collection(“Foo”).find(Selector(), options(sort = sortOptions(“foo”.ASC, “bar”.DESC)))
}


And mixed version:

native class Object

class Pair<K, V>(val first: K, val second: V)
fun <K, V> K.to(v: V) = Pair(this, v)
  
fun json<V>(vararg values: Pair<String, V>): Any {
  [native] val o = Object() as Map<String, Any?>
  for (v in values) {
  o[v.first] = v.second
  }
  return o
}

native(“Object”) class Selector

native(“Object”) class Options {
  var sort: Any = noImpl
  var skip: Number = noImpl
  // …
}

native
public object Meteor {
  
  public var isClient : Boolean = false
  public var isServer : Boolean = false
  public var release : String = “”

  native(“Meteor.Collection”)
  public class Collection(name : String) {
  native(“Meteor.Collection.Cursor”)
  public class Cursor {

  &nbsp;&nbsp;}

  &nbsp;&nbsp;fun find(selector : Selector, options: Options) : Cursor = noImpl

  }
}

fun test() {
  val o = Options()
  o.sort = json(“foo” to “asc”, “bar” to “desc”)
  
  o.sort = array(array(“foo”, “asc”), array(“bar”, “asc”), “baz”)
  
  Meteor.Collection(“Foo”).find(Selector(), o)
}

Thanks for your great reply.

I tried the second one. it worked great.

Thank you