import scala.collection.mutable.{ Map, MapLike }
import scala.scalajs.js
import js.annotation.JSBracketAccess
final class JSDictAsMap[A](dict: js.Dictionary[A]) extends Map[String, A]
with MapLike[String, A, JSMap[A]] {
def this() = this(js.Dictionary.empty)
override def empty: JSMap[A] = new JSMap[A]
override def get(key: String): Option[A] = {
if (dict.hasOwnProperty(key)) Some(dict(key))
else None
}
override def +=(kv: (String, A)): this.type = {
dict(kv._1) = kv._2
this
}
override def -=(key: String): this.type = {
dict.delete(key)
this
}
override def iterator: Iterator[(String, A)] = {
for {
key <- js.Object.keys(dict).iterator
} yield {
(key, dict(key))
}
}
}
object JSDictAsMap {
def empty[A]: JSMap[A] = new JSMap[A]
}
Cheers,
Sébastien
In javascript it is very common to iterate object properties in a loop and is widely used by a lot of javascript libs. I think it would be nice to have a method that will turn any external javascript object into a map on which one can make some simple pattern matching like defining if property is string, double, js.Object or js.Function as well as all other map operations.
--
You received this message because you are subscribed to the Google Groups "Scala.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-js+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
>Turn any object into a map: obj.asInstanceOf[js.Dictionary[A]]
One of the reasons why I asked it is because I promote scalajs in scala communities where I participate. And one of the point of criticism is thatthings that are natural (like iterating js.Object) in JavaScript do not feel natural in ScalaJS. And I agree with them, at lest partically, obj.asInstanceOf[js.Dictionary[A]] does not feel natural and it repels part of the people. I think it makes sense to consider some implicits (or even methods for js.Objects) that will be available by default